aboutsummaryrefslogtreecommitdiffstats
path: root/ms/blueprintsprocessor/functions/k8s-connection-plugin/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/k8s/definition/template/K8sConfigValueComponent.kt
blob: 2e7b34e015323846403726027a0de6fb019edbfe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package org.onap.ccsdk.cds.blueprintsprocessor.functions.k8s.definition.template

import com.fasterxml.jackson.databind.JsonNode
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.databind.node.ArrayNode
import com.fasterxml.jackson.databind.node.ObjectNode
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory
import com.fasterxml.jackson.module.kotlin.convertValue
import org.onap.ccsdk.cds.blueprintsprocessor.core.BlueprintPropertiesService
import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ExecutionServiceInput
import org.onap.ccsdk.cds.blueprintsprocessor.functions.k8s.K8sConnectionPluginConfiguration
import org.onap.ccsdk.cds.blueprintsprocessor.functions.k8s.instance.K8sConfigValueRequest
import org.onap.ccsdk.cds.blueprintsprocessor.functions.k8s.instance.K8sPluginInstanceApi
import org.onap.ccsdk.cds.blueprintsprocessor.services.execution.AbstractComponentFunction
import org.onap.ccsdk.cds.controllerblueprints.core.BlueprintConstants
import org.onap.ccsdk.cds.controllerblueprints.core.BlueprintProcessorException
import org.onap.ccsdk.cds.controllerblueprints.core.data.ArtifactDefinition
import org.onap.ccsdk.cds.controllerblueprints.core.returnNullIfMissing
import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.config.ConfigurableBeanFactory
import org.springframework.context.annotation.Scope
import org.springframework.stereotype.Component
import java.io.File
import java.nio.file.Path
import java.nio.file.Paths

@Component("component-k8s-config-value")
@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
open class K8sConfigValueComponent(
    private var bluePrintPropertiesService: BlueprintPropertiesService
) : AbstractComponentFunction() {

    private val log = LoggerFactory.getLogger(K8sConfigValueComponent::class.java)!!

    companion object {
        const val INPUT_RESOURCE_ASSIGNMENT_MAP = "resource-assignment-map"
        const val INPUT_ARTIFACT_PREFIX_NAMES = "artifact-prefix-names"
        const val INPUT_K8S_TEMPLATE_NAME = "k8s-template-name"
        const val INPUT_K8S_CONFIG_NAME = "k8s-config-name"
        const val INPUT_K8S_INSTANCE_ID = "k8s-instance-id"
        const val INPUT_K8S_TEMPLATE_VALUE_SOURCE = "k8s-rb-template-value-source"
        const val INPUT_K8S_OPERATION_TYPE = "k8s-operation-type"

        const val OUTPUT_STATUSES = "statuses"
        const val OUTPUT_SKIPPED = "skipped"
        const val OUTPUT_UPLOADED = "uploaded"
        const val OUTPUT_ERROR = "error"
    }

    override suspend fun processNB(executionRequest: ExecutionServiceInput) {
        log.info("Triggering K8s Config Value component logic.")
        val inputParameterNames = arrayOf(
            INPUT_K8S_TEMPLATE_NAME,
            INPUT_K8S_CONFIG_NAME,
            INPUT_K8S_INSTANCE_ID,
            INPUT_K8S_OPERATION_TYPE,
            INPUT_K8S_TEMPLATE_VALUE_SOURCE,
            INPUT_ARTIFACT_PREFIX_NAMES
        )
        val outputPrefixStatuses = mutableMapOf<String, String>()
        val inputParamsMap = mutableMapOf<String, JsonNode?>()

        inputParameterNames.forEach {
            inputParamsMap[it] = getOptionalOperationInput(it)?.returnNullIfMissing()
        }

        log.info("Getting the template prefixes")
        val prefixList: ArrayList<String> = getTemplatePrefixList(inputParamsMap[INPUT_ARTIFACT_PREFIX_NAMES])

        log.info("Iterating over prefixes in resource assignment map.")
        for (prefix in prefixList) {
            outputPrefixStatuses[prefix] = OUTPUT_SKIPPED
            val prefixNode: JsonNode = operationInputs[INPUT_RESOURCE_ASSIGNMENT_MAP]?.get(prefix) ?: continue
            val assignmentMapPrefix = JacksonUtils.jsonNode(prefixNode.toPrettyString()) as ObjectNode
            val prefixInputParamsMap = inputParamsMap.toMutableMap()
            prefixInputParamsMap.forEach { (inputParamName, value) ->
                if (value == null) {
                    val mapValue = assignmentMapPrefix.get(inputParamName)
                    log.debug("$inputParamName value was $value so we fetch $mapValue")
                    prefixInputParamsMap[inputParamName] = mapValue
                }
            }

            val templateName: String? = prefixInputParamsMap[INPUT_K8S_TEMPLATE_NAME]?.returnNullIfMissing()?.asText()
            val configName: String? = prefixInputParamsMap[INPUT_K8S_CONFIG_NAME]?.returnNullIfMissing()?.asText()
            val instanceId: String? = prefixInputParamsMap[INPUT_K8S_INSTANCE_ID]?.returnNullIfMissing()?.asText()
            val valueSource: String? = prefixInputParamsMap[INPUT_K8S_TEMPLATE_VALUE_SOURCE]?.returnNullIfMissing()?.asText()
            val operationType = prefixInputParamsMap[INPUT_K8S_TEMPLATE_VALUE_SOURCE]?.returnNullIfMissing()?.asText()?.toUpperCase()

            if (operationType == null || operationType == OperationType.CREATE.toString())
                createOperation(templateName, instanceId, valueSource, outputPrefixStatuses, prefix, configName)
            else if (operationType == OperationType.UPDATE.toString())
                updateOperation(templateName, instanceId, valueSource, outputPrefixStatuses, prefix, configName)
            else if (operationType == OperationType.ROLLBACK.toString())
                rollbackOperation(instanceId)
            else
                throw BlueprintProcessorException("Unknown operation type: $operationType")
        }
    }

    private fun createOperation(templateName: String?, instanceId: String?, valueSource: String?, outputPrefixStatuses: MutableMap<String, String>, prefix: String, configName: String?) {
        if (templateName == null || configName == null || instanceId == null || valueSource == null) {
            log.warn("$INPUT_K8S_TEMPLATE_NAME or $INPUT_K8S_INSTANCE_ID or $INPUT_K8S_TEMPLATE_VALUE_SOURCE or $INPUT_K8S_CONFIG_NAME is null")
        } else if (templateName.isEmpty()) {
            log.warn("$INPUT_K8S_TEMPLATE_NAME is empty")
        } else if (configName.isEmpty()) {
            log.warn("$INPUT_K8S_CONFIG_NAME is empty")
        } else {
            log.info("Uploading K8s template value..")
            outputPrefixStatuses[prefix] = OUTPUT_ERROR
            val bluePrintContext = bluePrintRuntimeService.bluePrintContext()
            val artifact: ArtifactDefinition = bluePrintContext.nodeTemplateArtifact(nodeTemplateName, valueSource)
            if (artifact.type != BlueprintConstants.MODEL_TYPE_ARTIFACT_K8S_PROFILE)
                throw BlueprintProcessorException(
                    "Unexpected profile artifact type for profile source $valueSource. Expecting: $artifact.type"
                )
            // Creating API connector
            val api = K8sPluginInstanceApi(K8sConnectionPluginConfiguration(bluePrintPropertiesService))
            val configValueRequest = K8sConfigValueRequest()
            configValueRequest.templateName = templateName
            configValueRequest.configName = configName
            configValueRequest.description = valueSource
            configValueRequest.values = parseResult(valueSource)
            api.createConfigurationValues(configValueRequest, instanceId)
        }
    }

    private fun updateOperation(templateName: String?, instanceId: String?, valueSource: String?, outputPrefixStatuses: MutableMap<String, String>, prefix: String, configName: String?) {
        if (templateName == null || configName == null || instanceId == null || valueSource == null) {
            log.warn("$INPUT_K8S_TEMPLATE_NAME or $INPUT_K8S_INSTANCE_ID or $INPUT_K8S_TEMPLATE_VALUE_SOURCE or $INPUT_K8S_CONFIG_NAME is null")
        } else if (templateName.isEmpty()) {
            log.warn("$INPUT_K8S_TEMPLATE_NAME is empty")
        } else if (configName.isEmpty()) {
            log.warn("$INPUT_K8S_CONFIG_NAME is empty")
        } else {
            log.info("Uploading K8s template value..")
            outputPrefixStatuses[prefix] = OUTPUT_ERROR
            val bluePrintContext = bluePrintRuntimeService.bluePrintContext()
            val artifact: ArtifactDefinition = bluePrintContext.nodeTemplateArtifact(nodeTemplateName, valueSource)
            if (artifact.type != BlueprintConstants.MODEL_TYPE_ARTIFACT_K8S_PROFILE)
                throw BlueprintProcessorException(
                    "Unexpected profile artifact type for profile source $valueSource. Expecting: $artifact.type"
                )
            // Creating API connector
            val api = K8sPluginInstanceApi(K8sConnectionPluginConfiguration(bluePrintPropertiesService))
            if (api.hasConfigurationValues(instanceId, configName)) {
                val configValueRequest = K8sConfigValueRequest()
                configValueRequest.templateName = templateName
                configValueRequest.configName = configName
                configValueRequest.description = valueSource
                configValueRequest.values = parseResult(valueSource)
                api.editConfigurationValues(configValueRequest, instanceId, configName)
            } else {
                throw BlueprintProcessorException("Error while getting configuration value")
            }
        }
    }

    private fun rollbackOperation(instanceId: String?) {
        if (instanceId != null) {
            val api = K8sPluginInstanceApi(K8sConnectionPluginConfiguration(bluePrintPropertiesService))
            api.rollbackConfigurationValues(instanceId)
        } else {
            throw BlueprintProcessorException("$INPUT_K8S_INSTANCE_ID is null")
        }
    }

    private fun parseResult(templateValueSource: String): Any {
        val ymlSourceFile = getYmlSourceFile(templateValueSource)
        val yamlReader = ObjectMapper(YAMLFactory())
        val obj: Any = yamlReader.readValue(ymlSourceFile, Any::class.java)

        val jsonWriter = ObjectMapper()
        return jsonWriter.convertValue(obj)
    }

    private fun getYmlSourceFile(templateValueSource: String): File {
        val bluePrintBasePath: String = bluePrintRuntimeService.bluePrintContext().rootPath
        val profileSourceFileFolderPath: Path = Paths.get(bluePrintBasePath.plus(File.separator).plus(templateValueSource))

        if (profileSourceFileFolderPath.toFile().exists() && !profileSourceFileFolderPath.toFile().isDirectory)
            return profileSourceFileFolderPath.toFile()
        else
            throw BlueprintProcessorException("Template value $profileSourceFileFolderPath is missing in CBA folder")
    }

    private fun getTemplatePrefixList(node: JsonNode?): ArrayList<String> {
        val result = ArrayList<String>()
        when (node) {
            is ArrayNode -> {
                val arrayNode = node.toList()
                for (prefixNode in arrayNode)
                    result.add(prefixNode.asText())
            }
            is ObjectNode -> {
                result.add(node.asText())
            }
        }
        return result
    }

    override suspend fun recoverNB(runtimeException: RuntimeException, executionRequest: ExecutionServiceInput) {
        bluePrintRuntimeService.getBlueprintError().addError(runtimeException.message!!)
    }

    private enum class OperationType {
        CREATE, UPDATE, ROLLBACK
    }
}