cd2d8ceb39f59b4e36147f4926c7f11916bd31b0
[transportpce.git] / tests / transportpce_tests / pce / test03_gnpy.py
1 #!/usr/bin/env python
2
3 ##############################################################################
4 # Copyright (c) 2017 Orange, Inc. and others.  All rights reserved.
5 #
6 # All rights reserved. This program and the accompanying materials
7 # are made available under the terms of the Apache License, Version 2.0
8 # which accompanies this distribution, and is available at
9 # http://www.apache.org/licenses/LICENSE-2.0
10 ##############################################################################
11
12 # pylint: disable=no-member
13 # pylint: disable=too-many-public-methods
14
15 import unittest
16 import os
17 # pylint: disable=wrong-import-order
18 import sys
19 import subprocess
20 import time
21 import requests
22 sys.path.append('transportpce_tests/common/')
23 # pylint: disable=wrong-import-position
24 # pylint: disable=import-error
25 import test_utils  # nopep8
26
27
28 class TransportGNPYtesting(unittest.TestCase):
29
30     topo_cllinet_data = None
31     topo_ordnet_data = None
32     topo_ordtopo_data = None
33     port_mapping_data = None
34     processes = []
35
36     @classmethod
37     def setUpClass(cls):
38         # pylint: disable=bare-except
39         sample_files_parsed = False
40         try:
41             TOPO_CLLINET_FILE = os.path.join(os.path.dirname(os.path.realpath(__file__)),
42                                              "..", "..", "sample_configs", "gnpy", "clliNetwork.json")
43             with open(TOPO_CLLINET_FILE, 'r', encoding='utf-8') as topo_cllinet:
44                 cls.topo_cllinet_data = topo_cllinet.read()
45
46             TOPO_ORDNET_FILE = os.path.join(os.path.dirname(os.path.realpath(__file__)),
47                                             "..", "..", "sample_configs", "gnpy", "openroadmNetwork.json")
48             with open(TOPO_ORDNET_FILE, 'r', encoding='utf-8') as topo_ordnet:
49                 cls.topo_ordnet_data = topo_ordnet.read()
50
51             TOPO_ORDTOPO_FILE = os.path.join(os.path.dirname(os.path.realpath(__file__)),
52                                              "..", "..", "sample_configs", "gnpy", "openroadmTopology.json")
53             with open(TOPO_ORDTOPO_FILE, 'r', encoding='utf-8') as topo_ordtopo:
54                 cls.topo_ordtopo_data = topo_ordtopo.read()
55             PORT_MAPPING_FILE = os.path.join(os.path.dirname(os.path.realpath(__file__)),
56                                              "..", "..", "sample_configs", "gnpy", "gnpy_portmapping_121.json")
57             with open(PORT_MAPPING_FILE, 'r', encoding='utf-8') as port_mapping:
58                 cls.port_mapping_data = port_mapping.read()
59             sample_files_parsed = True
60         except PermissionError as err:
61             print("Permission Error when trying to read sample files\n", err)
62             sys.exit(2)
63         except FileNotFoundError as err:
64             print("File Not found Error when trying to read sample files\n", err)
65             sys.exit(2)
66         except:
67             print("Unexpected error when trying to read sample files\n", sys.exc_info()[0])
68             sys.exit(2)
69         finally:
70             if sample_files_parsed:
71                 print("sample files content loaded")
72
73         with open('gnpy.log', 'w', encoding='utf-8') as outfile:
74             print('starting GNPy REST server...')
75             # pylint: disable=consider-using-with
76             test_utils.process_list.append(subprocess.Popen(
77                 ['gnpy-rest'], stdout=outfile, stderr=outfile, stdin=None))
78         cls.processes = test_utils.start_tpce()
79
80     @classmethod
81     def tearDownClass(cls):
82         # pylint: disable=not-an-iterable
83         for process in cls.processes:
84             test_utils.shutdown_process(process)
85         print("all processes killed")
86
87     def setUp(self):
88         time.sleep(2)
89
90      # Load port mapping
91     def test_00_load_port_mapping(self):
92         response = test_utils.rawpost_request(test_utils.URL_FULL_PORTMAPPING, self.port_mapping_data)
93         self.assertEqual(response.status_code, requests.codes.no_content)
94         time.sleep(2)
95
96     # Mount the different topologies
97     def test_01_connect_clliNetwork(self):
98         response = test_utils.rawput_request(test_utils.URL_CONFIG_CLLI_NET, self.topo_cllinet_data)
99         self.assertEqual(response.status_code, requests.codes.ok)
100         time.sleep(3)
101
102     def test_02_connect_openroadmNetwork(self):
103         response = test_utils.rawput_request(test_utils.URL_CONFIG_ORDM_NET, self.topo_ordnet_data)
104         self.assertEqual(response.status_code, requests.codes.ok)
105         time.sleep(3)
106
107     def test_03_connect_openroadmTopology(self):
108         response = test_utils.rawput_request(test_utils.URL_CONFIG_ORDM_TOPO, self.topo_ordtopo_data)
109         self.assertEqual(response.status_code, requests.codes.ok)
110         time.sleep(3)
111
112     # Path computed by PCE is feasible according to Gnpy
113     def test_04_path_computation_FeasibleWithPCE(self):
114         response = test_utils.path_computation_request("request-1", "service-1",
115                                                        {"node-id": "XPONDER-1", "service-rate": "100",
116                                                            "service-format": "Ethernet", "clli": "Node1"},
117                                                        {"node-id": "XPONDER-5", "service-rate": "100",
118                                                            "service-format": "Ethernet", "clli": "Node5"})
119         self.assertEqual(response.status_code, requests.codes.ok)
120         res = response.json()
121         self.assertEqual(res['output']['configuration-response-common'][
122             'response-code'], '200')
123         self.assertEqual(res['output']['configuration-response-common'][
124             'response-message'],
125             'Path is calculated by PCE')
126         self.assertIn('A-to-Z',
127                       [res['output']['gnpy-response'][0]['path-dir'],
128                        res['output']['gnpy-response'][1]['path-dir']])
129         self.assertIn('Z-to-A',
130                       [res['output']['gnpy-response'][0]['path-dir'],
131                        res['output']['gnpy-response'][1]['path-dir']])
132         self.assertEqual(res['output']['gnpy-response'][0]['feasibility'], True)
133         self.assertEqual(res['output']['gnpy-response'][1]['feasibility'], True)
134         time.sleep(5)
135
136     # Path computed by PCE is not feasible by GNPy and GNPy cannot find
137     # another one (low SNR)
138     def test_05_path_computation_FoundByPCE_NotFeasibleByGnpy(self):
139         response = test_utils.path_computation_request("request-2", "service-2",
140                                                        {"node-id": "XPONDER-1", "service-rate": "100",
141                                                            "service-format": "Ethernet", "clli": "Node1"},
142                                                        {"node-id": "XPONDER-5", "service-rate": "100",
143                                                            "service-format": "Ethernet", "clli": "Node5"},
144                                                        {"include_": {"ordered-hops": [
145                                                            {"hop-number": "0", "hop-type": {"node-id": "OpenROADM-2"}},
146                                                            {"hop-number": "1", "hop-type": {"node-id": "OpenROADM-3"}},
147                                                            {"hop-number": "2", "hop-type": {"node-id": "OpenROADM-4"}}]}
148                                                         })
149         self.assertEqual(response.status_code, requests.codes.ok)
150         res = response.json()
151         self.assertEqual(res['output']['configuration-response-common'][
152             'response-code'], '500')
153         self.assertEqual(res['output']['configuration-response-common'][
154             'response-message'],
155             'No path available by PCE and GNPy ')
156         self.assertIn('A-to-Z',
157                       [res['output']['gnpy-response'][0]['path-dir'],
158                        res['output']['gnpy-response'][1]['path-dir']])
159         self.assertIn('Z-to-A',
160                       [res['output']['gnpy-response'][0]['path-dir'],
161                        res['output']['gnpy-response'][1]['path-dir']])
162         self.assertEqual(res['output']['gnpy-response'][0]['feasibility'],
163                          False)
164         self.assertEqual(res['output']['gnpy-response'][1]['feasibility'],
165                          False)
166         time.sleep(5)
167
168     # #PCE cannot find a path while GNPy finds a feasible one
169     def test_06_path_computation_NotFoundByPCE_FoundByGNPy(self):
170         response = test_utils.path_computation_request("request-3", "service-3",
171                                                        {"node-id": "XPONDER-1", "service-rate": "100",
172                                                            "service-format": "Ethernet", "clli": "Node1"},
173                                                        {"node-id": "XPONDER-4", "service-rate": "100",
174                                                            "service-format": "Ethernet", "clli": "Node5"},
175                                                        {"include_": {"ordered-hops": [
176                                                            {"hop-number": "0", "hop-type": {"node-id": "OpenROADM-2"}},
177                                                            {"hop-number": "1", "hop-type": {"node-id": "OpenROADM-3"}}]}
178                                                         })
179         self.assertEqual(response.status_code, requests.codes.ok)
180         res = response.json()
181         self.assertEqual(res['output']['configuration-response-common'][
182             'response-code'], '200')
183         self.assertEqual(res['output']['configuration-response-common'][
184             'response-message'],
185             'Path is calculated by GNPy')
186         self.assertIn('A-to-Z',
187                       [res['output']['gnpy-response'][0]['path-dir'],
188                        res['output']['gnpy-response'][1]['path-dir']])
189         self.assertIn('Z-to-A',
190                       [res['output']['gnpy-response'][0]['path-dir'],
191                        res['output']['gnpy-response'][1]['path-dir']])
192         self.assertEqual(res['output']['gnpy-response'][1]['feasibility'], True)
193         self.assertEqual(res['output']['gnpy-response'][0]['feasibility'], True)
194         time.sleep(5)
195
196     # Not found path by PCE and GNPy cannot find another one
197     def test_07_path_computation_FoundByPCE_NotFeasibleByGnpy(self):
198         response = test_utils.path_computation_request("request-4", "service-4",
199                                                        {"node-id": "XPONDER-1", "service-rate": "400",
200                                                            "service-format": "Ethernet", "clli": "Node1"},
201                                                        {"node-id": "XPONDER-4", "service-rate": "400",
202                                                            "service-format": "Ethernet", "clli": "Node4"},
203                                                        {"include_": {"ordered-hops": [
204                                                            {"hop-number": "0", "hop-type": {"node-id": "OpenROADM-3"}},
205                                                            {"hop-number": "1", "hop-type": {"node-id": "OpenROADM-2"}},
206                                                            {"hop-number": "2", "hop-type": {"node-id": "OpenROADM-5"}}]}
207                                                         })
208         self.assertEqual(response.status_code, requests.codes.ok)
209         res = response.json()
210         self.assertEqual(res['output']['configuration-response-common'][
211             'response-code'], '500')
212         self.assertEqual(res['output']['configuration-response-common'][
213             'response-message'],
214             'No path available by PCE and GNPy ')
215         time.sleep(5)
216
217     # Disconnect the different topologies
218     def test_08_disconnect_openroadmTopology(self):
219         response = test_utils.delete_request(test_utils.URL_CONFIG_ORDM_TOPO)
220         self.assertEqual(response.status_code, requests.codes.ok)
221         time.sleep(3)
222
223     def test_09_disconnect_openroadmNetwork(self):
224         response = test_utils.delete_request(test_utils.URL_CONFIG_ORDM_NET)
225         self.assertEqual(response.status_code, requests.codes.ok)
226         time.sleep(3)
227
228     def test_10_disconnect_clliNetwork(self):
229         response = test_utils.delete_request(test_utils.URL_CONFIG_CLLI_NET)
230         self.assertEqual(response.status_code, requests.codes.ok)
231         time.sleep(3)
232
233     # Delete portmapping
234     def test_11_delete_port_mapping(self):
235         response = test_utils.delete_request(test_utils.URL_FULL_PORTMAPPING)
236         self.assertEqual(response.status_code, requests.codes.ok)
237         time.sleep(2)
238
239
240 if __name__ == "__main__":
241     # logging.basicConfig(filename='./transportpce_tests/log/response.log',filemode='w',level=logging.DEBUG)
242     unittest.main(verbosity=2)