Create ietf-network-topology json
[oam.git] / code / network-generator / view / network_viewer.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 Provides functions to convert the Network into different formats
18 """
19
20 import json
21 from model.python.o_ran_network import ORanNetwork
22 import xml.etree.ElementTree as ET
23
24
25 class NetworkViewer:
26     """
27     This class contains all functions converting the Network into different formats
28     """
29
30     __network: ORanNetwork = None
31
32     # constructor
33     def __init__(self, network: ORanNetwork):
34         self.__network = network
35
36     # json format
37
38     def json(self) -> "NetworkViewer":
39         """
40         Getter returns the class as json object
41         :return The class itself, as it is json serializable
42         """
43         return self
44
45     def show_as_json(self) -> dict[str, dict]:
46         """
47         Method printing the class in json format.
48         """
49         print(self.__network.json())
50
51     def show(self) -> None:
52         """
53         Method printing the network
54         """
55         print(self.__network)
56
57     def save(self, filename: str) -> None:
58         """
59         Method saving the class content to a file in json format.
60         :param filename: A valid path to a file on the system.
61         :type filename: string
62         """
63         with open(filename, "w", encoding="utf-8") as json_file:
64             output:dict[str, dict] = self.__network.to_topology()
65             json.dump(output, json_file, ensure_ascii=False, indent=2)
66             print("File '" + filename + "' saved!")
67
68     def readStylesFromFile(self) -> str:
69         """
70         Method reading the css styles from known file
71         return: content of the file as string
72         """
73         with open("view/svg.style.css") as styles:
74             content = styles.read()
75             return content
76
77     def svg(self, filename: str) -> None:
78         """
79         Method saving the class content to a file in xml/svg format.
80
81         :param filename: A valid path to a file on the system.
82         :type filename: string
83         """
84         root = self.__network.toSvg()
85         style = ET.Element("style")
86         style.text = self.readStylesFromFile()
87         root.findall(".//desc")[0].append(style)
88         ET.ElementTree(root).write(filename, encoding="utf-8", xml_declaration=True)
89         print("File '" + filename + "' saved!")
90
91     def kml(self, filename: str) -> None:
92         """
93         Method saving the class content to a file in xml/kml format.
94
95         :param filename: A valid path to a file on the system.
96         :type filename: string
97         """
98         root = self.__network.toKml()
99         with open("view/kml.styles.json") as kml_styles:
100             styles: dict[str, dict] = json.load(kml_styles)
101             for key, value in styles.items():
102                 # add style
103                 style = ET.Element("Style", {"id": key})
104                 line_style = ET.SubElement(style, "LineStyle")
105                 color = ET.SubElement(line_style, "color")
106                 color.text = value["stroke"]["color"]
107                 width = ET.SubElement(line_style, "width")
108                 width.text = value["stroke"]["width"]
109                 poly_style = ET.SubElement(style, "PolyStyle")
110                 fill = ET.SubElement(poly_style, "color")
111                 fill.text = value["fill"]["color"]
112                 root.findall(".//Document")[0].append(style)
113
114         ET.ElementTree(root).write(filename, encoding="utf-8", xml_declaration=True)
115         print("File '" + filename + "' saved!")