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