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.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.apps.controllerblueprints.core.BluePrintConstants
24 import org.onap.ccsdk.apps.controllerblueprints.core.data.NodeTemplate
25 import org.onap.ccsdk.apps.controllerblueprints.resource.dict.ResourceAssignment
26 import org.onap.ccsdk.apps.controllerblueprints.resource.dict.ResourceDefinition
27 import org.onap.ccsdk.apps.controllerblueprints.resource.dict.ResourceDictionaryConstants
28 import org.slf4j.LoggerFactory
31 object ResourceDictionaryUtils {
32 private val log = LoggerFactory.getLogger(ResourceDictionaryUtils::class.java)
35 fun populateSourceMapping(resourceAssignment: ResourceAssignment,
36 resourceDefinition: ResourceDefinition) {
38 if (StringUtils.isBlank(resourceAssignment.dictionarySource)) {
40 if (MapUtils.isNotEmpty(resourceDefinition.sources)) {
41 val source = findFirstSource(resourceDefinition.sources)
43 // Populate and Assign First Source
44 if (StringUtils.isNotBlank(source)) {
45 // Set Dictionary Source
46 resourceAssignment.dictionarySource = source
48 resourceAssignment.dictionarySource = ResourceDictionaryConstants.SOURCE_INPUT
50 log.info("auto map resourceAssignment : {}", resourceAssignment)
52 resourceAssignment.dictionarySource = ResourceDictionaryConstants.SOURCE_INPUT
58 fun findFirstSource(sources: Map<String, NodeTemplate>): String? {
59 var source: String? = null
60 if (MapUtils.isNotEmpty(sources)) {
61 source = sources.keys.stream().findFirst().get()
67 fun assignInputs(data: JsonNode, context: MutableMap<String, Any>) {
68 log.trace("assignInputs from input JSON ({})", data.toString())
69 data.fields().forEach { field ->
70 val valueNode: JsonNode = data.at("/".plus(field.key)) ?: NullNode.getInstance()
72 val path = BluePrintConstants.PATH_INPUTS.plus(BluePrintConstants.PATH_DIVIDER).plus(field.key)
73 log.trace("setting path ({}), values ({})", path, valueNode)
74 context[path] = valueNode