Updating README.md
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / blueprints / resource-dict / src / main / kotlin / org / onap / ccsdk / cds / controllerblueprints / resource / dict / utils / BulkResourceSequencingUtils.kt
1 /*
2  *  Copyright © 2017-2018 AT&T Intellectual Property.
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.controllerblueprints.resource.dict.utils
18
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
26
27 /**
28  * BulkResourceSequencingUtils.
29  *
30  * @author Brinda Santh
31  */
32 object BulkResourceSequencingUtils {
33
34     private val log = LoggerFactory.getLogger(BulkResourceSequencingUtils::class.java)
35
36     @JvmStatic
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)
41         // Prepare Map
42         resourceAssignments.forEach { resourceAssignment ->
43             log.trace("Processing Key ({})", resourceAssignment.name)
44             resourceAssignmentMap.put(resourceAssignment.name, resourceAssignment)
45         }
46
47         val startResourceAssignment = ResourceAssignment()
48         startResourceAssignment.name = "*"
49
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) {
55                 val dependencies =
56                     resourceAssignment.dictionarySourceDefinition?.properties?.get("key-dependencies")?.asListOfString()
57                 dependencies?.forEach { dependency ->
58                     topologySorting.add(resourceAssignmentMap[dependency]!!, resourceAssignment)
59                 }
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 " +
64                             "Key($dependency)")
65                     topologySorting.add(ra, resourceAssignment)
66                 }
67             } else {
68                 topologySorting.add(startResourceAssignment, resourceAssignment)
69             }
70         }
71
72         val sequencedResourceAssignments: MutableList<ResourceAssignment> =
73             topologySorting.topSort()!! as MutableList<ResourceAssignment>
74         log.trace("Sorted Sequenced Assignments ({})", sequencedResourceAssignments)
75
76         var batchResourceAssignment: MutableList<ResourceAssignment>? = null
77         var batchAssignmentName: MutableList<String>? = null
78
79         // Prepare Sorting
80         sequencedResourceAssignments.forEachIndexed { index, resourceAssignment ->
81
82             var previousResourceAssignment: ResourceAssignment? = null
83
84             if (index > 0) {
85                 previousResourceAssignment = sequencedResourceAssignments[index - 1]
86             }
87
88             var dependencyPresence = false
89             if (batchAssignmentName != null && resourceAssignment.dependencies != null) {
90                 dependencyPresence = CollectionUtils.containsAny(batchAssignmentName, resourceAssignment.dependencies)
91             }
92
93             log.trace(
94                 "({}) -> Checking ({}), with ({}), result ({})", resourceAssignment.name,
95                 batchAssignmentName, resourceAssignment.dependencies, dependencyPresence
96             )
97
98             if (previousResourceAssignment != null && resourceAssignment.dictionarySource != null &&
99                 resourceAssignment.dictionarySource!!.equals(previousResourceAssignment.dictionarySource, true) &&
100                 !dependencyPresence
101             ) {
102                 batchResourceAssignment!!.add(resourceAssignment)
103                 batchAssignmentName!!.add(resourceAssignment.name)
104             } else {
105                 if (batchResourceAssignment != null) {
106                     sequenceBatchResourceAssignment.add(batchResourceAssignment!!)
107                     log.trace("Created old Set ({})", batchAssignmentName)
108                 }
109                 batchResourceAssignment = arrayListOf()
110                 batchResourceAssignment!!.add(resourceAssignment)
111
112                 batchAssignmentName = arrayListOf()
113                 batchAssignmentName!!.add(resourceAssignment.name)
114             }
115
116             if (index == sequencedResourceAssignments.size - 1) {
117                 log.trace("Created old Set ({})", batchAssignmentName)
118                 sequenceBatchResourceAssignment.add(batchResourceAssignment!!)
119             }
120         }
121         log.info("Batched Sequence : ({})", sequenceBatchResourceAssignment)
122
123         return sequenceBatchResourceAssignment
124     }
125 }