Update INFO.yaml with new PTL
[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.k8s.definition.profile.K8sProfileUploadComponent
25 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.processor.ResourceAssignmentProcessor
26 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.utils.ResourceAssignmentUtils
27 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException
28 import org.onap.ccsdk.cds.controllerblueprints.core.isNullOrMissing
29 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceAssignment
30 import org.slf4j.LoggerFactory
31
32 open class ConfigDeploySetup() : ResourceAssignmentProcessor() {
33
34     private val log = LoggerFactory.getLogger(ConfigDeploySetup::class.java)!!
35
36     override fun getName(): String {
37         return "ConfigDeploySetup"
38     }
39
40     override suspend fun processNB(executionRequest: ResourceAssignment) {
41
42         var retValue: Any? = null
43
44         try {
45             if (executionRequest.name == "service-instance-id") {
46                 var value = raRuntimeService.getInputValue(executionRequest.name)
47                 if (!value.isNullOrMissing()) {
48                     retValue = value.asText()
49                 } else {
50                     val vnfRelationshipList = raRuntimeService.getResolutionStore("vnf-relationship-list")
51                     if (!vnfRelationshipList.isNullOrMissing()) {
52                         vnfRelationshipList["relationship-list"].forEach { relation ->
53                             if (relation["related-to"].asText() == "service-instance") {
54                                 relation["relationship-data"].forEach { data ->
55                                     if (data["relationship-key"].asText() == "service-instance.service-instance-id") {
56                                         retValue = data["relationship-value"].asText()
57                                     }
58                                 }
59                             }
60                         }
61                     }
62                 }
63             } else if (executionRequest.name == "vnf-id") {
64                 var value = raRuntimeService.getInputValue(executionRequest.name)
65                 if (!value.isNullOrMissing()) {
66                     retValue = value.asText()
67                 } else {
68                     value = raRuntimeService.getInputValue("generic-vnf.vnf-id")
69                     if (!value.isNullOrMissing()) {
70                         retValue = value.asText()
71                     }
72                 }
73             } else if (executionRequest.name == "replica-count") {
74                 retValue = "1"
75                 try {
76                     var value = raRuntimeService.getInputValue(executionRequest.name)
77                     if (!value.isNullOrMissing()) {
78                         retValue = value.asText()
79                     } else {
80                         value = raRuntimeService.getInputValue("data")
81                         if (!value.isNullOrMissing()) {
82                             if (value["replicaCount"] != null) {
83                                 retValue = value["replicaCount"].asText()
84                             }
85                         }
86                     }
87                 } catch (e: Exception) {
88                     log.error(e.message, e)
89                     log.info("Setting default replica count: 1")
90                 }
91             } else if (executionRequest.name == "config-deploy-setup") {
92                 val modulesSdnc = raRuntimeService.getResolutionStore("vf-modules-list-sdnc")["vf-modules"]
93                 val modulesAai = raRuntimeService.getResolutionStore("vf-modules-list-aai")["vf-modules"]
94                 val objectMapper = jacksonObjectMapper()
95                 val result: ObjectNode = objectMapper.createObjectNode()
96                 for (module in modulesSdnc) {
97                     val modelTopology = module.at("/vf-module-data/vf-module-topology")
98                     val moduleParameters = modelTopology.at("/vf-module-parameters/param")
99                     val label: String? = getParamValueByName(moduleParameters, "vf_module_label")
100                     if (label != null) {
101                         val modelInfo = modelTopology["onap-model-information"]
102                         val moduleData: ObjectNode = objectMapper.createObjectNode()
103                         result.put(label, moduleData)
104                         val profileName: String? = getParamValueByName(moduleParameters, K8sProfileUploadComponent.INPUT_K8S_PROFILE_NAME)
105                         val profileSource: String? = getParamValueByName(moduleParameters, K8sProfileUploadComponent.INPUT_K8S_PROFILE_SOURCE)
106                         val profileNamespace: String? = getParamValueByName(moduleParameters, K8sProfileUploadComponent.INPUT_K8S_PROFILE_NAMESPACE)
107                         val profileK8sVersion: String? = getParamValueByName(moduleParameters, K8sProfileUploadComponent.INPUT_K8S_PROFILE_K8S_VERSION)
108                         val templateName: String? = getParamValueByName(moduleParameters, K8sConfigTemplateComponent.INPUT_K8S_TEMPLATE_NAME)
109                         val templateSource: String? = getParamValueByName(moduleParameters, K8sConfigTemplateComponent.INPUT_K8S_TEMPLATE_SOURCE)
110                         val configValueSource: String? = getParamValueByName(moduleParameters, K8sConfigValueComponent.INPUT_K8S_CONFIG_VALUE_SOURCE)
111                         val configName: String? = getParamValueByName(moduleParameters, K8sConfigValueComponent.INPUT_K8S_RB_CONFIG_NAME)
112
113                         if (profileName != null)
114                             moduleData.put(K8sProfileUploadComponent.INPUT_K8S_PROFILE_NAME, profileName)
115                         if (profileSource != null)
116                             moduleData.put(K8sProfileUploadComponent.INPUT_K8S_PROFILE_SOURCE, profileSource)
117                         if (profileNamespace != null)
118                             moduleData.put(K8sProfileUploadComponent.INPUT_K8S_PROFILE_NAMESPACE, profileNamespace)
119                         if (profileK8sVersion != null)
120                             moduleData.put(K8sProfileUploadComponent.INPUT_K8S_PROFILE_K8S_VERSION, profileK8sVersion)
121                         if (templateName != null)
122                             moduleData.put(K8sConfigTemplateComponent.INPUT_K8S_TEMPLATE_NAME, templateName)
123                         if (templateSource != null)
124                             moduleData.put(K8sConfigTemplateComponent.INPUT_K8S_TEMPLATE_SOURCE, templateSource)
125                         if (configValueSource != null)
126                             moduleData.put(K8sConfigValueComponent.INPUT_K8S_CONFIG_VALUE_SOURCE, configValueSource)
127                         if (configName != null)
128                             moduleData.put(K8sConfigValueComponent.INPUT_K8S_RB_CONFIG_NAME, configName)
129
130                         for (aaiModule in modulesAai) {
131                             if (aaiModule["vf-module-id"].asText() == module["vf-module-id"].asText() && aaiModule["heat-stack-id"] != null) {
132                                 moduleData.put(K8sConfigValueComponent.INPUT_K8S_INSTANCE_ID, aaiModule["heat-stack-id"].asText())
133                                 moduleData.put(K8sConfigTemplateComponent.INPUT_K8S_DEFINITION_NAME, aaiModule["model-invariant-id"].asText())
134                                 moduleData.put(K8sConfigTemplateComponent.INPUT_K8S_DEFINITION_VERSION, aaiModule["model-customization-id"].asText())
135                                 break
136                             }
137                         }
138                     }
139                 }
140                 retValue = result
141             }
142             ResourceAssignmentUtils.setResourceDataValue(executionRequest, raRuntimeService, retValue)
143         } catch (e: Exception) {
144             log.error(e.message, e)
145             ResourceAssignmentUtils.setResourceDataValue(executionRequest, raRuntimeService, "ERROR")
146
147             throw BluePrintProcessorException("Failed in template key ($executionRequest) assignments, cause: ${e.message}", e)
148         }
149     }
150
151     private fun getParamValueByName(params: JsonNode, paramName: String): String? {
152         for (param in params) {
153             if (param["name"].asText() == paramName && param["value"].asText() != "null") {
154                 return param["value"].asText()
155             }
156         }
157         return null
158     }
159
160     override suspend fun recoverNB(runtimeException: RuntimeException, executionRequest: ResourceAssignment) {
161         this.addError("${runtimeException.message}")
162     }
163 }