Real vFW CNF config-assign and config-deploy
[demo.git] / heat / vFW_CNF_CDS / templates / cba / Scripts / kotlin / ConfigDeploySetup.kt
1 /*
2  * Copyright © 2021 Orange
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.onap.ccsdk.cds.blueprintsprocessor.services.execution.scripts
18
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.resource.dict.ResourceAssignment
28 import org.slf4j.LoggerFactory
29
30 open class ConfigDeploySetup() : ResourceAssignmentProcessor() {
31
32     private val log = LoggerFactory.getLogger(ConfigDeploySetup::class.java)!!
33
34     override fun getName(): String {
35         return "ConfigDeploySetup"
36     }
37
38     override suspend fun processNB(executionRequest: ResourceAssignment) {
39
40         var retValue: ObjectNode? = null
41
42         try {
43             if (executionRequest.name == "config-deploy-setup") {
44                 val modulesSdnc = raRuntimeService.getResolutionStore("vf-modules-list-sdnc")["vf-modules"]
45                 val modulesAai = raRuntimeService.getResolutionStore("vf-modules-list-aai")["vf-modules"]
46                 val objectMapper = jacksonObjectMapper()
47                 val result: ObjectNode = objectMapper.createObjectNode()
48                 for (module in modulesSdnc) {
49                     val modelTopology = module.at("/vf-module-data/vf-module-topology")
50                     val moduleParameters = modelTopology.at("/vf-module-parameters/param")
51                     val label: String? = getParamValueByName(moduleParameters, "vf_module_label")
52                     if (label != null) {
53                         val modelInfo = modelTopology["onap-model-information"]
54                         val moduleData: ObjectNode = objectMapper.createObjectNode()
55                         result.put(label, moduleData)
56                         moduleData.put(K8sConfigTemplateComponent.INPUT_K8S_DEFINITION_NAME, modelInfo["model-invariant-uuid"].asText())
57                         moduleData.put(K8sConfigTemplateComponent.INPUT_K8S_DEFINITION_VERSION, modelInfo["model-uuid"].asText())
58                         val templateName: String? = getParamValueByName(moduleParameters, K8sConfigTemplateComponent.INPUT_K8S_TEMPLATE_NAME)
59                         val templateSource: String? = getParamValueByName(moduleParameters, K8sConfigTemplateComponent.INPUT_K8S_TEMPLATE_SOURCE)
60                         val configValueSource: String? = getParamValueByName(moduleParameters, K8sConfigValueComponent.INPUT_K8S_CONFIG_VALUE_SOURCE)
61                         val configName: String? = getParamValueByName(moduleParameters, K8sConfigValueComponent.INPUT_K8S_RB_CONFIG_NAME)
62
63                         if (templateName != null)
64                             moduleData.put(K8sConfigTemplateComponent.INPUT_K8S_TEMPLATE_NAME, templateName)
65                         if (templateSource != null)
66                             moduleData.put(K8sConfigTemplateComponent.INPUT_K8S_TEMPLATE_SOURCE, templateSource)
67                         if (configValueSource != null)
68                             moduleData.put(K8sConfigValueComponent.INPUT_K8S_CONFIG_VALUE_SOURCE, configValueSource)
69                         if (configName != null)
70                             moduleData.put(K8sConfigValueComponent.INPUT_K8S_RB_CONFIG_NAME, configName)
71
72                         for (aaiModule in modulesAai) {
73                             if (aaiModule["vf-module-id"].asText() == module["vf-module-id"].asText()) {
74                                 moduleData.put(K8sConfigValueComponent.INPUT_K8S_INSTANCE_ID, aaiModule["heat-stack-id"].asText())
75                                 break
76                             }
77                         }
78                     }
79                 }
80                 retValue = result
81             }
82             ResourceAssignmentUtils.setResourceDataValue(executionRequest, raRuntimeService, retValue)
83         } catch (e: Exception) {
84             log.error(e.message, e)
85             ResourceAssignmentUtils.setResourceDataValue(executionRequest, raRuntimeService, "ERROR")
86
87             throw BlueprintProcessorException("Failed in template key ($executionRequest) assignments, cause: ${e.message}", e)
88         }
89     }
90
91     private fun getParamValueByName(params: JsonNode, paramName: String): String? {
92         for (param in params) {
93             if (param["name"].asText() == paramName) {
94                 return param["value"].asText()
95             }
96         }
97         return null
98     }
99
100     override suspend fun recoverNB(runtimeException: RuntimeException, executionRequest: ResourceAssignment) {
101         this.addError("${runtimeException.message}")
102     }
103 }