run autopep8 on functional 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         response = test_utils.path_computation_request("request-1", "service-1",
97                                                        {"node-id": "XPONDER-1", "service-rate": "100",
98                                                            "service-format": "Ethernet", "clli": "Node1"},
99                                                        {"node-id": "XPONDER-5", "service-rate": "100",
100                                                            "service-format": "Ethernet", "clli": "Node5"})
101         self.assertEqual(response.status_code, requests.codes.ok)
102         res = response.json()
103         self.assertEqual(res['output']['configuration-response-common'][
104             'response-code'], '200')
105         self.assertEqual(res['output']['configuration-response-common'][
106             'response-message'],
107             'Path is calculated by PCE')
108         self.assertEqual(res['output']['gnpy-response'][0]['path-dir'],
109                          'A-to-Z')
110         self.assertEqual(res['output']['gnpy-response'][0]['feasibility'], True)
111         self.assertEqual(res['output']['gnpy-response'][1]['path-dir'],
112                          'Z-to-A')
113         self.assertEqual(res['output']['gnpy-response'][1]['feasibility'], True)
114         time.sleep(5)
115
116     # Path computed by PCE is not feasible by GNPy and GNPy cannot find
117     # another one (low SNR)
118     def test_05_path_computation_FoundByPCE_NotFeasibleByGnpy(self):
119         response = test_utils.path_computation_request("request-2", "service-2",
120                                                        {"node-id": "XPONDER-1", "service-rate": "100",
121                                                            "service-format": "Ethernet", "clli": "Node1"},
122                                                        {"node-id": "XPONDER-5", "service-rate": "100",
123                                                            "service-format": "Ethernet", "clli": "Node5"},
124                                                        {"include_": {"ordered-hops": [
125                                                            {"hop-number": "0", "hop-type": {"node-id": "OpenROADM-2"}},
126                                                            {"hop-number": "1", "hop-type": {"node-id": "OpenROADM-3"}},
127                                                            {"hop-number": "2", "hop-type": {"node-id": "OpenROADM-4"}}]}})
128         self.assertEqual(response.status_code, requests.codes.ok)
129         res = response.json()
130         self.assertEqual(res['output']['configuration-response-common'][
131             'response-code'], '500')
132         self.assertEqual(res['output']['configuration-response-common'][
133             'response-message'],
134             'No path available by PCE and GNPy ')
135         self.assertEqual(res['output']['gnpy-response'][0]['path-dir'],
136                          'A-to-Z')
137         self.assertEqual(res['output']['gnpy-response'][0]['feasibility'],
138                          False)
139         self.assertEqual(res['output']['gnpy-response'][1]['path-dir'],
140                          'Z-to-A')
141         self.assertEqual(res['output']['gnpy-response'][1]['feasibility'],
142                          False)
143         time.sleep(5)
144
145     # #PCE cannot find a path while GNPy finds a feasible one
146     def test_06_path_computation_NotFoundByPCE_FoundByGNPy(self):
147         response = test_utils.path_computation_request("request-3", "service-3",
148                                                        {"node-id": "XPONDER-1", "service-rate": "100",
149                                                            "service-format": "Ethernet", "clli": "Node1"},
150                                                        {"node-id": "XPONDER-4", "service-rate": "100",
151                                                            "service-format": "Ethernet", "clli": "Node5"},
152                                                        {"include_": {"ordered-hops": [
153                                                            {"hop-number": "0", "hop-type": {"node-id": "OpenROADM-2"}},
154                                                            {"hop-number": "1", "hop-type": {"node-id": "OpenROADM-3"}}]}})
155         self.assertEqual(response.status_code, requests.codes.ok)
156         res = response.json()
157         self.assertEqual(res['output']['configuration-response-common'][
158             'response-code'], '200')
159         self.assertEqual(res['output']['configuration-response-common'][
160             'response-message'],
161             'Path is calculated by GNPy')
162         self.assertEqual(res['output']['gnpy-response'][0]['path-dir'],
163                          'A-to-Z')
164         self.assertEqual(res['output']['gnpy-response'][0]['feasibility'], True)
165         self.assertEqual(res['output']['gnpy-response'][1]['path-dir'],
166                          'Z-to-A')
167         self.assertEqual(res['output']['gnpy-response'][1]['feasibility'], True)
168         time.sleep(5)
169
170     # Not found path by PCE and GNPy cannot find another one
171     def test_07_path_computation_FoundByPCE_NotFeasibleByGnpy(self):
172         response = test_utils.path_computation_request("request-4", "service-4",
173                                                        {"node-id": "XPONDER-1", "service-rate": "100",
174                                                            "service-format": "Ethernet", "clli": "Node1"},
175                                                        {"node-id": "XPONDER-4", "service-rate": "100",
176                                                            "service-format": "Ethernet", "clli": "Node5"},
177                                                        {"include_": {"ordered-hops": [
178                                                            {"hop-number": "0", "hop-type": {"node-id": "OpenROADM-2"}},
179                                                            {"hop-number": "1", "hop-type": {"node-id": "OpenROADM-3"}},
180                                                            {"hop-number": "2", "hop-type": {"node-id": "OpenROADM-4"}},
181                                                            {"hop-number": "3", "hop-type": {"node-id": "OpenROADM-3"}}]}})
182         self.assertEqual(response.status_code, requests.codes.ok)
183         res = response.json()
184         self.assertEqual(res['output']['configuration-response-common'][
185             'response-code'], '500')
186         self.assertEqual(res['output']['configuration-response-common'][
187             'response-message'],
188             'No path available by PCE and GNPy ')
189         time.sleep(5)
190
191     # Disconnect the different topologies
192     def test_08_disconnect_openroadmTopology(self):
193         response = test_utils.delete_request(test_utils.URL_CONFIG_ORDM_TOPO)
194         self.assertEqual(response.status_code, requests.codes.ok)
195         time.sleep(3)
196
197     def test_09_disconnect_openroadmNetwork(self):
198         response = test_utils.delete_request(test_utils.URL_CONFIG_ORDM_NET)
199         self.assertEqual(response.status_code, requests.codes.ok)
200         time.sleep(3)
201
202     def test_10_disconnect_clliNetwork(self):
203         response = test_utils.delete_request(test_utils.URL_CONFIG_CLLI_NET)
204         self.assertEqual(response.status_code, requests.codes.ok)
205         time.sleep(3)
206
207
208 if __name__ == "__main__":
209     # logging.basicConfig(filename='./transportpce_tests/log/response.log',filemode='w',level=logging.DEBUG)
210     unittest.main(verbosity=2)