03d779b0951b98d7286388b0910ec50a0734bdd0
[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) -> 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:
70             result["tapi-common:context"].update(self.__context.json())
71         return result
72
73     def identifier(self) -> str:
74         """
75         Getter returning the TAPI common context identifier.
76         :return Object identifier as string
77         """
78         return self.__data["tapi-common:context"]["uuid"]
79
80     def name(self) -> str:
81         """
82         Getter for object name.
83         :return Human readable string as name.
84         """
85         return self.data()["tapi-common:context"]["name"][0]["value"]
86
87     def __svg_width(self) -> int:
88         p = self.configuration()['network']['pattern']
89         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
90
91     def __svg_height(self) -> int:
92         return (8 * 11 + 6) * self.FONTSIZE
93
94     def svg(self, x, y) -> etree.Element:
95         """
96         Getter for a xml/svg Element object representing the TAPI Topology Context.
97         :return TAPI Common Context as SVG object.
98         """
99         root: Element = etree.Element(
100             "svg",
101             width=str(self.__svg_width()),
102             height=str(self.__svg_height()),
103             viewBox=" ".join([
104                 str(-3*self.FONTSIZE),
105                 str(-3*self.FONTSIZE),
106                 str(self.__svg_width()),
107                 str(self.__svg_height())]
108             ),
109             xmlns="http://www.w3.org/2000/svg"
110         )
111         desc=etree.Element("desc")
112         desc.text="\n context: " + self.identifier() + "\n name: " + self.name()
113         root.append(desc)
114
115         title=etree.Element("title")
116         title.text=self.configuration()["network"]["name"]
117         root.append(title)
118
119         root.append(self.__context.svg(x, y))
120         return root
121
122     def topology_context(self) -> TapiTopologyContext:
123         """
124         Getter for next level object (TapiTopologyContext).
125         :return TAPI Topology Context
126         """
127         return self.__context