Creating a reusable Restconf client
[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 # ============LICENSE_END=========================================================
16
17 from org.onap.ccsdk.cds.blueprintsprocessor.functions.restconf.executor import \
18     RestconfComponentFunction
19 from java.lang import Exception as JavaException
20
21 from restconf_client import RestconfClient
22
23
24 class RestconfConfigDeploy(RestconfComponentFunction):
25
26     log = globals()["log"]
27     configlet_template_name = "config-assign"
28     configlet_resource_path = "/yang-ext:mount/mynetconf:netconflist"
29     restconf_server_identifier = "sdncodl"
30
31     def process(self, execution_request):
32
33         self.log.info("Started execution of process method")
34         try:
35             restconf_client = RestconfClient(self.log, self)
36             pnf_id, resolution_key = self.retrieve_parameters(execution_request)
37             web_client_service = self.restClientService(self.restconf_server_identifier)
38
39             try:
40                 mount_payload = self.resolveAndGenerateMessage("config-deploy-mapping", "config-deploy-template")
41                 restconf_client.mount_device(web_client_service, pnf_id, mount_payload)
42
43                 configlet = self.resolveFromDatabase(resolution_key, self.configlet_template_name)
44                 restconf_client.configure_device(web_client_service, pnf_id, self.configlet_resource_path, configlet)
45             except Exception, err:
46                 self.log.error("an error occurred while configuring device {}", err)
47                 raise err
48             finally:
49                 restconf_client.unmount_device(web_client_service, pnf_id)
50
51         except JavaException, err:
52             self.log.error("Java Exception in the script", err)
53             raise err
54         except Exception, err:
55             self.log.error("Python Exception in the script:" + str(err), err)
56             raise err
57         self.log.info("Ended execution of process method")
58
59     def retrieve_parameters(self, execution_request):
60         resolution_key = self.getDynamicProperties("resolution-key").asText()
61         self.log.info("resolution_key: {}", resolution_key)
62         pnf_id = execution_request.payload.get("config-deploy-request").get("config-deploy-properties").get("pnf-id")
63         pnf_id = str(pnf_id).strip('\"')
64         self.log.info("pnf-id: {}", pnf_id)
65         return pnf_id, resolution_key
66
67     def recover(self, runtime_exception, execution_request):
68         self.log.info("Recover function called!")
69         self.log.info("Execution request", execution_request)
70         self.log.error("Exception", runtime_exception)
71         print self.bluePrintRuntimeService.getBluePrintError().addError(runtime_exception.getMessage())
72         return None