[APACHE] Add Apache CNF use case files
[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                 var value = raRuntimeService.getInputValue(executionRequest.name)
74                 retValue = "1"
75                 if (!value.isNullOrMissing()) {
76                     retValue = value.asText()
77                 } else {
78                     value = raRuntimeService.getInputValue("data")
79                     if (!value.isNullOrMissing()) {
80                         if (value["replicaCount"] != null) {
81                             retValue = value["replicaCount"].asText()
82                         }
83                     }
84                 }
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")
94                     if (label != null) {
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)
104
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)
113
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())
117                                 break
118                             }
119                         }
120                     }
121                 }
122                 retValue = result
123             }
124             ResourceAssignmentUtils.setResourceDataValue(executionRequest, raRuntimeService, retValue)
125         } catch (e: Exception) {
126             log.error(e.message, e)
127             ResourceAssignmentUtils.setResourceDataValue(executionRequest, raRuntimeService, "ERROR")
128
129             throw BluePrintProcessorException("Failed in template key ($executionRequest) assignments, cause: ${e.message}", e)
130         }
131     }
132
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()
137             }
138         }
139         return null
140     }
141
142     override suspend fun recoverNB(runtimeException: RuntimeException, executionRequest: ResourceAssignment) {
143         this.addError("${runtimeException.message}")
144     }
145 }