Create an init version of a network viewer
[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     __network: ORanNetwork = None
30
31     # constructor
32     def __init__(self, network: ORanNetwork):
33         self.__network = network
34
35     # json format
36
37     def json(self) -> 'NetworkViewer':
38         """
39         Getter returns the class as json object
40         :return The class itself, as it is json serializable
41         """
42         return self
43
44     def show_as_json(self) -> dict:
45         """
46         Method printing the class in json format.
47         """
48         print(self.__network.json())
49
50     def show(self):
51         """
52         Method printing the network
53         """
54         print(self.__network)
55
56     def save(self, filename: str):
57         """
58         Method saving the class content to a file in json format.
59         :param filename: A valid path to a file on the system.
60         :type filename: string
61         """
62         with open(filename, "w", encoding='utf-8') as json_file:
63             output = self.__network.toTopology()
64             json.dump(output, json_file,
65                       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):
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,
89                                       encoding="utf-8",
90                                       xml_declaration=True
91                                       )
92         print("File '" + filename + "' saved!")
93
94     def kml(self, filename: str):
95         """
96         Method saving the class content to a file in xml/kml format.
97
98         :param filename: A valid path to a file on the system.
99         :type filename: string
100         """
101         root = self.__network.toKml()
102         style = ET.Element("style")
103         style.text = self.readStylesFromFile()
104         root.append(style)
105         ET.ElementTree(root).write(filename,
106                                       encoding="utf-8",
107                                       xml_declaration=True
108                                       )
109         print("File '" + filename + "' saved!")