1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
|
/*
* Copyright © 2017-2018 AT&T Intellectual Property.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onap.ccsdk.cds.controllerblueprints.resource.dict.utils
import org.apache.commons.collections.CollectionUtils
import org.onap.ccsdk.cds.controllerblueprints.core.asListOfString
import org.onap.ccsdk.cds.controllerblueprints.core.utils.TopologicalSortingUtils
import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceAssignment
import org.slf4j.LoggerFactory
import java.util.ArrayList
/**
* BulkResourceSequencingUtils.
*
* @author Brinda Santh
*/
object BulkResourceSequencingUtils {
private val log = LoggerFactory.getLogger(BulkResourceSequencingUtils::class.java)
@JvmStatic
fun process(resourceAssignments: MutableList<ResourceAssignment>): List<List<ResourceAssignment>> {
val resourceAssignmentMap: MutableMap<String, ResourceAssignment> = hashMapOf()
val sequenceBatchResourceAssignment = ArrayList<List<ResourceAssignment>>()
log.trace("Assignments ({})", resourceAssignments)
// Prepare Map
resourceAssignments.forEach { resourceAssignment ->
log.trace("Processing Key ({})", resourceAssignment.name)
resourceAssignmentMap.put(resourceAssignment.name, resourceAssignment)
}
val startResourceAssignment = ResourceAssignment()
startResourceAssignment.name = "*"
// Preepare Sorting Map
val topologySorting = TopologicalSortingUtils<ResourceAssignment>()
resourceAssignmentMap.forEach { _, resourceAssignment ->
// Get the dependencies from the assignment sources, if not get from the Resource Assignment dependencies
if (resourceAssignment.dictionarySourceDefinition != null) {
val dependencies = resourceAssignment.dictionarySourceDefinition?.properties?.get("key-dependencies")?.asListOfString()
dependencies?.forEach { dependency ->
topologySorting.add(resourceAssignmentMap[dependency]!!, resourceAssignment)
}
} else if (CollectionUtils.isNotEmpty(resourceAssignment.dependencies)) {
for (dependency in resourceAssignment.dependencies!!) {
topologySorting.add(resourceAssignmentMap[dependency]!!, resourceAssignment)
}
} else {
topologySorting.add(startResourceAssignment, resourceAssignment)
}
}
val sequencedResourceAssignments: MutableList<ResourceAssignment> = topologySorting.topSort()!! as MutableList<ResourceAssignment>
log.trace("Sorted Sequenced Assignments ({})", sequencedResourceAssignments)
var batchResourceAssignment: MutableList<ResourceAssignment>? = null
var batchAssignmentName: MutableList<String>? = null
// Prepare Sorting
sequencedResourceAssignments.forEachIndexed { index, resourceAssignment ->
var previousResourceAssignment: ResourceAssignment? = null
if (index > 0) {
previousResourceAssignment = sequencedResourceAssignments[index - 1]
}
var dependencyPresence = false
if (batchAssignmentName != null && resourceAssignment.dependencies != null) {
dependencyPresence = CollectionUtils.containsAny(batchAssignmentName, resourceAssignment.dependencies)
}
log.trace(
"({}) -> Checking ({}), with ({}), result ({})", resourceAssignment.name,
batchAssignmentName, resourceAssignment.dependencies, dependencyPresence
)
if (previousResourceAssignment != null && resourceAssignment.dictionarySource != null &&
resourceAssignment.dictionarySource!!.equals(previousResourceAssignment.dictionarySource, true) &&
!dependencyPresence
) {
batchResourceAssignment!!.add(resourceAssignment)
batchAssignmentName!!.add(resourceAssignment.name)
} else {
if (batchResourceAssignment != null) {
sequenceBatchResourceAssignment.add(batchResourceAssignment!!)
log.trace("Created old Set ({})", batchAssignmentName)
}
batchResourceAssignment = arrayListOf()
batchResourceAssignment!!.add(resourceAssignment)
batchAssignmentName = arrayListOf()
batchAssignmentName!!.add(resourceAssignment.name)
}
if (index == sequencedResourceAssignments.size - 1) {
log.trace("Created old Set ({})", batchAssignmentName)
sequenceBatchResourceAssignment.add(batchResourceAssignment!!)
}
}
log.info("Batched Sequence : ({})", sequenceBatchResourceAssignment)
return sequenceBatchResourceAssignment
}
}
|