cd5470953da6c2d8689f9f7a2917f7d04071d9ed
[ccsdk/cds.git] / ms / controllerblueprints / modules / resource-dict / src / main / kotlin / org / onap / ccsdk / cds / controllerblueprints / resource / dict / utils / ResourceDictionaryUtils.kt
1 /*
2  *  Copyright © 2018 IBM.
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.controllerblueprints.resource.dict.utils
18
19 import org.slf4j.LoggerFactory
20 import com.fasterxml.jackson.databind.JsonNode
21 import com.fasterxml.jackson.databind.node.NullNode
22 import org.apache.commons.collections.MapUtils
23 import org.apache.commons.lang3.StringUtils
24 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants
25 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException
26 import org.onap.ccsdk.cds.controllerblueprints.core.data.NodeTemplate
27 import org.onap.ccsdk.cds.controllerblueprints.core.utils.BluePrintFileUtils
28 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
29 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceAssignment
30 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceDefinition
31 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceDictionaryConstants
32 import java.io.File
33
34
35 object ResourceDictionaryUtils {
36     private val log= LoggerFactory.getLogger(ResourceDictionaryUtils::class.java)
37
38     @JvmStatic
39     fun populateSourceMapping(resourceAssignment: ResourceAssignment,
40                               resourceDefinition: ResourceDefinition) {
41
42         if (StringUtils.isBlank(resourceAssignment.dictionarySource)) {
43
44             if (MapUtils.isNotEmpty(resourceDefinition.sources)) {
45                 val source = findFirstSource(resourceDefinition.sources)
46
47                 // Populate and Assign First Source
48                 if (StringUtils.isNotBlank(source)) {
49                     // Set Dictionary Source
50                     resourceAssignment.dictionarySource = source
51                 } else {
52                     resourceAssignment.dictionarySource = ResourceDictionaryConstants.SOURCE_INPUT
53                 }
54                 log.info("auto map resourceAssignment : {}", resourceAssignment)
55             } else {
56                 resourceAssignment.dictionarySource = ResourceDictionaryConstants.SOURCE_INPUT
57             }
58         }
59     }
60
61     @JvmStatic
62     fun findFirstSource(sources: Map<String, NodeTemplate>): String? {
63         var source: String? = null
64         if (MapUtils.isNotEmpty(sources)) {
65             source = sources.keys.stream().findFirst().get()
66         }
67         return source
68     }
69
70     @JvmStatic
71     fun assignInputs(data: JsonNode, context: MutableMap<String, Any>) {
72         log.trace("assignInputs from input JSON ({})", data.toString())
73         data.fields().forEach { field ->
74             val valueNode: JsonNode = data.at("/".plus(field.key)) ?: NullNode.getInstance()
75
76             val path = BluePrintConstants.PATH_INPUTS.plus(BluePrintConstants.PATH_DIVIDER).plus(field.key)
77             log.trace("setting path ({}), values ({})", path, valueNode)
78             context[path] = valueNode
79         }
80     }
81
82     fun getResourceAssignmentFromFile(filePath: String): List<ResourceAssignment> {
83         return JacksonUtils.getListFromFile(filePath, ResourceAssignment::class.java)
84                 ?: throw BluePrintProcessorException("couldn't get ResourceAssignment definitions for the file($filePath)")
85     }
86
87     fun writeResourceDefinitionTypes(basePath: String, resourceDefinitions: List<ResourceDefinition>) {
88         val resourceDefinitionMap = resourceDefinitions.map { it.name to it }.toMap()
89         writeResourceDefinitionTypes(basePath, resourceDefinitionMap)
90
91     }
92
93     fun writeResourceDefinitionTypes(basePath: String, resourceDefinitionMap: Map<String, ResourceDefinition>) {
94         val typePath = basePath.plus(File.separator).plus(BluePrintConstants.TOSCA_DEFINITIONS_DIR)
95                 .plus(File.separator).plus("${ResourceDictionaryConstants.PATH_RESOURCE_DEFINITION_TYPE}.json")
96         val resourceDefinitionContent = JacksonUtils.getJson(resourceDefinitionMap.toSortedMap(), true)
97         BluePrintFileUtils.writeDefinitionFile(typePath, resourceDefinitionContent)
98     }
99 }