summaryrefslogtreecommitdiffstats
path: root/ms/controllerblueprints/modules/resource-dict/src/main/kotlin
diff options
context:
space:
mode:
authorMuthuramalingam, Brinda Santh(bs2796) <bs2796@att.com>2018-08-30 14:17:06 +0000
committerMuthuramalingam, Brinda Santh(bs2796) <bs2796@att.com>2018-08-30 14:17:06 +0000
commit905d8bf666e0667774bebccfabce65e3497e9c32 (patch)
treef9140a5b81ed876ead24759902ca7422eade2603 /ms/controllerblueprints/modules/resource-dict/src/main/kotlin
parent254217ffff5edea9069f96b992a5939b3745d376 (diff)
Controller Blueprints Microservice
Add Resource Seuencing validation and Optimise resource assignment validation logics Change-Id: I6f31ca5dbeb6f6aa89959b7d96fbfad25468b3a4 Issue-ID: CCSDK-416 Signed-off-by: Muthuramalingam, Brinda Santh(bs2796) <bs2796@att.com>
Diffstat (limited to 'ms/controllerblueprints/modules/resource-dict/src/main/kotlin')
-rw-r--r--ms/controllerblueprints/modules/resource-dict/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/resource/dict/ResourceDefinition.kt15
-rw-r--r--ms/controllerblueprints/modules/resource-dict/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/resource/dict/service/ResourceAssignmentValidationService.kt84
-rw-r--r--ms/controllerblueprints/modules/resource-dict/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/resource/dict/utils/BulkResourceSequencingUtils.kt108
3 files changed, 171 insertions, 36 deletions
diff --git a/ms/controllerblueprints/modules/resource-dict/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/resource/dict/ResourceDefinition.kt b/ms/controllerblueprints/modules/resource-dict/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/resource/dict/ResourceDefinition.kt
index 2287c6c8c..b4d68cbca 100644
--- a/ms/controllerblueprints/modules/resource-dict/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/resource/dict/ResourceDefinition.kt
+++ b/ms/controllerblueprints/modules/resource-dict/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/resource/dict/ResourceDefinition.kt
@@ -18,18 +18,19 @@ package org.onap.ccsdk.apps.controllerblueprints.resource.dict
import com.fasterxml.jackson.annotation.JsonFormat
import com.fasterxml.jackson.annotation.JsonProperty
+import org.apache.commons.lang3.builder.ToStringBuilder
import org.onap.ccsdk.apps.controllerblueprints.core.data.NodeTemplate
import org.onap.ccsdk.apps.controllerblueprints.core.data.PropertyDefinition
import java.io.Serializable
import java.util.*
-open class ResourceDefinition{
+open class ResourceDefinition {
@JsonProperty(value = "name", required = true)
lateinit var name: String
@JsonProperty(value = "property", required = true)
- lateinit var property : PropertyDefinition
+ lateinit var property: PropertyDefinition
var tags: String? = null
@@ -81,6 +82,16 @@ open class ResourceAssignment {
@JsonProperty("updated-by")
var updatedBy: String? = null
+
+ override fun toString(): String {
+ return StringBuilder()
+ .append("[")
+ .append("name=", name)
+ .append(", dictionaryName=", dictionaryName)
+ .append(", dictionarySource=", dictionarySource)
+ .append("]")
+ .toString()
+ }
}
/**
diff --git a/ms/controllerblueprints/modules/resource-dict/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/resource/dict/service/ResourceAssignmentValidationService.kt b/ms/controllerblueprints/modules/resource-dict/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/resource/dict/service/ResourceAssignmentValidationService.kt
index 6a78ac852..4578aca7d 100644
--- a/ms/controllerblueprints/modules/resource-dict/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/resource/dict/service/ResourceAssignmentValidationService.kt
+++ b/ms/controllerblueprints/modules/resource-dict/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/resource/dict/service/ResourceAssignmentValidationService.kt
@@ -22,7 +22,6 @@ import org.apache.commons.lang3.text.StrBuilder
import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintException
import org.onap.ccsdk.apps.controllerblueprints.core.utils.TopologicalSortingUtils
import org.onap.ccsdk.apps.controllerblueprints.resource.dict.ResourceAssignment
-import org.onap.ccsdk.apps.controllerblueprints.resource.dict.validator.ResourceAssignmentValidator
import org.slf4j.LoggerFactory
import java.io.Serializable
@@ -43,18 +42,21 @@ interface ResourceAssignmentValidationService : Serializable {
* @author Brinda Santh
*/
open class ResourceAssignmentValidationDefaultService : ResourceAssignmentValidationService {
- private val log = LoggerFactory.getLogger(ResourceAssignmentValidator::class.java)
- open var resourceAssignments: List<ResourceAssignment> = arrayListOf()
- open var resourceAssignmentMap: MutableMap<String, ResourceAssignment> = hashMapOf()
+ private val log = LoggerFactory.getLogger(ResourceAssignmentValidationDefaultService::class.java)
+
+ open var resourceAssignmentMap: Map<String, ResourceAssignment> = hashMapOf()
open val validationMessage = StrBuilder()
override fun validate(resourceAssignments: List<ResourceAssignment>): Boolean {
- this.resourceAssignments = resourceAssignments
- validateSources(resourceAssignments)
- validateDuplicateDictionaryKeys()
- validateCyclicDependency()
- if (StringUtils.isNotBlank(validationMessage)) {
- throw BluePrintException("Resource Assignment Validation :" + validationMessage.toString())
+ try {
+ validateSources(resourceAssignments)
+ validateTemplateNDictionaryKeys(resourceAssignments)
+ validateCyclicDependency(resourceAssignments)
+ if (StringUtils.isNotBlank(validationMessage)) {
+ throw BluePrintException("Resource Assignment Validation Failure")
+ }
+ } catch (e: Exception) {
+ throw BluePrintException("Resource Assignment Validation :" + validationMessage.toString(), e)
}
return true
}
@@ -63,40 +65,54 @@ open class ResourceAssignmentValidationDefaultService : ResourceAssignmentValida
log.info("validating resource assignment sources")
}
- open fun validateDuplicateDictionaryKeys() {
- val uniqueDictionaryKeys = hashSetOf<String>()
+ open fun validateTemplateNDictionaryKeys(resourceAssignments: List<ResourceAssignment>) {
- this.resourceAssignments.forEach { resourceAssignment ->
- // Check Duplicate Names
- if (!resourceAssignmentMap.containsKey(resourceAssignment.name)) {
- resourceAssignmentMap[resourceAssignment.name] = resourceAssignment
- } else {
- validationMessage.appendln(String.format("Duplicate Assignment Template Key (%s) is Present",
- resourceAssignment.name))
- }
- // Check duplicate Dictionary Keys
- if (!uniqueDictionaryKeys.contains(resourceAssignment.dictionaryName!!)) {
- uniqueDictionaryKeys.add(resourceAssignment.dictionaryName!!)
- } else {
- validationMessage.appendln(
- String.format("Duplicate Assignment Dictionary Key (%s) present with Template Key (%s)",
- resourceAssignment.dictionaryName, resourceAssignment.name))
- }
+ resourceAssignmentMap = resourceAssignments.map { it.name to it }.toMap()
+
+ val duplicateKeyNames = resourceAssignments.groupBy { it.name }
+ .filter { it.value.size > 1 }
+ .map { it.key }
+
+ if (duplicateKeyNames.isNotEmpty()) {
+ validationMessage.appendln(String.format("Duplicate Assignment Template Keys (%s) is Present", duplicateKeyNames))
+ }
+
+ val duplicateDictionaryKeyNames = resourceAssignments.groupBy { it.dictionaryName }
+ .filter { it.value.size > 1 }
+ .map { it.key }
+ if (duplicateDictionaryKeyNames.isNotEmpty()) {
+ validationMessage.appendln(String.format("Duplicate Assignment Dictionary Keys (%s) is Present", duplicateDictionaryKeyNames))
+ }
+
+ val dependenciesNames = resourceAssignments.mapNotNull { it.dependencies }.flatten()
+
+ log.info("Resource assignment definitions : {}", resourceAssignmentMap.keys)
+ log.info("Resource assignment Dictionary dependencies : {}", dependenciesNames)
+
+ val notPresentDictionaries = dependenciesNames.filter { !resourceAssignmentMap.containsKey(it) }.distinct()
+ if (notPresentDictionaries.isNotEmpty()) {
+ validationMessage.appendln(String.format("No assignments for Dictionary Keys (%s)", notPresentDictionaries))
+ }
+
+ if (StringUtils.isNotBlank(validationMessage)) {
+ throw BluePrintException("Resource Assignment Validation Failure")
}
}
- open fun validateCyclicDependency() {
+ open fun validateCyclicDependency(resourceAssignments: List<ResourceAssignment>) {
val startResourceAssignment = ResourceAssignment()
startResourceAssignment.name = "*"
val topologySorting = TopologicalSortingUtils<ResourceAssignment>()
- this.resourceAssignmentMap.forEach { assignmentKey, assignment ->
- if (CollectionUtils.isNotEmpty(assignment.dependencies)) {
- for (dependency in assignment.dependencies!!) {
- topologySorting.add(resourceAssignmentMap[dependency]!!, assignment)
+
+ resourceAssignmentMap.map { it.value }.map { resourceAssignment ->
+ if (CollectionUtils.isNotEmpty(resourceAssignment.dependencies)) {
+ resourceAssignment.dependencies!!.map {
+ log.info("Topological Graph link from {} to {}", it, resourceAssignment.name)
+ topologySorting.add(resourceAssignmentMap[it]!!, resourceAssignment)
}
} else {
- topologySorting.add(startResourceAssignment, assignment)
+ topologySorting.add(startResourceAssignment, resourceAssignment)
}
}
diff --git a/ms/controllerblueprints/modules/resource-dict/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/resource/dict/utils/BulkResourceSequencingUtils.kt b/ms/controllerblueprints/modules/resource-dict/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/resource/dict/utils/BulkResourceSequencingUtils.kt
new file mode 100644
index 000000000..82fbd3ac1
--- /dev/null
+++ b/ms/controllerblueprints/modules/resource-dict/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/resource/dict/utils/BulkResourceSequencingUtils.kt
@@ -0,0 +1,108 @@
+/*
+ * 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.apps.controllerblueprints.resource.dict.utils
+
+import org.apache.commons.collections.CollectionUtils
+import org.onap.ccsdk.apps.controllerblueprints.core.utils.TopologicalSortingUtils
+import org.onap.ccsdk.apps.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.info("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 ->
+ 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.info("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
+ }
+
+} \ No newline at end of file