Create a script to generate a Topology
[oam.git] / code / network-topology-instance-generator / model / python / tapi_link.py
1 # Copyright 2022 highstreet technologies GmbH
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #     http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 #!/usr/bin/python
16 """
17 Module for the class representing a TAPI Link
18 """
19 from typing import Dict, Union
20 import uuid
21 from lxml import etree
22 from model.python.link_config import LinkConfig
23 from model.python.top import Top
24
25
26 class TapiLink(Top):
27     """
28     Class representing a TAPI Link object.
29     """
30
31     __data: dict = {}
32     __configuration: dict = {}
33
34     # constructor
35     def __init__(self, configuration: dict):
36         super().__init__(configuration)
37         self.__configuration = configuration
38         self.__link_configuration = LinkConfig(
39             topology_reference=configuration["topology_reference"],
40             name_prefix=configuration["name_prefix"],
41             provider=configuration["provider"],
42             consumer=configuration["consumer"]
43         )
44         link_data = self.__link_configuration.json()
45         self.__data = {
46             "uuid": str(uuid.uuid4()),
47             "name": [{
48                 "value-name": "topology-link-name",
49                 "value": link_data['link']['name']
50             }],
51             "transitioned-layer-protocol-name": ["inETH", "outETH"],
52             "administrative-state": "LOCKED",
53             "operational-state": "ENABLED",
54             "direction": "BIDIRECTIONAL",
55             "lifecycle-state": "INSTALLED",
56             "node-edge-point": [
57                 link_data['link']['a'],
58                 link_data['link']['z']
59             ],
60             "latency-characteristic": [{
61                 "traffic-property-name": "property-1",
62                 "queing-latency-characteristic": "queue-1",
63                 "fixed-latency-characteristic": "latency-1",
64                 "jitter-characteristic": "jitter-1",
65                 "wander-characteristic": "wander-1"
66             }],
67             "layer-protocol-name": ["ETH"],
68             "risk-characteristic": [{
69                 "risk-characteristic-name": "risk-name",
70                 "risk-identifier-list": [
71                     "risk-1"]}],
72             "validation-mechanism": [{
73                 "validation-mechanism": "mechanism-1",
74                 "validation-robustness": "very-robust",
75                 "layer-protocol-adjacency-validated": "validated"}],
76             "cost-characteristic": [{
77                 "cost-name": "cost",
78                 "cost-algorithm": "alg1",
79                 "cost-value": "value-1"}]}
80
81     # getter
82     def configuration(self) -> Dict[str, Dict]:
83         """
84         Getter for a json object representing the initial configuration of a TAPI Link.
85         :return TAPI Link configuration as json object.
86         """
87         return self.__configuration
88
89     def data(self) -> dict:
90         """
91         Getter for a json object representing the TAPI Link.
92         :return TAPI Link as json object.
93         """
94         return self.__data
95
96     def json(self) -> dict:
97         """
98         Getter for a json object representing the TAPI Link.
99         :return TAPI Link as json object.
100         """
101         return self.data()
102
103     def identifier(self) -> str:
104         """
105         Getter returning the TAPI Link identifier.
106         :return Object identifier as UUID.
107         """
108         return self.__data["uuid"]
109
110     def name(self) -> str:
111         """
112         Getter for TAPI Link name.
113         :return TAPI Link as json object.
114         """
115         return self.__link_configuration.json()['link']['name']
116
117     def svg(self, svg_x: int, svg_y: int) -> etree.Element:
118         """
119         Getter for a xml Element object representing the TAPI Link.
120         :return TAPI Link as svg object.
121         """
122
123         group = etree.Element("g")
124         group.attrib["class"] = "link"
125         title = etree.Element("title")
126         title.text = "\n TAPI Link\n id: " + \
127             self.identifier() + "\n name: " + self.name()
128         group.append(title)
129
130         # cubic bezier curves
131         source_x = self.__link_configuration.consumer_node_edge_point().svg_x()
132         source_y = self.__link_configuration.consumer_node_edge_point().svg_y()
133         target_x = self.__link_configuration.provider_node_edge_point().svg_x()
134         target_y = self.__link_configuration.provider_node_edge_point().svg_y()
135
136         path = etree.Element("path")
137         path.attrib["d"] = " ".join(["M", str(source_x), str(source_y),
138                                      "C", str(source_x), str(target_y),
139                                      str(target_x), str(source_y),
140                                      str(target_x), str(target_y)])
141         path.attrib["class"] = "link"
142         group.append(path)
143
144         return group