Skip to content

Commit 8768002

Browse files
committed
derive speeds from ntd technology and avc bandwidth_profile
1 parent d51421e commit 8768002

19 files changed

Lines changed: 522 additions & 63 deletions

lib/access/util.ex

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ defmodule DiffoExample.Access.Util do
1818
"""
1919
def assignments(instance, type) when is_struct(instance, Ash.Resource) and is_atom(type) do
2020
Enum.reduce(instance.reverse_relationships, [], fn reverse_relationship, acc ->
21-
IO.inspect(reverse_relationship, label: :reverse_relationship)
22-
2321
case reverse_relationship.type do
2422
:assignedTo ->
2523
characteristic =

lib/nbn/nbn.ex

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,19 +41,22 @@ defmodule DiffoExample.Nbn do
4141
define :build_uni, action: :build
4242
define :define_uni, action: :define
4343
define :relate_uni, action: :relate
44+
define :mine_uni, action: :mine
4445
end
4546

4647
resource Avc do
4748
define :get_avc_by_id, action: :read, get_by: :id
4849
define :build_avc, action: :build
4950
define :define_avc, action: :define
5051
define :relate_avc, action: :relate
52+
define :mine_avc, action: :mine
5153
end
5254

5355
resource Ntd do
5456
define :get_ntd_by_id, action: :read, get_by: :id
5557
define :build_ntd, action: :build
5658
define :define_ntd, action: :define
59+
define :assign_port, action: :assign_port
5760
define :relate_ntd, action: :relate
5861
end
5962

@@ -63,6 +66,7 @@ defmodule DiffoExample.Nbn do
6366
define :define_cvc, action: :define
6467
define :assign_cvlan, action: :assign_cvlan
6568
define :relate_cvc, action: :relate
69+
define :mine_cvc, action: :mine
6670
end
6771

6872
resource NniGroup do

lib/nbn/resources/avc.ex

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ defmodule DiffoExample.Nbn.Avc do
3838

3939
characteristics do
4040
characteristic :avc, DiffoExample.Nbn.AvcValue
41+
characteristic :cvc, DiffoExample.Nbn.CvcValue
4142
end
4243

4344
actions do
@@ -86,9 +87,33 @@ defmodule DiffoExample.Nbn.Avc do
8687
do: {:ok, result}
8788
end)
8889
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
89105
end
90106

91107
def identifier() do
92108
DiffoExample.Nbn.Util.identifier("AVC")
93109
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
94119
end

lib/nbn/resources/characteristic_values/avc_value.ex

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,21 @@ defmodule DiffoExample.Nbn.AvcValue do
1010
"""
1111
use Ash.TypedStruct, extensions: [AshJason.TypedStruct, AshOutstanding.TypedStruct]
1212

13+
alias DiffoExample.Nbn.BandwidthProfile
14+
1315
jason do
14-
pick [:cir, :pir]
16+
pick [:cvlan, :bandwidth_profile]
1517
compact(true)
1618
end
1719

1820
outstanding do
19-
expect [:cir, :pir]
21+
expect [:cvlan, :bandwidth_profile]
2022
end
2123

2224
typed_struct do
23-
field :cir, :integer, description: "Committed Information Rate in Mbps"
25+
field :cvlan, :string, description: "the cvlan of the AVC, assigned by the related CVC"
2426

25-
field :pir, :integer, description: "Peak Information Rate in Mbps"
27+
field :bandwidth_profile, BandwidthProfile, description: "the bandwidth profile of the AVC"
2628
end
2729

2830
defimpl String.Chars do

lib/nbn/resources/characteristic_values/cvc_value.ex

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,16 @@ defmodule DiffoExample.Nbn.CvcValue do
1111
use Ash.TypedStruct, extensions: [AshJason.TypedStruct, AshOutstanding.TypedStruct]
1212

1313
jason do
14-
pick [:cvc_id, :bandwidth]
14+
pick [:svlan, :bandwidth]
1515
compact(true)
1616
end
1717

1818
outstanding do
19-
expect [:cvc_id, :bandwidth]
19+
expect [:svlan, :bandwidth]
2020
end
2121

2222
typed_struct do
23-
field :cvc_id, :string, description: "the unique CVC identifier"
23+
field :svlan, :string, description: "the svlan of the CVC, assigned by the related NNI Group"
2424

2525
field :bandwidth, :integer, description: "total CVC bandwidth in Mbps"
2626
end

lib/nbn/resources/characteristic_values/ntd_value.ex

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ defmodule DiffoExample.Nbn.NtdValue do
1010
"""
1111
use Ash.TypedStruct, extensions: [AshJason.TypedStruct, AshOutstanding.TypedStruct]
1212

13+
alias DiffoExample.Nbn.Technology
14+
1315
jason do
1416
pick [:model, :serial_number, :technology]
1517
compact(true)
@@ -24,8 +26,7 @@ defmodule DiffoExample.Nbn.NtdValue do
2426

2527
field :serial_number, :string, description: "the NTD serial number"
2628

27-
field :technology, :atom,
28-
description: "the access technology (:FTTP, :FTTN, :HFC, :Fixed_Wireless)"
29+
field :technology, Technology, description: "the access technology type", default: Technology.default
2930
end
3031

3132
defimpl String.Chars do

lib/nbn/resources/characteristic_values/pri_value.ex

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,28 +10,29 @@ defmodule DiffoExample.Nbn.PriValue do
1010
"""
1111
use Ash.TypedStruct, extensions: [AshJason.TypedStruct, AshOutstanding.TypedStruct]
1212

13-
@technologies [:FTTP, :FTTN, :FTTB, :FTTC, :HFC, :FixedWireless, :Satellite]
13+
alias DiffoExample.Nbn.Technology
14+
alias DiffoExample.Nbn.BandwidthProfile
15+
alias DiffoExample.Nbn.Speeds
1416

1517
jason do
16-
pick [:avcid, :uniid, :speed, :technology]
18+
pick [:avcid, :uniid, :technology, :bandwidth_profile, :speeds]
1719
compact(true)
1820
end
1921

2022
outstanding do
21-
expect [:circuit_id, :speed]
23+
expect [:avcid, :uniid, :technology, :bandwidth_profile, :speeds]
2224
end
2325

2426
typed_struct do
25-
field :avcid, :string, description: "the avcid from the owne Avc Resource"
27+
field :avcid, :string, description: "the avcid from the owned Avc Resource"
2628

2729
field :uniid, :string, description: "the uniid from the owned Uni Resource"
2830

29-
field :speed, :integer, description: "the circuit download speed in Mbps"
31+
field :technology, Technology, description: "the technology type"
3032

31-
field :technology, :atom,
32-
description: "the access technology",
33-
constraints: [one_of: @technologies],
34-
default: :FTTP
33+
field :bandwidth_profile, BandwidthProfile, description: "the bandwidth profile"
34+
35+
field :speeds, Speeds, description: "the downstream and upstream speeds in Mbps"
3536
end
3637

3738
defimpl String.Chars do

lib/nbn/resources/characteristic_values/uni_value.ex

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,23 @@ defmodule DiffoExample.Nbn.UniValue do
1010
"""
1111
use Ash.TypedStruct, extensions: [AshJason.TypedStruct, AshOutstanding.TypedStruct]
1212

13+
alias DiffoExample.Nbn.Technology
14+
1315
jason do
14-
pick [:vlan_id, :bandwidth_profile, :technology]
16+
pick [:port, :encapsulation, :technology]
1517
compact(true)
1618
end
1719

1820
outstanding do
19-
expect [:vlan_id, :technology]
21+
expect [:port, :encapsulation, :technology]
2022
end
2123

2224
typed_struct do
23-
field :vlan_id, :integer, description: "the VLAN ID for the UNI"
25+
field :port, :integer, description: "the port of the UNI, assigned by the related NTD"
2426

25-
field :bandwidth_profile, :string, description: "the bandwidth profile name for the UNI"
27+
field :encapsulation, :string, description: "the encapsulation of the UNI"
2628

27-
field :technology, :atom,
28-
description: "the access technology (:FTTP, :FTTN, :HFC, :Fixed_Wireless)"
29+
field :technology, Technology, description: "the access technology type"
2930
end
3031

3132
defimpl String.Chars do

lib/nbn/resources/cvc.ex

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ defmodule DiffoExample.Nbn.Cvc do
88
99
Cvc - Connectivity Virtual Circuit Resource Instance
1010
11-
A CVC is the wholesale bandwidth product that aggregates one or more AVC
12-
resources and terminates at an NNI Group resource. Each AVC has a related UNI.
11+
A CVC is the wholesale bandwidth product that supports AVC and terminates at an NNI Group.
12+
The CVC assigns cvlan to AVC.
1313
"""
1414

1515
alias Diffo.Provider.BaseInstance
@@ -42,7 +42,7 @@ defmodule DiffoExample.Nbn.Cvc do
4242

4343
characteristics do
4444
characteristic :cvc, DiffoExample.Nbn.CvcValue
45-
characteristic :cvlan_ids, Diffo.Provider.AssignableValue
45+
characteristic :cvlans, Diffo.Provider.AssignableValue
4646
end
4747

4848
actions do
@@ -86,7 +86,7 @@ defmodule DiffoExample.Nbn.Cvc do
8686
argument :assignment, :struct, constraints: [instance_of: Assignment]
8787

8888
change after_action(fn changeset, result, _context ->
89-
with {:ok, result} <- Assigner.assign(result, changeset, :cvlan_ids, :cvlan_id),
89+
with {:ok, result} <- Assigner.assign(result, changeset, :cvlans, :cvlan),
9090
{:ok, result} <- Nbn.get_cvc_by_id(result.id),
9191
do: {:ok, result}
9292
end)
@@ -102,15 +102,33 @@ defmodule DiffoExample.Nbn.Cvc do
102102
do: {:ok, result}
103103
end)
104104
end
105-
end
106105

107-
attributes do
108-
attribute :cvcid, :string do
109-
default &DiffoExample.Nbn.Cvc.identifier/0
106+
update :mine do
107+
description "updates the CVC with data mined from related instances"
108+
argument :characteristic_value_updates, {:array, :term}
109+
110+
change before_action(fn changeset, context ->
111+
DiffoExample.Nbn.Cvc.mine_related(changeset, context)
112+
end)
113+
114+
change after_action(fn changeset, result, _context ->
115+
with {:ok, result} <- Characteristic.update_values(result, changeset),
116+
{:ok, result} <- Nbn.get_cvc_by_id(result.id),
117+
do: {:ok, result}
118+
end)
110119
end
111120
end
112121

113122
def identifier() do
114123
DiffoExample.Nbn.Util.identifier("CVC")
115124
end
125+
126+
# mines related resource to characteristics
127+
def mine_related(changeset, _context) when is_struct(changeset, Ash.Changeset) do
128+
reverse_relationships = Ash.Changeset.get_attribute(changeset, :reverse_relationships)
129+
130+
svlan = {:svlan, hd(hd(reverse_relationships).characteristics).value}
131+
132+
Ash.Changeset.force_set_argument(changeset, :characteristic_value_updates, cvc: [svlan])
133+
end
116134
end

lib/nbn/resources/nbn_ethernet.ex

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@ defmodule DiffoExample.Nbn.NbnEthernet do
88
99
NbnEthernet - NBN Ethernet access Resource Instance
1010
11-
An NBN Ethernet access comprising a dedicated UNI and AVC resource.
12-
The access is related to its UNI, which in turn is aggregated by a CVC
13-
that terminates at an NNI Group.
11+
An NBN Ethernet access comprises of dedicated UNI and AVC resources.
1412
"""
1513

1614
alias Diffo.Provider.BaseInstance
@@ -19,6 +17,7 @@ defmodule DiffoExample.Nbn.NbnEthernet do
1917
alias Diffo.Provider.Instance.ActionHelper
2018

2119
alias DiffoExample.Nbn
20+
alias DiffoExample.Nbn.Util
2221

2322
use Ash.Resource,
2423
fragments: [BaseInstance],
@@ -117,12 +116,25 @@ defmodule DiffoExample.Nbn.NbnEthernet do
117116
forward_relationships = Ash.Changeset.get_attribute(changeset, :forward_relationships)
118117

119118
pri_updates =
120-
Enum.into(forward_relationships, [], fn forward_relationship ->
119+
Enum.reduce(forward_relationships, [], fn forward_relationship, acc ->
121120
{:ok, related} = Diffo.Provider.get_instance_by_id(forward_relationship.target_id)
122-
{alias_to_id(forward_relationship.alias), related.name}
121+
related_name = {alias_to_id(forward_relationship.alias), related.name}
122+
case forward_relationship.alias do
123+
:uni ->
124+
# extract technology from uni characteristic
125+
[{:technology, Util.extract(related.characteristics, :uni, :technology)} | [ related_name | acc]]
126+
:avc ->
127+
# extract bandwidth_profile from avc characteristic
128+
[{:bandwidth_profile, Util.extract(related.characteristics, :avc, :bandwidth_profile)} | [ related_name | acc]]
129+
_ ->
130+
[ related_name | acc]
131+
end
123132
end)
124133

125-
Ash.Changeset.force_set_argument(changeset, :characteristic_value_updates, pri: pri_updates)
134+
# calculate the speeds from the extracted technology and bandwidth_profile
135+
speeds = {:speeds, Util.speeds(Keyword.get(pri_updates, :bandwidth_profile), Keyword.get(pri_updates, :technology))}
136+
137+
Ash.Changeset.force_set_argument(changeset, :characteristic_value_updates, pri: [speeds | pri_updates])
126138
end
127139

128140
defp alias_to_id(alias) when is_atom(alias) do

0 commit comments

Comments
 (0)