Migrate ccsdk/apps to ccsdk/cds
[ccsdk/cds.git] / components / model-catalog / blueprint-model / test-blueprint / capability_restconf / Scripts / python / RestconfConfigDeploy.py
1 # ============LICENSE_START=======================================================
2 #  Copyright (C) 2019 Nordix Foundation.
3 # ================================================================================
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 #      http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 #
16 # SPDX-License-Identifier: Apache-2.0
17 # ============LICENSE_END=========================================================
18 from time import sleep
19
20 from org.onap.ccsdk.cds.blueprintsprocessor.functions.restconf.executor import \
21     RestconfComponentFunction
22 from java.lang import Exception as JavaException
23
24
25 class RestconfConfigDeploy(RestconfComponentFunction):
26
27     log = globals()["log"]
28     seconds_to_sleep = 5
29     base_mount_url = "restconf/config/network-topology:network-topology/topology/topology-netconf/node/"
30     server_identifier = "sdncodl"
31     configlet_template_name = "config-assign"
32
33     def process(self, execution_request):
34
35         self.log.info("Started execution of process method")
36         try:
37             self.log.info("getting resolution-key")
38             resolution_key = self.getDynamicProperties("resolution-key").asText()
39             self.log.info("resolution_key: {}", resolution_key)
40
41             self.log.info("getting pnf-id")
42             pnf_id = execution_request.payload.get("config-deploy-request").get("config-deploy-properties").get("entity").get("pnf-id")
43             pnf_id = str(pnf_id).strip('\"')
44             self.log.info("pnf-id: {}", pnf_id)
45
46             self.log.info("mounting device {}", pnf_id)
47             self.mount(pnf_id)
48
49             self.log.info("sleeping for {} seconds", self.seconds_to_sleep)
50             sleep(self.seconds_to_sleep)
51
52             try:
53                 self.log.info("configuring device {}", pnf_id)
54                 self.apply_configuration(pnf_id, resolution_key, self.configlet_template_name)
55             except Exception, err:
56                 self.log.error("an error occurred while configuring device {}", err)
57                 raise err
58             finally:
59                 self.log.info("unmounting device {}", pnf_id)
60                 self.unmount(pnf_id)
61
62             self.log.info("Ended execution of process method")
63
64         except JavaException, err:
65             self.log.error("Java Exception in the script", err)
66             raise err
67         except Exception, err:
68             self.log.error("Python Exception in the script", err)
69             raise err
70
71     def mount(self, pnf_id):
72         self.log.info("meshing mount payload")
73         mount_payload = self.resolveAndGenerateMessage("config-deploy-mapping", "config-deploy-template")
74         self.log.info("mount payload: \n {}", mount_payload)
75
76         # defining custom header
77         headers = {
78             "Content-Type": "application/xml"
79         }
80
81         url = self.base_mount_url + str(pnf_id)
82         self.log.info("sending mount request, url: {}", url)
83         web_client_service = self.restClientService(self.server_identifier)
84         web_client_service.exchangeResource("PUT", url, mount_payload, headers)
85
86     def unmount(self, pnf_id):
87         url = self.base_mount_url + str(pnf_id)
88         self.log.info("sending unmount request, url: {}", url)
89         web_client_service = self.restClientService(self.server_identifier)
90         web_client_service.exchangeResource("DELETE", url, "")
91
92     def apply_configuration(self, pnf_id, resolution_key, template_name):
93         self.log.info("Retrieving configlet from database (resolution-key: {}, template_name: {}",
94                       resolution_key, template_name)
95         configlet = self.resolveFromDatabase(resolution_key, template_name)
96         self.log.info("Configlet: {}", configlet)
97
98         # defining custom header
99         headers = {
100             "Content-Type": "application/yang.patch+json"
101         }
102
103         url = "restconf/config/network-topology:network-topology/topology/topology-netconf/node/" + pnf_id \
104               + "/yang-ext:mount/mynetconf:netconflist"
105         self.log.info("sending patch request,  url: {}", url)
106         web_client_service = self.restClientService(self.server_identifier)
107         result = web_client_service.exchangeResource("PATCH", url, configlet, headers)
108         self.log.info("Configuration application result: {}", result)
109
110     def recover(self, runtime_exception, execution_request):
111         self.log.info("Recover method, no code to execute")
112         return None