Create a script to generate a Topology
[oam.git] / code / network-topology-instance-generator / model / python / svg / svg.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 Module for an abstract class called "Top".
18 This calls should be inherited for common functions
19 """
20 from lxml import etree
21
22
23 class Svg():
24     """
25     The abstract "SVG" class adds common functions
26     """
27
28     __center_x: int = 0
29     __center_y: int = 0
30     __width: int = 0
31     __height: int = 0
32     __label: str = "my label"
33     __tapi_object = None
34
35     FONTSIZE: int = 10  # see svg.style.css file
36
37     """
38     Constructor
39     """
40
41     def __init__(self, tapi_object, center_x: int, center_y: int) -> None:
42         self.__center_x = center_x
43         self.__center_y = center_y
44         self.__tapi_object = tapi_object
45         self.__label = tapi_object.name().upper()
46         self.__width = 4 * self.FONTSIZE # default for 1 CEP == cep.width()
47         self.__height = 2 * self.FONTSIZE
48
49     # getter
50     def center_x(self) -> int:
51         """
52         Getter for x coordinate of the SVG center position
53         :return Center x coordinate in pixel
54         """
55         return self.__center_x
56
57     def center_y(self) -> int:
58         """
59         Getter for y coordinate of the SVG center position
60         :return Center y coordinate in pixel
61         """
62         return self.__center_y
63
64     def width(self) -> int:
65         """
66         Getter for width of the SVG Element
67         :return Width in pixel
68         """
69         return self.__width
70
71     def height(self) -> int:
72         """
73         Getter for height of the SVG Element
74         :return Height in pixel
75         """
76         return self.__height
77
78     def label(self) -> str:
79         """
80         Getter for the short label as displayed of the SVG Element
81         :return Label of the TAPI object
82         """
83         return self.__label
84
85     def tapi_object(self) -> int:
86         """
87         Getter for the given TAPI object
88         :return Python TAPI object
89         """
90         return self.__tapi_object
91
92     def type_name(self) -> int:
93         """
94         Getter for the given TAPI object type name
95         :return TAPI object type name
96         """
97         return type(self.tapi_object()).__name__
98
99     def svg_group(self) -> etree.Element:
100         """
101         Mothod generating the root SVG Element for the TAPI object
102         :return SVG Element as root for the TAPI object
103         """
104         group: etree.Element = etree.Element("g")
105         group.attrib["class"] = " ".join(["node", self.type_name()])
106         title = etree.Element("title")
107         title.text = "\n".join(
108             [
109                 self.type_name(),
110                 "id: " + self.tapi_object().identifier(),
111                 "name: " + self.tapi_object().name()
112             ]
113         )
114         group.append(title)
115         return group
116
117     def svg_main(self) -> etree.Element:
118         """
119         Mothod generating the main SVG Element shaping the TAPI object
120         :return SVG Element as main representations for the TAPI object
121         """
122         main = etree.Element("ellipse")
123         main.attrib['cx'] = str(self.center_x())
124         main.attrib['cy'] = str(self.center_y())
125         main.attrib['rx'] = str(int(self.width()/2))
126         main.attrib['ry'] = str(int(self.height()/2))
127         main.attrib['class'] = " ".join([self.type_name()])
128         return main
129
130     def svg_label(self) -> etree.Element:
131         """
132         Mothod generating the SVG Element of the label of the TAPI object
133         :return SVG Element for the label of the TAPI object
134         """
135         label = etree.Element('text')
136         label.attrib['x'] = str(self.center_x())
137         # +4px for font-size 14px (think of chars like 'gjy')
138         label.attrib['y'] = str(self.center_y() + 4)
139         label.text = self.label()
140         return label
141
142     def svg_center(self) -> etree.Element:
143         """
144         Mothod generating the SVG Element of the label of the TAPI object
145         :return SVG Element for the label of the TAPI object
146         """
147         dot = etree.Element('circle')
148         dot.attrib['cx'] = str(self.center_x())
149         dot.attrib['cy'] = str(self.center_y())
150         dot.attrib['r'] = "2"
151         dot.attrib['class'] = "dot"
152         return dot
153
154     def svg_element(self) -> etree.Element:
155         """
156         Method generating a SVG Element representing the TAPI Object
157         :return A SVG group element including the main shape and a label
158         """
159         group: etree.Element = self.svg_group()
160         group.attrib['id'] = self.tapi_object().identifier()
161         group.append(self.svg_main())
162         group.append(self.svg_label())
163         # group.append(self.svg_center())
164         return group