f925be061e366c75fe41df514f49359326c9ad43
[demo.git] / tutorials / ApacheCNF / 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.core.isNullOrMissing
28 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceAssignment
29 import org.slf4j.LoggerFactory
30
31 open class ConfigDeploySetup() : ResourceAssignmentProcessor() {
32
33     private val log = LoggerFactory.getLogger(ConfigDeploySetup::class.java)!!
34
35     override fun getName(): String {
36         return "ConfigDeploySetup"
37     }
38
39     override suspend fun processNB(executionRequest: ResourceAssignment) {
40
41         var retValue: Any? = null
42
43         try {
44             if (executionRequest.name == "service-instance-id") {
45                 var value = raRuntimeService.getInputValue(executionRequest.name)
46                 if (!value.isNullOrMissing()) {
47                     retValue = value.asText()
48                 } else {
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()
56                                     }
57                                 }
58                             }
59                         }
60                     }
61                 }
62             } else if (executionRequest.name == "vnf-id") {
63                 var value = raRuntimeService.getInputValue(executionRequest.name)
64                 if (!value.isNullOrMissing()) {
65                     retValue = value.asText()
66                 } else {
67                     value = raRuntimeService.getInputValue("generic-vnf.vnf-id")
68                     if (!value.isNullOrMissing()) {
69                         retValue = value.asText()
70                     }
71                 }
72             } else if (executionRequest.name == "replica-count") {
73                 retValue = "1"
74                 try {
75                     var value = raRuntimeService.getInputValue(executionRequest.name)
76                     if (!value.isNullOrMissing()) {
77                         retValue = value.asText()
78                     } else {
79                         value = raRuntimeService.getInputValue("data")
80                         if (!value.isNullOrMissing()) {
81                             if (value["replicaCount"] != null) {
82                                 retValue = value["replicaCount"].asText()
83                             }
84                         }
85                     }
86                 } catch (e: Exception) {
87                     log.error(e.message, e)
88                     log.info("Setting default replica count: 1")
89                 }
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")
99                     if (label != null) {
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)
109
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)
118
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())
122                                 break
123                             }
124                         }
125                     }
126                 }
127                 retValue = result
128             }
129             ResourceAssignmentUtils.setResourceDataValue(executionRequest, raRuntimeService, retValue)
130         } catch (e: Exception) {
131             log.error(e.message, e)
132             ResourceAssignmentUtils.setResourceDataValue(executionRequest, raRuntimeService, "ERROR")
133
134             throw BluePrintProcessorException("Failed in template key ($executionRequest) assignments, cause: ${e.message}", e)
135         }
136     }
137
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()
142             }
143         }
144         return null
145     }
146
147     override suspend fun recoverNB(runtimeException: RuntimeException, executionRequest: ResourceAssignment) {
148         this.addError("${runtimeException.message}")
149     }
150 }