Add to_directory method to relevant object classes
[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 json
21 from lxml import etree
22 from model.python.tapi_common_context import TapiCommonContext
23
24
25 class NetworkViewer:
26     """
27     This class contains all functions converting the Network into different formats
28     """
29     __network: TapiCommonContext = None
30
31     # constructor
32     def __init__(self, network: TapiCommonContext):
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 itsself, 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, running: bool):
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.json(running)
64             json.dump(output, json_file,
65                       ensure_ascii=False, indent=2)
66             if running is False:
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 readStylesFromFile(self) -> str:
76         """
77         Method reading the css styles from known file
78         return: content of the file as string
79         """
80         with open('view/svg.style.css') as styles:
81             content = styles.read()
82             return content
83
84     def svg(self, filename: str):
85         """
86         Method saving the class content to a file in xml/svg format.
87
88         :param filename: A valid path to a file on the system.
89         :type filename: string
90         """
91         root = self.__network.svg(0, 0)
92         # not preferred see OAM-257
93         # root.addprevious(
94         #     etree.ProcessingInstruction("xml-stylesheet",
95         #                                 'href="svg.style.css" type="text/css"')
96         # )
97         style = etree.Element("style")
98         style.text = self.readStylesFromFile()
99         root.getchildren()[0].addnext(style)
100         etree.ElementTree(root).write(filename,
101                                       encoding="utf-8",
102                                       xml_declaration=True,
103                                       doctype=(
104                                           '<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"\n'
105                                           '  "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">'
106                                       ),
107                                       pretty_print=True
108                                       )
109         print("File '" + filename + "' saved!")