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.apps.controllerblueprints.resource.dict.utils
19 import com.att.eelf.configuration.EELFLogger
20 import com.att.eelf.configuration.EELFManager
21 import com.fasterxml.jackson.databind.JsonNode
22 import com.fasterxml.jackson.databind.node.NullNode
23 import org.apache.commons.collections.MapUtils
24 import org.apache.commons.lang3.StringUtils
25 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintConstants
26 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintProcessorException
27 import org.onap.ccsdk.apps.controllerblueprints.core.data.NodeTemplate
28 import org.onap.ccsdk.apps.controllerblueprints.core.utils.BluePrintFileUtils
29 import org.onap.ccsdk.apps.controllerblueprints.core.utils.JacksonUtils
30 import org.onap.ccsdk.apps.controllerblueprints.resource.dict.ResourceAssignment
31 import org.onap.ccsdk.apps.controllerblueprints.resource.dict.ResourceDefinition
32 import org.onap.ccsdk.apps.controllerblueprints.resource.dict.ResourceDictionaryConstants
36 object ResourceDictionaryUtils {
37 private val log: EELFLogger = EELFManager.getInstance().getLogger(ResourceDictionaryUtils::class.java)
40 fun populateSourceMapping(resourceAssignment: ResourceAssignment,
41 resourceDefinition: ResourceDefinition) {
43 if (StringUtils.isBlank(resourceAssignment.dictionarySource)) {
45 if (MapUtils.isNotEmpty(resourceDefinition.sources)) {
46 val source = findFirstSource(resourceDefinition.sources)
48 // Populate and Assign First Source
49 if (StringUtils.isNotBlank(source)) {
50 // Set Dictionary Source
51 resourceAssignment.dictionarySource = source
53 resourceAssignment.dictionarySource = ResourceDictionaryConstants.SOURCE_INPUT
55 log.info("auto map resourceAssignment : {}", resourceAssignment)
57 resourceAssignment.dictionarySource = ResourceDictionaryConstants.SOURCE_INPUT
63 fun findFirstSource(sources: Map<String, NodeTemplate>): String? {
64 var source: String? = null
65 if (MapUtils.isNotEmpty(sources)) {
66 source = sources.keys.stream().findFirst().get()
72 fun assignInputs(data: JsonNode, context: MutableMap<String, Any>) {
73 log.trace("assignInputs from input JSON ({})", data.toString())
74 data.fields().forEach { field ->
75 val valueNode: JsonNode = data.at("/".plus(field.key)) ?: NullNode.getInstance()
77 val path = BluePrintConstants.PATH_INPUTS.plus(BluePrintConstants.PATH_DIVIDER).plus(field.key)
78 log.trace("setting path ({}), values ({})", path, valueNode)
79 context[path] = valueNode
83 fun getResourceAssignmentFromFile(filePath: String): List<ResourceAssignment> {
84 return JacksonUtils.getListFromFile(filePath, ResourceAssignment::class.java)
85 ?: throw BluePrintProcessorException("couldn't get ResourceAssignment definitions for the file($filePath)")
88 fun writeResourceDefinitionTypes(basePath: String, resourceDefinitionMap: Map<String, ResourceDefinition>) {
89 val typePath = basePath.plus(File.separator).plus(BluePrintConstants.TOSCA_DEFINITIONS_DIR)
90 .plus(File.separator).plus("${ResourceDictionaryConstants.PATH_RESOURCE_DEFINITION_TYPE}.json")
91 val resourceDefinitionContent = JacksonUtils.getJson(resourceDefinitionMap.toSortedMap(), true)
92 BluePrintFileUtils.writeDefinitionFile(typePath, resourceDefinitionContent)