733a443fd44a31a9c76d1162e120bb1f7e360144
[ccsdk/cds.git] /
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.apps.controllerblueprints.resource.dict.utils
18
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
29
30
31 object ResourceDictionaryUtils {
32     private val log = LoggerFactory.getLogger(ResourceDictionaryUtils::class.java)
33
34     @JvmStatic
35     fun populateSourceMapping(resourceAssignment: ResourceAssignment,
36                               resourceDefinition: ResourceDefinition) {
37
38         if (StringUtils.isBlank(resourceAssignment.dictionarySource)) {
39
40             if (MapUtils.isNotEmpty(resourceDefinition.sources)) {
41                 val source = findFirstSource(resourceDefinition.sources)
42
43                 // Populate and Assign First Source
44                 if (StringUtils.isNotBlank(source)) {
45                     // Set Dictionary Source
46                     resourceAssignment.dictionarySource = source
47                 } else {
48                     resourceAssignment.dictionarySource = ResourceDictionaryConstants.SOURCE_INPUT
49                 }
50                 log.info("auto map resourceAssignment : {}", resourceAssignment)
51             }else {
52                 resourceAssignment.dictionarySource = ResourceDictionaryConstants.SOURCE_INPUT
53             }
54         }
55     }
56
57     @JvmStatic
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()
62         }
63         return source
64     }
65
66     @JvmStatic
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()
71
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
75         }
76     }
77 }