Creating a reusable Restconf client
[ccsdk/cds.git] / components / scripts / python / ccsdk_restconf / restconf_client.py
1 #
2 # ============LICENSE_START=======================================================
3 #  Copyright (C) 2019 Nordix Foundation.
4 # ================================================================================
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 #      http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 #
17 # SPDX-License-Identifier: Apache-2.0
18 # ============LICENSE_END=========================================================
19 #
20 from time import sleep
21
22
23 class RestconfClient:
24
25     __odl_status_check_limit = 10
26     __odl_status_check_pause = 1
27     __odl_status_check_url = "restconf/operational/network-topology:network-topology/topology/topology-netconf/node/"
28     __base_odl_url = "restconf/config/network-topology:network-topology/topology/topology-netconf/node/"
29
30     def __init__(self, log, restconf_component_function):
31         self.__log = log
32         self.__component_function = restconf_component_function
33
34     def mount_device(self, web_client_service, pnf_id, mount_payload):
35         self.__log.debug("mounting device {}", pnf_id)
36         headers = {"Content-Type": "application/xml"}
37         url = self.__base_odl_url + pnf_id
38         self.__log.debug("sending mount request, url: {}", url)
39         web_client_service.exchangeResource("PUT", url, mount_payload, headers)
40         self.__wait_for_odl_to_mount(web_client_service, pnf_id)
41
42     def __wait_for_odl_to_mount(self, web_client_service, pnf_id):
43         counter = 0
44         url = self.__odl_status_check_url + pnf_id
45         self.__log.info("url for ODL status check: {}", url)
46         expected_result = '"netconf-node-topology:connection-status":"connected"'
47         while counter < self.__odl_status_check_limit:
48             result = web_client_service.exchangeResource("GET", url, "")
49             if expected_result in result:
50                 self.__log.info("PNF was mounted successfully on ODL")
51                 return None
52             sleep(self.__odl_status_check_pause)
53             counter += 1
54         raise Exception("PNF was not mounted on ODL, aborting configuration procedure")
55
56     def configure_device(self, web_client_service, pnf_id, configlet_resource_path, configlet_to_apply):
57         self.log_current_configlet(web_client_service, pnf_id, configlet_resource_path)
58         self.__log.info("configuring device: {}, Configlet: {}", pnf_id, configlet_to_apply)
59         headers = {"Content-Type": "application/yang.patch+json"}
60         url = self.__base_odl_url + pnf_id + configlet_resource_path
61         self.__log.debug("sending patch request,  url: {}", url)
62         result = web_client_service.exchangeResource("PATCH", url, configlet_to_apply, headers)
63         self.__log.info("Configuration application result: {}", result)
64
65     def log_current_configlet(self, web_client_service, pnf_id, configlet_resource_path):
66         url = self.__base_odl_url + pnf_id + configlet_resource_path
67         self.__log.debug("sending GET request,  url: {}", url)
68         result = web_client_service.exchangeResource("GET", url, "")
69         self.__log.info("Current configuration: {}", result)
70
71     def unmount_device(self, web_client_service, pnf_id):
72         url = self.__base_odl_url + str(pnf_id)
73         self.__log.debug("sending unmount request, url: {}", url)
74         web_client_service.exchangeResource("DELETE", url, "")