Add to_directory method to relevant object classes
[oam.git] / code / network-topology-instance-generator / model / python / tapi_node_smo.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 SMO as TAPI Node.
18 """
19 from typing import List
20 from lxml import etree
21 from model.python.tapi_node import TapiNode
22 from model.python.tapi_node_edge_point import TapiNodeEdgePoint
23
24
25 class TapiNodeSmo(TapiNode):
26     """
27     Class representing a SMO as TAPI Node
28     """
29     __width: 0
30
31     # constructor
32     def __init__(self, parent: TapiNode, config):
33         super().__init__(parent, config)
34
35         # add O2 consumer interface
36         nep_configuration = {
37             "parent": self.identifier(),
38             "nodeEdgePoint": {
39                 "interface": "o2", "cep": [{"protocol": "REST", "role": "consumer"}]
40             }
41         }
42         self.add(TapiNodeEdgePoint(nep_configuration))
43
44         # add A1 consumer interface
45         nep_configuration = {
46             "parent": self.identifier(),
47             "nodeEdgePoint": {
48                 "interface": "a1", "cep": [{"protocol": "REST", "role": "consumer"}]
49             }
50         }
51         self.add(TapiNodeEdgePoint(nep_configuration))
52
53         # add O1/OAM NetConf Consumer interface
54         nep_configuration = {
55             "parent": self.identifier(),
56             "nodeEdgePoint": {
57                 "interface": "oam", "cep": [{"protocol": "NETCONF", "role": "consumer"}]
58             }
59         }
60         self.add(TapiNodeEdgePoint(nep_configuration))
61
62         # add O1 VES Provider interface
63         nep_configuration = {
64             "parent": self.identifier(),
65             "nodeEdgePoint": {
66                 "interface": "o1", "cep": [{"protocol": "VES", "role": "provider"}]
67             }
68         }
69         self.add(TapiNodeEdgePoint(nep_configuration))
70
71         # add O1 File Transfer Consumer interface
72         nep_configuration = {
73             "parent": self.identifier(),
74             "nodeEdgePoint": {
75                 "interface": "o1", "cep": [{"protocol": "FILE", "role": "consumer"}]
76             }
77         }
78         self.add(TapiNodeEdgePoint(nep_configuration))
79
80     def __smo_component(self, x: int, y: int, label: str) -> etree.Element:
81         group = etree.Element("g")
82         group.attrib["class"] = " ".join(["node", label])
83
84         width = (2 + 2) * (2.2*self.FONTSIZE)
85         height = 2 * (2*self.FONTSIZE)
86
87         rect = etree.Element("rect")
88         rect.attrib["x"] = str(int(x - width/2))
89         rect.attrib["y"] = str(int(y - height/2))
90         rect.attrib["width"] = str(int(width))
91         rect.attrib["height"] = str(int(height))
92         rect.attrib["rx"] = str(self.FONTSIZE)
93         rect.attrib["class"] = " ".join(["node", label])
94         group.append(rect)
95
96         labelElement = etree.Element('text')
97         labelElement.attrib['x'] = str(x)
98         # +4px for font-size 14px (think of chars like 'gjy')
99         labelElement.attrib['y'] = str(y + 4)
100         labelElement.attrib['class'] = " ".join(["node", label])
101         labelElement.text = label.upper()
102         group.append(labelElement)
103         return group
104
105     def svg(self, x: int, y: int) -> etree.Element:
106         """
107         Getter for a xml Element object representing the TAPI Node.
108         :return TAPI Node as svg object.
109         """
110         super().svg(x, y)
111
112         components = ["o2-controller", "non-rt-ric", "oam-controller",
113                       "ves-collector", "file-server"]
114
115         group = etree.Element("g")
116         group.attrib["class"] = "node"
117         title = etree.Element("title")
118         title.text = "\n TAPI Node\n id: " + \
119             self.identifier() + "\n name: " + self.name()
120         group.append(title)
121
122         width = (len(components)*5 +1) * (2.2*self.FONTSIZE)
123         height = 2 * (2.2*self.FONTSIZE)
124
125         rect = etree.Element("rect")
126         rect.attrib["x"] = str(int(x - width/2))
127         rect.attrib["y"] = str(int(y - height/2))
128         rect.attrib["width"] = str(int(width))
129         rect.attrib["height"] = str(int(height))
130         rect.attrib["rx"] = str(self.FONTSIZE)
131         rect.attrib["class"] = " ".join(
132             ["node", self.function_label().lower()])
133         group.append(rect)
134
135         label = etree.Element('text')
136         label.attrib['x'] = str(x)
137         # +4px for font-size 14px (think of chars like 'gjy')
138         label.attrib['y'] = str(y + 4)
139         label.attrib['class'] = " ".join(
140             ["node", self.function_label().lower()])
141         label.text = self.function_label()
142         group.append(label)
143
144         for component in components:
145             x_mapping = {
146                 "o2-controller": -4*6*self.FONTSIZE,
147                 "non-rt-ric": -2*6*self.FONTSIZE,
148                 "oam-controller": -0*6*self.FONTSIZE,
149                 "ves-collector": +2*6*self.FONTSIZE,
150                 "file-server": +4*6*self.FONTSIZE
151             }
152             comp_x = x + x_mapping[component]
153             comp_y = y-0*self.FONTSIZE
154             group.append(self.__smo_component(comp_x, comp_y, component))
155
156         for nep in self.data()['owned-node-edge-point']:
157             nep_x = x + \
158                 super().x_offset_by_cep_name(
159                     nep.connection_edge_points()[0].name(), 0)
160             nep_y = y + \
161                 super().y_offset_by_cep_name(
162                     nep.connection_edge_points()[0].name())
163             group.append(nep.svg(nep_x, nep_y))
164
165         return group