Renaming Files having BluePrint to have Blueprint
[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
55             /** Get the dependency property fields for the the resource definition to resolve */
56             val definitionDependencies = definitionDependencies(resourceDefinition, sources)
57             definitionDependencies.forEach { definitionDependencyName ->
58                 val definitionDependency = resourceDefinitions[definitionDependencyName]
59                     ?: throw BlueprintProcessorException("failed to get dependency definition($definitionDependencyName)")
60
61                 val resourceAssignment = ResourceAssignment().apply {
62                     name = definitionDependency.name
63                     dictionaryName = definitionDependency.name
64                     /** The assumption is al resource are already resolved and shall get as input source */
65                     dictionarySource = "input"
66                     property = definitionDependency.property
67                 }
68                 resourceAssignments.add(resourceAssignment)
69             }
70
71             resourceDefinition.sources.forEach { (sourceName, source) ->
72                 if (sources.contains(sourceName)) {
73                     val resourceAssignment = ResourceAssignment().apply {
74                         name = "$sourceName:${resourceDefinition.name}"
75                         dictionaryName = resourceDefinition.name
76                         dictionarySource = sourceName
77                         dictionarySourceDefinition = source
78                         // Clone the PropertyDefinition, otherwise property value will be overridden
79                         property = JacksonUtils
80                             .readValue(resourceDefinition.property.asJsonString(), PropertyDefinition::class.java)
81                         val keyDependenciesExists = source.properties?.containsKey("key-dependencies") ?: false
82                         if (keyDependenciesExists) {
83                             dependencies = source.properties!!["key-dependencies"]!!.asListOfString().toMutableList()
84                         }
85                     }
86                     resourceAssignments.add(resourceAssignment)
87                 }
88             }
89             // Populate Resource Definition's dependencies as Input Resource Assignment
90             return resourceAssignments
91         }
92 }