Renaming Files having BluePrint to have Blueprint
[ccsdk/cds.git] / components / model-catalog / blueprint-model / uat-blueprints / pnf_config / 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 package cba.pnf.config
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
33 class RestconfConfigDeploy : AbstractScriptComponentFunction() {
34
35     private val CONFIGLET_TEMPLATE_NAME = "config-assign"
36     private val CONFIGLET_RESOURCE_PATH = "yang-ext:mount/mynetconf:netconflist"
37     private val RESTCONF_SERVER_IDENTIFIER = "sdnc"
38     private val log = logger(AbstractScriptComponentFunction::class.java)
39
40     override suspend fun processNB(executionRequest: ExecutionServiceInput) {
41         log.info("Started execution of process method")
42         try {
43             // Extract Resolution key & Device ID
44             val resolutionKey = getDynamicProperties("resolution-key").asText()
45             log.info("resolution_key: $resolutionKey")
46             val deviceID: String = requestPayloadActionProperty("config-deploy-properties")?.get(0)?.get("pnf-id")?.textValue()!!
47             log.info("device_id: $deviceID")
48             val webclientService = restconfClientService(RESTCONF_SERVER_IDENTIFIER)
49
50             try {
51                 // Mount the device
52                 val mountPayload = contentFromResolvedArtifactNB("config-deploy")
53                 log.debug("Mounting Device : $deviceID")
54                 restconfMountDevice(webclientService, deviceID, mountPayload, mutableMapOf("Content-Type" to "application/json"))
55
56                 // Log the current configuration for the subtree
57                 val currentConfig: Any = restconfDeviceConfig(webclientService, deviceID, CONFIGLET_RESOURCE_PATH)
58                 log.info("Current configuration subtree : $currentConfig")
59                 // Apply configlet
60                 restconfApplyDeviceConfig(
61                     webclientService, deviceID, CONFIGLET_RESOURCE_PATH,
62                     storedContentFromResolvedArtifactNB(resolutionKey, CONFIGLET_TEMPLATE_NAME),
63                     mutableMapOf("Content-Type" to "application/yang.patch+json")
64                 )
65             } catch (err: Exception) {
66                 log.error("an error occurred while configuring device {}", err)
67             } finally {
68                 // Un mount device
69                 restconfUnMountDevice(webclientService, deviceID, "")
70             }
71         } catch (bpe: BlueprintProcessorException) {
72             log.error("Error looking up server identifier ", bpe)
73         }
74     }
75
76     override suspend fun recoverNB(runtimeException: RuntimeException, executionRequest: ExecutionServiceInput) {
77         log.info("Recover function called!")
78         log.info("Execution request : $executionRequest")
79         log.error("Exception", runtimeException)
80     }
81 }