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
9 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package cba.pnf.config.aai
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
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)
44 override suspend fun processNB(executionRequest: ExecutionServiceInput) {
45 log.info("Started execution of process method")
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)
56 val mountPayload = contentFromResolvedArtifactNB("config-deploy")
57 log.debug("Mounting Device : $deviceID")
58 restconfMountDevice(webclientService, deviceID, mountPayload, mutableMapOf("Content-Type" to "application/json"))
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")
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<*>
68 val jsonResult = mapper.readTree((result.body).toString())
70 if(jsonResult.get("ietf-yang-patch:yang-patch-status").get("errors") != null) {
71 log.error("There was an error configuring device")
73 log.info("Device has been configured succesfully")
76 } catch (err: Exception) {
77 log.error("an error occurred while configuring device {}", err)
80 restconfUnMountDevice(webclientService, deviceID, "")
82 } catch (bpe: BluePrintProcessorException) {
83 log.error("Error looking up server identifier ", bpe)
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)