Skip to content

Commit 9b7ca44

Browse files
Merge branch '15-nbn-domain' into nbn-domain-dev
2 parents 33e5787 + 8768002 commit 9b7ca44

25 files changed

Lines changed: 1840 additions & 5 deletions

config/config.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,5 @@ config :spark,
3737
]
3838

3939
config :diffo, ash_domains: [Diffo.Provider]
40-
config :diffo_example, ash_domains: [DiffoExample.Access]
40+
config :diffo_example, ash_domains: [DiffoExample.Access, DiffoExample.Nbn]
4141
import_config "#{config_env()}.exs"

lib/access/util.ex

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@ defmodule DiffoExample.Access.Util do
1313

1414
alias Diffo.Provider.Assignment
1515

16-
def assignments(instance, type) do
16+
@doc """
17+
Lists things that are assigned_to an Instance, as Assignments
18+
"""
19+
def assignments(instance, type) when is_struct(instance, Ash.Resource) and is_atom(type) do
1720
Enum.reduce(instance.reverse_relationships, [], fn reverse_relationship, acc ->
18-
IO.inspect(reverse_relationship, label: :reverse_relationship)
19-
2021
case reverse_relationship.type do
2122
:assignedTo ->
2223
characteristic =

lib/nbn/nbn.ex

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# SPDX-FileCopyrightText: 2025 diffo_example contributors <https://github.com/diffo-dev/diffo_example/graphs.contributors>
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
defmodule DiffoExample.Nbn do
6+
@moduledoc """
7+
Diffo - TMF Service and Resource Management with a difference
8+
9+
Nbn - example NBN domain
10+
11+
Models NBN network resources including the Ethernet circuit (NbnEthernet)
12+
and its constituent resources: UNI (dedicated), AVC (dedicated), NTD,
13+
CVC (aggregates AVCs, terminates at NNI Group), NNI Group, and NNI.
14+
"""
15+
use Ash.Domain,
16+
otp_app: :diffo
17+
18+
alias DiffoExample.Nbn.NbnEthernet
19+
alias DiffoExample.Nbn.Uni
20+
alias DiffoExample.Nbn.Avc
21+
alias DiffoExample.Nbn.Ntd
22+
alias DiffoExample.Nbn.Cvc
23+
alias DiffoExample.Nbn.NniGroup
24+
alias DiffoExample.Nbn.Nni
25+
26+
domain do
27+
description "An example showing how TMF Resources for a fictional NBN domain can be extended from the Provider domain"
28+
end
29+
30+
resources do
31+
resource NbnEthernet do
32+
define :get_nbn_ethernet_by_id, action: :read, get_by: :id
33+
define :build_nbn_ethernet, action: :build
34+
define :define_nbn_ethernet, action: :define
35+
define :relate_nbn_ethernet, action: :relate
36+
define :mine_nbn_ethernet, action: :mine
37+
end
38+
39+
resource Uni do
40+
define :get_uni_by_id, action: :read, get_by: :id
41+
define :build_uni, action: :build
42+
define :define_uni, action: :define
43+
define :relate_uni, action: :relate
44+
define :mine_uni, action: :mine
45+
end
46+
47+
resource Avc do
48+
define :get_avc_by_id, action: :read, get_by: :id
49+
define :build_avc, action: :build
50+
define :define_avc, action: :define
51+
define :relate_avc, action: :relate
52+
define :mine_avc, action: :mine
53+
end
54+
55+
resource Ntd do
56+
define :get_ntd_by_id, action: :read, get_by: :id
57+
define :build_ntd, action: :build
58+
define :define_ntd, action: :define
59+
define :assign_port, action: :assign_port
60+
define :relate_ntd, action: :relate
61+
end
62+
63+
resource Cvc do
64+
define :get_cvc_by_id, action: :read, get_by: :id
65+
define :build_cvc, action: :build
66+
define :define_cvc, action: :define
67+
define :assign_cvlan, action: :assign_cvlan
68+
define :relate_cvc, action: :relate
69+
define :mine_cvc, action: :mine
70+
end
71+
72+
resource NniGroup do
73+
define :get_nni_group_by_id, action: :read, get_by: :id
74+
define :build_nni_group, action: :build
75+
define :define_nni_group, action: :define
76+
define :assign_svlan, action: :assign_svlan
77+
define :relate_nni_group, action: :relate
78+
end
79+
80+
resource Nni do
81+
define :get_nni_by_id, action: :read, get_by: :id
82+
define :build_nni, action: :build
83+
define :define_nni, action: :define
84+
define :relate_nni, action: :relate
85+
end
86+
end
87+
end

lib/nbn/resources/avc.ex

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
# SPDX-FileCopyrightText: 2025 diffo_example contributors <https://github.com/diffo-dev/diffo_example/graphs.contributors>
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
defmodule DiffoExample.Nbn.Avc do
6+
@moduledoc """
7+
Diffo - TMF Service and Resource Management with a difference
8+
9+
Avc - Access Virtual Circuit Resource Instance
10+
11+
An AVC is the virtual circuit dedicated to an NBN Ethernet circuit,
12+
carrying traffic between its related UNI and the CVC that aggregates it.
13+
"""
14+
15+
alias Diffo.Provider.BaseInstance
16+
alias Diffo.Provider.Instance.Relationship
17+
alias Diffo.Provider.Instance.Characteristic
18+
alias Diffo.Provider.Instance.ActionHelper
19+
20+
alias DiffoExample.Nbn
21+
22+
use Ash.Resource,
23+
fragments: [BaseInstance],
24+
domain: Nbn
25+
26+
resource do
27+
description "An Ash Resource representing an Access Virtual Circuit (AVC)"
28+
plural_name :Avcs
29+
end
30+
31+
specification do
32+
id "b2c3d4e5-6f7a-4b8c-9d0e-1f2a3b4c5d6e"
33+
name "avc"
34+
type :resourceSpecification
35+
description "An AVC Resource Instance dedicated to an NBN Ethernet circuit"
36+
category "Network Resource"
37+
end
38+
39+
characteristics do
40+
characteristic :avc, DiffoExample.Nbn.AvcValue
41+
characteristic :cvc, DiffoExample.Nbn.CvcValue
42+
end
43+
44+
actions do
45+
create :build do
46+
description "creates a new AVC resource instance"
47+
accept [:id, :which]
48+
argument :specified_by, :uuid, public?: false
49+
argument :relationships, {:array, :struct}
50+
argument :features, {:array, :uuid}, public?: false
51+
argument :characteristics, {:array, :uuid}, public?: false
52+
argument :places, {:array, :struct}
53+
argument :parties, {:array, :struct}
54+
55+
change set_attribute(:name, &DiffoExample.Nbn.Avc.identifier/0)
56+
57+
change set_attribute(:type, :resource)
58+
59+
change before_action(fn changeset, _context -> ActionHelper.build_before(changeset) end)
60+
61+
change after_action(fn changeset, result, _context ->
62+
ActionHelper.build_after(changeset, result, Nbn, :get_avc_by_id)
63+
end)
64+
65+
change load [:href]
66+
upsert? false
67+
end
68+
69+
update :define do
70+
description "defines the AVC"
71+
argument :characteristic_value_updates, {:array, :term}
72+
73+
change after_action(fn changeset, result, _context ->
74+
with {:ok, result} <- Characteristic.update_values(result, changeset),
75+
{:ok, result} <- Nbn.get_avc_by_id(result.id),
76+
do: {:ok, result}
77+
end)
78+
end
79+
80+
update :relate do
81+
description "relates the AVC with other instances"
82+
argument :relationships, {:array, :struct}
83+
84+
change after_action(fn changeset, result, _context ->
85+
with {:ok, result} <- Relationship.relate_instance(result, changeset),
86+
{:ok, result} <- Nbn.get_avc_by_id(result.id),
87+
do: {:ok, result}
88+
end)
89+
end
90+
91+
update :mine do
92+
description "updates the AVC with data mined from related instances"
93+
argument :characteristic_value_updates, {:array, :term}
94+
95+
change before_action(fn changeset, context ->
96+
DiffoExample.Nbn.Avc.mine_related(changeset, context)
97+
end)
98+
99+
change after_action(fn changeset, result, _context ->
100+
with {:ok, result} <- Characteristic.update_values(result, changeset),
101+
{:ok, result} <- Nbn.get_avc_by_id(result.id),
102+
do: {:ok, result}
103+
end)
104+
end
105+
end
106+
107+
def identifier() do
108+
DiffoExample.Nbn.Util.identifier("AVC")
109+
end
110+
111+
# mines related resource to characteristics
112+
def mine_related(changeset, _context) when is_struct(changeset, Ash.Changeset) do
113+
reverse_relationships = Ash.Changeset.get_attribute(changeset, :reverse_relationships)
114+
115+
cvlan = {:cvlan, hd(hd(reverse_relationships).characteristics).value}
116+
117+
Ash.Changeset.force_set_argument(changeset, :characteristic_value_updates, avc: [cvlan])
118+
end
119+
end
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# SPDX-FileCopyrightText: 2025 diffo_example contributors <https://github.com/diffo-dev/diffo_example/graphs.contributors>
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
defmodule DiffoExample.Nbn.AvcValue do
6+
@moduledoc """
7+
Diffo - TMF Service and Resource Management with a difference
8+
9+
AvcValue - AshTyped Struct for AVC Characteristic Value
10+
"""
11+
use Ash.TypedStruct, extensions: [AshJason.TypedStruct, AshOutstanding.TypedStruct]
12+
13+
alias DiffoExample.Nbn.BandwidthProfile
14+
15+
jason do
16+
pick [:cvlan, :bandwidth_profile]
17+
compact(true)
18+
end
19+
20+
outstanding do
21+
expect [:cvlan, :bandwidth_profile]
22+
end
23+
24+
typed_struct do
25+
field :cvlan, :string, description: "the cvlan of the AVC, assigned by the related CVC"
26+
27+
field :bandwidth_profile, BandwidthProfile, description: "the bandwidth profile of the AVC"
28+
end
29+
30+
defimpl String.Chars do
31+
def to_string(struct) do
32+
inspect(struct)
33+
end
34+
end
35+
end
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# SPDX-FileCopyrightText: 2025 diffo_example contributors <https://github.com/diffo-dev/diffo_example/graphs.contributors>
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
defmodule DiffoExample.Nbn.CvcValue do
6+
@moduledoc """
7+
Diffo - TMF Service and Resource Management with a difference
8+
9+
CvcValue - AshTyped Struct for Connectivity Virtual Circuit Characteristic Value
10+
"""
11+
use Ash.TypedStruct, extensions: [AshJason.TypedStruct, AshOutstanding.TypedStruct]
12+
13+
jason do
14+
pick [:svlan, :bandwidth]
15+
compact(true)
16+
end
17+
18+
outstanding do
19+
expect [:svlan, :bandwidth]
20+
end
21+
22+
typed_struct do
23+
field :svlan, :string, description: "the svlan of the CVC, assigned by the related NNI Group"
24+
25+
field :bandwidth, :integer, description: "total CVC bandwidth in Mbps"
26+
end
27+
28+
defimpl String.Chars do
29+
def to_string(struct) do
30+
inspect(struct)
31+
end
32+
end
33+
end
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# SPDX-FileCopyrightText: 2025 diffo_example contributors <https://github.com/diffo-dev/diffo_example/graphs.contributors>
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
defmodule DiffoExample.Nbn.NniGroupValue do
6+
@moduledoc """
7+
Diffo - TMF Service and Resource Management with a difference
8+
9+
NniGroupValue - AshTyped Struct for NNI Group Characteristic Value
10+
"""
11+
use Ash.TypedStruct, extensions: [AshJason.TypedStruct, AshOutstanding.TypedStruct]
12+
13+
jason do
14+
pick [:name, :location]
15+
compact(true)
16+
end
17+
18+
outstanding do
19+
expect [:name, :location]
20+
end
21+
22+
typed_struct do
23+
field :name, :string, description: "the NNI group name"
24+
25+
field :location, :string, description: "the Point of Interconnect (PoI) location"
26+
end
27+
28+
defimpl String.Chars do
29+
def to_string(struct) do
30+
inspect(struct)
31+
end
32+
end
33+
end
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# SPDX-FileCopyrightText: 2025 diffo_example contributors <https://github.com/diffo-dev/diffo_example/graphs.contributors>
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
defmodule DiffoExample.Nbn.NniValue do
6+
@moduledoc """
7+
Diffo - TMF Service and Resource Management with a difference
8+
9+
NniValue - AshTyped Struct for NNI Characteristic Value
10+
"""
11+
use Ash.TypedStruct, extensions: [AshJason.TypedStruct, AshOutstanding.TypedStruct]
12+
13+
jason do
14+
pick [:port_id, :capacity, :technology]
15+
compact(true)
16+
end
17+
18+
outstanding do
19+
expect [:port_id, :capacity]
20+
end
21+
22+
typed_struct do
23+
field :port_id, :string, description: "the NNI port identifier"
24+
25+
field :capacity, :integer, description: "the NNI port capacity in Gbps"
26+
27+
field :technology, :atom, description: "the NNI technology (:Ethernet, :Fibre)"
28+
end
29+
30+
defimpl String.Chars do
31+
def to_string(struct) do
32+
inspect(struct)
33+
end
34+
end
35+
end

0 commit comments

Comments
 (0)