a3456cd43c699840b620797c5f76a67550c358d6
[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.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
30
31
32 object ResourceDictionaryUtils {
33     private val log: EELFLogger = EELFManager.getInstance().getLogger(ResourceDictionaryUtils::class.java)
34
35     @JvmStatic
36     fun populateSourceMapping(resourceAssignment: ResourceAssignment,
37                               resourceDefinition: ResourceDefinition) {
38
39         if (StringUtils.isBlank(resourceAssignment.dictionarySource)) {
40
41             if (MapUtils.isNotEmpty(resourceDefinition.sources)) {
42                 val source = findFirstSource(resourceDefinition.sources)
43
44                 // Populate and Assign First Source
45                 if (StringUtils.isNotBlank(source)) {
46                     // Set Dictionary Source
47                     resourceAssignment.dictionarySource = source
48                 } else {
49                     resourceAssignment.dictionarySource = ResourceDictionaryConstants.SOURCE_INPUT
50                 }
51                 log.info("auto map resourceAssignment : {}", resourceAssignment)
52             }else {
53                 resourceAssignment.dictionarySource = ResourceDictionaryConstants.SOURCE_INPUT
54             }
55         }
56     }
57
58     @JvmStatic
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()
63         }
64         return source
65     }
66
67     @JvmStatic
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()
72
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
76         }
77     }
78 }