Add declarative acceptance tests
[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         return 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, content_type="application/xml"):
47         self.__log.debug("mounting device {}", nf_id)
48         headers = {"Content-Type": content_type}
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         # TODO: allow JSON format change
59         expected_result = '"netconf-node-topology:connection-status":"connected"'
60         while counter < self.__odl_status_check_limit:
61             result = web_client_service.exchangeResource("GET", url, "")
62             if expected_result in result.body:
63                 self.__log.info("NF was mounted successfully on ODL")
64                 return None
65             sleep(self.__odl_status_check_pause)
66             counter += 1
67         raise Exception("NF was not mounted on ODL, aborting configuration procedure")
68
69     def configure_device_json_patch(self, web_client_service, nf_id, configlet_resource_path, configlet_to_apply):
70         headers = {"Content-Type": "application/yang.patch+json"}
71         self.__configure_device(web_client_service, nf_id, configlet_resource_path, configlet_to_apply, headers)
72
73     def configure_device_xml_patch(self, web_client_service, nf_id, configlet_resource_path, configlet_to_apply):
74         headers = {"Content-Type": "application/yang.patch+xml"}
75         self.__configure_device(web_client_service, nf_id, configlet_resource_path, configlet_to_apply, headers)
76
77     def __configure_device(self, web_client_service, nf_id, configlet_resource_path, configlet_to_apply, headers):
78         self.__log.debug("headers: {}", headers)
79         self.__log.info("configuring device: {}, Configlet: {}", nf_id, configlet_to_apply)
80         url = self.__base_odl_url + nf_id + configlet_resource_path
81         self.__log.debug("sending patch request,  url: {}", url)
82         result = web_client_service.exchangeResource("PATCH", url, configlet_to_apply, headers)
83         self.__log.info("Configuration application result: {}", result)
84
85     def retrieve_device_configuration_subtree(self, web_client_service, nf_id, configlet_resource_path):
86         url = self.__base_odl_url + nf_id + configlet_resource_path
87         self.__log.debug("sending GET request,  url: {}", url)
88         result = web_client_service.exchangeResource("GET", url, "")
89         return result
90
91     def unmount_device(self, web_client_service, nf_id):
92         url = self.__base_odl_url + nf_id
93         self.__log.debug("sending unmount request, url: {}", url)
94         web_client_service.exchangeResource("DELETE", url, "")