Skip to content

Commit d51421e

Browse files
committed
improved identifiers
1 parent 4ab8f95 commit d51421e

14 files changed

Lines changed: 214 additions & 49 deletions

File tree

lib/access/util.ex

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,13 @@ 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 ->
1821
IO.inspect(reverse_relationship, label: :reverse_relationship)
22+
1923
case reverse_relationship.type do
2024
:assignedTo ->
2125
characteristic =

lib/nbn/nbn.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ defmodule DiffoExample.Nbn do
3333
define :build_nbn_ethernet, action: :build
3434
define :define_nbn_ethernet, action: :define
3535
define :relate_nbn_ethernet, action: :relate
36+
define :mine_nbn_ethernet, action: :mine
3637
end
3738

3839
resource Uni do

lib/nbn/resources/avc.ex

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,16 @@ defmodule DiffoExample.Nbn.Avc do
4343
actions do
4444
create :build do
4545
description "creates a new AVC resource instance"
46-
accept [:id, :name, :type, :which]
46+
accept [:id, :which]
4747
argument :specified_by, :uuid, public?: false
4848
argument :relationships, {:array, :struct}
4949
argument :features, {:array, :uuid}, public?: false
5050
argument :characteristics, {:array, :uuid}, public?: false
5151
argument :places, {:array, :struct}
5252
argument :parties, {:array, :struct}
5353

54+
change set_attribute(:name, &DiffoExample.Nbn.Avc.identifier/0)
55+
5456
change set_attribute(:type, :resource)
5557

5658
change before_action(fn changeset, _context -> ActionHelper.build_before(changeset) end)
@@ -85,4 +87,8 @@ defmodule DiffoExample.Nbn.Avc do
8587
end)
8688
end
8789
end
90+
91+
def identifier() do
92+
DiffoExample.Nbn.Util.identifier("AVC")
93+
end
8894
end

lib/nbn/resources/characteristic_values/nbn_ethernet_value.ex renamed to lib/nbn/resources/characteristic_values/pri_value.ex

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,18 @@
22
#
33
# SPDX-License-Identifier: MIT
44

5-
defmodule DiffoExample.Nbn.NbnEthernetValue do
5+
defmodule DiffoExample.Nbn.PriValue do
66
@moduledoc """
77
Diffo - TMF Service and Resource Management with a difference
88
9-
NbnEthernetValue - AshTyped Struct for NBN Ethernet Circuit Characteristic Value
9+
NbnEthernetValue - AshTyped Struct for NBN Ethernet Access Characteristic Value
1010
"""
1111
use Ash.TypedStruct, extensions: [AshJason.TypedStruct, AshOutstanding.TypedStruct]
1212

13+
@technologies [:FTTP, :FTTN, :FTTB, :FTTC, :HFC, :FixedWireless, :Satellite]
14+
1315
jason do
14-
pick [:circuit_id, :speed, :technology]
16+
pick [:avcid, :uniid, :speed, :technology]
1517
compact(true)
1618
end
1719

@@ -20,12 +22,16 @@ defmodule DiffoExample.Nbn.NbnEthernetValue do
2022
end
2123

2224
typed_struct do
23-
field :circuit_id, :string, description: "the unique NBN circuit identifier"
25+
field :avcid, :string, description: "the avcid from the owne Avc Resource"
26+
27+
field :uniid, :string, description: "the uniid from the owned Uni Resource"
2428

2529
field :speed, :integer, description: "the circuit download speed in Mbps"
2630

2731
field :technology, :atom,
28-
description: "the access technology (:FTTP, :FTTN, :HFC, :Fixed_Wireless)"
32+
description: "the access technology",
33+
constraints: [one_of: @technologies],
34+
default: :FTTP
2935
end
3036

3137
defimpl String.Chars do

lib/nbn/resources/cvc.ex

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ defmodule DiffoExample.Nbn.Cvc do
3434
id "d4e5f6a7-8b9c-4d0e-bf1a-3b4c5d6e7f8a"
3535
name "cvc"
3636
type :resourceSpecification
37+
3738
description "A Connectivity Virtual Circuit Resource Instance that aggregates AVCs and terminates at an NNI Group"
39+
3840
category "Network Resource"
3941
end
4042

@@ -46,14 +48,16 @@ defmodule DiffoExample.Nbn.Cvc do
4648
actions do
4749
create :build do
4850
description "creates a new CVC resource instance"
49-
accept [:id, :name, :type, :which]
51+
accept [:id, :which]
5052
argument :specified_by, :uuid, public?: false
5153
argument :relationships, {:array, :struct}
5254
argument :features, {:array, :uuid}, public?: false
5355
argument :characteristics, {:array, :uuid}, public?: false
5456
argument :places, {:array, :struct}
5557
argument :parties, {:array, :struct}
5658

59+
change set_attribute(:name, &DiffoExample.Nbn.Cvc.identifier/0)
60+
5761
change set_attribute(:type, :resource)
5862

5963
change before_action(fn changeset, _context -> ActionHelper.build_before(changeset) end)
@@ -99,4 +103,14 @@ defmodule DiffoExample.Nbn.Cvc do
99103
end)
100104
end
101105
end
106+
107+
attributes do
108+
attribute :cvcid, :string do
109+
default &DiffoExample.Nbn.Cvc.identifier/0
110+
end
111+
end
112+
113+
def identifier() do
114+
DiffoExample.Nbn.Util.identifier("CVC")
115+
end
102116
end

lib/nbn/resources/nbn_ethernet.ex

Lines changed: 53 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ defmodule DiffoExample.Nbn.NbnEthernet do
66
@moduledoc """
77
Diffo - TMF Service and Resource Management with a difference
88
9-
NbnEthernet - NBN Ethernet Circuit Resource Instance
9+
NbnEthernet - NBN Ethernet access Resource Instance
1010
11-
An NBN Ethernet circuit comprising a dedicated UNI and AVC resource.
12-
The circuit is related to its UNI, which in turn is aggregated by a CVC
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
1313
that terminates at an NNI Group.
1414
"""
1515

@@ -25,33 +25,39 @@ defmodule DiffoExample.Nbn.NbnEthernet do
2525
domain: Nbn
2626

2727
resource do
28-
description "An Ash Resource representing an NBN Ethernet Circuit"
28+
description "An Ash Resource representing an NBN Ethernet access"
2929
plural_name :NbnEthernets
3030
end
3131

3232
specification do
3333
id "f2a4c6e8-1b3d-4f5a-8c7e-9d0b2e4f6a8c"
3434
name "nbnEthernet"
3535
type :resourceSpecification
36-
description "An NBN Ethernet Circuit comprising a dedicated UNI and AVC"
36+
description "An NBN Ethernet access comprising a dedicated UNI and AVC"
3737
category "Network Resource"
3838
end
3939

4040
characteristics do
41-
characteristic :nbn_ethernet, DiffoExample.Nbn.NbnEthernetValue
41+
characteristic :pri, DiffoExample.Nbn.PriValue
42+
# values do
43+
# value :uniid, DiffoExample.Nbn.Uni, :owns, :name
44+
# value :avcid, DiffoExample.Nbn.Avc, :owns, :name
45+
# end
4246
end
4347

4448
actions do
4549
create :build do
46-
description "creates a new NBN Ethernet circuit resource instance"
47-
accept [:id, :name, :type, :which]
50+
description "creates a new NBN Ethernet access resource instance"
51+
accept [:id, :which]
4852
argument :specified_by, :uuid, public?: false
4953
argument :relationships, {:array, :struct}
5054
argument :features, {:array, :uuid}, public?: false
5155
argument :characteristics, {:array, :uuid}, public?: false
5256
argument :places, {:array, :struct}
5357
argument :parties, {:array, :struct}
5458

59+
change set_attribute(:name, &DiffoExample.Nbn.NbnEthernet.identifier/0)
60+
5561
change set_attribute(:type, :resource)
5662

5763
change before_action(fn changeset, _context -> ActionHelper.build_before(changeset) end)
@@ -65,7 +71,7 @@ defmodule DiffoExample.Nbn.NbnEthernet do
6571
end
6672

6773
update :define do
68-
description "defines the NBN Ethernet circuit"
74+
description "defines the NBN Ethernet access"
6975
argument :characteristic_value_updates, {:array, :term}
7076

7177
change after_action(fn changeset, result, _context ->
@@ -76,7 +82,7 @@ defmodule DiffoExample.Nbn.NbnEthernet do
7682
end
7783

7884
update :relate do
79-
description "relates the NBN Ethernet circuit with other instances (e.g. UNI)"
85+
description "relates the NBN Ethernet access with other instances (e.g. UNI)"
8086
argument :relationships, {:array, :struct}
8187

8288
change after_action(fn changeset, result, _context ->
@@ -85,5 +91,42 @@ defmodule DiffoExample.Nbn.NbnEthernet do
8591
do: {:ok, result}
8692
end)
8793
end
94+
95+
update :mine do
96+
description "updates the NBN Ethernet access with data mined from related instances"
97+
argument :characteristic_value_updates, {:array, :term}
98+
99+
change before_action(fn changeset, context ->
100+
DiffoExample.Nbn.NbnEthernet.mine_related(changeset, context)
101+
end)
102+
103+
change after_action(fn changeset, result, _context ->
104+
with {:ok, result} <- Characteristic.update_values(result, changeset),
105+
{:ok, result} <- Nbn.get_nbn_ethernet_by_id(result.id),
106+
do: {:ok, result}
107+
end)
108+
end
109+
end
110+
111+
def identifier() do
112+
DiffoExample.Nbn.Util.identifier("PRI")
113+
end
114+
115+
# mines related resource to characteristics
116+
def mine_related(changeset, _context) when is_struct(changeset, Ash.Changeset) do
117+
forward_relationships = Ash.Changeset.get_attribute(changeset, :forward_relationships)
118+
119+
pri_updates =
120+
Enum.into(forward_relationships, [], fn forward_relationship ->
121+
{:ok, related} = Diffo.Provider.get_instance_by_id(forward_relationship.target_id)
122+
{alias_to_id(forward_relationship.alias), related.name}
123+
end)
124+
125+
Ash.Changeset.force_set_argument(changeset, :characteristic_value_updates, pri: pri_updates)
126+
end
127+
128+
defp alias_to_id(alias) when is_atom(alias) do
129+
(Atom.to_string(alias) <> "id")
130+
|> String.to_atom()
88131
end
89132
end

lib/nbn/resources/nni.ex

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ defmodule DiffoExample.Nbn.Nni do
4444
actions do
4545
create :build do
4646
description "creates a new NNI resource instance"
47-
accept [:id, :name, :type, :which]
47+
accept [:id, :which]
4848
argument :specified_by, :uuid, public?: false
4949
argument :relationships, {:array, :struct}
5050
argument :features, {:array, :uuid}, public?: false
@@ -54,6 +54,8 @@ defmodule DiffoExample.Nbn.Nni do
5454

5555
change set_attribute(:type, :resource)
5656

57+
change set_attribute(:name, &DiffoExample.Nbn.Nni.identifier/0)
58+
5759
change before_action(fn changeset, _context -> ActionHelper.build_before(changeset) end)
5860

5961
change after_action(fn changeset, result, _context ->
@@ -85,5 +87,9 @@ defmodule DiffoExample.Nbn.Nni do
8587
do: {:ok, result}
8688
end)
8789
end
90+
91+
def identifier() do
92+
DiffoExample.Nbn.Util.identifier("NNI")
93+
end
8894
end
8995
end

lib/nbn/resources/nni_group.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ defmodule DiffoExample.Nbn.NniGroup do
4646
actions do
4747
create :build do
4848
description "creates a new NNI Group resource instance"
49-
accept [:id, :name, :type, :which]
49+
accept [:id, :name, :which]
5050
argument :specified_by, :uuid, public?: false
5151
argument :relationships, {:array, :struct}
5252
argument :features, {:array, :uuid}, public?: false

lib/nbn/resources/ntd.ex

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ defmodule DiffoExample.Nbn.Ntd do
4343
actions do
4444
create :build do
4545
description "creates a new NTD resource instance"
46-
accept [:id, :name, :type, :which]
46+
accept [:id, :which]
4747
argument :specified_by, :uuid, public?: false
4848
argument :relationships, {:array, :struct}
4949
argument :features, {:array, :uuid}, public?: false
@@ -53,6 +53,8 @@ defmodule DiffoExample.Nbn.Ntd do
5353

5454
change set_attribute(:type, :resource)
5555

56+
change set_attribute(:name, &DiffoExample.Nbn.Ntd.identifier/0)
57+
5658
change before_action(fn changeset, _context -> ActionHelper.build_before(changeset) end)
5759

5860
change after_action(fn changeset, result, _context ->
@@ -85,4 +87,8 @@ defmodule DiffoExample.Nbn.Ntd do
8587
end)
8688
end
8789
end
90+
91+
def identifier() do
92+
DiffoExample.Nbn.Util.identifier("NTD")
93+
end
8894
end

lib/nbn/resources/uni.ex

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ defmodule DiffoExample.Nbn.Uni do
99
Uni - User Network Interface Resource Instance
1010
1111
A UNI is the physical/logical interface at the customer premises. It is
12-
related to an NTD resource and to its parent NBN Ethernet circuit.
12+
related to an NTD resource and to its parent NBN Ethernet access.
1313
It is related to an AVC resource, which is in turn aggregated by a CVC.
1414
"""
1515

@@ -33,7 +33,7 @@ defmodule DiffoExample.Nbn.Uni do
3333
id "a1b2c3d4-5e6f-4a7b-8c9d-0e1f2a3b4c5d"
3434
name "uni"
3535
type :resourceSpecification
36-
description "A UNI Resource Instance related to an NTD and an NBN Ethernet circuit"
36+
description "A UNI Resource Instance related to an NTD and an NBN Ethernet access"
3737
category "Network Resource"
3838
end
3939

@@ -44,7 +44,7 @@ defmodule DiffoExample.Nbn.Uni do
4444
actions do
4545
create :build do
4646
description "creates a new UNI resource instance"
47-
accept [:id, :name, :type, :which]
47+
accept [:id, :which]
4848
argument :specified_by, :uuid, public?: false
4949
argument :relationships, {:array, :struct}
5050
argument :features, {:array, :uuid}, public?: false
@@ -54,6 +54,8 @@ defmodule DiffoExample.Nbn.Uni do
5454

5555
change set_attribute(:type, :resource)
5656

57+
change set_attribute(:name, &DiffoExample.Nbn.Uni.identifier/0)
58+
5759
change before_action(fn changeset, _context -> ActionHelper.build_before(changeset) end)
5860

5961
change after_action(fn changeset, result, _context ->
@@ -76,7 +78,7 @@ defmodule DiffoExample.Nbn.Uni do
7678
end
7779

7880
update :relate do
79-
description "relates the UNI with other instances (e.g. NTD, NBN Ethernet circuit)"
81+
description "relates the UNI with other instances (e.g. NTD, NBN Ethernet access)"
8082
argument :relationships, {:array, :struct}
8183

8284
change after_action(fn changeset, result, _context ->
@@ -86,4 +88,8 @@ defmodule DiffoExample.Nbn.Uni do
8688
end)
8789
end
8890
end
91+
92+
def identifier() do
93+
DiffoExample.Nbn.Util.identifier("UNI")
94+
end
8995
end

0 commit comments

Comments
 (0)