e08c09c8e413a75298581c8a48dfcbb8fb0fcdc5
[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 org.apache.commons.collections.MapUtils
20 import org.apache.commons.lang3.StringUtils
21 import org.onap.ccsdk.apps.controllerblueprints.core.data.NodeTemplate
22 import org.onap.ccsdk.apps.controllerblueprints.resource.dict.ResourceAssignment
23 import org.onap.ccsdk.apps.controllerblueprints.resource.dict.ResourceDefinition
24 import org.onap.ccsdk.apps.controllerblueprints.resource.dict.ResourceDictionaryConstants
25 import org.slf4j.LoggerFactory
26
27
28 object ResourceDictionaryUtils {
29     private val log = LoggerFactory.getLogger(ResourceDictionaryUtils::class.java)
30
31     @JvmStatic
32     fun populateSourceMapping(resourceAssignment: ResourceAssignment,
33                               resourceDefinition: ResourceDefinition) {
34
35         if (StringUtils.isBlank(resourceAssignment.dictionarySource)) {
36
37             if (MapUtils.isNotEmpty(resourceDefinition.sources)) {
38                 val source = findFirstSource(resourceDefinition.sources)
39
40                 // Populate and Assign First Source
41                 if (StringUtils.isNotBlank(source)) {
42                     // Set Dictionary Source
43                     resourceAssignment.dictionarySource = source
44                 } else {
45                     resourceAssignment.dictionarySource = ResourceDictionaryConstants.SOURCE_INPUT
46                 }
47                 log.info("auto map resourceAssignment : {}", resourceAssignment)
48             }else {
49                 resourceAssignment.dictionarySource = ResourceDictionaryConstants.SOURCE_INPUT
50             }
51         }
52     }
53
54     @JvmStatic
55     fun findFirstSource(sources: Map<String, NodeTemplate>): String? {
56         var source: String? = null
57         if (MapUtils.isNotEmpty(sources)) {
58             source = sources.keys.stream().findFirst().get()
59         }
60         return source
61     }
62 }