f0190e8ec08f3f38a2a656d4ab25f02464979fff
[ccsdk/cds.git] / components / model-catalog / blueprint-model / uat-blueprints / pnf_config_aai / Scripts / kotlin / RestconfConfigDeploy.kt
1 /*
2 * ============LICENSE_START=======================================================
3 *  Copyright (C) 2019 Nordix Foundation.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 *      http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 * ============LICENSE_END=========================================================
17  */
18
19
20 package cba.pnf.config.aai
21
22
23 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ExecutionServiceInput
24 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.contentFromResolvedArtifactNB
25 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.storedContentFromResolvedArtifactNB
26 import org.onap.ccsdk.cds.blueprintsprocessor.functions.restconf.executor.restconfMountDevice
27 import org.onap.ccsdk.cds.blueprintsprocessor.functions.restconf.executor.restconfApplyDeviceConfig
28 import org.onap.ccsdk.cds.blueprintsprocessor.functions.restconf.executor.restconfUnMountDevice
29 import org.onap.ccsdk.cds.blueprintsprocessor.functions.restconf.executor.restconfDeviceConfig
30 import org.onap.ccsdk.cds.blueprintsprocessor.functions.restconf.executor.restconfClientService
31 import org.onap.ccsdk.cds.blueprintsprocessor.services.execution.AbstractScriptComponentFunction
32 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException
33 import org.onap.ccsdk.cds.controllerblueprints.core.logger
34 import com.fasterxml.jackson.databind.ObjectMapper
35 import org.onap.ccsdk.cds.blueprintsprocessor.rest.service.BlueprintWebClientService.WebClientResponse
36
37 class RestconfConfigDeploy : AbstractScriptComponentFunction() {
38     private val CONFIGLET_TEMPLATE_NAME = "config-assign"
39     private val CONFIGLET_RESOURCE_PATH = "yang-ext:mount/mynetconf:netconflist"
40     private val RESTCONF_SERVER_IDENTIFIER = "sdnc"
41     private val mapper = ObjectMapper()
42     private val log = logger(AbstractScriptComponentFunction::class.java)
43
44     override suspend fun processNB(executionRequest: ExecutionServiceInput) {
45         log.info("Started execution of process method")
46         try {
47             // Extract Resolution key & Device ID
48             val resolutionKey = getDynamicProperties("resolution-key").asText()
49             log.info("resolution_key: $resolutionKey")
50             val deviceID: String = requestPayloadActionProperty("config-deploy-properties")?.get(0)?.get("pnf-id")?.textValue()!!
51             log.info("device_id: $deviceID")
52             val webclientService = restconfClientService(RESTCONF_SERVER_IDENTIFIER)
53
54             try {
55                 // Mount the device
56                 val mountPayload = contentFromResolvedArtifactNB("config-deploy")
57                 log.debug("Mounting Device : $deviceID")
58                 restconfMountDevice(webclientService, deviceID, mountPayload, mutableMapOf("Content-Type" to "application/json"))
59
60                 //Log the current configuration for the subtree
61                 val currentConfig: Any = restconfDeviceConfig(webclientService, deviceID, CONFIGLET_RESOURCE_PATH)
62                 log.info("Current configuration subtree : $currentConfig")
63                 //Apply configlet
64                 val result = restconfApplyDeviceConfig(webclientService, deviceID, CONFIGLET_RESOURCE_PATH,
65                         storedContentFromResolvedArtifactNB(resolutionKey, CONFIGLET_TEMPLATE_NAME),
66                         mutableMapOf("Content-Type" to "application/yang.patch+json")) as WebClientResponse<*>
67
68                 val jsonResult = mapper.readTree((result.body).toString())
69
70                 if(jsonResult.get("ietf-yang-patch:yang-patch-status").get("errors") != null) {
71                     log.error("There was an error configuring device")
72                 }  else {
73                     log.info("Device has been configured succesfully")
74                 }
75
76             } catch (err: Exception) {
77                 log.error("an error occurred while configuring device {}", err)
78             } finally {
79                 //Un mount device
80                 restconfUnMountDevice(webclientService, deviceID, "")
81             }
82         } catch (bpe: BluePrintProcessorException) {
83             log.error("Error looking up server identifier ", bpe)
84         }
85     }
86
87
88     override suspend fun recoverNB(runtimeException: RuntimeException, executionRequest: ExecutionServiceInput) {
89         log.info("Recover function called!")
90         log.info("Execution request : $executionRequest")
91         log.error("Exception", runtimeException)
92     }
93 }