6a2b3cb4f434fc88952ac29aff7b401cf477bf66
[oam.git] / code / network-generator / network_generation / model / python / o_ran_node.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 An abstract Class for O-RAN Node
19 """
20 import xml.etree.ElementTree as ET
21 from abc import abstractmethod
22 from typing import Any, cast
23
24 import network_generation.model.python.hexagon as Hexagon
25 from network_generation.model.python.countries import Country
26 from network_generation.model.python.geo_location import GeoLocation
27 from network_generation.model.python.hexagon import Hex, Layout
28 from network_generation.model.python.o_ran_object import (
29     IORanObject,
30     ORanObject,
31 )
32 from network_generation.model.python.o_ran_spiral_radius_profile import (
33     SpiralRadiusProfile,
34 )
35 from network_generation.model.python.o_ran_termination_point import (
36     ORanTerminationPoint,
37 )
38 from network_generation.model.python.point import Point
39 from network_generation.model.python.type_definitions import AddressType
40
41
42 # Define the "IORanObject" interface
43 class IORanNode(IORanObject):
44     address: AddressType
45     geoLocation: GeoLocation
46     url: str
47     position: Hex
48     layout: Layout
49     spiralRadiusProfile: SpiralRadiusProfile
50     parent: Any
51
52
53 default_address: AddressType = {
54     "street": "highstreet",
55     "building": "none",
56     "city": "heaven",
57     "room": "frist",
58     "zip": "12345",
59     "state": "none",
60     "country": Country.Germany,
61 }
62 default_value: IORanNode = cast(
63     IORanNode,
64     {
65         **ORanObject.default(),
66         **{
67             "address": default_address,
68             "geoLocation": GeoLocation(),
69             "url": "non-url",
70             "position": Hex(0, 0, 0),
71             "layout": Layout(Hexagon.layout_flat, Point(1, 1), Point(0, 0)),
72             "spiralRadiusProfile": SpiralRadiusProfile(),
73             "parent": None,
74         },
75     },
76 )
77
78
79 # Define an abstract O-RAN Node class
80 class ORanNode(ORanObject):
81     @staticmethod
82     def default() -> dict[str, Any]:
83         return cast(dict[str, Any], default_value)
84
85     def __init__(
86         self,
87         data: dict[str, Any] = cast(dict[str, Any], default_value),
88         **kwargs: dict[str, Any]
89     ) -> None:
90         o_ran_node_data: IORanNode = self._to_o_ran_node_data(data)
91         super().__init__(cast(dict[str, Any], data), **kwargs)
92         self._address: AddressType = cast(
93             AddressType, o_ran_node_data["address"]
94         )
95         self._geo_location: GeoLocation = cast(
96             GeoLocation, o_ran_node_data["geoLocation"]
97         )
98         self._url: str = str(o_ran_node_data["url"])
99         self._position: Hex = cast(Hex, o_ran_node_data["position"])
100         self._layout: Layout = cast(Layout, o_ran_node_data["layout"])
101         self._spiral_radius_profile: SpiralRadiusProfile = cast(
102             SpiralRadiusProfile, o_ran_node_data["spiralRadiusProfile"]
103         )
104         self._parent: Any = o_ran_node_data["parent"]
105         self._termination_points: list[ORanTerminationPoint] = []
106
107     def _to_o_ran_node_data(self, data: dict[str, Any]) -> IORanNode:
108         result: IORanNode = default_value
109         for key, key_type in IORanNode.__annotations__.items():
110             if key in data:
111                 result[key] = data[key]  # type: ignore
112         return result
113
114     @property
115     def address(self) -> AddressType:
116         return self._address
117
118     @address.setter
119     def address(self, value: AddressType) -> None:
120         self._address = value
121
122     @property
123     def geo_location(self) -> GeoLocation:
124         return self._geo_location
125
126     @geo_location.setter
127     def geo_location(self, value: GeoLocation) -> None:
128         self._geo_location = value
129
130     @property
131     def url(self) -> str:
132         return self._url
133
134     @url.setter
135     def url(self, value: str) -> None:
136         self._url = value
137
138     @property
139     def position(self) -> Hex:
140         return self._position
141
142     @position.setter
143     def position(self, value: Hex) -> None:
144         self._position = value
145
146     @property
147     def layout(self) -> Layout:
148         return self._layout
149
150     @layout.setter
151     def layout(self, value: Layout) -> None:
152         self._layout = value
153
154     @property
155     def spiral_radius_profile(self) -> SpiralRadiusProfile:
156         return self._spiral_radius_profile
157
158     @spiral_radius_profile.setter
159     def spiral_radius_profile(self, value: SpiralRadiusProfile) -> None:
160         self._spiral_radius_profile = value
161
162     @property
163     def parent(
164         self,
165     ) -> Any:  # expected are ORanNodes and all inherits for ORanNode
166         return self._parent
167
168     @parent.setter
169     def parent(self, value: Any) -> None:
170         self._parent = value
171
172     # @property
173     # @abstractmethod
174     def termination_points(self) -> list[ORanTerminationPoint]:
175         return self._termination_points
176
177     @abstractmethod
178     def to_topology_nodes(self) -> list[dict[str, Any]]:
179         tps: list[dict[str, Any]] = []
180         for tp in self.termination_points():
181             tps.append(tp.to_topology())
182
183         result: list[dict[str, Any]] = []
184         result.append(
185             {
186                 "node-id": self.name,
187                 "ietf-network-topology:termination-point": tps,
188             }
189         )
190         return result
191
192     @abstractmethod
193     def to_topology_links(self) -> list[dict[str, Any]]:
194         result: list[dict[str, Any]] = []
195         source_tp: str = "-".join([self.name, "phy".upper()])
196         dest_tp: str = "-".join([self.parent.name, "phy".upper()])
197         if self.parent and "Tower" not in source_tp and "Tower" not in dest_tp:
198             link_id: str = "".join(
199                 ["phy", ":", self.name, "<->", self.parent.name]
200             )
201             link = {
202                 "link-id": link_id,
203                 "source": {"source-node": self.name, "source-tp": source_tp},
204                 "destination": {
205                     "dest-node": self.parent.name,
206                     "dest-tp": dest_tp,
207                 },
208             }
209             result.append(link)
210         return result
211
212     @abstractmethod
213     def toKml(self) -> ET.Element | None:
214         pass
215
216     @abstractmethod
217     def toSvg(self) -> ET.Element:
218         pass