Fixing restconf_client.py script
[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     __base_odl_url = "restconf/config/network-topology:network-topology/topology/topology-netconf/node/"
26     __odl_status_check_limit = 10
27     __odl_status_check_pause = 1
28     # Once confirmed to be reliable, the check should change to use the connection-status API
29     __odl_status_check_url = "restconf/operational/network-topology:network-topology/topology/topology-netconf/node/"
30
31     def __init__(self, log, restconf_component_function):
32         self.__log = log
33         self.__component_function = restconf_component_function
34
35     def mount_device(self, web_client_service, nf_id, mount_payload):
36         self.__log.debug("mounting device {}", nf_id)
37         headers = {"Content-Type": "application/xml"}
38         url = self.__base_odl_url + nf_id
39         self.__log.debug("sending mount request, url: {}", url)
40         web_client_service.exchangeResource("PUT", url, mount_payload, headers)
41         self.__wait_for_odl_to_mount(web_client_service, nf_id)
42
43     def __wait_for_odl_to_mount(self, web_client_service, nf_id):
44         counter = 0
45         url = self.__odl_status_check_url + nf_id
46         self.__log.info("url for ODL status check: {}", url)
47         expected_result = '"netconf-node-topology:connection-status":"connected"'
48         while counter < self.__odl_status_check_limit:
49             result = web_client_service.exchangeResource("GET", url, "")
50             if expected_result in result.body:
51                 self.__log.info("NF was mounted successfully on ODL")
52                 return None
53             sleep(self.__odl_status_check_pause)
54             counter += 1
55         raise Exception("NF was not mounted on ODL, aborting configuration procedure")
56
57     def configure_device_json_patch(self, web_client_service, nf_id, configlet_resource_path, configlet_to_apply):
58         headers = {"Content-Type": "application/yang.patch+json"}
59         self.__configure_device(web_client_service, nf_id, configlet_resource_path, configlet_to_apply, headers)
60
61     def configure_device_xml_patch(self, web_client_service, nf_id, configlet_resource_path, configlet_to_apply):
62         headers = {"Content-Type": "application/yang.patch+xml"}
63         self.__configure_device(web_client_service, nf_id, configlet_resource_path, configlet_to_apply, headers)
64
65     def __configure_device(self, web_client_service, nf_id, configlet_resource_path, configlet_to_apply, headers):
66         self.__log.debug("headers: {}", headers)
67         self.__log.info("configuring device: {}, Configlet: {}", nf_id, configlet_to_apply)
68         url = self.__base_odl_url + nf_id + configlet_resource_path
69         self.__log.debug("sending patch request,  url: {}", url)
70         result = web_client_service.exchangeResource("PATCH", url, configlet_to_apply, headers)
71         self.__log.info("Configuration application result: {}", result)
72
73     def retrieve_device_configuration_subtree(self, web_client_service, nf_id, configlet_resource_path):
74         url = self.__base_odl_url + nf_id + configlet_resource_path
75         self.__log.debug("sending GET request,  url: {}", url)
76         result = web_client_service.exchangeResource("GET", url, "")
77         return result
78
79     def unmount_device(self, web_client_service, nf_id):
80         url = self.__base_odl_url + nf_id
81         self.__log.debug("sending unmount request, url: {}", url)
82         web_client_service.exchangeResource("DELETE", url, "")