Add to_directory method to relevant object classes
[oam.git] / code / network-generator / network_generation / model / python / o_ran_cu.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 centralized unit (ORanCu)
19 and at the same time a location for an O-Cloud resource pool
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_cloud_du import ORanCloudDu
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 "IORanCu" interface
39 IORanCu = IORanNode
40
41
42 # Define an abstract O-RAN Node class
43 class ORanCu(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_cu_data: IORanCu = self._to_o_ran_cu_data(data)
50         super().__init__(cast(dict[str, Any], o_ran_cu_data), **kwargs)
51         self._o_ran_cloud_dus: list[ORanCloudDu] = self._calculate_o_ran_dus()
52
53     def _to_o_ran_cu_data(self, data: dict[str, Any]) -> IORanCu:
54         result: IORanCu = default_value
55         for key, key_type in IORanCu.__annotations__.items():
56             if key in data:
57                 result[key] = data[key]  # type: ignore
58         return result
59
60     def _calculate_o_ran_dus(self) -> list[ORanCloudDu]:
61         hex_ring_radius: int = (
62             self.parent.parent.parent
63             .spiral_radius_profile.oRanCuSpiralRadiusOfODus
64         )
65         hex_list: list[
66             Hex
67         ] = self.parent.parent.parent.spiral_radius_profile.oRanDuSpiral(
68             self.position, hex_ring_radius
69         )
70         result: list[ORanCloudDu] = []
71         for index, hex in enumerate(hex_list):
72             s: str = "00" + str(index)
73             name: str = "-".join(
74                 [self.name.replace("CU", "O-Cloud-DU"), s[len(s) - 2: len(s)]]
75             )
76             network_center: GeoLocation = self.parent.parent.parent.center
77             newGeo = Hexagon.hex_to_geo_location(
78                 self.layout, hex, network_center
79             ).json()
80             result.append(
81                 ORanCloudDu(
82                     {
83                         "name": name,
84                         "geoLocation": newGeo,
85                         "position": hex,
86                         "layout": self.layout,
87                         "parent": self,
88                     }
89                 )
90             )
91         return result
92
93     @property
94     def o_ran_cloud_dus(self) -> list[ORanCloudDu]:
95         return self._o_ran_cloud_dus
96
97     @property
98     def towers(self) -> list[Tower]:
99         result: list[Tower] = []
100         for du in self.o_ran_cloud_dus:
101             for tower in du.towers:
102                 result.append(tower)
103         return result
104
105     def termination_points(self) -> list[ORanTerminationPoint]:
106         result: list[ORanTerminationPoint] = super().termination_points()
107         phy_tp: str = "-".join([self.name, "phy".upper()])
108         result.append(ORanTerminationPoint({"tp-id": phy_tp, "name": phy_tp}))
109         for interface in ["e2", "o1"]:
110             id: str = "-".join([self.name, interface.upper()])
111             result.append(
112                 ORanTerminationPoint(
113                     {"id": id, "name": id, "supporter": phy_tp, "parent": self}
114                 )
115             )
116         return result
117
118     def to_topology_nodes(self) -> list[dict[str, Any]]:
119         result: list[dict[str, Any]] = super().to_topology_nodes()
120         # for o_ran_du in self.o_ran_dus: # TODO
121         #     result.extend(o_ran_du.to_topology_nodes())
122         for o_ran_cloud_du in self.o_ran_cloud_dus:
123             result.extend(o_ran_cloud_du.to_topology_nodes())
124         return result
125
126     def to_topology_links(self) -> list[dict[str, Any]]:
127         result: list[dict[str, Any]] = super().to_topology_links()
128         # for o_ran_du in self.o_ran_dus:
129         # result.extend(o_ran_du.to_topology_links())
130         for o_ran_cloud_du in self.o_ran_cloud_dus:
131             result.extend(o_ran_cloud_du.to_topology_links())
132         return result
133
134     def toKml(self) -> ET.Element:
135         o_ran_cu: ET.Element = ET.Element("Folder")
136         open: ET.Element = ET.SubElement(o_ran_cu, "open")
137         open.text = "1"
138         name: ET.Element = ET.SubElement(o_ran_cu, "name")
139         name.text = self.name
140         for o_ran_cloud_du in self.o_ran_cloud_dus:
141             o_ran_cu.append(o_ran_cloud_du.toKml())
142         return o_ran_cu
143
144     def toSvg(self) -> ET.Element:
145         return ET.Element("to-be-implemented")
146
147     def to_directory(self, parent_dir: str) -> None:
148         for o_ran_cloud_du in self.o_ran_cloud_dus:
149             o_ran_cloud_du.to_directory(parent_dir)