2 * Copyright © 2017-2018 AT&T Intellectual Property.
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.controllerblueprints.resource.dict.utils
19 import org.apache.commons.collections.CollectionUtils
20 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException
21 import org.onap.ccsdk.cds.controllerblueprints.core.asListOfString
22 import org.onap.ccsdk.cds.controllerblueprints.core.utils.TopologicalSortingUtils
23 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceAssignment
24 import org.slf4j.LoggerFactory
25 import java.util.ArrayList
28 * BulkResourceSequencingUtils.
30 * @author Brinda Santh
32 object BulkResourceSequencingUtils {
34 private val log = LoggerFactory.getLogger(BulkResourceSequencingUtils::class.java)
37 fun process(resourceAssignments: MutableList<ResourceAssignment>): List<List<ResourceAssignment>> {
38 val resourceAssignmentMap: MutableMap<String, ResourceAssignment> = hashMapOf()
39 val sequenceBatchResourceAssignment = ArrayList<List<ResourceAssignment>>()
40 log.trace("Assignments ({})", resourceAssignments)
42 resourceAssignments.forEach { resourceAssignment ->
43 log.trace("Processing Key ({})", resourceAssignment.name)
44 resourceAssignmentMap.put(resourceAssignment.name, resourceAssignment)
47 val startResourceAssignment = ResourceAssignment()
48 startResourceAssignment.name = "*"
50 // Preepare Sorting Map
51 val topologySorting = TopologicalSortingUtils<ResourceAssignment>()
52 resourceAssignmentMap.forEach { _, resourceAssignment ->
53 // Get the dependencies from the assignment sources, if not get from the Resource Assignment dependencies
54 if (resourceAssignment.dictionarySourceDefinition != null) {
56 resourceAssignment.dictionarySourceDefinition?.properties?.get("key-dependencies")?.asListOfString()
57 dependencies?.forEach { dependency ->
58 topologySorting.add(resourceAssignmentMap[dependency]!!, resourceAssignment)
60 } else if (CollectionUtils.isNotEmpty(resourceAssignment.dependencies)) {
61 for (dependency in resourceAssignment.dependencies!!) {
62 val ra = resourceAssignmentMap[dependency]
63 ?: throw BluePrintProcessorException("Couldn't get Resource Assignment dependency " +
65 topologySorting.add(ra, resourceAssignment)
68 topologySorting.add(startResourceAssignment, resourceAssignment)
72 val sequencedResourceAssignments: MutableList<ResourceAssignment> =
73 topologySorting.topSort()!! as MutableList<ResourceAssignment>
74 log.trace("Sorted Sequenced Assignments ({})", sequencedResourceAssignments)
76 var batchResourceAssignment: MutableList<ResourceAssignment>? = null
77 var batchAssignmentName: MutableList<String>? = null
80 sequencedResourceAssignments.forEachIndexed { index, resourceAssignment ->
82 var previousResourceAssignment: ResourceAssignment? = null
85 previousResourceAssignment = sequencedResourceAssignments[index - 1]
88 var dependencyPresence = false
89 if (batchAssignmentName != null && resourceAssignment.dependencies != null) {
90 dependencyPresence = CollectionUtils.containsAny(batchAssignmentName, resourceAssignment.dependencies)
94 "({}) -> Checking ({}), with ({}), result ({})", resourceAssignment.name,
95 batchAssignmentName, resourceAssignment.dependencies, dependencyPresence
98 if (previousResourceAssignment != null && resourceAssignment.dictionarySource != null &&
99 resourceAssignment.dictionarySource!!.equals(previousResourceAssignment.dictionarySource, true) &&
102 batchResourceAssignment!!.add(resourceAssignment)
103 batchAssignmentName!!.add(resourceAssignment.name)
105 if (batchResourceAssignment != null) {
106 sequenceBatchResourceAssignment.add(batchResourceAssignment!!)
107 log.trace("Created old Set ({})", batchAssignmentName)
109 batchResourceAssignment = arrayListOf()
110 batchResourceAssignment!!.add(resourceAssignment)
112 batchAssignmentName = arrayListOf()
113 batchAssignmentName!!.add(resourceAssignment.name)
116 if (index == sequencedResourceAssignments.size - 1) {
117 log.trace("Created old Set ({})", batchAssignmentName)
118 sequenceBatchResourceAssignment.add(batchResourceAssignment!!)
121 log.info("Batched Sequence : ({})", sequenceBatchResourceAssignment)
123 return sequenceBatchResourceAssignment