Abort power setup if setting gainloss fails
[transportpce.git] / tests / transportpce_tests / pce / test01_pce.py
1 #!/usr/bin/env python
2 ##############################################################################
3 # Copyright (c) 2017 Orange, Inc. and others.  All rights reserved.
4 #
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
9 ##############################################################################
10
11 # pylint: disable=no-member
12 # pylint: disable=too-many-public-methods
13
14 import unittest
15 import os
16 # pylint: disable=wrong-import-order
17 import sys
18 import time
19 import requests
20 sys.path.append('transportpce_tests/common/')
21 # pylint: disable=wrong-import-position
22 # pylint: disable=import-error
23 import test_utils  # nopep8
24
25
26 class TransportPCEtesting(unittest.TestCase):
27     path_computation_input_data = {
28         "service-name": "service-1",
29         "resource-reserve": "true",
30         "service-handler-header": {
31             "request-id": "request1"
32         },
33         "service-a-end": {
34             "service-rate": "100",
35             "clli": "NodeA",
36             "service-format": "Ethernet",
37             "node-id": "XPDRA01"
38         },
39         "service-z-end": {
40             "service-rate": "100",
41             "clli": "NodeC",
42             "service-format": "Ethernet",
43             "node-id": "XPDRC01"
44         },
45         "pce-routing-metric": "hop-count"
46     }
47
48     simple_topo_bi_dir_data = None
49     simple_topo_uni_dir_data = None
50     complex_topo_uni_dir_data = None
51     port_mapping_data = None
52     processes = None
53
54     @classmethod
55     def setUpClass(cls):
56         # pylint: disable=bare-except
57         sample_files_parsed = False
58         time.sleep(10)
59         try:
60             TOPO_BI_DIR_FILE = os.path.join(os.path.dirname(os.path.realpath(__file__)),
61                                             "..", "..", "sample_configs", "honeynode-topo.json")
62             with open(TOPO_BI_DIR_FILE, 'r', encoding='utf-8') as topo_bi_dir:
63                 cls.simple_topo_bi_dir_data = topo_bi_dir.read()
64
65             TOPO_UNI_DIR_FILE = os.path.join(os.path.dirname(os.path.realpath(__file__)),
66                                              "..", "..", "sample_configs", "NW-simple-topology.json")
67
68             with open(TOPO_UNI_DIR_FILE, 'r', encoding='utf-8') as topo_uni_dir:
69                 cls.simple_topo_uni_dir_data = topo_uni_dir.read()
70
71             TOPO_UNI_DIR_COMPLEX_FILE = os.path.join(os.path.dirname(os.path.realpath(__file__)),
72                                                      "..", "..", "sample_configs", "NW-for-test-5-4.json")
73             with open(TOPO_UNI_DIR_COMPLEX_FILE, 'r', encoding='utf-8') as topo_uni_dir_complex:
74                 cls.complex_topo_uni_dir_data = topo_uni_dir_complex.read()
75             PORT_MAPPING_FILE = os.path.join(os.path.dirname(os.path.realpath(__file__)),
76                                              "..", "..", "sample_configs", "pce_portmapping_121.json")
77             with open(PORT_MAPPING_FILE, 'r', encoding='utf-8') as port_mapping:
78                 cls.port_mapping_data = port_mapping.read()
79             sample_files_parsed = True
80         except PermissionError as err:
81             print("Permission Error when trying to read sample files\n", err)
82             sys.exit(2)
83         except FileNotFoundError as err:
84             print("File Not found Error when trying to read sample files\n", err)
85             sys.exit(2)
86         except:
87             print("Unexpected error when trying to read sample files\n", sys.exc_info()[0])
88             sys.exit(2)
89         finally:
90             if sample_files_parsed:
91                 print("sample files content loaded")
92
93         cls.processes = test_utils.start_tpce()
94         time.sleep(5)
95
96     @classmethod
97     def tearDownClass(cls):
98         # clean datastores
99         test_utils.del_portmapping()
100         test_utils.del_ietf_network('openroadm-topology')
101         # pylint: disable=not-an-iterable
102         for process in cls.processes:
103             test_utils.shutdown_process(process)
104         print("all processes killed")
105
106     def setUp(self):  # instruction executed before each test method
107         time.sleep(1)
108
109     # Load port mapping
110     def test_01_load_port_mapping(self):
111         response = test_utils.post_portmapping(self.port_mapping_data)
112         self.assertIn(response['status_code'], (requests.codes.created, requests.codes.no_content))
113         time.sleep(1)
114
115     # Load simple bidirectional topology
116     def test_02_load_simple_topology_bi(self):
117         response = test_utils.put_ietf_network('openroadm-topology', self.simple_topo_bi_dir_data)
118         self.assertIn(response['status_code'], (requests.codes.ok, requests.codes.no_content))
119         time.sleep(1)
120
121     # Get existing nodeId
122     def test_03_get_nodeId(self):
123         response = test_utils.get_ietf_network_node_request('openroadm-topology', 'ROADMA01-SRG1', 'config')
124         self.assertEqual(response['status_code'], requests.codes.ok)
125         self.assertEqual(response['node']['node-id'], 'ROADMA01-SRG1')
126         time.sleep(1)
127
128     # Get existing linkId
129     def test_04_get_linkId(self):
130         response = test_utils.get_ietf_network_link_request(
131             'openroadm-topology', 'XPDRA01-XPDR1-XPDR1-NETWORK1toROADMA01-SRG1-SRG1-PP1-TXRX', 'config')
132         self.assertEqual(response['status_code'], requests.codes.ok)
133         self.assertEqual(response['link']['link-id'], 'XPDRA01-XPDR1-XPDR1-NETWORK1toROADMA01-SRG1-SRG1-PP1-TXRX')
134         time.sleep(1)
135
136     # Path Computation success
137     def test_05_path_computation_xpdr_bi(self):
138         response = test_utils.transportpce_api_rpc_request('transportpce-pce',
139                                                            'path-computation-request',
140                                                            self.path_computation_input_data)
141         self.assertEqual(response['status_code'], requests.codes.ok)
142         self.assertIn('Path is calculated',
143                       response['output']['configuration-response-common']['response-message'])
144         time.sleep(2)
145
146     # Path Computation success
147     def test_06_path_computation_rdm_bi(self):
148         self.path_computation_input_data["service-a-end"]["node-id"] = "ROADMA01"
149         self.path_computation_input_data["service-z-end"]["node-id"] = "ROADMC01"
150         response = test_utils.transportpce_api_rpc_request('transportpce-pce',
151                                                            'path-computation-request',
152                                                            self.path_computation_input_data)
153         self.assertEqual(response['status_code'], requests.codes.ok)
154         self.assertIn('Path is calculated',
155                       response['output']['configuration-response-common']['response-message'])
156         time.sleep(2)
157
158     # Load simple bidirectional topology
159     def test_07_load_simple_topology_uni(self):
160         response = test_utils.put_ietf_network('openroadm-topology', self.simple_topo_uni_dir_data)
161         self.assertIn(response['status_code'], (requests.codes.ok, requests.codes.no_content))
162         time.sleep(1)
163
164     # Get existing nodeId
165     def test_08_get_nodeId(self):
166         response = test_utils.get_ietf_network_node_request('openroadm-topology', 'XPONDER-1-2', 'config')
167         self.assertEqual(response['status_code'], requests.codes.ok)
168         self.assertEqual(response['node']['node-id'], 'XPONDER-1-2')
169         time.sleep(1)
170
171     # Get existing linkId
172     def test_09_get_linkId(self):
173         response = test_utils.get_ietf_network_link_request(
174             'openroadm-topology', 'XPONDER-1-2XPDR-NW1-TX-toOpenROADM-1-2-SRG1-SRG1-PP1-RX', 'config')
175         self.assertEqual(response['status_code'], requests.codes.ok)
176         self.assertEqual(response['link']['link-id'], 'XPONDER-1-2XPDR-NW1-TX-toOpenROADM-1-2-SRG1-SRG1-PP1-RX')
177         time.sleep(1)
178
179     # Path Computation success
180     def test_10_path_computation_xpdr_uni(self):
181         self.path_computation_input_data["service-a-end"]["node-id"] = "XPONDER-1-2"
182         self.path_computation_input_data["service-a-end"]["clli"] = "ORANGE1"
183         self.path_computation_input_data["service-z-end"]["node-id"] = "XPONDER-3-2"
184         self.path_computation_input_data["service-z-end"]["clli"] = "ORANGE3"
185         response = test_utils.transportpce_api_rpc_request('transportpce-pce',
186                                                            'path-computation-request',
187                                                            self.path_computation_input_data)
188         self.assertEqual(response['status_code'], requests.codes.ok)
189         self.assertIn('Path is calculated',
190                       response['output']['configuration-response-common']['response-message'])
191         time.sleep(2)
192
193     # Path Computation success
194     def test_11_path_computation_rdm_uni(self):
195         self.path_computation_input_data["service-a-end"]["node-id"] = "OpenROADM-2-1"
196         self.path_computation_input_data["service-a-end"]["clli"] = "cll21"
197         self.path_computation_input_data["service-z-end"]["node-id"] = "OpenROADM-2-2"
198         self.path_computation_input_data["service-z-end"]["clli"] = "ncli22"
199         response = test_utils.transportpce_api_rpc_request('transportpce-pce',
200                                                            'path-computation-request',
201                                                            self.path_computation_input_data)
202         self.assertEqual(response['status_code'], requests.codes.ok)
203         self.assertIn('Path is calculated',
204                       response['output']['configuration-response-common']['response-message'])
205         # ZtoA path test
206         atozList = len(response['output']['response-parameters']['path-description']['aToZ-direction']['aToZ'])
207         ztoaList = len(response['output']['response-parameters']['path-description']['zToA-direction']['zToA'])
208         self.assertEqual(atozList, 15)
209         self.assertEqual(ztoaList, 15)
210         for i in range(0, 15):
211             atoz = response['output']['response-parameters']['path-description']['aToZ-direction']['aToZ'][i]
212             ztoa = response['output']['response-parameters']['path-description']['zToA-direction']['zToA'][i]
213             if atoz['id'] == '14':
214                 self.assertEqual(atoz['resource']['tp-id'], 'SRG1-PP1-TX')
215             if ztoa['id'] == '0':
216                 self.assertEqual(ztoa['resource']['tp-id'], 'SRG1-PP1-RX')
217         time.sleep(2)
218
219     # Load complex topology
220     def test_12_load_complex_topology(self):
221         response = test_utils.put_ietf_network('openroadm-topology', self.complex_topo_uni_dir_data)
222         self.assertIn(response['status_code'], (requests.codes.ok, requests.codes.no_content))
223         time.sleep(1)
224
225     # Get existing nodeId
226     def test_13_get_nodeId(self):
227         response = test_utils.get_ietf_network_node_request('openroadm-topology', 'XPONDER-3-2', 'config')
228         self.assertEqual(response['status_code'], requests.codes.ok)
229         self.assertEqual(response['node']['node-id'], 'XPONDER-3-2')
230         time.sleep(1)
231
232     # Test failed path computation
233     def test_14_fail_path_computation(self):
234         del self.path_computation_input_data["service-name"]
235         del self.path_computation_input_data["service-a-end"]
236         del self.path_computation_input_data["service-z-end"]
237         response = test_utils.transportpce_api_rpc_request('transportpce-pce',
238                                                            'path-computation-request',
239                                                            self.path_computation_input_data)
240         self.assertEqual(response['status_code'], requests.codes.ok)
241         self.assertIn('Service Name is not set',
242                       response['output']['configuration-response-common']['response-message'])
243         time.sleep(2)
244
245     # Test1 success path computation
246     def test_15_success1_path_computation(self):
247         self.path_computation_input_data["service-name"] = "service 1"
248         self.path_computation_input_data["service-a-end"] = {"service-format": "Ethernet", "service-rate": "100",
249                                                              "clli": "ORANGE2", "node-id": "XPONDER-2-2"}
250         self.path_computation_input_data["service-z-end"] = {"service-format": "Ethernet", "service-rate": "100",
251                                                              "clli": "ORANGE1", "node-id": "XPONDER-1-2"}
252         self.path_computation_input_data["hard-constraints"] = {"customer-code": ["Some customer-code"],
253                                                                 "co-routing": {
254                                                                     "service-identifier-list": [{
255                                                                         "service-identifier": "Some existing-service"}]
256         }}
257         self.path_computation_input_data["soft-constraints"] = {"customer-code": ["Some customer-code"],
258                                                                 "co-routing": {
259                                                                     "service-identifier-list": [{
260                                                                         "service-identifier": "Some existing-service"}]
261         }}
262         response = test_utils.transportpce_api_rpc_request('transportpce-pce',
263                                                            'path-computation-request',
264                                                            self.path_computation_input_data)
265         self.assertEqual(response['status_code'], requests.codes.ok)
266         self.assertIn('Path is calculated',
267                       response['output']['configuration-response-common']['response-message'])
268
269         time.sleep(4)
270
271     # Test2 success path computation with path description
272     def test_16_success2_path_computation(self):
273         self.path_computation_input_data["service-a-end"]["node-id"] = "XPONDER-1-2"
274         self.path_computation_input_data["service-a-end"]["clli"] = "ORANGE1"
275         self.path_computation_input_data["service-z-end"]["node-id"] = "XPONDER-3-2"
276         self.path_computation_input_data["service-z-end"]["clli"] = "ORANGE3"
277         del self.path_computation_input_data["hard-constraints"]
278         del self.path_computation_input_data["soft-constraints"]
279         response = test_utils.transportpce_api_rpc_request('transportpce-pce',
280                                                            'path-computation-request',
281                                                            self.path_computation_input_data)
282         self.assertEqual(response['status_code'], requests.codes.ok)
283         self.assertIn('Path is calculated',
284                       response['output']['configuration-response-common']['response-message'])
285         self.assertEqual(5, response['output']['response-parameters']['path-description']
286                          ['aToZ-direction']['aToZ-wavelength-number'])
287         self.assertEqual(5, response['output']['response-parameters']['path-description']
288                          ['zToA-direction']['zToA-wavelength-number'])
289         time.sleep(4)
290
291     # Test3 success path computation with hard-constraints exclude
292     def test_17_success3_path_computation(self):
293         self.path_computation_input_data["hard-constraints"] = {"exclude":
294                                                                 {"node-id": ["OpenROADM-2-1", "OpenROADM-2-2"]}}
295         response = test_utils.transportpce_api_rpc_request('transportpce-pce',
296                                                            'path-computation-request',
297                                                            self.path_computation_input_data)
298         self.assertEqual(response['status_code'], requests.codes.ok)
299         self.assertIn('Path is calculated',
300                       response['output']['configuration-response-common']['response-message'])
301         self.assertEqual(9, response['output']['response-parameters']['path-description']
302                          ['aToZ-direction']['aToZ-wavelength-number'])
303         self.assertEqual(9, response['output']['response-parameters']['path-description']
304                          ['zToA-direction']['zToA-wavelength-number'])
305         time.sleep(4)
306
307     # Path computation before deleting oms-attribute of the link :openroadm1-3 to openroadm1-2
308     def test_18_path_computation_before_oms_attribute_deletion(self):
309         self.path_computation_input_data["service-a-end"]["node-id"] = "XPONDER-2-2"
310         self.path_computation_input_data["service-a-end"]["clli"] = "ORANGE2"
311         self.path_computation_input_data["service-z-end"]["node-id"] = "XPONDER-1-2"
312         self.path_computation_input_data["service-z-end"]["clli"] = "ORANGE1"
313         del self.path_computation_input_data["hard-constraints"]
314         response = test_utils.transportpce_api_rpc_request('transportpce-pce',
315                                                            'path-computation-request',
316                                                            self.path_computation_input_data)
317         self.assertEqual(response['status_code'], requests.codes.ok)
318         self.assertIn('Path is calculated',
319                       response['output']['configuration-response-common']['response-message'])
320         path_depth = len(response['output']['response-parameters']['path-description']
321                          ['aToZ-direction']['aToZ'])
322         self.assertEqual(31, path_depth)
323         link = {"link-id": "OpenROADM-1-3-DEG2-to-OpenROADM-1-2-DEG2", "state": "inService"}
324         find = False
325         for i in range(0, path_depth):
326             resource_i = (response['output']['response-parameters']['path-description']['aToZ-direction']['aToZ'][i]
327                           ['resource'])
328             if resource_i == link:
329                 find = True
330         self.assertEqual(find, True)
331         time.sleep(4)
332
333     # Delete oms-attribute in the link :openroadm1-3 to openroadm1-2
334     def test_19_delete_oms_attribute_in_openroadm13toopenroadm12_link(self):
335         response = test_utils.del_oms_attr_request("OpenROADM-1-3-DEG2-to-OpenROADM-1-2-DEG2")
336         self.assertIn(response.status_code, (requests.codes.ok, requests.codes.no_content))
337         time.sleep(2)
338
339     # Path computation after deleting oms-attribute of the link :openroadm1-3 to openroadm1-2
340     def test_20_path_computation_after_oms_attribute_deletion(self):
341         response = test_utils.transportpce_api_rpc_request('transportpce-pce',
342                                                            'path-computation-request',
343                                                            self.path_computation_input_data)
344         self.assertEqual(response['status_code'], requests.codes.ok)
345         self.assertIn('Path is calculated',
346                       response['output']['configuration-response-common']['response-message'])
347         path_depth = len(response['output']['response-parameters']['path-description']
348                          ['aToZ-direction']['aToZ'])
349         self.assertEqual(47, path_depth)
350         link = {"link-id": "OpenROADM-1-3-DEG2-to-OpenROADM-1-2-DEG2", "state": "inService"}
351         find = False
352         for i in range(0, path_depth):
353             resource_i = (response['output']['response-parameters']['path-description']['aToZ-direction']['aToZ'][i]
354                           ['resource'])
355             if resource_i == link:
356                 find = True
357         self.assertNotEqual(find, True)
358         time.sleep(5)
359
360
361 if __name__ == "__main__":
362     unittest.main(verbosity=2)