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
8 # http://www.apache.org/licenses/LICENSE-2.0
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.
16 # SPDX-License-Identifier: Apache-2.0
17 # ============LICENSE_END=========================================================
18 from time import sleep
20 from org.onap.ccsdk.cds.blueprintsprocessor.functions.restconf.executor import \
21 RestconfComponentFunction
22 from java.lang import Exception as JavaException
25 class RestconfConfigDeploy(RestconfComponentFunction):
27 log = globals()["log"]
29 base_mount_url = "restconf/config/network-topology:network-topology/topology/topology-netconf/node/"
30 server_identifier = "sdncodl"
31 configlet_template_name = "config-assign"
33 def process(self, execution_request):
35 self.log.info("Started execution of process method")
37 self.log.info("getting resolution-key")
38 resolution_key = self.getDynamicProperties("resolution-key").asText()
39 self.log.info("resolution_key: {}", resolution_key)
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)
46 self.log.info("mounting device {}", pnf_id)
49 self.log.info("sleeping for {} seconds", self.seconds_to_sleep)
50 sleep(self.seconds_to_sleep)
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)
59 self.log.info("unmounting device {}", pnf_id)
62 self.log.info("Ended execution of process method")
64 except JavaException, err:
65 self.log.error("Java Exception in the script", err)
67 except Exception, err:
68 self.log.error("Python Exception in the script", err)
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)
76 # defining custom header
78 "Content-Type": "application/xml"
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)
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, "")
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)
98 # defining custom header
100 "Content-Type": "application/yang.patch+json"
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)
110 def recover(self, runtime_exception, execution_request):
111 self.log.info("Recover method, no code to execute")