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.
15 # ============LICENSE_END=========================================================
17 from time import sleep
19 from org.onap.ccsdk.cds.blueprintsprocessor.functions.restconf.executor import \
20 RestconfComponentFunction
21 from java.lang import Exception as JavaException
24 class RestconfConfigDeploy(RestconfComponentFunction):
26 log = globals()["log"]
27 odl_status_check_limit = 10
28 odl_status_check_pause = 1
29 odl_status_check_url = "restconf/operational/network-topology:network-topology/topology/topology-netconf/node/"
30 base_odl_url = "restconf/config/network-topology:network-topology/topology/topology-netconf/node/"
31 server_identifier = "sdncodl"
32 configlet_template_name = "config-assign"
33 configlet_odl_resource = "/yang-ext:mount/mynetconf:netconflist"
35 def process(self, execution_request):
37 self.log.info("Started execution of process method")
39 pnf_id, resolution_key = self.retrieve_parameters(execution_request)
40 self.interact_with_odl(pnf_id, resolution_key)
41 except JavaException, err:
42 self.log.error("Java Exception in the script", err)
44 except Exception, err:
45 self.log.error("Python Exception in the script:" + str(err), err)
47 self.log.info("Ended execution of process method")
49 def retrieve_parameters(self, execution_request):
50 resolution_key = self.getDynamicProperties("resolution-key").asText()
51 self.log.info("resolution_key: {}", resolution_key)
52 pnf_id = execution_request.payload.get("config-deploy-request").get("config-deploy-properties").get("pnf-id")
53 pnf_id = str(pnf_id).strip('\"')
54 self.log.info("pnf-id: {}", pnf_id)
55 return pnf_id, resolution_key
57 def interact_with_odl(self, pnf_id, resolution_key):
60 self.log_current_configlet(pnf_id)
61 self.apply_configuration(pnf_id, resolution_key, self.configlet_template_name)
62 except Exception, err:
63 self.log.error("an error occurred while configuring device {}", err)
66 self.log.info("unmounting device {}", pnf_id)
69 def mount(self, pnf_id):
70 self.log.info("mounting device {}", pnf_id)
71 mount_payload = self.resolveAndGenerateMessage("config-deploy-mapping", "config-deploy-template")
72 self.log.info("mount payload: \n {}", mount_payload)
73 headers = {"Content-Type": "application/xml"} # defining custom header
74 url = self.base_odl_url + str(pnf_id)
75 self.log.info("sending mount request, url: {}", url)
76 web_client_service = self.restClientService(self.server_identifier)
77 web_client_service.exchangeResource("PUT", url, mount_payload, headers)
78 self.wait_for_odl_to_mount(pnf_id)
80 def wait_for_odl_to_mount(self, pnf_id):
82 url = self.odl_status_check_url + pnf_id
83 self.log.info("url for ODL status check: {}", url)
84 web_client_service = self.restClientService(self.server_identifier)
85 expected_result = '"netconf-node-topology:connection-status":"connected"'
86 while counter < self.odl_status_check_limit:
87 result = web_client_service.exchangeResource("GET", url, "")
88 self.log.info("ODL status check result: {}", result)
89 if expected_result in result:
90 self.log.info("PNF was mounted successfully on ODL")
94 raise JavaException("PNF was not mounted on ODL, aborting configuration procedure")
96 def log_current_configlet(self, pnf_id):
97 self.log.info("retrieving configuration for device {}", pnf_id)
98 url = self.base_odl_url + pnf_id + self.configlet_odl_resource
99 self.log.info("sending GET request, url: {}", url)
100 web_client_service = self.restClientService(self.server_identifier)
101 result = web_client_service.exchangeResource("GET", url, "")
102 self.log.info("Current configuration: {}", result)
104 def apply_configuration(self, pnf_id, resolution_key, template_name):
105 self.log.info("configuring device {}", pnf_id)
106 self.log.info("Retrieving configlet from database (resolution-key: {}, template_name: {}",
107 resolution_key, template_name)
108 configlet = self.resolveFromDatabase(resolution_key, template_name)
109 self.log.info("Configlet: {}", configlet)
110 headers = { "Content-Type": "application/yang.patch+json" } # defining custom header
111 url = self.base_odl_url + pnf_id + self.configlet_odl_resource
112 self.log.info("sending patch request, url: {}", url)
113 web_client_service = self.restClientService(self.server_identifier)
114 result = web_client_service.exchangeResource("PATCH", url, configlet, headers)
115 self.log.info("Configuration application result: {}", result)
117 def unmount(self, pnf_id):
118 url = self.base_odl_url + str(pnf_id)
119 self.log.info("sending unmount request, url: {}", url)
120 web_client_service = self.restClientService(self.server_identifier)
121 web_client_service.exchangeResource("DELETE", url, "")
123 def recover(self, runtime_exception, execution_request):
124 self.log.info("Recover function called!")
125 self.log.error(runtime_exception.getMessage())
126 print self.bluePrintRuntimeService.getBluePrintError().addError(runtime_exception.getMessage())