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