|
| 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 |
0 commit comments