15a8c6c80102cb6b1bf90dae9346be3fcfee29dc
[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(resourceDefinitions: MutableMap<String, ResourceDefinition>,
44                                   resolveDefinition: String, sources: List<String>)
45             : MutableList<ResourceAssignment> {
46         /** Check if resolve definition is defined in the resource definition Map */
47         val resourceDefinition = resourceDefinitions[resolveDefinition]
48                 ?: throw BluePrintProcessorException("failed to get resolve definition($resolveDefinition)")
49
50         val resourceAssignments: MutableList<ResourceAssignment> = arrayListOf()
51         /** Get the dependency property fields for the the resource definition to resolve */
52         val definitionDependencies = definitionDependencies(resourceDefinition, sources)
53         definitionDependencies.forEach { definitionDependencyName ->
54             val definitionDependency = resourceDefinitions[definitionDependencyName]
55                     ?: throw BluePrintProcessorException("failed to get dependency definition($definitionDependencyName)")
56
57             val resourceAssignment = ResourceAssignment().apply {
58                 name = definitionDependency.name
59                 dictionaryName = definitionDependency.name
60                 /** The assumption is al resource are already resolved and shall get as input source */
61                 dictionarySource = "input"
62                 property = definitionDependency.property
63             }
64             resourceAssignments.add(resourceAssignment)
65         }
66
67         resourceDefinition.sources.forEach { (sourceName, source) ->
68             if (sources.contains(sourceName)) {
69                 val resourceAssignment = ResourceAssignment().apply {
70                     name = "$sourceName:${resourceDefinition.name}"
71                     dictionaryName = resourceDefinition.name
72                     dictionarySource = sourceName
73                     dictionarySourceDefinition = source
74                     // Clone the PropertyDefinition, otherwise property value will be overridden
75                     property = JacksonUtils
76                             .readValue(resourceDefinition.property.asJsonString(), PropertyDefinition::class.java)
77                     val keyDependenciesExists = source.properties?.containsKey("key-dependencies") ?: false
78                     if (keyDependenciesExists) {
79                         dependencies = source.properties!!["key-dependencies"]!!.asListOfString().toMutableList()
80                     }
81                 }
82                 resourceAssignments.add(resourceAssignment)
83             }
84         }
85         // Populate Resource Definition's dependencies as Input Resource Assignment
86         return resourceAssignments
87     }
88 }