Remove transportpce-routing-constraint model
[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": {"node-id": [
145                                                            "OpenROADM-2", "OpenROADM-3", "OpenROADM-4"]}})
146         self.assertEqual(response.status_code, requests.codes.ok)
147         res = response.json()
148         self.assertEqual(res['output']['configuration-response-common'][
149             'response-code'], '500')
150         self.assertEqual(res['output']['configuration-response-common'][
151             'response-message'],
152             'No path available by PCE and GNPy ')
153         self.assertIn('A-to-Z',
154                       [res['output']['gnpy-response'][0]['path-dir'],
155                        res['output']['gnpy-response'][1]['path-dir']])
156         self.assertIn('Z-to-A',
157                       [res['output']['gnpy-response'][0]['path-dir'],
158                        res['output']['gnpy-response'][1]['path-dir']])
159         self.assertEqual(res['output']['gnpy-response'][0]['feasibility'],
160                          False)
161         self.assertEqual(res['output']['gnpy-response'][1]['feasibility'],
162                          False)
163         time.sleep(5)
164
165     # #PCE cannot find a path while GNPy finds a feasible one
166     def test_06_path_computation_NotFoundByPCE_FoundByGNPy(self):
167         response = test_utils.path_computation_request("request-3", "service-3",
168                                                        {"node-id": "XPONDER-1", "service-rate": "100",
169                                                            "service-format": "Ethernet", "clli": "Node1"},
170                                                        {"node-id": "XPONDER-4", "service-rate": "100",
171                                                            "service-format": "Ethernet", "clli": "Node5"},
172                                                        {"include": {"node-id": [
173                                                            "OpenROADM-2", "OpenROADM-3"]}})
174         self.assertEqual(response.status_code, requests.codes.ok)
175         res = response.json()
176         self.assertEqual(res['output']['configuration-response-common'][
177             'response-code'], '200')
178         self.assertEqual(res['output']['configuration-response-common'][
179             'response-message'],
180             'Path is calculated by GNPy')
181         self.assertIn('A-to-Z',
182                       [res['output']['gnpy-response'][0]['path-dir'],
183                        res['output']['gnpy-response'][1]['path-dir']])
184         self.assertIn('Z-to-A',
185                       [res['output']['gnpy-response'][0]['path-dir'],
186                        res['output']['gnpy-response'][1]['path-dir']])
187         self.assertEqual(res['output']['gnpy-response'][1]['feasibility'], True)
188         self.assertEqual(res['output']['gnpy-response'][0]['feasibility'], True)
189         time.sleep(5)
190
191     # Not found path by PCE and GNPy cannot find another one
192     def test_07_path_computation_FoundByPCE_NotFeasibleByGnpy(self):
193         response = test_utils.path_computation_request("request-4", "service-4",
194                                                        {"node-id": "XPONDER-1", "service-rate": "400",
195                                                            "service-format": "Ethernet", "clli": "Node1"},
196                                                        {"node-id": "XPONDER-4", "service-rate": "400",
197                                                            "service-format": "Ethernet", "clli": "Node4"},
198                                                        {"include": {"node-id": [
199                                                            "OpenROADM-3", "OpenROADM-2",
200                                                            "OpenROADM-5"]}})
201         self.assertEqual(response.status_code, requests.codes.ok)
202         res = response.json()
203         self.assertEqual(res['output']['configuration-response-common'][
204             'response-code'], '500')
205         self.assertEqual(res['output']['configuration-response-common'][
206             'response-message'],
207             'No path available by PCE and GNPy ')
208         time.sleep(5)
209
210     # Disconnect the different topologies
211     def test_08_disconnect_openroadmTopology(self):
212         response = test_utils.delete_request(test_utils.URL_CONFIG_ORDM_TOPO)
213         self.assertEqual(response.status_code, requests.codes.ok)
214         time.sleep(3)
215
216     def test_09_disconnect_openroadmNetwork(self):
217         response = test_utils.delete_request(test_utils.URL_CONFIG_ORDM_NET)
218         self.assertEqual(response.status_code, requests.codes.ok)
219         time.sleep(3)
220
221     def test_10_disconnect_clliNetwork(self):
222         response = test_utils.delete_request(test_utils.URL_CONFIG_CLLI_NET)
223         self.assertEqual(response.status_code, requests.codes.ok)
224         time.sleep(3)
225
226     # Delete portmapping
227     def test_11_delete_port_mapping(self):
228         response = test_utils.delete_request(test_utils.URL_FULL_PORTMAPPING)
229         self.assertEqual(response.status_code, requests.codes.ok)
230         time.sleep(2)
231
232
233 if __name__ == "__main__":
234     # logging.basicConfig(filename='./transportpce_tests/log/response.log',filemode='w',level=logging.DEBUG)
235     unittest.main(verbosity=2)