Reformat files according to template
[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 json
21 import xml.etree.ElementTree as ET
22 from abc import abstractmethod, abstractproperty
23 from typing import Any
24
25 import network_generation.model.python.hexagon as Hexagon
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     def __init__(
45         self,
46         address: AddressType = None,
47         geoLocation: GeoLocation = None,
48         url: str = None,
49         position: Hex = None,
50         layout: Layout = None,
51         spiralRadiusProfile: SpiralRadiusProfile = None,
52         parent=None,
53         **kwargs
54     ):
55         super().__init__(**kwargs)
56         self.address = address
57         self.geoLocation = geoLocation
58         self.url = url
59         self.position = position
60         self.layout = layout
61         self.spiralRadiusProfile = (spiralRadiusProfile,)
62         self.parent = parent
63
64
65 # Define an abstract O-RAN Node class
66 class ORanNode(ORanObject, IORanNode):
67     def __init__(self, of: IORanNode = None, **kwargs):
68         super().__init__(of, **kwargs)
69         self.address = of["address"] if of and "address" in of else None
70         self.geoLocation = (
71             of["geoLocation"] if of and "geoLocation" in of else GeoLocation()
72         )
73         self.url = of["url"] if of and "url" in of else self.id
74         self.position = (
75             of["position"] if of and "position" in of else Hex(0, 0, 0)
76         )
77         self.layout = (
78             of["layout"]
79             if of and "layout" in of
80             else Layout(Hexagon.layout_flat, Point(1, 1), Point(0, 0))
81         )
82         self.spiralRadiusProfile = (
83             of["spiralRadiusProfile"]
84             if of and "spiralRadiusProfile" in of
85             else SpiralRadiusProfile()
86         )
87         self.parent = of["parent"] if of and "parent" in of else None
88         self._termination_points: list[ORanTerminationPoint] = []
89
90     @property
91     def address(self) -> str:
92         return self._address
93
94     @address.setter
95     def address(self, value: str):
96         self._address = value
97
98     @property
99     def geoLocation(self) -> GeoLocation:
100         return self._geographicalLocation
101
102     @geoLocation.setter
103     def geoLocation(self, value: GeoLocation):
104         self._geographicalLocation = value
105
106     @property
107     def url(self) -> str:
108         return self._url
109
110     @url.setter
111     def url(self, value: str):
112         self._url = value
113
114     @property
115     def position(self) -> Hex:
116         return self._position
117
118     @position.setter
119     def position(self, value: Hex):
120         self._position = value
121
122     @property
123     def layout(self) -> Layout:
124         return self._layout
125
126     @layout.setter
127     def layout(self, value: Layout):
128         self._layout = value
129
130     @property
131     def spiralRadiusProfile(self) -> SpiralRadiusProfile:
132         return self._spiralRadiusProfile
133
134     @spiralRadiusProfile.setter
135     def spiralRadiusProfile(self, value: SpiralRadiusProfile):
136         self._spiralRadiusProfile = value
137
138     @property
139     def parent(
140         self,
141     ) -> Any:  # expected are ORanNodes and all inherits for ORanNode
142         return self._parent
143
144     @parent.setter
145     def parent(self, value: Any):
146         self._parent = value
147
148     @abstractproperty
149     def termination_points(self) -> list[ORanTerminationPoint]:
150         return self._termination_points
151
152     def json(self) -> dict[str, dict]:
153         result: dict = super().json()
154         result["address"] = self.address
155         result["geoLocation"] = self.geoLocation
156         result["url"] = self.url
157         result["layout"] = self.layout
158         result["spiralRadiusProfile"] = self.spiralRadiusProfile
159         result["parent"] = self.parent
160         return result
161
162     @abstractmethod
163     def to_topology_nodes(self) -> list[dict[str, dict]]:
164         tps: list[dict[str, dict]] = []
165         for tp in self.termination_points:
166             if (
167                 str(type(tp))
168                 == "<class 'model.python.o_ran_termination_point.ORanTerminationPoint'>"
169             ):
170                 tps.append(tp.to_topology())
171
172         result: list[dict[str, dict]] = []
173         result.append(
174             {
175                 "node-id": self.name,
176                 "ietf-network-topology:termination-point": tps,
177             }
178         )
179         return result
180
181     @abstractmethod
182     def to_topology_links(self) -> list[dict[str, dict]]:
183         result: list[dict[str, dict]] = []
184         source_tp: str = "-".join([self.name, "phy".upper()])
185         dest_tp: str = "-".join([self.parent.name, "phy".upper()])
186         if self.parent and not "Tower" in source_tp and not "Tower" in dest_tp:
187             link_id: str = "".join(
188                 ["phy", ":", self.name, "<->", self.parent.name]
189             )
190             link = {
191                 "link-id": link_id,
192                 "source": {"source-node": self.name, "source-tp": source_tp},
193                 "destination": {
194                     "dest-node": self.parent.name,
195                     "dest-tp": dest_tp,
196                 },
197             }
198             result.append(link)
199         return result
200
201     @abstractmethod
202     def toKml(self) -> ET.Element | None:
203         pass
204
205     @abstractmethod
206     def toSvg(self) -> ET.Element | None:
207         pass