add a method to manage ordm network in func tests
[transportpce.git] / tests / transportpce_tests / 1.2.1 / test_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 import unittest
13 import json
14 import os
15 import sys
16 import time
17 import requests
18 from common import test_utils
19
20
21 class TransportGNPYtesting(unittest.TestCase):
22
23     @classmethod
24     def __init_logfile(cls):
25         GNPY_LOGFILE = os.path.join(os.path.dirname(os.path.realpath(__file__)),
26                                     "..", "..", "transportpce_tests", "gnpy.log")
27         if os.path.isfile(GNPY_LOFGILE):
28             os.remove(GNPY_LOFGILE)
29
30     topo_cllinet_data = None
31     topo_ordnet_data = None
32     topo_ordtopo_data = None
33     processes = None
34
35     @classmethod
36     def setUpClass(cls):
37         try:
38             sample_files_parsed = False
39             TOPO_CLLINET_FILE = os.path.join(os.path.dirname(os.path.realpath(__file__)),
40                                              "..", "..", "sample_configs", "gnpy", "clliNetwork.json")
41             with open(TOPO_CLLINET_FILE, 'r') as topo_cllinet:
42                 cls.topo_cllinet_data = topo_cllinet.read()
43
44             TOPO_ORDNET_FILE = os.path.join(os.path.dirname(os.path.realpath(__file__)),
45                                             "..", "..", "sample_configs", "gnpy", "openroadmNetwork.json")
46             with open(TOPO_ORDNET_FILE, 'r') as topo_ordnet:
47                 cls.topo_ordnet_data = topo_ordnet.read()
48
49             TOPO_ORDTOPO_FILE = os.path.join(os.path.dirname(os.path.realpath(__file__)),
50                                              "..", "..", "sample_configs", "gnpy", "openroadmTopology.json")
51             with open(TOPO_ORDTOPO_FILE, 'r') as topo_ordtopo:
52                 cls.topo_ordtopo_data = topo_ordtopo.read()
53             sample_files_parsed = True
54         except PermissionError as err:
55             print("Permission Error when trying to read sample files\n", err)
56             sys.exit(2)
57         except FileNotFoundError as err:
58             print("File Not found Error when trying to read sample files\n", err)
59             sys.exit(2)
60         except:
61             print("Unexpected error when trying to read sample files\n", sys.exc_info()[0])
62             sys.exit(2)
63         finally:
64             if sample_files_parsed:
65                 print("sample files content loaded")
66
67         cls.processes = test_utils.start_tpce()
68
69     @classmethod
70     def tearDownClass(cls):
71         for process in cls.processes:
72             test_utils.shutdown_process(process)
73         print("all processes killed")
74
75     def setUp(self):
76         time.sleep(2)
77
78     # Mount the different topologies
79     def test_01_connect_clliNetwork(self):
80         response = test_utils.rawput_request(test_utils.URL_CONFIG_CLLI_NET, self.topo_cllinet_data)
81         self.assertEqual(response.status_code, requests.codes.ok)
82         time.sleep(3)
83
84     def test_02_connect_openroadmNetwork(self):
85         response = test_utils.rawput_request(test_utils.URL_CONFIG_ORDM_NET, self.topo_ordnet_data)
86         self.assertEqual(response.status_code, requests.codes.ok)
87         time.sleep(3)
88
89     def test_03_connect_openroadmTopology(self):
90         response = test_utils.rawput_request(test_utils.URL_CONFIG_ORDM_TOPO, self.topo_ordtopo_data)
91         self.assertEqual(response.status_code, requests.codes.ok)
92         time.sleep(3)
93
94     # Path computed by PCE is feasible according to Gnpy
95     def test_04_path_computation_FeasibleWithPCE(self):
96         url = "{}/operations/transportpce-pce:path-computation-request"
97         data = {
98             "input": {
99                 "service-name": "service-1",
100                 "resource-reserve": "true",
101                 "pce-metric": "hop-count",
102                 "service-handler-header": {
103                     "request-id": "request-1"
104                 },
105                 "service-a-end": {
106                     "node-id": "XPONDER-1",
107                     "service-rate": "100",
108                     "service-format": "Ethernet",
109                     "clli": "Node1"
110                 },
111                 "service-z-end": {
112                     "node-id": "XPONDER-5",
113                     "service-rate": "100",
114                     "service-format": "Ethernet",
115                     "clli": "Node5"
116                 }
117             }
118         }
119         response = test_utils.post_request(url, data)
120         self.assertEqual(response.status_code, requests.codes.ok)
121         res = response.json()
122         self.assertEqual(res['output']['configuration-response-common'][
123             'response-code'], '200')
124         self.assertEqual(res['output']['configuration-response-common'][
125             'response-message'],
126             'Path is calculated by PCE')
127         self.assertEqual(res['output']['gnpy-response'][0]['path-dir'],
128                          'A-to-Z')
129         self.assertEqual(res['output']['gnpy-response'][0]['feasibility'], True)
130         self.assertEqual(res['output']['gnpy-response'][1]['path-dir'],
131                          'Z-to-A')
132         self.assertEqual(res['output']['gnpy-response'][1]['feasibility'], True)
133         time.sleep(5)
134
135     # Path computed by PCE is not feasible by GNPy and GNPy cannot find
136     # another one (low SNR)
137     def test_05_path_computation_FoundByPCE_NotFeasibleByGnpy(self):
138         url = "{}/operations/transportpce-pce:path-computation-request"
139         data = {
140             "input": {
141                 "service-name": "service-2",
142                 "resource-reserve": "true",
143                 "pce-metric": "hop-count",
144                 "service-handler-header": {
145                     "request-id": "request-2"
146                 },
147                 "service-a-end": {
148                     "node-id": "XPONDER-1",
149                     "service-rate": "100",
150                     "service-format": "Ethernet",
151                     "clli": "Node1"
152                 },
153                 "service-z-end": {
154                     "node-id": "XPONDER-5",
155                     "service-rate": "100",
156                     "service-format": "Ethernet",
157                     "clli": "Node5"
158                 },
159                 "hard-constraints": {
160                     "include_": {
161                         "ordered-hops": [
162                             {
163                                 "hop-number": "0",
164                                 "hop-type": {
165                                     "node-id": "OpenROADM-2"
166                                 }
167                             },
168                             {
169                                 "hop-number": "1",
170                                 "hop-type": {
171                                     "node-id": "OpenROADM-3"
172                                 }
173                             },
174                             {
175                                 "hop-number": "2",
176                                 "hop-type": {
177                                     "node-id": "OpenROADM-4"
178                                 }
179                             }
180                         ]
181                     }
182                 }
183             }
184         }
185         response = test_utils.post_request(url, data)
186         self.assertEqual(response.status_code, requests.codes.ok)
187         res = response.json()
188         self.assertEqual(res['output']['configuration-response-common'][
189             'response-code'], '500')
190         self.assertEqual(res['output']['configuration-response-common'][
191             'response-message'],
192             'No path available by PCE and GNPy ')
193         self.assertEqual(res['output']['gnpy-response'][0]['path-dir'],
194                          'A-to-Z')
195         self.assertEqual(res['output']['gnpy-response'][0]['feasibility'],
196                          False)
197         self.assertEqual(res['output']['gnpy-response'][1]['path-dir'],
198                          'Z-to-A')
199         self.assertEqual(res['output']['gnpy-response'][1]['feasibility'],
200                          False)
201         time.sleep(5)
202
203     # #PCE cannot find a path while GNPy finds a feasible one
204     def test_06_path_computation_NotFoundByPCE_FoundByGNPy(self):
205         url = "{}/operations/transportpce-pce:path-computation-request"
206         data = {
207             "input": {
208                 "service-name": "service-3",
209                 "resource-reserve": "true",
210                 "pce-metric": "hop-count",
211                 "service-handler-header": {
212                     "request-id": "request-3"
213                 },
214                 "service-a-end": {
215                     "node-id": "XPONDER-1",
216                     "service-rate": "100",
217                     "service-format": "Ethernet",
218                     "clli": "Node1"
219                 },
220                 "service-z-end": {
221                     "node-id": "XPONDER-4",
222                     "service-rate": "100",
223                     "service-format": "Ethernet",
224                     "clli": "Node5"
225                 },
226                 "hard-constraints": {
227                     "include_": {
228                         "ordered-hops": [
229                             {
230                                 "hop-number": "0",
231                                 "hop-type": {
232                                     "node-id": "OpenROADM-2"
233                                 }
234                             },
235                             {
236                                 "hop-number": "1",
237                                 "hop-type": {
238                                     "node-id": "OpenROADM-3"
239                                 }
240                             }
241                         ]
242                     }
243                 }
244             }
245         }
246         response = test_utils.post_request(url, data)
247         self.assertEqual(response.status_code, requests.codes.ok)
248         res = response.json()
249         self.assertEqual(res['output']['configuration-response-common'][
250             'response-code'], '200')
251         self.assertEqual(res['output']['configuration-response-common'][
252             'response-message'],
253             'Path is calculated by GNPy')
254         self.assertEqual(res['output']['gnpy-response'][0]['path-dir'],
255                          'A-to-Z')
256         self.assertEqual(res['output']['gnpy-response'][0]['feasibility'], True)
257         self.assertEqual(res['output']['gnpy-response'][1]['path-dir'],
258                          'Z-to-A')
259         self.assertEqual(res['output']['gnpy-response'][1]['feasibility'], True)
260         time.sleep(5)
261
262     # Not found path by PCE and GNPy cannot find another one
263     def test_07_path_computation_FoundByPCE_NotFeasibleByGnpy(self):
264         url = "{}/operations/transportpce-pce:path-computation-request"
265         data = {
266             "input": {
267                 "service-name": "service-4",
268                 "resource-reserve": "true",
269                 "pce-metric": "hop-count",
270                 "service-handler-header": {
271                     "request-id": "request-4"
272                 },
273                 "service-a-end": {
274                     "node-id": "XPONDER-1",
275                     "service-rate": "100",
276                     "service-format": "Ethernet",
277                     "clli": "Node1"
278                 },
279                 "service-z-end": {
280                     "node-id": "XPONDER-4",
281                     "service-rate": "100",
282                     "service-format": "Ethernet",
283                     "clli": "Node5"
284                 },
285                 "hard-constraints": {
286                     "include_": {
287                         "ordered-hops": [
288                             {
289                                 "hop-number": "0",
290                                 "hop-type": {
291                                     "node-id": "OpenROADM-2"
292                                 }
293                             },
294                             {
295                                 "hop-number": "1",
296                                 "hop-type": {
297                                     "node-id": "OpenROADM-3"
298                                 }
299                             },
300                             {
301                                 "hop-number": "2",
302                                 "hop-type": {
303                                     "node-id": "OpenROADM-4"
304                                 }
305                             },
306                             {
307                                 "hop-number": "3",
308                                 "hop-type": {
309                                     "node-id": "OpenROADM-3"
310                                 }
311                             }
312                         ]
313                     }
314                 }
315             }
316         }
317         response = test_utils.post_request(url, data)
318         self.assertEqual(response.status_code, requests.codes.ok)
319         res = response.json()
320         self.assertEqual(res['output']['configuration-response-common'][
321             'response-code'], '500')
322         self.assertEqual(res['output']['configuration-response-common'][
323             'response-message'],
324             'No path available by PCE and GNPy ')
325         time.sleep(5)
326
327     # Disconnect the different topologies
328     def test_08_disconnect_openroadmTopology(self):
329         response = test_utils.delete_request(test_utils.URL_CONFIG_ORDM_TOPO)
330         self.assertEqual(response.status_code, requests.codes.ok)
331         time.sleep(3)
332
333     def test_09_disconnect_openroadmNetwork(self):
334         response = test_utils.delete_request(test_utils.URL_CONFIG_ORDM_NET)
335         self.assertEqual(response.status_code, requests.codes.ok)
336         time.sleep(3)
337
338     def test_10_disconnect_clliNetwork(self):
339         response = test_utils.delete_request(test_utils.URL_CONFIG_CLLI_NET)
340         self.assertEqual(response.status_code, requests.codes.ok)
341         time.sleep(3)
342
343
344 if __name__ == "__main__":
345     # logging.basicConfig(filename='./transportpce_tests/log/response.log',filemode='w',level=logging.DEBUG)
346     unittest.main(verbosity=2)