Formatting Code base with ktlint
[ccsdk/cds.git] / ms / blueprintsprocessor / functions / resource-resolution / src / main / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / functions / resource / resolution / utils / ResourceDefinitionUtils.kt
1 /*
2  *  Copyright © 2019 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.cds.blueprintsprocessor.functions.resource.resolution.utils
18
19 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException
20 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonString
21 import org.onap.ccsdk.cds.controllerblueprints.core.asListOfString
22 import org.onap.ccsdk.cds.controllerblueprints.core.data.PropertyDefinition
23 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
24 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceAssignment
25 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceDefinition
26
27 object ResourceDefinitionUtils {
28
29     fun definitionDependencies(definition: ResourceDefinition, sources: List<String>): Set<String> {
30         val dependencies: MutableSet<String> = mutableSetOf()
31         definition.sources.forEach { (sourceName, source) ->
32             if (sources.contains(sourceName)) {
33                 val keyDependenciesExists = source.properties?.containsKey("key-dependencies") ?: false
34                 if (keyDependenciesExists) {
35                     dependencies.addAll(source.properties!!["key-dependencies"]!!.asListOfString())
36                 }
37             }
38         }
39         return dependencies
40     }
41
42     /** Create a processing resource assignments for the resource definition  */
43     fun createResourceAssignments(
44         resourceDefinitions: MutableMap<String, ResourceDefinition>,
45         resolveDefinition: String,
46         sources: List<String>
47     ):
48             MutableList<ResourceAssignment> {
49         /** Check if resolve definition is defined in the resource definition Map */
50         val resourceDefinition = resourceDefinitions[resolveDefinition]
51             ?: throw BluePrintProcessorException("failed to get resolve definition($resolveDefinition)")
52
53         val resourceAssignments: MutableList<ResourceAssignment> = arrayListOf()
54         /** Get the dependency property fields for the the resource definition to resolve */
55         val definitionDependencies = definitionDependencies(resourceDefinition, sources)
56         definitionDependencies.forEach { definitionDependencyName ->
57             val definitionDependency = resourceDefinitions[definitionDependencyName]
58                 ?: throw BluePrintProcessorException("failed to get dependency definition($definitionDependencyName)")
59
60             val resourceAssignment = ResourceAssignment().apply {
61                 name = definitionDependency.name
62                 dictionaryName = definitionDependency.name
63                 /** The assumption is al resource are already resolved and shall get as input source */
64                 dictionarySource = "input"
65                 property = definitionDependency.property
66             }
67             resourceAssignments.add(resourceAssignment)
68         }
69
70         resourceDefinition.sources.forEach { (sourceName, source) ->
71             if (sources.contains(sourceName)) {
72                 val resourceAssignment = ResourceAssignment().apply {
73                     name = "$sourceName:${resourceDefinition.name}"
74                     dictionaryName = resourceDefinition.name
75                     dictionarySource = sourceName
76                     dictionarySourceDefinition = source
77                     // Clone the PropertyDefinition, otherwise property value will be overridden
78                     property = JacksonUtils
79                         .readValue(resourceDefinition.property.asJsonString(), PropertyDefinition::class.java)
80                     val keyDependenciesExists = source.properties?.containsKey("key-dependencies") ?: false
81                     if (keyDependenciesExists) {
82                         dependencies = source.properties!!["key-dependencies"]!!.asListOfString().toMutableList()
83                     }
84                 }
85                 resourceAssignments.add(resourceAssignment)
86             }
87         }
88         // Populate Resource Definition's dependencies as Input Resource Assignment
89         return resourceAssignments
90     }
91 }