Add to_directory method to relevant object classes
[oam.git] / code / network-generator / network_generation / model / python / o_ran_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 an O-RAN distributed unit (ORanDu)
19 """
20 import xml.etree.ElementTree as ET
21 import os
22 from typing import Any, cast
23
24 from network_generation.model.python.o_ran_node import IORanNode, ORanNode
25 from network_generation.model.python.o_ran_termination_point import (
26     ORanTerminationPoint,
27 )
28
29
30 # Define the "IORanDu" interface
31 class IORanDu(IORanNode):
32     o_ran_ru_count: int
33
34
35 default_value: IORanDu = cast(
36     IORanDu,
37     {
38         **ORanNode.default(),
39         **{"oRanRuCount": 1},
40     },
41 )
42
43
44 # Define an abstract O-RAN Node class
45 class ORanDu(ORanNode):
46     def __init__(
47         self,
48         data: dict[str, Any] = cast(dict[str, Any], default_value),
49         **kwargs: dict[str, Any]
50     ) -> None:
51         o_ran_du_data: IORanDu = self._to_o_ran_du_data(data)
52         super().__init__(cast(dict[str, Any], o_ran_du_data), **kwargs)
53         self._o_ran_ru_count = (
54             o_ran_du_data["oRanRuCount"]
55             if o_ran_du_data and "oRanRuCount" in o_ran_du_data
56             else 1
57         )
58         self.type = "ntsim-ng-o-du"
59
60     def _to_o_ran_du_data(self, data: dict[str, Any]) -> IORanDu:
61         result: IORanDu = default_value
62         for key, key_type in IORanDu.__annotations__.items():
63             if key in data:
64                 result[key] = data[key]  # type: ignore
65         return result
66
67     def termination_points(self) -> list[ORanTerminationPoint]:
68         result: list[ORanTerminationPoint] = super().termination_points()
69         phy_tp: str = "-".join([self.name, "phy".upper()])
70         result.append(ORanTerminationPoint({"id": phy_tp, "name": phy_tp}))
71         for interface in ["e2", "o1", "ofhm", "ofhc", "ofhu", "ofhs"]:
72             id: str = "-".join([self.name, interface.upper()])
73             result.append(
74                 ORanTerminationPoint(
75                     {"id": id, "name": id, "supporter": phy_tp, "parent": self}
76                 )
77             )
78         return result
79
80     def to_topology_nodes(self) -> list[dict[str, Any]]:
81         result: list[dict[str, Any]] = super().to_topology_nodes()
82         return result
83
84     def to_topology_links(self) -> list[dict[str, Any]]:
85         result: list[dict[str, Any]] = super().to_topology_links()
86         for interface in ["e2", "o1"]:
87             link_id: str = "".join(
88                 [interface, ":", self.name, "<->", self.parent.name]
89             )
90             source_tp: str = "-".join([self.name, interface.upper()])
91             dest_tp: str = "-".join([self.parent.name, interface.upper()])
92             result.append(
93                 {
94                     "link-id": link_id,
95                     "source": {
96                         "source-node": self.name,
97                         "source-tp": source_tp,
98                     },
99                     "destination": {
100                         "dest-node": self.parent.name,
101                         "dest-tp": dest_tp,
102                     },
103                 }
104             )
105         return result
106
107     def toKml(self) -> ET.Element:
108         o_ran_du: ET.Element = ET.Element("Folder")
109         open: ET.Element = ET.SubElement(o_ran_du, "open")
110         open.text = "1"
111         name: ET.Element = ET.SubElement(o_ran_du, "name")
112         name.text = self.name
113         # for tower in self.towers:
114         #     o_ran_du.append(tower.toKml())
115         return o_ran_du
116
117     def toSvg(self) -> ET.Element:
118         return ET.Element("to-be-implemented")
119
120     def to_directory(self, parent_dir: str) -> None:
121         parent_path = os.path.join(parent_dir, self.type)
122         path = os.path.join(parent_path, self.name)
123         if not os.path.exists(parent_path):
124             os.makedirs(parent_path, exist_ok=True)
125         if not os.path.exists(path):
126             os.mkdir(path)