2 * Copyright © 2019 IBM.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.utils
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
27 object ResourceDefinitionUtils {
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())
42 /** Create a processing resource assignments for the resource definition */
43 fun createResourceAssignments(
44 resourceDefinitions: MutableMap<String, ResourceDefinition>,
45 resolveDefinition: String,
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)")
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)")
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
67 resourceAssignments.add(resourceAssignment)
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()
85 resourceAssignments.add(resourceAssignment)
88 // Populate Resource Definition's dependencies as Input Resource Assignment
89 return resourceAssignments