2 * Copyright © 2018 IBM.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.onap.ccsdk.cds.controllerblueprints.resource.dict.utils
19 import com.fasterxml.jackson.databind.JsonNode
20 import com.fasterxml.jackson.databind.node.NullNode
21 import org.apache.commons.collections.MapUtils
22 import org.apache.commons.lang3.StringUtils
23 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants
24 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException
25 import org.onap.ccsdk.cds.controllerblueprints.core.data.NodeTemplate
26 import org.onap.ccsdk.cds.controllerblueprints.core.utils.BluePrintFileUtils
27 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
28 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceAssignment
29 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceDefinition
30 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceDictionaryConstants
31 import org.slf4j.LoggerFactory
34 object ResourceDictionaryUtils {
36 private val log = LoggerFactory.getLogger(ResourceDictionaryUtils::class.java)
39 fun populateSourceMapping(
40 resourceAssignment: ResourceAssignment,
41 resourceDefinition: ResourceDefinition
44 if (StringUtils.isBlank(resourceAssignment.dictionarySource)) {
46 if (MapUtils.isNotEmpty(resourceDefinition.sources)) {
47 val source = findFirstSource(resourceDefinition.sources)
49 // Populate and Assign First Source
50 if (StringUtils.isNotBlank(source)) {
51 // Set Dictionary Source
52 resourceAssignment.dictionarySource = source
54 resourceAssignment.dictionarySource = ResourceDictionaryConstants.SOURCE_INPUT
56 log.info("auto map resourceAssignment : {}", resourceAssignment)
58 resourceAssignment.dictionarySource = ResourceDictionaryConstants.SOURCE_INPUT
64 fun findFirstSource(sources: Map<String, NodeTemplate>): String? {
65 var source: String? = null
66 if (MapUtils.isNotEmpty(sources)) {
67 source = sources.keys.stream().findFirst().get()
73 fun assignInputs(data: JsonNode, context: MutableMap<String, Any>) {
74 log.trace("assignInputs from input JSON ({})", data.toString())
75 data.fields().forEach { field ->
76 val valueNode: JsonNode = data.at("/".plus(field.key)) ?: NullNode.getInstance()
78 val path = BluePrintConstants.PATH_INPUTS.plus(BluePrintConstants.PATH_DIVIDER).plus(field.key)
79 log.trace("setting path ({}), values ({})", path, valueNode)
80 context[path] = valueNode
84 fun getResourceAssignmentFromFile(filePath: String): List<ResourceAssignment> {
85 return JacksonUtils.getListFromFile(filePath, ResourceAssignment::class.java)
86 ?: throw BluePrintProcessorException("couldn't get ResourceAssignment definitions for the file($filePath)")
89 fun writeResourceDefinitionTypes(basePath: String, resourceDefinitions: List<ResourceDefinition>) {
90 val resourceDefinitionMap = resourceDefinitions.map { it.name to it }.toMap()
91 writeResourceDefinitionTypes(basePath, resourceDefinitionMap)
94 fun writeResourceDefinitionTypes(basePath: String, resourceDefinitionMap: Map<String, ResourceDefinition>) {
95 val typePath = basePath.plus(File.separator).plus(BluePrintConstants.TOSCA_DEFINITIONS_DIR)
96 .plus(File.separator).plus("${ResourceDictionaryConstants.PATH_RESOURCE_DEFINITION_TYPE}.json")
97 val resourceDefinitionContent = JacksonUtils.getJson(resourceDefinitionMap.toSortedMap(), true)
98 BluePrintFileUtils.writeDefinitionFile(typePath, resourceDefinitionContent)