Refactor NBINotifications and serviceHandlerImpl
[transportpce.git] / tests / transportpce_tests / with_docker / test02_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_process_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_process_service_request(data)
232         self.assertEqual(response.status_code, requests.codes.ok)
233         res = response.json()
234         self.assertEqual(res['output']['notifications-process-service'][-1]['service-name'], 'service1')
235         self.assertEqual(res['output']['notifications-process-service'][-1]['connection-type'], 'service')
236         self.assertEqual(res['output']['notifications-process-service'][-1]['message'],
237                          'ServiceCreate request failed ...')
238         self.assertEqual(res['output']['notifications-process-service'][-1]['response-failed'],
239                          'PCE path computation failed !')
240         time.sleep(2)
241
242     def test_12_add_omsAttributes_ROADMA_ROADMC(self):
243         # Config ROADMA-ROADMC oms-attributes
244         data = {"span": {
245             "auto-spanloss": "true",
246             "spanloss-base": 11.4,
247             "spanloss-current": 12,
248             "engineered-spanloss": 12.2,
249             "link-concatenation": [{
250                 "SRLG-Id": 0,
251                 "fiber-type": "smf",
252                 "SRLG-length": 100000,
253                 "pmd": 0.5}]}}
254         response = test_utils.add_oms_attr_request("ROADM-A1-DEG2-DEG2-TTP-TXRXtoROADM-C1-DEG1-DEG1-TTP-TXRX", data)
255         self.assertEqual(response.status_code, requests.codes.created)
256
257     def test_13_add_omsAttributes_ROADMC_ROADMA(self):
258         # Config ROADMC-ROADMA oms-attributes
259         data = {"span": {
260             "auto-spanloss": "true",
261             "spanloss-base": 11.4,
262             "spanloss-current": 12,
263             "engineered-spanloss": 12.2,
264             "link-concatenation": [{
265                 "SRLG-Id": 0,
266                 "fiber-type": "smf",
267                 "SRLG-length": 100000,
268                 "pmd": 0.5}]}}
269         response = test_utils.add_oms_attr_request("ROADM-C1-DEG1-DEG1-TTP-TXRXtoROADM-A1-DEG2-DEG2-TTP-TXRX", data)
270         self.assertEqual(response.status_code, requests.codes.created)
271
272     # test service-create for Eth service from xpdr to xpdr
273     def test_14_create_eth_service1(self):
274         self.cr_serv_sample_data["input"]["service-name"] = "service1"
275         response = test_utils.service_create_request(self.cr_serv_sample_data)
276         self.assertEqual(response.status_code, requests.codes.ok)
277         res = response.json()
278         self.assertIn('PCE calculation in progress',
279                       res['output']['configuration-response-common']['response-message'])
280         time.sleep(self.WAITING)
281
282     def test_15_get_eth_service1(self):
283         response = test_utils.get_service_list_request("services/service1")
284         self.assertEqual(response.status_code, requests.codes.ok)
285         res = response.json()
286         self.assertEqual(
287             res['services'][0]['administrative-state'], 'inService')
288         self.assertEqual(
289             res['services'][0]['service-name'], 'service1')
290         self.assertEqual(
291             res['services'][0]['connection-type'], 'service')
292         self.assertEqual(
293             res['services'][0]['lifecycle-state'], 'planned')
294         time.sleep(2)
295
296     def test_16_get_notifications_service1(self):
297         data = {
298             "input": {
299                 "connection-type": "service",
300                 "id-consumer": "consumer",
301                 "group-id": "transportpceTest"
302             }
303         }
304         response = test_utils.get_notifications_process_service_request(data)
305         self.assertEqual(response.status_code, requests.codes.ok)
306         res = response.json()
307         self.assertEqual(res['output']['notifications-process-service'][-1]['service-name'], 'service1')
308         self.assertEqual(res['output']['notifications-process-service'][-1]['connection-type'], 'service')
309         self.assertEqual(res['output']['notifications-process-service'][-1]['message'], 'Service implemented !')
310         time.sleep(2)
311
312     def test_17_get_notifications_alarm_service1(self):
313         data = {
314             "input": {
315                 "connection-type": "service",
316                 "id-consumer": "consumer",
317                 "group-id": "transportpceTest"
318             }
319         }
320         response = test_utils.get_notifications_alarm_service_request(data)
321         self.assertEqual(response.status_code, requests.codes.ok)
322         res = response.json()
323         self.assertEqual(res['output']['notifications-alarm-service'][-1]['service-name'], 'service1')
324         self.assertEqual(res['output']['notifications-alarm-service'][-1]['connection-type'], 'service')
325         self.assertEqual(res['output']['notifications-alarm-service'][-1]['operational-state'], 'inService')
326         self.assertEqual(res['output']['notifications-alarm-service'][-1]['message'], 'The service is now inService')
327         time.sleep(2)
328
329     def test_18_change_status_port_roadma_srg(self):
330         url = "{}/config/org-openroadm-device:org-openroadm-device/circuit-packs/3%2F0/ports/C1"
331         body = {"ports": [{
332             "port-name": "C1",
333             "logical-connection-point": "SRG1-PP1",
334             "port-type": "client",
335             "circuit-id": "SRG1",
336             "administrative-state": "outOfService",
337             "port-qual": "roadm-external"}]}
338         response = requests.request("PUT", url.format("http://127.0.0.1:8141/restconf"),
339                                     data=json.dumps(body), headers=test_utils.TYPE_APPLICATION_JSON,
340                                     auth=(test_utils.ODL_LOGIN, test_utils.ODL_PWD))
341         self.assertEqual(response.status_code, requests.codes.ok)
342         time.sleep(2)
343
344     def test_19_get_notifications_alarm_service1(self):
345         data = {
346             "input": {
347                 "connection-type": "service",
348                 "id-consumer": "consumer",
349                 "group-id": "transportpceTest"
350             }
351         }
352         response = test_utils.get_notifications_alarm_service_request(data)
353         self.assertEqual(response.status_code, requests.codes.ok)
354         res = response.json()
355         self.assertEqual(res['output']['notifications-alarm-service'][-1]['service-name'], 'service1')
356         self.assertEqual(res['output']['notifications-alarm-service'][-1]['connection-type'], 'service')
357         self.assertEqual(res['output']['notifications-alarm-service'][-1]['operational-state'], 'outOfService')
358         self.assertEqual(res['output']['notifications-alarm-service'][-1]['message'], 'The service is now outOfService')
359         time.sleep(2)
360
361     def test_20_restore_status_port_roadma_srg(self):
362         url = "{}/config/org-openroadm-device:org-openroadm-device/circuit-packs/3%2F0/ports/C1"
363         body = {"ports": [{
364             "port-name": "C1",
365             "logical-connection-point": "SRG1-PP1",
366             "port-type": "client",
367             "circuit-id": "SRG1",
368             "administrative-state": "inService",
369             "port-qual": "roadm-external"}]}
370         response = requests.request("PUT", url.format("http://127.0.0.1:8141/restconf"),
371                                     data=json.dumps(body), headers=test_utils.TYPE_APPLICATION_JSON,
372                                     auth=(test_utils.ODL_LOGIN, test_utils.ODL_PWD))
373         self.assertEqual(response.status_code, requests.codes.ok)
374         time.sleep(2)
375
376     def test_21_get_notifications_alarm_service1(self):
377         self.test_17_get_notifications_alarm_service1()
378
379     def test_22_delete_eth_service1(self):
380         response = test_utils.service_delete_request("service1")
381         self.assertEqual(response.status_code, requests.codes.ok)
382         res = response.json()
383         self.assertIn('Renderer service delete in progress',
384                       res['output']['configuration-response-common']['response-message'])
385         time.sleep(20)
386
387     def test_23_get_notifications_service1(self):
388         data = {
389             "input": {
390                 "connection-type": "service",
391                 "id-consumer": "consumer",
392                 "group-id": "transportpceTest"
393             }
394         }
395         response = test_utils.get_notifications_process_service_request(data)
396         self.assertEqual(response.status_code, requests.codes.ok)
397         res = response.json()
398         self.assertEqual(res['output']['notifications-process-service'][-1]['service-name'], 'service1')
399         self.assertEqual(res['output']['notifications-process-service'][-1]['connection-type'], 'service')
400         self.assertEqual(res['output']['notifications-process-service'][-1]['message'], 'Service deleted !')
401         time.sleep(2)
402
403     def test_24_disconnect_XPDRA(self):
404         response = test_utils.unmount_device("XPDR-A1")
405         self.assertEqual(response.status_code, requests.codes.ok, test_utils.CODE_SHOULD_BE_200)
406
407     def test_25_disconnect_XPDRC(self):
408         response = test_utils.unmount_device("XPDR-C1")
409         self.assertEqual(response.status_code, requests.codes.ok, test_utils.CODE_SHOULD_BE_200)
410
411     def test_26_disconnect_ROADMA(self):
412         response = test_utils.unmount_device("ROADM-A1")
413         self.assertEqual(response.status_code, requests.codes.ok, test_utils.CODE_SHOULD_BE_200)
414
415     def test_27_disconnect_ROADMC(self):
416         response = test_utils.unmount_device("ROADM-C1")
417         self.assertEqual(response.status_code, requests.codes.ok, test_utils.CODE_SHOULD_BE_200)
418
419
420 if __name__ == "__main__":
421     unittest.main(verbosity=2)