2e6ec816ebbc98e25a48cd4c2745b606a5a6292a
[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.resource.resolution.processor.ResourceAssignmentProcessor
23 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.utils.ResourceAssignmentUtils
24 import org.onap.ccsdk.cds.controllerblueprints.core.BlueprintProcessorException
25 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceAssignment
26 import org.slf4j.LoggerFactory
27
28 open class ConfigDeploySetup() : ResourceAssignmentProcessor() {
29
30     private val log = LoggerFactory.getLogger(ConfigDeploySetup::class.java)!!
31
32     override fun getName(): String {
33         return "ConfigDeploySetup"
34     }
35
36     override suspend fun processNB(resourceAssignment: ResourceAssignment) {
37
38         var retValue: ObjectNode? = null
39
40         try {
41             if (resourceAssignment.name == "config-deploy-setup") {
42                 val modulesSdnc = raRuntimeService.getResolutionStore("vf-modules-list-sdnc")["vf-modules"]
43                 val modulesAai = raRuntimeService.getResolutionStore("vf-modules-list-aai")["vf-modules"]
44                 val objectMapper = jacksonObjectMapper()
45                 val result: ObjectNode = objectMapper.createObjectNode()
46                 for (module in modulesSdnc) {
47                     val modelTopology = module.at("/vf-module-data/vf-module-topology")
48                     val moduleParameters = modelTopology.at("/vf-module-parameters/param")
49                     val label: String? = getParamValueByName(moduleParameters,"vf_module_label")
50                     if (label != null) {
51                         val modelInfo = modelTopology["onap-model-information"]
52                         val moduleData: ObjectNode = objectMapper.createObjectNode()
53                         result.put(label, moduleData)
54                         moduleData.put("k8s-rb-definition-name", modelInfo["model-invariant-uuid"].asText())
55                         moduleData.put("k8s-rb-definition-version", modelInfo["model-uuid"].asText())
56                         val templateName: String? = getParamValueByName(moduleParameters,"k8s-rb-config-template-name")
57                         val templateSource: String? = getParamValueByName(moduleParameters,"k8s-rb-config-template-source")
58                         if (templateName != null)
59                             moduleData.put("k8s-rb-config-template-name", templateName)
60                         if (templateSource != null)
61                             moduleData.put("k8s-rb-config-template-source", templateSource)
62                         for (aaiModule in modulesAai) {
63                             if (aaiModule["vf-module-id"].asText() == module["vf-module-id"].asText()) {
64                                 moduleData.put("k8s-instance-id", aaiModule["heat-stack-id"].asText())
65                                 break
66                             }
67                         }
68                     }
69                 }
70                 retValue = result
71             }
72             ResourceAssignmentUtils.setResourceDataValue(resourceAssignment, raRuntimeService, retValue)
73         } catch (e: Exception) {
74             log.error(e.message, e)
75             ResourceAssignmentUtils.setResourceDataValue(resourceAssignment, raRuntimeService, "ERROR")
76
77             throw BlueprintProcessorException("Failed in template key ($resourceAssignment) assignments, cause: ${e.message}", e)
78         }
79     }
80
81     private fun getParamValueByName(params: JsonNode, paramName: String): String? {
82         for (param in params) {
83             if (param["name"].asText() == paramName) {
84                 return param["value"].asText()
85             }
86         }
87         return null
88     }
89
90     override suspend fun recoverNB(runtimeException: RuntimeException, resourceAssignment: ResourceAssignment) {
91         this.addError("${runtimeException.message}")
92     }
93 }