Add to_directory method to relevant object classes
[oam.git] / code / network-topology-instance-generator / model / python / tapi_common_context.py
1 #!/usr/bin/python
2
3 # Copyright 2022 highstreet technologies GmbH
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 #     http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 #!/usr/bin/python
18 """
19 Module for a class representing a TAPI Common Context
20 """
21 from typing import Dict, Union
22 import uuid
23 from xml.dom.minidom import Element
24 from lxml import etree
25 from model.python.tapi_topology_context import TapiTopologyContext
26 from model.python.top import Top
27
28
29 class TapiCommonContext(Top):
30     """
31     Class representing a TAPI Common Context object.
32     """
33
34     __configuration: dict = {}
35     __context: TapiTopologyContext = None
36     __data: dict = {
37         "tapi-common:context": {
38             "uuid": str(uuid.uuid4()),
39             "name": [{"value-name": "context-name",
40                       "value": "Generated Topology"}]}}
41
42     # constructor
43     def __init__(self, configuration: Dict[str, Union[str, Dict[str, int]]]):
44         super().__init__(configuration)
45         self.__configuration = configuration
46         self.__context = TapiTopologyContext(configuration)
47
48     # getter
49     def configuration(self) -> Dict[str, Dict]:
50         """
51         Getter for a json object representing the TAPI Common Context.
52         :return TAPI Common Context as json object.
53         """
54         return self.__configuration
55
56     def data(self) -> Dict:
57         """
58         Getter for a json object representing the TAPI Common Context.
59         :return TAPI Common Context as json object.
60         """
61         return self.__data
62
63     def json(self, running: bool) -> Dict:
64         """
65         Getter for a json object representing the TAPI Topology Context.
66         :return TAPI Common Context as json object.
67         """
68         result = self.data().copy()
69         if self.__context is not None and running is False:
70             result["tapi-common:context"].update(self.__context.json())
71
72         if running is True and "tapi-topology:topology-context" in result["tapi-common:context"]:
73             result["tapi-common:context"].pop("tapi-topology:topology-context")
74
75         return result
76
77     def identifier(self) -> str:
78         """
79         Getter returning the TAPI common context identifier.
80         :return Object identifier as string
81         """
82         return self.__data["tapi-common:context"]["uuid"]
83
84     def name(self) -> str:
85         """
86         Getter for object name.
87         :return Human readable string as name.
88         """
89         return self.data()["tapi-common:context"]["name"][0]["value"]
90
91     def __svg_width(self) -> int:
92         p = self.configuration()['network']['pattern']
93         return (p['smo'] * p['near-rt-ric'] * p['o-cu'] * p['o-du'] * p['fronthaul-gateway'] * p['o-ru'] * p['user-equipment'] + 1) * 4*2*self.FONTSIZE
94
95     def __svg_height(self) -> int:
96         return (8 * 11 + 6) * self.FONTSIZE
97
98     def svg(self, x, y) -> etree.Element:
99         """
100         Getter for a xml/svg Element object representing the TAPI Topology Context.
101         :return TAPI Common Context as SVG object.
102         """
103         root: Element = etree.Element(
104             "svg",
105             width=str(self.__svg_width()),
106             height=str(self.__svg_height()),
107             viewBox=" ".join([
108                 str(-3*self.FONTSIZE),
109                 str(-3*self.FONTSIZE),
110                 str(self.__svg_width()),
111                 str(self.__svg_height())]
112             ),
113             xmlns="http://www.w3.org/2000/svg"
114         )
115         desc=etree.Element("desc")
116         desc.text="\n context: " + self.identifier() + "\n name: " + self.name()
117         root.append(desc)
118
119         title=etree.Element("title")
120         if "name" in self.configuration()["network"]:
121           title.text=self.configuration()["network"]["name"]
122         else:
123           title.text="o-ran-sc-topology-view"
124         root.append(title)
125
126         root.append(self.__context.svg(x, y))
127         return root
128
129     def topology_context(self) -> TapiTopologyContext:
130         """
131         Getter for next level object (TapiTopologyContext).
132         :return TAPI Topology Context
133         """
134         return self.__context