2 * Copyright © 2021 Orange
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.onap.ccsdk.cds.blueprintsprocessor.services.execution.scripts
19 import com.fasterxml.jackson.databind.JsonNode
20 import com.fasterxml.jackson.databind.node.ObjectNode
21 import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
22 import org.onap.ccsdk.cds.blueprintsprocessor.functions.k8s.definition.template.K8sConfigTemplateComponent
23 import org.onap.ccsdk.cds.blueprintsprocessor.functions.k8s.definition.template.K8sConfigValueComponent
24 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.processor.ResourceAssignmentProcessor
25 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.utils.ResourceAssignmentUtils
26 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException
27 import org.onap.ccsdk.cds.controllerblueprints.core.isNullOrMissing
28 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceAssignment
29 import org.slf4j.LoggerFactory
31 open class ConfigDeploySetup() : ResourceAssignmentProcessor() {
33 private val log = LoggerFactory.getLogger(ConfigDeploySetup::class.java)!!
35 override fun getName(): String {
36 return "ConfigDeploySetup"
39 override suspend fun processNB(executionRequest: ResourceAssignment) {
41 var retValue: Any? = null
44 if (executionRequest.name == "service-instance-id") {
45 var value = raRuntimeService.getInputValue(executionRequest.name)
46 if (!value.isNullOrMissing()) {
47 retValue = value.asText()
49 val vnfRelationshipList = raRuntimeService.getResolutionStore("vnf-relationship-list")
50 if (!vnfRelationshipList.isNullOrMissing()) {
51 vnfRelationshipList["relationship-list"].forEach { relation ->
52 if (relation["related-to"].asText() == "service-instance") {
53 relation["relationship-data"].forEach { data ->
54 if (data["relationship-key"].asText() == "service-instance.service-instance-id") {
55 retValue = data["relationship-value"].asText()
62 } else if (executionRequest.name == "vnf-id") {
63 var value = raRuntimeService.getInputValue(executionRequest.name)
64 if (!value.isNullOrMissing()) {
65 retValue = value.asText()
67 value = raRuntimeService.getInputValue("generic-vnf.vnf-id")
68 if (!value.isNullOrMissing()) {
69 retValue = value.asText()
72 } else if (executionRequest.name == "replica-count") {
73 var value = raRuntimeService.getInputValue(executionRequest.name)
75 if (!value.isNullOrMissing()) {
76 retValue = value.asText()
78 value = raRuntimeService.getInputValue("data")
79 if (!value.isNullOrMissing()) {
80 if (value["replicaCount"] != null) {
81 retValue = value["replicaCount"].asText()
85 } else if (executionRequest.name == "config-deploy-setup") {
86 val modulesSdnc = raRuntimeService.getResolutionStore("vf-modules-list-sdnc")["vf-modules"]
87 val modulesAai = raRuntimeService.getResolutionStore("vf-modules-list-aai")["vf-modules"]
88 val objectMapper = jacksonObjectMapper()
89 val result: ObjectNode = objectMapper.createObjectNode()
90 for (module in modulesSdnc) {
91 val modelTopology = module.at("/vf-module-data/vf-module-topology")
92 val moduleParameters = modelTopology.at("/vf-module-parameters/param")
93 val label: String? = getParamValueByName(moduleParameters, "vf_module_label")
95 val modelInfo = modelTopology["onap-model-information"]
96 val moduleData: ObjectNode = objectMapper.createObjectNode()
97 result.put(label, moduleData)
98 moduleData.put(K8sConfigTemplateComponent.INPUT_K8S_DEFINITION_NAME, modelInfo["model-invariant-uuid"].asText())
99 moduleData.put(K8sConfigTemplateComponent.INPUT_K8S_DEFINITION_VERSION, modelInfo["model-customization-uuid"].asText())
100 val templateName: String? = getParamValueByName(moduleParameters, K8sConfigTemplateComponent.INPUT_K8S_TEMPLATE_NAME)
101 val templateSource: String? = getParamValueByName(moduleParameters, K8sConfigTemplateComponent.INPUT_K8S_TEMPLATE_SOURCE)
102 val configValueSource: String? = getParamValueByName(moduleParameters, K8sConfigValueComponent.INPUT_K8S_CONFIG_VALUE_SOURCE)
103 val configName: String? = getParamValueByName(moduleParameters, K8sConfigValueComponent.INPUT_K8S_RB_CONFIG_NAME)
105 if (templateName != null)
106 moduleData.put(K8sConfigTemplateComponent.INPUT_K8S_TEMPLATE_NAME, templateName)
107 if (templateSource != null)
108 moduleData.put(K8sConfigTemplateComponent.INPUT_K8S_TEMPLATE_SOURCE, templateSource)
109 if (configValueSource != null)
110 moduleData.put(K8sConfigValueComponent.INPUT_K8S_CONFIG_VALUE_SOURCE, configValueSource)
111 if (configName != null)
112 moduleData.put(K8sConfigValueComponent.INPUT_K8S_RB_CONFIG_NAME, configName)
114 for (aaiModule in modulesAai) {
115 if (aaiModule["vf-module-id"].asText() == module["vf-module-id"].asText() && aaiModule["heat-stack-id"] != null) {
116 moduleData.put(K8sConfigValueComponent.INPUT_K8S_INSTANCE_ID, aaiModule["heat-stack-id"].asText())
124 ResourceAssignmentUtils.setResourceDataValue(executionRequest, raRuntimeService, retValue)
125 } catch (e: Exception) {
126 log.error(e.message, e)
127 ResourceAssignmentUtils.setResourceDataValue(executionRequest, raRuntimeService, "ERROR")
129 throw BluePrintProcessorException("Failed in template key ($executionRequest) assignments, cause: ${e.message}", e)
133 private fun getParamValueByName(params: JsonNode, paramName: String): String? {
134 for (param in params) {
135 if (param["name"].asText() == paramName && param["value"].asText() != "null") {
136 return param["value"].asText()
142 override suspend fun recoverNB(runtimeException: RuntimeException, executionRequest: ResourceAssignment) {
143 this.addError("${runtimeException.message}")