be8a1d031cc9911fe89709e7bcd57d995b0f578d
[oam.git] / code / network-topology-instance-generator / model / python / tapi_node_edge_point.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 containing a class representing a TAPI Node Edge Point
18 """
19 import uuid
20 from typing import Dict, List, Union
21 from lxml import etree
22 from model.python.svg.node_edge_point import NodeEdgePoint
23 from model.python.tapi_connection_edge_point import TapiConnectionEdgePoint
24 from model.python.top import Top
25
26
27 class TapiNodeEdgePoint(Top):
28     """
29     Class representing a TAPI Node Edge Point object
30     """
31
32     __data: Dict = {}
33     __configuration: Dict = {
34         "parent": "unknown",
35         "nodeEdgePoint": {
36             "interface": "unknown-interface",
37             "protocol": "unknown-protocol",
38             "role": "consumer"
39         }
40     }
41     __ceps: List[TapiConnectionEdgePoint] = []
42
43     # constructor
44     def __init__(self, configuration: dict):
45         super().__init__(configuration)
46         self.__configuration = configuration
47         self.__ceps = []
48         self.__data = {
49             "uuid": str(uuid.uuid4()),
50             "name": [{
51                 "value-name": "interface-name",
52                 "value": self.name()
53             }],
54             "administrative-state": "LOCKED",
55             "operational-state": "ENABLED",
56             "lifecycle-state": "INSTALLED",
57             "link-port-role": "SYMMETRIC",
58             "layer-protocol-name": "ETH",
59             "supported-cep-layer-protocol-qualifier": [
60                 "tapi-dsr:DIGITAL_SIGNAL_TYPE_GigE"
61             ],
62             "link-port-direction": "BIDIRECTIONAL",
63             "termination-state": self.termination_state(),
64             "termination-direction": self.termination_direction()
65         }
66         for cep in configuration['nodeEdgePoint']['cep']:
67             cep["parent"] = {
68                 "node": self.parent(),
69                 "node-local-id": self.local_id(),
70                 "node-edge-point": self.__data["uuid"],
71                 "interface": self.interface()
72             }
73             self.__ceps.append(TapiConnectionEdgePoint(cep))
74
75     # getter
76     def __x_offset_by_cep_name(self, name) -> int:
77         mapping: Dict[str, int] = {
78             "o2-rest-consumer": 0*self.FONTSIZE,
79             "a1-rest-consumer": 0*self.FONTSIZE,
80             "oam-netconf-consumer": 0*self.FONTSIZE,
81             "o1-ves-provider": 0*self.FONTSIZE,
82             "o1-file-consumer": 0*self.FONTSIZE,
83
84             "o2-rest-provider": 0*self.FONTSIZE,
85             "a1-rest-provider": 0*self.FONTSIZE,
86             "e2-rest-consumer": 0*self.FONTSIZE,
87
88             "f1-c-unknown-consumer": 0*self.FONTSIZE,
89             "f1-u-unknown-consumer": 0*self.FONTSIZE,
90
91             "e1-unknown-provider": +2.5*self.FONTSIZE,
92             "e1-unknown-consumer": -2.5*self.FONTSIZE,
93
94             "e2-rest-provider": 0*self.FONTSIZE,
95             "f1-c-unknown-provider": 0*self.FONTSIZE,
96             "f1-u-unknown-provider": 0*self.FONTSIZE,
97             "f1-unknown-provider": 0*self.FONTSIZE,
98             "o1-netconf-provider": -4*self.FONTSIZE,
99             "o1-ves-consumer": 0*self.FONTSIZE,
100             "o1-file-provider": +4*self.FONTSIZE,
101             "ofh-netconf-consumer": 0*self.FONTSIZE,
102
103             "eth-ofh-provider": 0*self.FONTSIZE,
104             "oam-netconf-provider": 0*self.FONTSIZE,
105             "eth-ofh-consumer": 0*self.FONTSIZE,
106
107             "ofh-netconf-provider": 0*self.FONTSIZE,
108             "uu-unknown-provider": 0*self.FONTSIZE,
109
110             "uu-unknown-consumer": 0*self.FONTSIZE
111         }
112         if name in mapping:
113             return mapping[name]
114
115         print(" NEP: CEP name", name, "for x postion calculation not found")
116         return 0
117
118     def __y_offset_by_cep_name(self, name: str) -> int:
119         mapping: Dict[str, int] = {
120             "o2-rest-consumer": -1.5*self.FONTSIZE,
121             "a1-rest-consumer": -1.5*self.FONTSIZE,
122             "oam-netconf-consumer": -1.5*self.FONTSIZE,
123             "o1-ves-provider": -1.5*self.FONTSIZE,
124             "o1-file-consumer": -1.5*self.FONTSIZE,
125
126             "o2-rest-provider": +1.5*self.FONTSIZE,
127             "a1-rest-provider": +1.5*self.FONTSIZE,
128             "e2-rest-consumer": -1.5*self.FONTSIZE,
129
130             "e1-unknown-provider": 0*self.FONTSIZE,
131             "e1-unknown-consumer": 0*self.FONTSIZE,
132
133             "f1-c-unknown-consumer": -1.5*self.FONTSIZE,
134             "f1-u-unknown-consumer": -1.5*self.FONTSIZE,
135
136             "e2-rest-provider": +1.5*self.FONTSIZE,
137             "f1-c-unknown-provider": +1.5*self.FONTSIZE,
138             "f1-u-unknown-provider": +1.5*self.FONTSIZE,
139             "f1-unknown-provider": +1.5*self.FONTSIZE,
140             "o1-netconf-provider": +1.5*self.FONTSIZE,
141             "o1-ves-consumer": +1.5*self.FONTSIZE,
142             "o1-file-provider": +1.5*self.FONTSIZE,
143             "ofh-netconf-consumer": -1.5*self.FONTSIZE,
144
145             "eth-ofh-provider": +1.5*self.FONTSIZE,
146             "oam-netconf-provider": +1.5*self.FONTSIZE,
147             "eth-ofh-consumer": -1.5*self.FONTSIZE,
148
149             "ofh-netconf-provider": +1.5*self.FONTSIZE,
150             "uu-unknown-provider": -1.5*self.FONTSIZE,
151
152             "uu-unknown-consumer": +1.5*self.FONTSIZE
153         }
154         if name in mapping:
155             return mapping[name]
156
157         print(" NEP: CEP name", name, "for y postion calculation not found")
158         return 0
159
160     def configuration(self) -> dict:
161         """
162         Getter for a json object representing the TAPI Node Edge Point intiail
163         configuration.
164         :return TAPI Node Edge Point configuration as json object.
165         """
166         return self.__configuration
167
168     def data(self) -> dict:
169         """
170         Getter for a json object representing the TAPI Node Edge Point.
171         :return TAPI Node Edge Point as json object.
172         """
173         return self.__data
174
175     def identifier(self) -> str:
176         """
177         Getter returning the TAPI Node Edge Point identifier.
178         :return Object identifier as UUID.
179         """
180         return self.__data["uuid"]
181
182     def json(self) -> dict:
183         """
184         Getter for a json object representing the TAPI Node Edge Point.
185         :return TAPI Node Edge Point as json object.
186         """
187         result = self.__data.copy()
188         result['tapi-connectivity:cep-list'] = {}
189         result['tapi-connectivity:cep-list']['connection-end-point'] = []
190         for cep in self.connection_edge_points():
191             result['tapi-connectivity:cep-list']['connection-end-point'].append(
192                 cep.json())
193         return result
194
195     def local_id(self) -> int:
196         if "local-id" in self.configuration()["nodeEdgePoint"]:
197             return self.configuration()["nodeEdgePoint"]["local-id"]
198         return 0
199
200     def name(self) -> str:
201         """
202         Getter a human readable identifier of the TAPI Node Edge Point.
203         :return TAPI Node Edge Point name as String.
204         """
205         result = self.interface().lower()
206         if "local-id" in self.configuration()["nodeEdgePoint"]:
207             result = "#".join(
208                 [result, str(self.configuration()["nodeEdgePoint"]["local-id"])])
209         return result
210
211     def interface(self) -> str:
212         """
213         Getter a human readable identifier of the TAPI Node Edge Point interface.
214         :return Interface label.
215         """
216         return self.__configuration['nodeEdgePoint']['interface'].lower()
217
218     def connection_edge_points(self) -> List[TapiConnectionEdgePoint]:
219         """
220         Getter a human readable identifier of the TAPI Node Edge Point interface.
221         :return Interface label.
222         """
223         return self.__ceps
224
225     def parent(self) -> str:
226         """
227         Getter returning the identifier the the TAPI Node hosting the Node
228         Edge Point.
229         :return Identifier of the TAPI Node containing this NEP.
230         """
231         return self.__configuration["parent"]
232
233     def svg_x(self) -> int:
234         return self.__svg_x
235
236     def svg_y(self) -> int:
237         return self.__svg_y
238
239     def svg(self, x: int, y: int) -> etree.Element:
240         """
241         Getter for a xml Element object representing the TAPI Node Edge Point.
242         :return TAPI Node Edge Point as SVG object.
243         """
244         self.__svg_x = x
245         self.__svg_y = y
246
247         svg_nep = NodeEdgePoint(self, x, y)
248         group: etree.Element = svg_nep.svg_element()
249
250         for cep in self.connection_edge_points():
251             cep_x = x + self.__x_offset_by_cep_name(cep.name())
252             cep_y = y + self.__y_offset_by_cep_name(cep.name())
253             group.append(cep.svg(cep_x, cep_y))
254         return group
255
256     def termination_direction(self) -> str:
257         """
258         Getter returning the TAPI Node Edge Point direction.
259         :return TAPI Node Edge Point direction as String.
260         """
261         value = "BIDIRECTIONAL"
262         mapping = {
263             "consumer": "SINK",
264             "provider": "SOURCE"
265         }
266         if self.__configuration['nodeEdgePoint']['cep'][0]['role'].lower() in mapping:
267             return mapping[self.__configuration['nodeEdgePoint']['cep'][0]['role'].lower()]
268         return value
269
270     def termination_state(self) -> str:
271         """
272         Getter returning the TAPI Node Edge Point state.
273         :return TAPI Node Edge Point state as String.
274         """
275         return "LT_PERMENANTLY_TERMINATED"