Add to_directory method to relevant object classes
[oam.git] / code / network-generator / network_generation / 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
19 (ORanNearRtRic)
20 """
21 import xml.etree.ElementTree as ET
22 from typing import Any, cast
23
24 import network_generation.model.python.hexagon as Hexagon
25 from network_generation.model.python.geo_location import GeoLocation
26 from network_generation.model.python.hexagon import Hex
27 from network_generation.model.python.o_ran_cu import ORanCu
28 from network_generation.model.python.o_ran_node import (
29     IORanNode,
30     ORanNode,
31     default_value,
32 )
33 from network_generation.model.python.o_ran_termination_point import (
34     ORanTerminationPoint,
35 )
36 from network_generation.model.python.tower import Tower
37
38 # Define the "IORanNearRtRic" interface
39 IORanNearRtRic = IORanNode
40
41
42 # Define an abstract O-RAN Node class
43 class ORanNearRtRic(ORanNode):
44     def __init__(
45         self,
46         data: dict[str, Any] = cast(dict[str, Any], default_value),
47         **kwargs: dict[str, Any]
48     ) -> None:
49         o_ran_near_rt_ric_data: IORanNearRtRic = (
50             self._to_o_ran_near_rt_ric_data(data)
51         )
52         super().__init__(
53             cast(dict[str, Any], o_ran_near_rt_ric_data), **kwargs
54         )
55         self._o_ran_cus: list[ORanCu] = self._calculate_o_ran_cus()
56
57     def _to_o_ran_near_rt_ric_data(
58         self, data: dict[str, Any]
59     ) -> IORanNearRtRic:
60         result: IORanNearRtRic = default_value
61         for key, key_type in IORanNearRtRic.__annotations__.items():
62             if key in data:
63                 result[key] = data[key]  # type: ignore
64         return result
65
66     def _calculate_o_ran_cus(self) -> list[ORanCu]:
67         hex_ring_radius: int = (
68             self.parent.parent
69             .spiral_radius_profile.oRanNearRtRicSpiralRadiusOfOCus
70         )
71         hex_list: list[
72             Hex
73         ] = self.parent.parent.spiral_radius_profile.oRanCuSpiral(
74             self.position, hex_ring_radius
75         )
76         result: list[ORanCu] = []
77         for index, hex in enumerate(hex_list):
78             s: str = "00" + str(index)
79             name: str = "-".join(
80                 [self.name.replace("NearRtRic", "CU"), s[len(s) - 2: len(s)]]
81             )
82             network_center: GeoLocation = self.parent.parent.center
83             newGeo = Hexagon.hex_to_geo_location(
84                 self.layout, hex, network_center
85             ).json()
86             result.append(
87                 ORanCu(
88                     {
89                         "name": name,
90                         "geoLocation": newGeo,
91                         "position": hex,
92                         "layout": self.layout,
93                         "parent": self,
94                     }
95                 )
96             )
97         return result
98
99     @property
100     def o_ran_cus(self) -> list[ORanCu]:
101         return self._o_ran_cus
102
103     @property
104     def towers(self) -> list[Tower]:
105         result: list[Tower] = []
106         for cu in self.o_ran_cus:
107             for tower in cu.towers:
108                 result.append(tower)
109         return result
110
111     def termination_points(self) -> list[ORanTerminationPoint]:
112         result: list[ORanTerminationPoint] = super().termination_points()
113         phy_tp: str = "-".join([self.name, "phy".upper()])
114         result.append(ORanTerminationPoint({"tp-id": phy_tp, "name": phy_tp}))
115         for interface in ["a1", "o1", "o2", "e2"]:
116             id: str = "-".join([self.name, interface.upper()])
117             result.append(
118                 ORanTerminationPoint(
119                     {"id": id, "name": id, "supporter": phy_tp, "parent": self}
120                 )
121             )
122         return result
123
124     def to_topology_nodes(self) -> list[dict[str, Any]]:
125         result: list[dict[str, Any]] = super().to_topology_nodes()
126         for o_ran_cu in self.o_ran_cus:
127             result.extend(o_ran_cu.to_topology_nodes())
128         return result
129
130     def to_topology_links(self) -> list[dict[str, Any]]:
131         result: list[dict[str, Any]] = super().to_topology_links()
132         for o_ran_cu in self.o_ran_cus:
133             result.extend(o_ran_cu.to_topology_links())
134         return result
135
136     def toKml(self) -> ET.Element:
137         ric: ET.Element = ET.Element("Folder")
138         open: ET.Element = ET.SubElement(ric, "open")
139         open.text = "1"
140         name: ET.Element = ET.SubElement(ric, "name")
141         name.text = self.name
142         for o_ran_cu in self.o_ran_cus:
143             ric.append(o_ran_cu.toKml())
144         return ric
145
146     def toSvg(self) -> ET.Element:
147         return ET.Element("to-be-implemented")
148
149     def to_directory(self, parent_dir: str) -> None:
150         for o_ran_cu in self.o_ran_cus:
151             o_ran_cu.to_directory(parent_dir)