@@ -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
89132end
0 commit comments