python 3.10 type definitions
[oam.git] / code / network-generator / model / python / o_ran_network.py
1 # Copyright 2023 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 a class representing a O-RAN Network
18 """
19 from typing import Any
20
21 from model.python.o_ran_smo import ORanSmo
22 from model.python.o_ran_spiral_radius_profile import SpiralRadiusProfile
23 from model.python.o_ran_object import IORanObject, ORanObject
24 import model.python.hexagon as Hexagon
25 from model.python.hexagon import Layout
26 from model.python.point import Point
27 import xml.etree.ElementTree as ET
28
29
30 class ORanNetwork(ORanObject):
31     """
32     Class representing an O-RAN Network object.
33     """
34
35     # constructor
36     def __init__(self, configuration: dict[str, Any], of: IORanObject = None, **kwargs):
37         super().__init__(of, **kwargs)
38         self.__configuration = configuration
39         self.name = configuration["name"]
40         self.center = configuration["center"]
41         size = configuration["pattern"]["o-ran-ru"]["max-reach"]
42         layout = Layout(
43             Hexagon.layout_flat, Point(size, size), Point(0, 0)
44         )  # 1 pixel = 1 meter
45         spiral_radius_profile = SpiralRadiusProfile(
46             {
47                 "oRanSmoSpiralRadiusOfNearRtRics": configuration["pattern"]["smo"][
48                     "near-rt-ric-spiral-radius"
49                 ],
50                 "oRanNearRtRicSpiralRadiusOfOCus": configuration["pattern"][
51                     "near-rt-ric"
52                 ]["o-ran-cu-spiral-radius"],
53                 "oRanCuSpiralRadiusOfODus": configuration["pattern"]["o-ran-cu"][
54                     "o-ran-du-spiral-radius"
55                 ],
56                 "oRanDuSpiralRadiusOfTowers": configuration["pattern"]["o-ran-du"][
57                     "tower-spiral-radius"
58                 ],
59             }
60         )
61         self._o_ran_smo = ORanSmo(
62             {
63                 "name": "SMO",
64                 "geoLocation": self.center,
65                 "layout": layout,
66                 "spiralRadiusProfile": spiral_radius_profile,
67                 "parent": self,
68             }
69         )
70
71     # getter
72     def configuration(self) -> dict[str, dict]:
73         """
74         Getter for a json object representing the O-RAN Network.
75         :return O-RAN Network as json object.
76         """
77         return self.__configuration
78
79     def __appendNodes(self) -> list[dict[str, Any]]:
80         result: list[dict[str, Any]] = []
81         for tower in self._o_ran_smo.towers:
82             result.append(tower.toTopology())
83         return result
84
85     def toTopology(self) -> dict[str, Any]:
86         return {
87             "ietf-network:networks": {
88                 "network": [
89                     {
90                         "network-id": self.id,
91                         "node": self.__appendNodes(),
92                         "ietf-network-topology:link": [],
93                     }
94                 ],
95             }
96         }
97
98     def toKml(self) -> ET.Element:
99         root: ET.Element = ET.Element("kml", xmlns="http://www.opengis.net/kml/2.2")
100         document = ET.SubElement(root, "Document")
101         open = ET.SubElement(document, "open")
102         open.text = "1"
103         name = ET.SubElement(document, "name")
104         name.text = self.name
105         folder = ET.SubElement(document, "Folder")
106         open = ET.SubElement(folder, "open")
107         open.text = "1"
108         name = ET.SubElement(folder, "name")
109         name.text = "Towers"
110         for tower in self._o_ran_smo.towers:
111             folder.append(tower.toKml())
112
113         return root
114
115     def toSvg(self) -> ET.Element:
116         """
117         Getter for a xml/svg Element object representing the Network.
118         :return Network as SVG object.
119         """
120         root: ET.Element = ET.Element(
121             "svg",
122             # width=str(self.__svg_width()),
123             # height=str(self.__svg_height()),
124             # viewBox=" ".join([
125             #     str(-3*self.FONTSIZE),
126             #     str(-3*self.FONTSIZE),
127             #     str(self.__svg_width()),
128             #     str(self.__svg_height())]
129             # ),
130             xmlns="http://www.w3.org/2000/svg",
131         )
132         desc = ET.Element("desc")
133         # desc.text="\n context: " + str(self.id()) + "\n name: " + str(self.name())
134         root.append(desc)
135
136         title = ET.Element("title")
137         title.text = self.configuration()["name"]
138         root.append(title)
139
140         # root.append(self.__context.svg(x, y))
141         return root