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") {
75 var value = raRuntimeService.getInputValue(executionRequest.name)
76 if (!value.isNullOrMissing()) {
77 retValue = value.asText()
79 value = raRuntimeService.getInputValue("data")
80 if (!value.isNullOrMissing()) {
81 if (value["replicaCount"] != null) {
82 retValue = value["replicaCount"].asText()
86 } catch (e: Exception) {
87 log.error(e.message, e)
88 log.info("Setting default replica count: 1")
90 } else if (executionRequest.name == "config-deploy-setup") {
91 val modulesSdnc = raRuntimeService.getResolutionStore("vf-modules-list-sdnc")["vf-modules"]
92 val modulesAai = raRuntimeService.getResolutionStore("vf-modules-list-aai")["vf-modules"]
93 val objectMapper = jacksonObjectMapper()
94 val result: ObjectNode = objectMapper.createObjectNode()
95 for (module in modulesSdnc) {
96 val modelTopology = module.at("/vf-module-data/vf-module-topology")
97 val moduleParameters = modelTopology.at("/vf-module-parameters/param")
98 val label: String? = getParamValueByName(moduleParameters, "vf_module_label")
100 val modelInfo = modelTopology["onap-model-information"]
101 val moduleData: ObjectNode = objectMapper.createObjectNode()
102 result.put(label, moduleData)
103 moduleData.put(K8sConfigTemplateComponent.INPUT_K8S_DEFINITION_NAME, modelInfo["model-invariant-uuid"].asText())
104 moduleData.put(K8sConfigTemplateComponent.INPUT_K8S_DEFINITION_VERSION, modelInfo["model-customization-uuid"].asText())
105 val templateName: String? = getParamValueByName(moduleParameters, K8sConfigTemplateComponent.INPUT_K8S_TEMPLATE_NAME)
106 val templateSource: String? = getParamValueByName(moduleParameters, K8sConfigTemplateComponent.INPUT_K8S_TEMPLATE_SOURCE)
107 val configValueSource: String? = getParamValueByName(moduleParameters, K8sConfigValueComponent.INPUT_K8S_CONFIG_VALUE_SOURCE)
108 val configName: String? = getParamValueByName(moduleParameters, K8sConfigValueComponent.INPUT_K8S_RB_CONFIG_NAME)
110 if (templateName != null)
111 moduleData.put(K8sConfigTemplateComponent.INPUT_K8S_TEMPLATE_NAME, templateName)
112 if (templateSource != null)
113 moduleData.put(K8sConfigTemplateComponent.INPUT_K8S_TEMPLATE_SOURCE, templateSource)
114 if (configValueSource != null)
115 moduleData.put(K8sConfigValueComponent.INPUT_K8S_CONFIG_VALUE_SOURCE, configValueSource)
116 if (configName != null)
117 moduleData.put(K8sConfigValueComponent.INPUT_K8S_RB_CONFIG_NAME, configName)
119 for (aaiModule in modulesAai) {
120 if (aaiModule["vf-module-id"].asText() == module["vf-module-id"].asText() && aaiModule["heat-stack-id"] != null) {
121 moduleData.put(K8sConfigValueComponent.INPUT_K8S_INSTANCE_ID, aaiModule["heat-stack-id"].asText())
129 ResourceAssignmentUtils.setResourceDataValue(executionRequest, raRuntimeService, retValue)
130 } catch (e: Exception) {
131 log.error(e.message, e)
132 ResourceAssignmentUtils.setResourceDataValue(executionRequest, raRuntimeService, "ERROR")
134 throw BluePrintProcessorException("Failed in template key ($executionRequest) assignments, cause: ${e.message}", e)
138 private fun getParamValueByName(params: JsonNode, paramName: String): String? {
139 for (param in params) {
140 if (param["name"].asText() == paramName && param["value"].asText() != "null") {
141 return param["value"].asText()
147 override suspend fun recoverNB(runtimeException: RuntimeException, executionRequest: ResourceAssignment) {
148 this.addError("${runtimeException.message}")