40fa66b8c3d71f4182b5598bd298cf2dbd302eee
[oam.git] / code / network-generator / network_generation / model / python / nr_cell_du.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 a 3GPP new radio cell du (NrCellDu)
19 """
20 import xml.etree.ElementTree as ET
21 from typing import Any, cast
22
23 import network_generation.model.python.hexagon as Hexagon
24 from network_generation.model.python.geo_location import (
25     GeoLocation,
26     IGeoLocation,
27 )
28 from network_generation.model.python.o_ran_node import IORanNode, ORanNode
29 from network_generation.model.python.o_ran_termination_point import (
30     ORanTerminationPoint,
31 )
32 from network_generation.model.python.point import Point
33
34
35 # Define the "INrCellDu" interface
36 class INrCellDu(IORanNode):
37     cell_angle: int
38     azimuth: int
39
40
41 default_value: INrCellDu = cast(
42     INrCellDu,
43     {
44         **ORanNode.default(),
45         **{"cellAngle": 120, "azimuth": 120},
46     },
47 )
48
49
50 # Define an abstract O-RAN Node class
51 class NrCellDu(ORanNode):
52     def __init__(
53         self,
54         data: dict[str, Any] = cast(dict[str, Any], default_value),
55         **kwargs: dict[str, Any]
56     ) -> None:
57         cell_data: INrCellDu = self._to_cell_data(data)
58         super().__init__(cast(dict[str, Any], cell_data), **kwargs)
59         self._cell_angle: int = int(str(cell_data["cellAngle"]))
60         self._azimuth: int = int(str(cell_data["azimuth"]))
61
62     def _to_cell_data(self, data: dict[str, Any]) -> INrCellDu:
63         result: INrCellDu = default_value
64         for key, key_type in INrCellDu.__annotations__.items():
65             if key in data:
66                 result[key] = data[key]  # type: ignore
67         return result
68
69     def termination_points(self) -> list[ORanTerminationPoint]:
70         result: list[ORanTerminationPoint] = super().termination_points()
71         result.append(
72             ORanTerminationPoint({"id": self.name, "name": self.name})
73         )
74         return result
75
76     def to_topology_nodes(self) -> list[dict[str, Any]]:
77         # a cell is not a node it is a Termination Point
78         result: list[dict[str, Any]] = []  # super().to_topology_nodes()
79         return result
80
81     def to_topology_links(self) -> list[dict[str, Any]]:
82         # as a cell is not a node, it does not have links
83         result: list[dict[str, Any]] = []  # super().to_topology_links()
84         return result
85
86     def toKml(self) -> ET.Element:
87         placemark: ET.Element = ET.Element("Placemark")
88         name: ET.Element = ET.SubElement(placemark, "name")
89         name.text = self.name
90         style: ET.Element = ET.SubElement(placemark, "styleUrl")
91         style.text = "#" + self.__class__.__name__
92         multi_geometry: ET.Element = ET.SubElement(placemark, "MultiGeometry")
93         polygon: ET.Element = ET.SubElement(multi_geometry, "Polygon")
94         outer_boundary: ET.Element = ET.SubElement(polygon, "outerBoundaryIs")
95         linear_ring: ET.Element = ET.SubElement(outer_boundary, "LinearRing")
96         coordinates: ET.Element = ET.SubElement(linear_ring, "coordinates")
97
98         points: list[Point] = Hexagon.polygon_corners(
99             self.layout, self.position
100         )
101         method = (
102             self.parent.parent.parent.parent.parent.parent
103             .geo_location.point_to_geo_location
104         )
105         geo_locations: list[GeoLocation] = list(map(method, points))
106         text: list[str] = []
107
108         index: int = 1 + int(self._azimuth / self._cell_angle)
109         network_center: GeoLocation = (
110             self.parent.parent.parent.parent.parent.parent.geo_location
111         )
112
113         intersect1: Point = Point(
114             (points[(2 * index + 1) % 6].x + points[(2 * index + 2) % 6].x)
115             / 2,
116             (points[(2 * index + 1) % 6].y + points[(2 * index + 2) % 6].y)
117             / 2,
118         )
119         intersect_geo_location1: GeoLocation = (
120             network_center.point_to_geo_location(intersect1)
121         )
122
123         intersect2: Point = Point(
124             (points[(2 * index + 3) % 6].x + points[(2 * index + 4) % 6].x)
125             / 2,
126             (points[(2 * index + 3) % 6].y + points[(2 * index + 4) % 6].y)
127             / 2,
128         )
129         intersect_geo_location2: GeoLocation = (
130             network_center.point_to_geo_location(intersect2)
131         )
132
133         tower: GeoLocation = GeoLocation(cast(IGeoLocation, self.geo_location))
134         # TODO: Why a cast is required
135
136         cell_polygon: list[GeoLocation] = []
137         cell_polygon.append(tower)
138         cell_polygon.append(intersect_geo_location1)
139         cell_polygon.append(geo_locations[(2 * index + 2) % 6])
140         cell_polygon.append(geo_locations[(2 * index + 3) % 6])
141         cell_polygon.append(intersect_geo_location2)
142         # close polygon
143         cell_polygon.append(tower)
144
145         for gl in cell_polygon:
146             index += 1
147             strs: list[str] = [
148                 str("%.6f" % float(gl.longitude)),
149                 str("%.6f" % float(gl.latitude)),
150                 str("%.6f" % float(gl.aboveMeanSeaLevel)),
151             ]
152             text.append(",".join(strs))
153         coordinates.text = " ".join(text)
154
155         return placemark
156
157     def toSvg(self) -> ET.Element:
158         return ET.Element("to-be-implemented")