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.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.apps.controllerblueprints.core.BluePrintConstants
25 import org.onap.ccsdk.apps.controllerblueprints.core.data.NodeTemplate
26 import org.onap.ccsdk.apps.controllerblueprints.resource.dict.ResourceAssignment
27 import org.onap.ccsdk.apps.controllerblueprints.resource.dict.ResourceDefinition
28 import org.onap.ccsdk.apps.controllerblueprints.resource.dict.ResourceDictionaryConstants
29 import com.att.eelf.configuration.EELFManager
32 object ResourceDictionaryUtils {
33 private val log: EELFLogger = EELFManager.getInstance().getLogger(ResourceDictionaryUtils::class.java)
36 fun populateSourceMapping(resourceAssignment: ResourceAssignment,
37 resourceDefinition: ResourceDefinition) {
39 if (StringUtils.isBlank(resourceAssignment.dictionarySource)) {
41 if (MapUtils.isNotEmpty(resourceDefinition.sources)) {
42 val source = findFirstSource(resourceDefinition.sources)
44 // Populate and Assign First Source
45 if (StringUtils.isNotBlank(source)) {
46 // Set Dictionary Source
47 resourceAssignment.dictionarySource = source
49 resourceAssignment.dictionarySource = ResourceDictionaryConstants.SOURCE_INPUT
51 log.info("auto map resourceAssignment : {}", resourceAssignment)
53 resourceAssignment.dictionarySource = ResourceDictionaryConstants.SOURCE_INPUT
59 fun findFirstSource(sources: Map<String, NodeTemplate>): String? {
60 var source: String? = null
61 if (MapUtils.isNotEmpty(sources)) {
62 source = sources.keys.stream().findFirst().get()
68 fun assignInputs(data: JsonNode, context: MutableMap<String, Any>) {
69 log.trace("assignInputs from input JSON ({})", data.toString())
70 data.fields().forEach { field ->
71 val valueNode: JsonNode = data.at("/".plus(field.key)) ?: NullNode.getInstance()
73 val path = BluePrintConstants.PATH_INPUTS.plus(BluePrintConstants.PATH_DIVIDER).plus(field.key)
74 log.trace("setting path ({}), values ({})", path, valueNode)
75 context[path] = valueNode