Revert "Renaming Files having BluePrint to have Blueprint"
[ccsdk/cds.git] / components / model-catalog / blueprint-model / uat-blueprints / PNF_CDS_RESTCONF / Scripts / kotlin / RestconfConfigDeploy.kt
1 /*
2 * ============LICENSE_START=======================================================
3 *  Copyright (C) 2020 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 package cba.pnf.config.aai
20
21 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ExecutionServiceInput
22 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.contentFromResolvedArtifactNB
23 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.storedContentFromResolvedArtifactNB
24 import org.onap.ccsdk.cds.blueprintsprocessor.functions.restconf.executor.restconfMountDevice
25 import org.onap.ccsdk.cds.blueprintsprocessor.functions.restconf.executor.restconfApplyDeviceConfig
26 import org.onap.ccsdk.cds.blueprintsprocessor.functions.restconf.executor.restconfUnMountDevice
27 import org.onap.ccsdk.cds.blueprintsprocessor.functions.restconf.executor.restconfDeviceConfig
28 import org.onap.ccsdk.cds.blueprintsprocessor.functions.restconf.executor.restconfClientService
29 import org.onap.ccsdk.cds.blueprintsprocessor.services.execution.AbstractScriptComponentFunction
30 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException
31 import org.onap.ccsdk.cds.controllerblueprints.core.logger
32 import com.fasterxml.jackson.databind.ObjectMapper
33 import org.onap.ccsdk.cds.blueprintsprocessor.rest.service.BlueprintWebClientService.WebClientResponse
34
35 class RestconfConfigDeploy : AbstractScriptComponentFunction() {
36
37     private val CONFIGLET_TEMPLATE_NAME = "config-assign"
38     private val CONFIGLET_RESOURCE_PATH = "yang-ext:mount/mynetconf:netconflist"
39     private val RESTCONF_SERVER_IDENTIFIER = "sdnc"
40     private val mapper = ObjectMapper()
41     private val log = logger(AbstractScriptComponentFunction::class.java)
42
43     override suspend fun processNB(executionRequest: ExecutionServiceInput) {
44         log.info("Started execution of process method")
45         try {
46             // Extract Resolution key & Device ID
47             val resolutionKey = getDynamicProperties("resolution-key").asText()
48             log.info("resolution_key: $resolutionKey")
49             val deviceID: String = requestPayloadActionProperty("config-deploy-properties")?.get(0)?.get("pnf-id")?.textValue()!!
50             log.info("device_id: $deviceID")
51             val webclientService = restconfClientService(RESTCONF_SERVER_IDENTIFIER)
52
53             try {
54                 // Mount the device
55                 val mountPayload = contentFromResolvedArtifactNB("config-deploy")
56                 log.debug("Mounting Device : $deviceID")
57                 restconfMountDevice(webclientService, deviceID, mountPayload, mutableMapOf("Content-Type" to "application/json"))
58
59                 // Log the current configuration for the subtree
60                 val currentConfig: Any = restconfDeviceConfig(webclientService, deviceID, CONFIGLET_RESOURCE_PATH)
61                 log.info("Current configuration subtree : $currentConfig")
62                 // Apply configlet
63                 val result = restconfApplyDeviceConfig(
64                     webclientService, deviceID, CONFIGLET_RESOURCE_PATH,
65                     storedContentFromResolvedArtifactNB(resolutionKey, CONFIGLET_TEMPLATE_NAME),
66                     mutableMapOf("Content-Type" to "application/yang.patch+json")
67                 ) as WebClientResponse<*>
68
69                 val jsonResult = mapper.readTree((result.body).toString())
70
71                 if (jsonResult.get("ietf-yang-patch:yang-patch-status").get("errors") != null) {
72                     log.error("There was an error configuring device")
73                 } else {
74                     log.info("Device has been configured succesfully")
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     override suspend fun recoverNB(runtimeException: RuntimeException, executionRequest: ExecutionServiceInput) {
88         log.info("Recover function called!")
89         log.info("Execution request : $executionRequest")
90         log.error("Exception", runtimeException)
91     }
92 }