Add to_directory method to relevant object classes
[oam.git] / code / network-generator / network_generation / model / python / nr_cell_du.py
1 # Copyright 2023 highstreet technologies USA CORP.
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 a 3GPP new radio cell du (NrCellDu)
19 """
20 import math
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 (
26     GeoLocation,
27     IGeoLocation,
28 )
29 from network_generation.model.python.o_ran_node import IORanNode, ORanNode
30 from network_generation.model.python.o_ran_termination_point import (
31     ORanTerminationPoint,
32 )
33 from network_generation.model.python.point import Point
34
35
36 # Define the "INrCellDu" interface
37 class INrCellDu(IORanNode):
38     cellAngle: int
39     cellScaleFactorForHandoverArea: int
40     maxReach: int
41     azimuth: int
42
43
44 default_value: INrCellDu = cast(
45     INrCellDu,
46     {
47         **ORanNode.default(),
48         **{
49             "cellAngle": 120,
50             "cellScaleFactorForHandoverArea": 0,
51             "maxReach": 100,
52             "azimuth": 120,
53         },
54     },
55 )
56
57
58 # Define an abstract O-RAN Node class
59 class NrCellDu(ORanNode):
60     def __init__(
61         self,
62         data: dict[str, Any] = cast(dict[str, Any], default_value),
63         **kwargs: dict[str, Any]
64     ) -> None:
65         cell_data: INrCellDu = self._to_cell_data(data)
66         super().__init__(cast(dict[str, Any], cell_data), **kwargs)
67         self._cell_angle: int = int(str(cell_data["cellAngle"]))
68         self._cell_scale_factor: int = int(
69             str(cell_data["cellScaleFactorForHandoverArea"])
70         )
71         self._maxReach: int = int(str(cell_data["maxReach"]))
72         self._azimuth: int = int(str(cell_data["azimuth"]))
73
74     def _to_cell_data(self, data: dict[str, Any]) -> INrCellDu:
75         result: INrCellDu = default_value
76         for key, key_type in INrCellDu.__annotations__.items():
77             if key in data:
78                 result[key] = data[key]  # type: ignore
79         return result
80
81     @property
82     def cell_angle(self) -> int:
83         return self._cell_angle
84
85     @cell_angle.setter
86     def cell_angle(self, value: int) -> None:
87         self._cell_angle = value
88
89     @property
90     def cell_scale_factor(self) -> int:
91         return self._cell_scale_factor
92
93     @cell_scale_factor.setter
94     def cell_scale_factor(self, value: int) -> None:
95         self._cell_scale_factor = value
96
97     @property
98     def maxReach(self) -> int:
99         return self._maxReach
100
101     @maxReach.setter
102     def maxReach(self, value: int) -> None:
103         self._maxReach = value
104
105     @property
106     def azimuth(self) -> int:
107         return self._azimuth
108
109     @azimuth.setter
110     def azimuth(self, value: int) -> None:
111         self._azimuth = value
112
113     def termination_points(self) -> list[ORanTerminationPoint]:
114         result: list[ORanTerminationPoint] = super().termination_points()
115         result.append(ORanTerminationPoint(
116             {"id": self.name, "name": self.name}
117         ))
118         return result
119
120     def to_topology_nodes(self) -> list[dict[str, Any]]:
121         # a cell is not a node it is a Termination Point
122         result: list[dict[str, Any]] = []  # super().to_topology_nodes()
123         return result
124
125     def to_topology_links(self) -> list[dict[str, Any]]:
126         # as a cell is not a node, it does not have links
127         result: list[dict[str, Any]] = []  # super().to_topology_links()
128         return result
129
130     def toKml(self) -> ET.Element:
131         placemark: ET.Element = ET.Element("Placemark")
132         name: ET.Element = ET.SubElement(placemark, "name")
133         name.text = self.name
134         style: ET.Element = ET.SubElement(placemark, "styleUrl")
135         style.text = "#" + self.__class__.__name__
136         multi_geometry: ET.Element = ET.SubElement(placemark, "MultiGeometry")
137         polygon: ET.Element = ET.SubElement(multi_geometry, "Polygon")
138         outer_boundary: ET.Element = ET.SubElement(polygon, "outerBoundaryIs")
139         linear_ring: ET.Element = ET.SubElement(outer_boundary, "LinearRing")
140         coordinates: ET.Element = ET.SubElement(linear_ring, "coordinates")
141
142         points: list[Point] = Hexagon.polygon_corners(
143             self.layout, self.position
144         )
145         method = (
146             self.parent.parent.parent.parent.parent.parent
147             .geo_location.point_to_geo_location
148         )
149         geo_locations: list[GeoLocation] = list(map(method, points))
150         text: list[str] = []
151
152         index: int = 1 + int(self._azimuth / self._cell_angle)
153         network_center: GeoLocation = (
154             self.parent.parent.parent.parent.parent.parent.geo_location
155         )
156
157         p1: int = (2 * index + 1) % 6
158         p2: int = (2 * index + 2) % 6
159         intersect1: Point = Point(
160             (points[p1].x + points[p2].x) / 2,
161             (points[p1].y + points[p2].y) / 2,
162         )
163         intersect_gl1: GeoLocation = network_center.point_to_geo_location(
164             intersect1
165         )
166
167         p3: int = (2 * index + 3) % 6
168         p4: int = (2 * index + 4) % 6
169         intersect2: Point = Point(
170             (points[p3].x + points[p4].x) / 2,
171             (points[p3].y + points[p4].y) / 2,
172         )
173         intersect_gl2: GeoLocation = network_center.point_to_geo_location(
174             intersect2
175         )
176
177         tower: GeoLocation = GeoLocation(cast(IGeoLocation, self.geo_location))
178         # TODO: Why a cast is required
179
180         cell_polygon: list[GeoLocation] = []
181         cell_polygon.append(tower)
182         cell_polygon.append(intersect_gl1)
183         cell_polygon.append(geo_locations[(2 * index + 2) % 6])
184         cell_polygon.append(geo_locations[(2 * index + 3) % 6])
185         cell_polygon.append(intersect_gl2)
186         # close polygon
187         cell_polygon.append(tower)
188
189         for gl in cell_polygon:
190             strs: list[str] = [
191                 str("%.6f" % float(gl.longitude)),
192                 str("%.6f" % float(gl.latitude)),
193                 str("%.6f" % float(gl.aboveMeanSeaLevel)),
194             ]
195             text.append(",".join(strs))
196         coordinates.text = " ".join(text)
197
198         if self.cell_scale_factor > 0:
199             scaled_polygon: ET.Element = ET.SubElement(
200                 multi_geometry, "Polygon")
201             scaled_outer_boundary: ET.Element = ET.SubElement(
202                 scaled_polygon, "outerBoundaryIs")
203             scaled_linear_ring: ET.Element = ET.SubElement(
204                 scaled_outer_boundary, "LinearRing")
205             scaled_coordinates: ET.Element = ET.SubElement(
206                 scaled_linear_ring, "coordinates")
207
208             arc: float = self.azimuth * math.pi / 180
209             meterToDegree: float = (
210                 2 * math.pi * GeoLocation().equatorialRadius / 360
211             )
212             centerX: float = self.layout.size.x * 0.5 * math.sin(arc)
213             centerY: float = self.layout.size.y * 0.5 * math.cos(arc)
214             cell_center: GeoLocation = GeoLocation(
215                 {
216                     "latitude": tower.latitude + centerY / meterToDegree,
217                     "longitude": tower.longitude + centerX / meterToDegree,
218                     "aboveMeanSeaLevel": tower.aboveMeanSeaLevel,
219                 }
220             )
221             point_index: int = 0
222             text = []
223             for gl in cell_polygon:
224                 scale: float = 1 + self.cell_scale_factor / 100
225                 lng_new: float = (
226                     1 * scale * (gl.longitude - cell_center.longitude)
227                 ) + cell_center.longitude
228                 lat_new: float = (
229                     1 * scale * (gl.latitude - cell_center.latitude)
230                 ) + cell_center.latitude
231                 scaled_strs: list[str] = [
232                     str("%.6f" % float(lng_new)),
233                     str("%.6f" % float(lat_new)),
234                     str("%.6f" % float(gl.aboveMeanSeaLevel)),
235                 ]
236                 text.append(",".join(scaled_strs))
237                 point_index += 1
238             scaled_coordinates.text = " ".join(text)
239         return placemark
240
241     def toSvg(self) -> ET.Element:
242         return ET.Element("to-be-implemented")
243
244     def to_directory(self, parent_dir: str) -> None:
245         pass