Refactor NBINotification & add ServiceListener tests
[transportpce.git] / tests / transportpce_tests / 2.2.1 / test_nbinotifications.py
1 #!/usr/bin/env python
2 ##############################################################################
3 # Copyright (c) 2020 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 os
15 import json
16 import sys
17 import unittest
18 import time
19 import requests
20 sys.path.append('transportpce_tests/common/')
21 import test_utils
22
23
24 class TransportNbiNotificationstesting(unittest.TestCase):
25     processes = None
26     cr_serv_sample_data = {"input": {
27         "sdnc-request-header": {
28             "request-id": "e3028bae-a90f-4ddd-a83f-cf224eba0e58",
29             "rpc-action": "service-create",
30             "request-system-id": "appname",
31             "notification-url": "http://localhost:8585/NotificationServer/notify"
32         },
33         "service-name": "service1",
34         "common-id": "ASATT1234567",
35         "connection-type": "service",
36         "service-a-end": {
37             "service-rate": "100",
38             "node-id": "XPDR-A1",
39             "service-format": "Ethernet",
40             "clli": "SNJSCAMCJP8",
41             "tx-direction": {
42                 "port": {
43                     "port-device-name": "ROUTER_SNJSCAMCJP8_000000.00_00",
44                     "port-type": "router",
45                     "port-name": "Gigabit Ethernet_Tx.ge-5/0/0.0",
46                     "port-rack": "000000.00",
47                     "port-shelf": "00"
48                 },
49                 "lgx": {
50                     "lgx-device-name": "LGX Panel_SNJSCAMCJP8_000000.00_00",
51                     "lgx-port-name": "LGX Back.3",
52                     "lgx-port-rack": "000000.00",
53                     "lgx-port-shelf": "00"
54                 }
55             },
56             "rx-direction": {
57                 "port": {
58                     "port-device-name": "ROUTER_SNJSCAMCJP8_000000.00_00",
59                     "port-type": "router",
60                     "port-name": "Gigabit Ethernet_Rx.ge-5/0/0.0",
61                     "port-rack": "000000.00",
62                     "port-shelf": "00"
63                 },
64                 "lgx": {
65                     "lgx-device-name": "LGX Panel_SNJSCAMCJP8_000000.00_00",
66                     "lgx-port-name": "LGX Back.4",
67                     "lgx-port-rack": "000000.00",
68                     "lgx-port-shelf": "00"
69                 }
70             },
71             "optic-type": "gray"
72         },
73         "service-z-end": {
74             "service-rate": "100",
75             "node-id": "XPDR-C1",
76             "service-format": "Ethernet",
77             "clli": "SNJSCAMCJT4",
78             "tx-direction": {
79                 "port": {
80                     "port-device-name": "ROUTER_SNJSCAMCJT4_000000.00_00",
81                     "port-type": "router",
82                     "port-name": "Gigabit Ethernet_Tx.ge-1/0/0.0",
83                     "port-rack": "000000.00",
84                     "port-shelf": "00"
85                 },
86                 "lgx": {
87                     "lgx-device-name": "LGX Panel_SNJSCAMCJT4_000000.00_00",
88                     "lgx-port-name": "LGX Back.29",
89                     "lgx-port-rack": "000000.00",
90                     "lgx-port-shelf": "00"
91                 }
92             },
93             "rx-direction": {
94                 "port": {
95                     "port-device-name": "ROUTER_SNJSCAMCJT4_000000.00_00",
96                     "port-type": "router",
97                     "port-name": "Gigabit Ethernet_Rx.ge-1/0/0.0",
98                     "port-rack": "000000.00",
99                     "port-shelf": "00"
100                 },
101                 "lgx": {
102                     "lgx-device-name": "LGX Panel_SNJSCAMCJT4_000000.00_00",
103                     "lgx-port-name": "LGX Back.30",
104                     "lgx-port-rack": "000000.00",
105                     "lgx-port-shelf": "00"
106                 }
107             },
108             "optic-type": "gray"
109         },
110         "due-date": "2016-11-28T00:00:01Z",
111         "operator-contact": "pw1234"
112     }
113     }
114
115     WAITING = 20  # nominal value is 300
116     NODE_VERSION = '2.2.1'
117
118     @classmethod
119     def setUpClass(cls):
120         # TODO: for lighty manage the activation of NBI notification feature
121         cls.init_failed = False
122         cls.processes = test_utils.start_tpce()
123         # NBI notification feature is not installed by default in Karaf
124         if "USE_LIGHTY" not in os.environ or os.environ['USE_LIGHTY'] != 'True':
125             print("installing NBI notification feature...")
126             result = test_utils.install_karaf_feature("odl-transportpce-nbinotifications")
127             if result.returncode != 0:
128                 cls.init_failed = True
129             print("Restarting OpenDaylight...")
130             test_utils.shutdown_process(cls.processes[0])
131             cls.processes[0] = test_utils.start_karaf()
132             test_utils.process_list[0] = cls.processes[0]
133             cls.init_failed = not test_utils.wait_until_log_contains(
134                 test_utils.KARAF_LOG, test_utils.KARAF_OK_START_MSG, time_to_wait=60)
135         if cls.init_failed:
136             print("NBI notification installation feature failed...")
137             test_utils.shutdown_process(cls.processes[0])
138             sys.exit(2)
139         cls.processes = test_utils.start_sims([('xpdra', cls.NODE_VERSION),
140                                                ('roadma', cls.NODE_VERSION),
141                                                ('roadmc', cls.NODE_VERSION),
142                                                ('xpdrc', cls.NODE_VERSION)])
143
144     @classmethod
145     def tearDownClass(cls):
146         # pylint: disable=not-an-iterable
147         for process in cls.processes:
148             test_utils.shutdown_process(process)
149         print("all processes killed")
150
151     def setUp(self):  # instruction executed before each test method
152         print("execution of {}".format(self.id().split(".")[-1]))
153
154     def test_01_connect_xpdrA(self):
155         response = test_utils.mount_device("XPDR-A1", ('xpdra', self.NODE_VERSION))
156         self.assertEqual(response.status_code, requests.codes.created, test_utils.CODE_SHOULD_BE_201)
157
158     def test_02_connect_xpdrC(self):
159         response = test_utils.mount_device("XPDR-C1", ('xpdrc', self.NODE_VERSION))
160         self.assertEqual(response.status_code, requests.codes.created, test_utils.CODE_SHOULD_BE_201)
161
162     def test_03_connect_rdmA(self):
163         response = test_utils.mount_device("ROADM-A1", ('roadma', self.NODE_VERSION))
164         self.assertEqual(response.status_code, requests.codes.created, test_utils.CODE_SHOULD_BE_201)
165
166     def test_04_connect_rdmC(self):
167         response = test_utils.mount_device("ROADM-C1", ('roadmc', self.NODE_VERSION))
168         self.assertEqual(response.status_code, requests.codes.created, test_utils.CODE_SHOULD_BE_201)
169
170     def test_05_connect_xprdA_N1_to_roadmA_PP1(self):
171         response = test_utils.connect_xpdr_to_rdm_request("XPDR-A1", "1", "1",
172                                                           "ROADM-A1", "1", "SRG1-PP1-TXRX")
173         self.assertEqual(response.status_code, requests.codes.ok)
174         res = response.json()
175         self.assertIn('Xponder Roadm Link created successfully', res["output"]["result"])
176         time.sleep(2)
177
178     def test_06_connect_roadmA_PP1_to_xpdrA_N1(self):
179         response = test_utils.connect_rdm_to_xpdr_request("XPDR-A1", "1", "1",
180                                                           "ROADM-A1", "1", "SRG1-PP1-TXRX")
181         self.assertEqual(response.status_code, requests.codes.ok)
182         res = response.json()
183         self.assertIn('Roadm Xponder links created successfully', res["output"]["result"])
184         time.sleep(2)
185
186     def test_07_connect_xprdC_N1_to_roadmC_PP1(self):
187         response = test_utils.connect_xpdr_to_rdm_request("XPDR-C1", "1", "1",
188                                                           "ROADM-C1", "1", "SRG1-PP1-TXRX")
189         self.assertEqual(response.status_code, requests.codes.ok)
190         res = response.json()
191         self.assertIn('Xponder Roadm Link created successfully', res["output"]["result"])
192         time.sleep(2)
193
194     def test_08_connect_roadmC_PP1_to_xpdrC_N1(self):
195         response = test_utils.connect_rdm_to_xpdr_request("XPDR-C1", "1", "1",
196                                                           "ROADM-C1", "1", "SRG1-PP1-TXRX")
197         self.assertEqual(response.status_code, requests.codes.ok)
198         res = response.json()
199         self.assertIn('Roadm Xponder links created successfully', res["output"]["result"])
200         time.sleep(2)
201
202     def test_09_get_notifications_service1(self):
203         data = {
204             "input": {
205                 "connection-type": "service",
206                 "id-consumer": "consumer",
207                 "group-id": "transportpceTest"
208             }
209         }
210         response = test_utils.get_notifications_service_request(data)
211         self.assertEqual(response.status_code, requests.codes.no_content)
212         time.sleep(2)
213
214     def test_10_create_eth_service1(self):
215         self.cr_serv_sample_data["input"]["service-name"] = "service1"
216         response = test_utils.service_create_request(self.cr_serv_sample_data)
217         self.assertEqual(response.status_code, requests.codes.ok)
218         res = response.json()
219         self.assertIn('PCE calculation in progress',
220                       res['output']['configuration-response-common']['response-message'])
221         time.sleep(self.WAITING)
222
223     def test_11_get_notifications_service1(self):
224         data = {
225             "input": {
226                 "connection-type": "service",
227                 "id-consumer": "consumer",
228                 "group-id": "transportpceTest"
229             }
230         }
231         response = test_utils.get_notifications_service_request(data)
232         self.assertEqual(response.status_code, requests.codes.ok)
233         res = response.json()
234         self.assertEqual(res['output']['notification-service'][-1]['service-name'], 'service1')
235         self.assertEqual(res['output']['notification-service'][-1]['connection-type'], 'service')
236         self.assertEqual(res['output']['notification-service'][-1]['message'], 'ServiceCreate request failed ...')
237         self.assertEqual(res['output']['notification-service'][-1]['response-failed'],
238                          'PCE path computation failed !')
239         time.sleep(2)
240
241     def test_12_add_omsAttributes_ROADMA_ROADMC(self):
242         # Config ROADMA-ROADMC oms-attributes
243         data = {"span": {
244             "auto-spanloss": "true",
245             "spanloss-base": 11.4,
246             "spanloss-current": 12,
247             "engineered-spanloss": 12.2,
248             "link-concatenation": [{
249                 "SRLG-Id": 0,
250                 "fiber-type": "smf",
251                 "SRLG-length": 100000,
252                 "pmd": 0.5}]}}
253         response = test_utils.add_oms_attr_request("ROADM-A1-DEG2-DEG2-TTP-TXRXtoROADM-C1-DEG1-DEG1-TTP-TXRX", data)
254         self.assertEqual(response.status_code, requests.codes.created)
255
256     def test_13_add_omsAttributes_ROADMC_ROADMA(self):
257         # Config ROADMC-ROADMA oms-attributes
258         data = {"span": {
259             "auto-spanloss": "true",
260             "spanloss-base": 11.4,
261             "spanloss-current": 12,
262             "engineered-spanloss": 12.2,
263             "link-concatenation": [{
264                 "SRLG-Id": 0,
265                 "fiber-type": "smf",
266                 "SRLG-length": 100000,
267                 "pmd": 0.5}]}}
268         response = test_utils.add_oms_attr_request("ROADM-C1-DEG1-DEG1-TTP-TXRXtoROADM-A1-DEG2-DEG2-TTP-TXRX", data)
269         self.assertEqual(response.status_code, requests.codes.created)
270
271     # test service-create for Eth service from xpdr to xpdr
272     def test_14_create_eth_service1(self):
273         self.cr_serv_sample_data["input"]["service-name"] = "service1"
274         response = test_utils.service_create_request(self.cr_serv_sample_data)
275         self.assertEqual(response.status_code, requests.codes.ok)
276         res = response.json()
277         self.assertIn('PCE calculation in progress',
278                       res['output']['configuration-response-common']['response-message'])
279         time.sleep(self.WAITING)
280
281     def test_15_get_eth_service1(self):
282         response = test_utils.get_service_list_request("services/service1")
283         self.assertEqual(response.status_code, requests.codes.ok)
284         res = response.json()
285         self.assertEqual(
286             res['services'][0]['administrative-state'], 'inService')
287         self.assertEqual(
288             res['services'][0]['service-name'], 'service1')
289         self.assertEqual(
290             res['services'][0]['connection-type'], 'service')
291         self.assertEqual(
292             res['services'][0]['lifecycle-state'], 'planned')
293         time.sleep(2)
294
295     def test_16_get_notifications_service1(self):
296         data = {
297             "input": {
298                 "connection-type": "service",
299                 "id-consumer": "consumer",
300                 "group-id": "transportpceTest"
301             }
302         }
303         response = test_utils.get_notifications_service_request(data)
304         self.assertEqual(response.status_code, requests.codes.ok)
305         res = response.json()
306         self.assertEqual(res['output']['notification-service'][-1]['service-name'], 'service1')
307         self.assertEqual(res['output']['notification-service'][-1]['connection-type'], 'service')
308         self.assertEqual(res['output']['notification-service'][-1]['message'], 'Service implemented !')
309         time.sleep(2)
310
311     def test_17_get_notifications_alarm_service1(self):
312         data = {
313             "input": {
314                 "connection-type": "service",
315                 "id-consumer": "consumer",
316                 "group-id": "transportpceTest"
317             }
318         }
319         response = test_utils.get_notifications_alarm_service_request(data)
320         self.assertEqual(response.status_code, requests.codes.ok)
321         res = response.json()
322         self.assertEqual(res['output']['notification-alarm-service'][-1]['service-name'], 'service1')
323         self.assertEqual(res['output']['notification-alarm-service'][-1]['connection-type'], 'service')
324         self.assertEqual(res['output']['notification-alarm-service'][-1]['operational-state'], 'inService')
325         self.assertEqual(res['output']['notification-alarm-service'][-1]['message'], 'The service is now inService')
326         time.sleep(2)
327
328     def test_18_change_status_port_roadma_srg(self):
329         url = "{}/config/org-openroadm-device:org-openroadm-device/circuit-packs/3%2F0/ports/C1"
330         body = {"ports": [{
331             "port-name": "C1",
332             "logical-connection-point": "SRG1-PP1",
333             "port-type": "client",
334             "circuit-id": "SRG1",
335             "administrative-state": "outOfService",
336             "port-qual": "roadm-external"}]}
337         response = requests.request("PUT", url.format("http://127.0.0.1:8141/restconf"),
338                                     data=json.dumps(body), headers=test_utils.TYPE_APPLICATION_JSON,
339                                     auth=(test_utils.ODL_LOGIN, test_utils.ODL_PWD))
340         self.assertEqual(response.status_code, requests.codes.ok)
341         time.sleep(2)
342
343     def test_19_get_notifications_alarm_service1(self):
344         data = {
345             "input": {
346                 "connection-type": "service",
347                 "id-consumer": "consumer",
348                 "group-id": "transportpceTest"
349             }
350         }
351         response = test_utils.get_notifications_alarm_service_request(data)
352         self.assertEqual(response.status_code, requests.codes.ok)
353         res = response.json()
354         self.assertEqual(res['output']['notification-alarm-service'][-1]['service-name'], 'service1')
355         self.assertEqual(res['output']['notification-alarm-service'][-1]['connection-type'], 'service')
356         self.assertEqual(res['output']['notification-alarm-service'][-1]['operational-state'], 'outOfService')
357         self.assertEqual(res['output']['notification-alarm-service'][-1]['message'], 'The service is now outOfService')
358         time.sleep(2)
359
360     def test_20_restore_status_port_roadma_srg(self):
361         url = "{}/config/org-openroadm-device:org-openroadm-device/circuit-packs/3%2F0/ports/C1"
362         body = {"ports": [{
363             "port-name": "C1",
364             "logical-connection-point": "SRG1-PP1",
365             "port-type": "client",
366             "circuit-id": "SRG1",
367             "administrative-state": "inService",
368             "port-qual": "roadm-external"}]}
369         response = requests.request("PUT", url.format("http://127.0.0.1:8141/restconf"),
370                                     data=json.dumps(body), headers=test_utils.TYPE_APPLICATION_JSON,
371                                     auth=(test_utils.ODL_LOGIN, test_utils.ODL_PWD))
372         self.assertEqual(response.status_code, requests.codes.ok)
373         time.sleep(2)
374
375     def test_21_get_notifications_alarm_service1(self):
376         self.test_17_get_notifications_alarm_service1()
377
378     def test_22_delete_eth_service1(self):
379         response = test_utils.service_delete_request("service1")
380         self.assertEqual(response.status_code, requests.codes.ok)
381         res = response.json()
382         self.assertIn('Renderer service delete in progress',
383                       res['output']['configuration-response-common']['response-message'])
384         time.sleep(20)
385
386     def test_23_get_notifications_service1(self):
387         data = {
388             "input": {
389                 "connection-type": "service",
390                 "id-consumer": "consumer",
391                 "group-id": "transportpceTest"
392             }
393         }
394         response = test_utils.get_notifications_service_request(data)
395         self.assertEqual(response.status_code, requests.codes.ok)
396         res = response.json()
397         self.assertEqual(res['output']['notification-service'][-1]['service-name'], 'service1')
398         self.assertEqual(res['output']['notification-service'][-1]['connection-type'], 'service')
399         self.assertEqual(res['output']['notification-service'][-1]['message'], 'Service deleted !')
400         time.sleep(2)
401
402     def test_24_disconnect_XPDRA(self):
403         response = test_utils.unmount_device("XPDR-A1")
404         self.assertEqual(response.status_code, requests.codes.ok, test_utils.CODE_SHOULD_BE_200)
405
406     def test_25_disconnect_XPDRC(self):
407         response = test_utils.unmount_device("XPDR-C1")
408         self.assertEqual(response.status_code, requests.codes.ok, test_utils.CODE_SHOULD_BE_200)
409
410     def test_26_disconnect_ROADMA(self):
411         response = test_utils.unmount_device("ROADM-A1")
412         self.assertEqual(response.status_code, requests.codes.ok, test_utils.CODE_SHOULD_BE_200)
413
414     def test_27_disconnect_ROADMC(self):
415         response = test_utils.unmount_device("ROADM-C1")
416         self.assertEqual(response.status_code, requests.codes.ok, test_utils.CODE_SHOULD_BE_200)
417
418
419 if __name__ == "__main__":
420     unittest.main(verbosity=2)