dc6037a0e0387c5611c962b074d8d5c009087f66
[oam.git] / code / network-topology-instance-generator / view / network_viewer.py
1 # Copyright 2022 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 encodings
21 import json
22 from lxml import etree
23 from model.python.tapi_common_context import TapiCommonContext
24
25
26 class NetworkViewer:
27     """
28     This class contains all functions converting the Network into different formats
29     """
30     __network: TapiCommonContext = None
31
32     # constructor
33     def __init__(self, network: TapiCommonContext):
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 itsself, as it is json serializable
42         """
43         return self
44
45     def show_as_json(self) -> dict:
46         """
47         Method printing the class in json format.
48         """
49         print(self.__network.json())
50
51     def show(self):
52         """
53         Method printing the network
54         """
55         print(self.__network)
56
57     def save(self, filename: str):
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 = self.__network.json()
65             json.dump(output, json_file,
66                       ensure_ascii=False, indent=2)
67             for key in ["Node", "Link"]:
68                 print(key + "s:", len(output
69                                       ["tapi-common:context"]
70                                       ["tapi-topology:topology-context"]
71                                       ["topology"][0][key.lower()])
72                       )
73             print("File '" + filename + "' saved!")
74
75     def svg(self, filename: str):
76         """
77         Method saving the class content to a file in xml/svg format.
78
79         :param filename: A valid path to a file on the system.
80         :type filename: string
81         """
82         root = self.__network.svg(0, 0)
83         root.addprevious(
84             etree.ProcessingInstruction("xml-stylesheet",
85                                         'href="svg.style.css" type="text/css"')
86         )
87         etree.ElementTree(root).write(filename,
88                                       encoding="utf-8",
89                                       xml_declaration=True,
90                                       doctype=(
91                                           '<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"\n'
92                                           '  "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">'
93                                       ),
94                                       pretty_print=True
95                                       )
96         print("File '" + filename + "' saved!")