Move all business logic code under template folder
[oam.git] / code / network-generator / model / python / o_ran_near_rt_ric.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 Near real-time intelligent controller (ORanNearRtRic)
19 """
20 from typing import overload
21 from model.python.tower import Tower
22 from model.python.o_ran_cu import ORanCu
23 from model.python.o_ran_object import IORanObject
24 from model.python.o_ran_node import ORanNode
25 from model.python.o_ran_termination_point import ORanTerminationPoint
26 from model.python.hexagon import Hex
27 import model.python.hexagon as Hexagon
28 import xml.etree.ElementTree as ET
29
30
31 # Define the "IORanNearRtRic" interface
32 class IORanNearRtRic(IORanObject):
33     def __init__(self, **kwargs):
34         super().__init__(**kwargs)
35
36
37 # Define an abstract O-RAN Node class
38 class ORanNearRtRic(ORanNode, IORanNearRtRic):
39     def __init__(self, o_ran_near_rt_ric_data: IORanNearRtRic = None, **kwargs):
40         super().__init__(o_ran_near_rt_ric_data, **kwargs)
41         self._o_ran_cus: list[ORanCu] = self._calculate_o_ran_cus()
42
43     def _calculate_o_ran_cus(self) -> list[ORanCu]:
44         hex_ring_radius: int = self.spiralRadiusProfile.oRanNearRtRicSpiralRadiusOfOCus
45         hex_list: list[Hex] = self.spiralRadiusProfile.oRanCuSpiral(
46             self.position, hex_ring_radius
47         )
48         result: list[ORanCu] = []
49         for index, hex in enumerate(hex_list):
50             s: str = "00" + str(index)
51             name: str = "-".join(
52                 [self.name.replace("NearRtRic", "CU"), s[len(s) - 2 : len(s)]]
53             )
54             network_center: dict = self.parent.parent.center
55             newGeo = Hexagon.hex_to_geo_location(
56                 self.layout, hex, network_center
57             ).json()
58             result.append(
59                 ORanCu(
60                     {
61                         "name": name,
62                         "geoLocation": newGeo,
63                         "position": hex,
64                         "layout": self.layout,
65                         "spiralRadiusProfile": self.spiralRadiusProfile,
66                         "parent": self
67                     }
68                 )
69             )
70         return result
71
72     @property
73     def o_ran_cus(self) -> list[ORanCu]:
74         return self._o_ran_cus
75
76     @property
77     def towers(self) -> list[Tower]:
78         result: list[Tower] = []
79         for cu in self.o_ran_cus:
80             for tower in cu.towers:
81                 result.append(tower)
82         return result
83
84     @property
85     def termination_points(self) -> list[ORanTerminationPoint]:
86         result: list[ORanTerminationPoint] = super().termination_points
87         phy_tp: str = "-".join([self.name, "phy".upper()])
88         result.append({"tp-id": phy_tp, "name": phy_tp})
89         for interface in ["a1", "o1", "o2", "e2"]:
90             id:str = "-".join([self.name, interface.upper()])
91             result.append(ORanTerminationPoint({"id": id, "name":id, "supporter": phy_tp, "parent":self}))
92         return result
93
94     def to_topology_nodes(self) -> list[dict[str, dict]]:
95         result: list[dict[str, dict]] = super().to_topology_nodes()
96         for o_ran_cu in self.o_ran_cus:
97             result.extend(o_ran_cu.to_topology_nodes())
98         return result
99
100     def to_topology_links(self) -> list[dict[str, dict]]:
101         result: list[dict[str, dict]] = super().to_topology_links()
102         for o_ran_cu in self.o_ran_cus:
103             result.extend(o_ran_cu.to_topology_links())
104         return result
105     
106     def toKml(self) -> ET.Element:
107         ric: ET.Element = ET.Element("Folder")
108         open: ET.Element = ET.SubElement(ric, "open")
109         open.text = "1"
110         name: ET.Element = ET.SubElement(ric, "name")
111         name.text = self.name
112         for o_ran_cu in self.o_ran_cus:
113             ric.append(o_ran_cu.toKml())
114         return ric
115
116     def toSvg(self) -> None:
117         return None