Add declarative acceptance tests
[ccsdk/cds.git] / components / model-catalog / blueprint-model / uat-blueprints / pnf_config / 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 java.lang import Exception as JavaException
18
19 from restconf_client import RestconfClient
20 from org.onap.ccsdk.cds.blueprintsprocessor.services.execution import AbstractScriptComponentFunction
21
22
23 class RestconfConfigDeploy(AbstractScriptComponentFunction):
24     log = globals()["log"]
25     configlet_template_name = "config-assign"
26     configlet_resource_path = "/yang-ext:mount/mynetconf:netconflist"
27     restconf_server_identifier = "sdncodl"
28
29     def process(self, execution_request):
30
31         self.log.info("Started execution of process method")
32         try:
33             restconf_client = RestconfClient(self.log, self)
34             pnf_id, resolution_key = self.retrieve_parameters(execution_request)
35             web_client_service = restconf_client.web_client_service(self.restconf_server_identifier)
36
37             try:
38                 # mount the device
39                 mount_payload = restconf_client.resolve_and_generate_message_from_template_prefix("config-deploy")
40                 restconf_client.mount_device(web_client_service, pnf_id, mount_payload, "application/json")
41
42                 # log the current configuration subtree
43                 current_configuration = restconf_client.retrieve_device_configuration_subtree(
44                     web_client_service, pnf_id, self.configlet_resource_path)
45                 self.log.info("Current configuration subtree: {}", current_configuration)
46
47                 # apply configuration
48                 configlet = restconf_client.retrieve_resolved_template_from_database(resolution_key, self.configlet_template_name)
49                 restconf_client.configure_device_json_patch(
50                     web_client_service, pnf_id, self.configlet_resource_path, configlet)
51             except Exception, err:
52                 self.log.error("an error occurred while configuring device {}", err)
53                 raise err
54             finally:
55                 restconf_client.unmount_device(web_client_service, pnf_id)
56
57         except JavaException, err:
58             self.log.error("Java Exception in the script", err)
59             raise err
60         except Exception, err:
61             self.log.error("Python Exception in the script:" + str(err), err)
62             raise err
63         self.log.info("Ended execution of process method")
64
65     def retrieve_parameters(self, execution_request):
66         resolution_key = self.getDynamicProperties("resolution-key").asText()
67         self.log.info("resolution_key: {}", resolution_key)
68         pnf_id = execution_request.payload.get("config-deploy-request").get("config-deploy-properties").get("pnf-id")
69         pnf_id = str(pnf_id).strip('\"')
70         self.log.info("pnf-id: {}", pnf_id)
71         return pnf_id, resolution_key
72
73     def recover(self, runtime_exception, execution_request):
74         self.log.info("Recover function called!")
75         self.log.info("Execution request", execution_request)
76         self.log.error("Exception", runtime_exception)
77         print self.bluePrintRuntimeService.getBluePrintError().addError(runtime_exception.getMessage())
78         return None