Create ietf-network-topology json
[oam.git] / code / network-generator / model / python / o_ran_ru.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 """
18 A Class representing an O-RAN radio unit (ORanRu)
19 """
20 from typing import overload
21
22 from model.python.o_ran_du import ORanDu
23 from model.python.o_ran_termination_point import ORanTerminationPoint
24 from model.python.nr_cell_du import NrCellDu
25 from model.python.o_ran_object import IORanObject
26 from model.python.o_ran_node import ORanNode
27 import xml.etree.ElementTree as ET
28
29
30 # Define the "IORanRu" interface
31 class IORanRu(IORanObject):
32     def __init__(self, cell_count: int, ru_angle: int, ru_azimuth: int, **kwargs):
33         super().__init__(**kwargs)
34         self._cell_count = cell_count
35         self._ru_angle = ru_angle
36         self._ru_azimuth = ru_azimuth
37
38
39 # Define an abstract O-RAN Node class
40 class ORanRu(ORanNode, IORanRu):
41     def __init__(self, o_ran_ru_data: IORanRu = None, **kwargs):
42         super().__init__(o_ran_ru_data, **kwargs)
43         self._cell_count = (
44             o_ran_ru_data["cellCount"]
45             if o_ran_ru_data and "cellCount" in o_ran_ru_data
46             else 1
47         )
48         self._ru_angle = (
49             o_ran_ru_data["ruAngle"]
50             if o_ran_ru_data and "ruAngle" in o_ran_ru_data
51             else 120
52         )
53         self._ru_azimuth = (
54             o_ran_ru_data["ruAzimuth"]
55             if o_ran_ru_data and "ruAzimuth" in o_ran_ru_data
56             else 0
57         )
58         self._cells: list[NrCellDu] = self._create_cells()
59         name: str = self.name.replace("RU", "DU")
60         self._oRanDu: ORanDu = ORanDu(
61             {
62                 "name": name,
63                 "geoLocation": self.parent.geoLocation,
64                 "position": self.parent.position,
65                 "layout": self.layout,
66                 "parent": self.parent.parent.parent,
67             }
68         )
69
70     def _create_cells(self) -> list[NrCellDu]:
71         result: list[NrCellDu] = []
72         cell_angle: int = (
73             self.parent.parent.parent.parent.parent.parent.configuration()["pattern"][
74                 "nr-cell-du"
75             ]["cell-angle"]
76         )
77         for index in range(self._cell_count):
78             s: str = "00" + str(index)
79             name: str = "-".join(
80                 [self.name.replace("RU", "NRCellDu"), s[len(s) - 2 : len(s)]]
81             )
82             azimuth: int = index * cell_angle + self._ru_azimuth
83             result.append(
84                 NrCellDu(
85                     {
86                         "name": name,
87                         "geoLocation": self.geoLocation,
88                         "position": self.position,
89                         "layout": self.layout,
90                         "spiralRadiusProfile": self.spiralRadiusProfile,
91                         "parent": self,
92                         "cellAngle": cell_angle,
93                         "azimuth": azimuth,
94                     }
95                 )
96             )
97         return result
98
99     @property
100     def cells(self) -> list[NrCellDu]:
101         return self._cells
102
103     @property
104     def oRanDu(self) -> ORanDu:
105         return self._oRanDu
106
107     @property
108     def termination_points(self) -> list[ORanTerminationPoint]:
109         result: list[ORanTerminationPoint] = super().termination_points
110         phy_tp: str = "-".join([self.name, "phy".upper()])
111         result.append(ORanTerminationPoint({"id": phy_tp, "name": phy_tp}))
112         for interface in ["ofhm", "ofhc", "ofhu", "ofhs"]:
113             id: str = "-".join([self.name, interface.upper()])
114             result.append(
115                 ORanTerminationPoint(
116                     {"id": id, "name": id, "supporter": phy_tp, "parent": self}
117                 )
118             )
119         for cell in self.cells:
120             result.extend(cell.termination_points)
121         return result
122
123     def to_topology_nodes(self) -> list[dict[str, dict]]:
124         result: list[dict[str, dict]] = super().to_topology_nodes()
125         result.extend(self.oRanDu.to_topology_nodes())
126         return result
127
128     def to_topology_links(self) -> list[dict[str, dict]]:
129         result: list[dict[str, dict]] = super().to_topology_links()
130         result.extend(self.oRanDu.to_topology_links())
131         for interface in ["phy", "ofhm", "ofhc", "ofhu", "ofhs"]:
132             link_id: str = "".join([interface, ":", self.name, "<->", self.oRanDu.name])
133             source_tp: str = "-".join([self.name, interface.upper()])
134             dest_tp: str = "-".join([self.oRanDu.name, interface.upper()])
135             result.append(
136                 {
137                     "link-id": link_id,
138                     "source": {"source-node": self.name, "source-tp": source_tp},
139                     "destination": {"dest-node": self.oRanDu.name, "dest-tp": dest_tp},
140                 }
141             )
142         return result
143
144     def toKml(self) -> ET.Element:
145         o_ran_ru: ET.Element = ET.Element("Folder")
146         open: ET.Element = ET.SubElement(o_ran_ru, "open")
147         open.text = "1"
148         name: ET.Element = ET.SubElement(o_ran_ru, "name")
149         name.text = self.name
150         for cell in self.cells:
151             o_ran_ru.append(cell.toKml())
152         return o_ran_ru
153
154     def toSvg(self) -> None:
155         return None