Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,5 @@ config :spark,
]

config :diffo, ash_domains: [Diffo.Provider]
config :diffo_example, ash_domains: [DiffoExample.Access]
config :diffo_example, ash_domains: [DiffoExample.Access, DiffoExample.Nbn]
import_config "#{config_env()}.exs"
6 changes: 0 additions & 6 deletions lib/access/resources/characteristic_values/cable_value.ex
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,4 @@ defmodule DiffoExample.Access.CableValue do

field :technology, :atom, description: "the cable technology"
end

defimpl String.Chars do
def to_string(struct) do
inspect(struct)
end
end
end
6 changes: 0 additions & 6 deletions lib/access/resources/characteristic_values/card_value.ex
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,4 @@ defmodule DiffoExample.Access.CardValue do

field :technology, :atom, description: "the card technology"
end

defimpl String.Chars do
def to_string(struct) do
inspect(struct)
end
end
end
6 changes: 0 additions & 6 deletions lib/access/resources/characteristic_values/float_unit.ex
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,4 @@ defmodule DiffoExample.Access.FloatUnit do

field :unit, :atom, description: "the unit"
end

defimpl String.Chars do
def to_string(struct) do
inspect(struct)
end
end
end
6 changes: 0 additions & 6 deletions lib/access/resources/characteristic_values/integer_unit.ex
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,4 @@ defmodule DiffoExample.Access.IntegerUnit do

field :unit, :atom, description: "the unit"
end

defimpl String.Chars do
def to_string(struct) do
inspect(struct)
end
end
end
6 changes: 0 additions & 6 deletions lib/access/resources/characteristic_values/path_value.ex
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,4 @@ defmodule DiffoExample.Access.PathValue do

field :technology, :atom, description: "the path technology"
end

defimpl String.Chars do
def to_string(struct) do
inspect(struct)
end
end
end
6 changes: 0 additions & 6 deletions lib/access/resources/characteristic_values/shelf_value.ex
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,4 @@ defmodule DiffoExample.Access.ShelfValue do

field :technology, :atom, description: "the shelf technology"
end

defimpl String.Chars do
def to_string(struct) do
inspect(struct)
end
end
end
12 changes: 6 additions & 6 deletions lib/access/services/characteristic_values/aggregate_interface.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ defmodule DiffoExample.Access.AggregateInterface do
jason do
pick [:name, :physical_interface, :physical_layer, :link_layer, :svlan_id, :vpi]
compact(true)

rename physical_interface: "physicalInterface",
physical_layer: "physicalLayer",
link_layer: "linkLayer",
svlan_id: "svlanId",
vpi: "VPI"
end

outstanding do
Expand Down Expand Up @@ -46,10 +52,4 @@ defmodule DiffoExample.Access.AggregateInterface do
default: 0,
description: "the aggregate interface vpi"
end

defimpl String.Chars do
def to_string(struct) do
inspect(struct)
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,4 @@ defmodule DiffoExample.Access.BandwidthProfile do
constraints: [one_of: [:kbps, :Mbps]],
description: "the bandwidth profile units"
end

defimpl String.Chars do
def to_string(struct) do
inspect(struct)
end
end
end
10 changes: 2 additions & 8 deletions lib/access/services/characteristic_values/circuit.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ defmodule DiffoExample.Access.Circuit do
jason do
pick [:circuit_id, :cvlan_id, :vci, :encapsulation, :bandwidth_profile]
compact(true)
rename circuit_id: "circuitId", vci: "VCI", bandwidth_profile: "bandwidthProfile"
end

outstanding do
Expand All @@ -41,13 +42,6 @@ defmodule DiffoExample.Access.Circuit do
constraints: [one_of: [:PPPoA, :PPPoE, :IPoE]],
description: "the circuit encapsulation"

field :bandwidth_profile, BandwidthProfile,
description: "the circuit bandwidth profile"
end

defimpl String.Chars do
def to_string(struct) do
inspect(struct)
end
field :bandwidth_profile, BandwidthProfile, description: "the circuit bandwidth profile"
end
end
7 changes: 1 addition & 6 deletions lib/access/services/characteristic_values/constraints.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ defmodule DiffoExample.Access.Constraints do
jason do
pick [:max_latency, :min_profile]
compact(true)
rename max_latency: "maxLatency", min_profile: "minProfile"
end

outstanding do
Expand All @@ -28,10 +29,4 @@ defmodule DiffoExample.Access.Constraints do
constraints: [instance_of: BandwidthProfile],
description: "the circuit bandwidth profile"
end

defimpl String.Chars do
def to_string(struct) do
inspect(struct)
end
end
end
6 changes: 0 additions & 6 deletions lib/access/services/characteristic_values/dslam.ex
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,4 @@ defmodule DiffoExample.Access.Dslam do
default: :eth,
description: "the DSLAM technology"
end

defimpl String.Chars do
def to_string(struct) do
inspect(struct)
end
end
end
6 changes: 0 additions & 6 deletions lib/access/services/characteristic_values/line.ex
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,4 @@ defmodule DiffoExample.Access.Line do

field :profile, :string, description: "the line port profile"
end

defimpl String.Chars do
def to_string(struct) do
inspect(struct)
end
end
end
7 changes: 4 additions & 3 deletions lib/access/util.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ defmodule DiffoExample.Access.Util do

alias Diffo.Provider.Assignment

def assignments(instance, type) do
@doc """
Lists things that are assigned_to an Instance, as Assignments
"""
def assignments(instance, type) when is_struct(instance, Ash.Resource) and is_atom(type) do
Enum.reduce(instance.reverse_relationships, [], fn reverse_relationship, acc ->
IO.inspect(reverse_relationship, label: :reverse_relationship)

case reverse_relationship.type do
:assignedTo ->
characteristic =
Expand Down
87 changes: 87 additions & 0 deletions lib/nbn/nbn.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# SPDX-FileCopyrightText: 2025 diffo_example contributors <https://github.com/diffo-dev/diffo_example/graphs.contributors>
#
# SPDX-License-Identifier: MIT

defmodule DiffoExample.Nbn do
@moduledoc """
Diffo - TMF Service and Resource Management with a difference

Nbn - example NBN domain

Models NBN network resources including the Ethernet circuit (NbnEthernet)
and its constituent resources: UNI (dedicated), AVC (dedicated), NTD,
CVC (aggregates AVCs, terminates at NNI Group), NNI Group, and NNI.
"""
use Ash.Domain,
otp_app: :diffo

alias DiffoExample.Nbn.NbnEthernet
alias DiffoExample.Nbn.Uni
alias DiffoExample.Nbn.Avc
alias DiffoExample.Nbn.Ntd
alias DiffoExample.Nbn.Cvc
alias DiffoExample.Nbn.NniGroup
alias DiffoExample.Nbn.Nni

domain do
description "An example showing how TMF Resources for a fictional NBN domain can be extended from the Provider domain"
end

resources do
resource NbnEthernet do
define :get_nbn_ethernet_by_id, action: :read, get_by: :id
define :build_nbn_ethernet, action: :build
define :define_nbn_ethernet, action: :define
define :relate_nbn_ethernet, action: :relate
define :mine_nbn_ethernet, action: :mine
end

resource Uni do
define :get_uni_by_id, action: :read, get_by: :id
define :build_uni, action: :build
define :define_uni, action: :define
define :relate_uni, action: :relate
define :mine_uni, action: :mine
end

resource Avc do
define :get_avc_by_id, action: :read, get_by: :id
define :build_avc, action: :build
define :define_avc, action: :define
define :relate_avc, action: :relate
define :mine_avc, action: :mine
end

resource Ntd do
define :get_ntd_by_id, action: :read, get_by: :id
define :build_ntd, action: :build
define :define_ntd, action: :define
define :assign_port, action: :assign_port
define :relate_ntd, action: :relate
end

resource Cvc do
define :get_cvc_by_id, action: :read, get_by: :id
define :build_cvc, action: :build
define :define_cvc, action: :define
define :assign_cvlan, action: :assign_cvlan
define :relate_cvc, action: :relate
define :mine_cvc, action: :mine
end

resource NniGroup do
define :get_nni_group_by_id, action: :read, get_by: :id
define :build_nni_group, action: :build
define :define_nni_group, action: :define
define :assign_svlan, action: :assign_svlan
define :relate_nni_group, action: :relate
end

resource Nni do
define :get_nni_by_id, action: :read, get_by: :id
define :build_nni, action: :build
define :define_nni, action: :define
define :relate_nni, action: :relate
end
end
end
119 changes: 119 additions & 0 deletions lib/nbn/resources/avc.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
# SPDX-FileCopyrightText: 2025 diffo_example contributors <https://github.com/diffo-dev/diffo_example/graphs.contributors>
#
# SPDX-License-Identifier: MIT

defmodule DiffoExample.Nbn.Avc do
@moduledoc """
Diffo - TMF Service and Resource Management with a difference

Avc - Access Virtual Circuit Resource Instance

An AVC is the virtual circuit dedicated to an NBN Ethernet circuit,
carrying traffic between its related UNI and the CVC that aggregates it.
"""

alias Diffo.Provider.BaseInstance
alias Diffo.Provider.Instance.Relationship
alias Diffo.Provider.Instance.Characteristic
alias Diffo.Provider.Instance.ActionHelper

alias DiffoExample.Nbn

use Ash.Resource,
fragments: [BaseInstance],
domain: Nbn

resource do
description "An Ash Resource representing an Access Virtual Circuit (AVC)"
plural_name :Avcs
end

specification do
id "b2c3d4e5-6f7a-4b8c-9d0e-1f2a3b4c5d6e"
name "avc"
type :resourceSpecification
description "An AVC Resource Instance dedicated to an NBN Ethernet circuit"
category "Network Resource"
end

characteristics do
characteristic :avc, DiffoExample.Nbn.AvcValue
characteristic :cvc, DiffoExample.Nbn.CvcValue
end

actions do
create :build do
description "creates a new AVC resource instance"
accept [:id, :which]
argument :specified_by, :uuid, public?: false
argument :relationships, {:array, :struct}
argument :features, {:array, :uuid}, public?: false
argument :characteristics, {:array, :uuid}, public?: false
argument :places, {:array, :struct}
argument :parties, {:array, :struct}

change set_attribute(:name, &DiffoExample.Nbn.Avc.identifier/0)

change set_attribute(:type, :resource)

change before_action(fn changeset, _context -> ActionHelper.build_before(changeset) end)

change after_action(fn changeset, result, _context ->
ActionHelper.build_after(changeset, result, Nbn, :get_avc_by_id)
end)

change load [:href]
upsert? false
end

update :define do
description "defines the AVC"
argument :characteristic_value_updates, {:array, :term}

change after_action(fn changeset, result, _context ->
with {:ok, result} <- Characteristic.update_values(result, changeset),
{:ok, result} <- Nbn.get_avc_by_id(result.id),
do: {:ok, result}
end)
end

update :relate do
description "relates the AVC with other instances"
argument :relationships, {:array, :struct}

change after_action(fn changeset, result, _context ->
with {:ok, result} <- Relationship.relate_instance(result, changeset),
{:ok, result} <- Nbn.get_avc_by_id(result.id),
do: {:ok, result}
end)
end

update :mine do
description "updates the AVC with data mined from related instances"
argument :characteristic_value_updates, {:array, :term}

change before_action(fn changeset, context ->
DiffoExample.Nbn.Avc.mine_related(changeset, context)
end)

change after_action(fn changeset, result, _context ->
with {:ok, result} <- Characteristic.update_values(result, changeset),
{:ok, result} <- Nbn.get_avc_by_id(result.id),
do: {:ok, result}
end)
end
end

def identifier() do
DiffoExample.Nbn.Util.identifier("AVC")
end

# mines related resource to characteristics
def mine_related(changeset, _context) when is_struct(changeset, Ash.Changeset) do
reverse_relationships = Ash.Changeset.get_attribute(changeset, :reverse_relationships)

cvlan = {:cvlan, Diffo.Unwrap.unwrap(hd(hd(reverse_relationships).characteristics).value)}

Ash.Changeset.force_set_argument(changeset, :characteristic_value_updates, avc: [cvlan])
end
end
Loading
Loading