Merge "Add Get function in Netconf execution service for operational commands"
[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 from restconf_constant import *
22 from org.onap.ccsdk.cds.blueprintsprocessor.functions.restconf.executor import RestconfExecutorExtensionsKt
23 from org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution import ResourceResolutionExtensionsKt
24
25
26 class RestconfClient:
27     __base_odl_url = "restconf/config/network-topology:network-topology/topology/topology-netconf/node/"
28     __odl_status_check_limit = 10
29     __odl_status_check_pause = 1
30     # Once confirmed to be reliable, the check should change to use the connection-status API
31     __odl_status_check_url = "restconf/operational/network-topology:network-topology/topology/topology-netconf/node/"
32
33     def __init__(self, log, restconf_component_function):
34         self.__log = log
35         self.__component_function = restconf_component_function
36
37     def web_client_service(self, identifier):
38         return RestconfExecutorExtensionsKt.restconfClientService(self.__component_function, identifier)
39
40     def resolve_and_generate_message_from_template_prefix(self, artifact_prefix):
41         return ResourceResolutionExtensionsKt.contentFromResolvedArtifact(self.__component_function, artifact_prefix)
42
43     def retrieve_resolved_template_from_database(self, key, artifact_template):
44         return ResourceResolutionExtensionsKt.storedContentFromResolvedArtifact(self.__component_function, key,
45                                                                                 artifact_template)
46
47     def mount_device(self, web_client_service, nf_id, mount_payload, content_type="application/xml"):
48         self.__log.debug("mounting device {}", nf_id)
49         headers = {"Content-Type": content_type}
50         url = self.__base_odl_url + nf_id
51         self.__log.debug("sending mount request, url: {}", url)
52         web_client_service.exchangeResource("PUT", url, mount_payload, headers)
53         self.__wait_for_odl_to_mount(web_client_service, nf_id)
54
55     def __wait_for_odl_to_mount(self, web_client_service, nf_id):
56         counter = 0
57         url = self.__odl_status_check_url + nf_id
58         self.__log.info("url for ODL status check: {}", url)
59         # TODO: allow JSON format change
60         expected_result = '"netconf-node-topology:connection-status":"connected"'
61         while counter < self.__odl_status_check_limit:
62             result = web_client_service.exchangeResource("GET", url, "")
63             if expected_result in result.body:
64                 self.__log.info("NF was mounted successfully on ODL")
65                 return None
66             sleep(self.__odl_status_check_pause)
67             counter += 1
68         raise Exception("NF was not mounted on ODL, aborting configuration procedure")
69
70     def configure_device_json_patch(self, web_client_service, nf_id, configlet_resource_path, configlet_to_apply):
71         headers = {"Content-Type": "application/yang.patch+json"}
72         self.__configure_device(web_client_service, nf_id, configlet_resource_path, configlet_to_apply, headers)
73
74     def configure_device_xml_patch(self, web_client_service, nf_id, configlet_resource_path, configlet_to_apply):
75         headers = {"Content-Type": "application/yang.patch+xml"}
76         self.__configure_device(web_client_service, nf_id, configlet_resource_path, configlet_to_apply, headers)
77
78     def __configure_device(self, web_client_service, nf_id, configlet_resource_path, configlet_to_apply, headers):
79         self.__log.debug("headers: {}", headers)
80         self.__log.info("configuring device: {}, Configlet: {}", nf_id, configlet_to_apply)
81         url = self.__base_odl_url + nf_id + configlet_resource_path
82         self.__log.debug("sending patch request,  url: {}", url)
83         result = web_client_service.exchangeResource("PATCH", url, configlet_to_apply, headers)
84         self.__log.info("Configuration application result: {}", result)
85
86     def retrieve_device_configuration_subtree(self, web_client_service, nf_id, configlet_resource_path):
87         url = self.__base_odl_url + nf_id + configlet_resource_path
88         self.__log.debug("sending GET request,  url: {}", url)
89         result = web_client_service.exchangeResource("GET", url, "")
90         return result
91
92     def unmount_device(self, web_client_service, nf_id):
93         url = self.__base_odl_url + nf_id
94         self.__log.debug("sending unmount request, url: {}", url)
95         web_client_service.exchangeResource("DELETE", url, "")
96
97     def set_execution_attribute_response_data(self, response_data):
98         self.setAttribute(ATTRIBUTE_RESPONSE_DATA, response_data)