improve devices connection methods in tests
[transportpce.git] / tests / transportpce_tests / 1.2.1 / test_topoPortMapping.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 json
13 import signal
14 import time
15 import unittest
16 import requests
17 import psutil
18 from common import test_utils
19
20
21 class TransportPCEtesting(unittest.TestCase):
22
23     processes = None
24
25     @classmethod
26     def setUpClass(cls):
27         cls.processes = test_utils.start_tpce()
28         cls.processes = test_utils.start_sims(['xpdra', 'roadma'])
29
30     @classmethod
31     def tearDownClass(cls):
32         for process in cls.processes:
33             test_utils.shutdown_process(process)
34         print("all processes killed")
35
36     def setUp(self):
37         time.sleep(10)
38
39     # Connect the ROADMA
40     def test_01_connect_rdm(self):
41         response = test_utils.mount_device("ROADMA01", 'roadma')
42         self.assertEqual(response.status_code, requests.codes.created, test_utils.CODE_SHOULD_BE_201)
43
44     # Verify the termination points of the ROADMA
45     def test_02_compareOpenroadmTopologyPortMapping_rdm(self):
46         urlTopo = ("{}/config/ietf-network:networks/network/openroadm-topology"
47                    .format(test_utils.RESTCONF_BASE_URL))
48         responseTopo = requests.request("GET", urlTopo, headers=test_utils.TYPE_APPLICATION_JSON,
49                                         auth=(test_utils.ODL_LOGIN, test_utils.ODL_PWD))
50         resTopo = responseTopo.json()
51         nbNode = len(resTopo['network'][0]['node'])
52         nbMapCumul = 0
53         nbMappings = 0
54         for i in range(0, nbNode):
55             nodeId = resTopo['network'][0]['node'][i]['node-id']
56             nodeMapId = nodeId.split("-")[0]
57             urlMapList = "{}/config/transportpce-portmapping:network/nodes/" + nodeMapId
58             urlMapListFull = urlMapList.format(test_utils.RESTCONF_BASE_URL)
59             responseMapList = requests.request("GET", urlMapListFull, headers=test_utils.TYPE_APPLICATION_JSON,
60                                                auth=(test_utils.ODL_LOGIN, test_utils.ODL_PWD))
61             resMapList = responseMapList.json()
62
63             nbMappings = len(resMapList['nodes'][0]['mapping']) - nbMapCumul
64             nbTp = len(resTopo['network'][0]['node'][i]['ietf-network-topology:termination-point'])
65             nbMapCurrent = 0
66             for j in range(0, nbTp):
67                 tpId = resTopo['network'][0]['node'][i]['ietf-network-topology:termination-point'][j]['tp-id']
68                 if((not "CP" in tpId) and (not "CTP" in tpId)):
69                     urlMap = "{}/config/transportpce-portmapping:network/nodes/" + nodeMapId + "/mapping/" + tpId
70                     urlMapFull = urlMap.format(test_utils.RESTCONF_BASE_URL)
71                     responseMap = requests.request("GET", urlMapFull, headers=test_utils.TYPE_APPLICATION_JSON,
72                                                    auth=(test_utils.ODL_LOGIN, test_utils.ODL_PWD))
73                     self.assertEqual(responseMap.status_code, requests.codes.ok)
74                     if responseMap.status_code == requests.codes.ok:
75                         nbMapCurrent += 1
76             nbMapCumul += nbMapCurrent
77         nbMappings -= nbMapCurrent
78         self.assertEqual(nbMappings, 0)
79
80     # Disconnect the ROADMA
81     def test_03_disconnect_rdm(self):
82         response = test_utils.unmount_device("ROADMA01")
83         self.assertEqual(response.status_code, requests.codes.ok, test_utils.CODE_SHOULD_BE_200)
84
85 #     #Connect the XPDRA
86     def test_04_connect_xpdr(self):
87         response = test_utils.mount_device("XPDRA01", 'xpdra')
88         self.assertEqual(response.status_code, requests.codes.created, test_utils.CODE_SHOULD_BE_201)
89
90 #     #Verify the termination points related to XPDR
91     def test_05_compareOpenroadmTopologyPortMapping_xpdr(self):
92         self.test_02_compareOpenroadmTopologyPortMapping_rdm()
93
94     # Disconnect the XPDRA
95     def test_06_disconnect_device(self):
96         response = test_utils.unmount_device("XPDRA01")
97         self.assertEqual(response.status_code, requests.codes.ok, test_utils.CODE_SHOULD_BE_200)
98
99
100 if __name__ == "__main__":
101     # logging.basicConfig(filename='./transportpce_tests/log/response.log',filemode='w',level=logging.DEBUG)
102     #logging.debug('I am there')
103     unittest.main(verbosity=2)