Clean restconf duplicate models and Implementation.
[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 from org.onap.ccsdk.cds.blueprintsprocessor.services.execution import AbstractScriptComponentFunction
23
24
25 class RestconfConfigDeploy(AbstractScriptComponentFunction):
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 = restconf_client.web_client_service(self.restconf_server_identifier)
38
39             try:
40                 # mount the device
41                 mount_payload = restconf_client.resolve_and_generate_message_from_template_prefix("config-deploy")
42                 restconf_client.mount_device(web_client_service, pnf_id, mount_payload)
43
44                 # log the current configuration subtree
45                 current_configuration = restconf_client.retrieve_device_configuration_subtree(
46                     web_client_service, pnf_id, self.configlet_resource_path)
47                 self.log.info("Current configuration subtree: {}", current_configuration)
48
49                 # apply configuration
50                 configlet = restconf_client.retrieve_resolved_template_from_database(resolution_key, self.configlet_template_name)
51                 restconf_client.configure_device_json_patch(
52                     web_client_service, pnf_id, self.configlet_resource_path, configlet)
53             except Exception, err:
54                 self.log.error("an error occurred while configuring device {}", err)
55                 raise err
56             finally:
57                 restconf_client.unmount_device(web_client_service, pnf_id)
58
59         except JavaException, err:
60             self.log.error("Java Exception in the script", err)
61             raise err
62         except Exception, err:
63             self.log.error("Python Exception in the script:" + str(err), err)
64             raise err
65         self.log.info("Ended execution of process method")
66
67     def retrieve_parameters(self, execution_request):
68         resolution_key = self.getDynamicProperties("resolution-key").asText()
69         self.log.info("resolution_key: {}", resolution_key)
70         pnf_id = execution_request.payload.get("config-deploy-request").get("config-deploy-properties").get("pnf-id")
71         pnf_id = str(pnf_id).strip('\"')
72         self.log.info("pnf-id: {}", pnf_id)
73         return pnf_id, resolution_key
74
75     def recover(self, runtime_exception, execution_request):
76         self.log.info("Recover function called!")
77         self.log.info("Execution request", execution_request)
78         self.log.error("Exception", runtime_exception)
79         print self.bluePrintRuntimeService.getBluePrintError().addError(runtime_exception.getMessage())
80         return None