diff options
Diffstat (limited to 'ms/controllerblueprints/modules/resource-dict/src')
6 files changed, 213 insertions, 200 deletions
diff --git a/ms/controllerblueprints/modules/resource-dict/src/main/java/org/onap/ccsdk/apps/controllerblueprints/resource/dict/validator/ResourceAssignmentValidator.java b/ms/controllerblueprints/modules/resource-dict/src/main/java/org/onap/ccsdk/apps/controllerblueprints/resource/dict/validator/ResourceAssignmentValidator.java deleted file mode 100644 index c9b37c26..00000000 --- a/ms/controllerblueprints/modules/resource-dict/src/main/java/org/onap/ccsdk/apps/controllerblueprints/resource/dict/validator/ResourceAssignmentValidator.java +++ /dev/null @@ -1,163 +0,0 @@ -/*
- * 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.validator;
-
-import org.apache.commons.lang3.StringUtils;
-import org.apache.commons.lang3.text.StrBuilder;
-import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintException;
-import org.onap.ccsdk.apps.controllerblueprints.core.ConfigModelConstant;
-import org.onap.ccsdk.apps.controllerblueprints.core.data.CapabilityAssignment;
-import org.onap.ccsdk.apps.controllerblueprints.core.data.NodeTemplate;
-import org.onap.ccsdk.apps.controllerblueprints.core.utils.JacksonUtils;
-import org.onap.ccsdk.apps.controllerblueprints.core.utils.TopologicalSortingUtils;
-import org.onap.ccsdk.apps.controllerblueprints.resource.dict.ResourceAssignment;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import java.util.*;
-
-/**
- * ResourceAssignmentValidator.java Purpose:
- *
- * @author Brinda Santh
- */
-public class ResourceAssignmentValidator {
- private static final Logger log = LoggerFactory.getLogger(ResourceAssignmentValidator.class);
- private List<ResourceAssignment> assignments;
- private Map<String, ResourceAssignment> resourceAssignmentMap = new HashMap<>();
- private StrBuilder validationMessage = new StrBuilder();
-
- public ResourceAssignmentValidator(List<ResourceAssignment> assignments) {
- this.assignments = assignments;
- }
-
- public ResourceAssignmentValidator(NodeTemplate nodeTemplate) throws BluePrintException {
-
- if (nodeTemplate != null && nodeTemplate.getCapabilities() != null) {
- CapabilityAssignment capabilityAssignment =
- nodeTemplate.getCapabilities().get(ConfigModelConstant.CAPABILITY_PROPERTY_MAPPING);
- if (capabilityAssignment != null && capabilityAssignment.getProperties() != null) {
- Object mappingObject =
- capabilityAssignment.getProperties().get(ConfigModelConstant.CAPABILITY_PROPERTY_MAPPING);
- if (mappingObject != null) {
- String mappingContent = JacksonUtils.getJson(mappingObject);
- if (StringUtils.isNotBlank(mappingContent)) {
- this.assignments =
- JacksonUtils.getListFromJson(mappingContent, ResourceAssignment.class);
- } else {
- validationMessage
- .appendln(String.format("Failed to transform Mapping Content (%s) ", mappingContent));
- throw new BluePrintException(
- String.format("Failed to transform Mapping Content (%s) ", mappingContent));
- }
- }
- }
- }
- }
-
- /**
- * This is a validateResourceAssignment to validate the Topology Template
- *
- * @return boolean
- * @throws BluePrintException BluePrintException
- */
- public boolean validateResourceAssignment() throws BluePrintException {
- if (assignments != null && !assignments.isEmpty()) {
- validateDuplicateDictionaryKeys();
- validateCyclicDependency();
- if (validationMessage.length() > 0) {
- throw new BluePrintException("Resource Assignment Validation :" + validationMessage.toString());
- }
- }
- return true;
- }
-
- @SuppressWarnings("squid:S3776")
- private void validateDuplicateDictionaryKeys() {
- this.assignments.forEach(resourceMapping -> {
- if (resourceMapping != null) {
- if (!resourceAssignmentMap.containsKey(resourceMapping.getName())) {
- resourceAssignmentMap.put(resourceMapping.getName(), resourceMapping);
- } else {
- validationMessage.appendln(String.format("Duplicate Assignment Template Key (%s) is Present",
- resourceMapping.getName()));
- }
- }
- });
-
- if (!assignments.isEmpty()) {
- Set<String> uniqueSet = new HashSet<>();
- for (ResourceAssignment resourceAssignment : assignments) {
- if (resourceAssignment != null) {
- boolean added = uniqueSet.add(resourceAssignment.getDictionaryName());
- if (!added) {
- validationMessage.appendln(
- String.format("Duplicate Assignment Dictionary Key (%s) present with Template Key (%s)",
- resourceAssignment.getDictionaryName(), resourceAssignment.getName()));
- }
- }
- }
- }
- }
-
- private void validateCyclicDependency() {
- TopologicalSortingUtils<ResourceAssignment> topologySorting = new TopologicalSortingUtils<>();
- this.resourceAssignmentMap.forEach((mappingKey, mapping) -> {
- if (mapping != null) {
- if (mapping.getDependencies() != null && !mapping.getDependencies().isEmpty()) {
- for (String dependency : mapping.getDependencies()) {
- topologySorting.add(resourceAssignmentMap.get(dependency), mapping);
- }
- } else {
- topologySorting.add(null, mapping);
- }
- }
- });
-
- if (!topologySorting.isDag()) {
- String graph = getTopologicalGraph(topologySorting);
- validationMessage.appendln("Cyclic Dependency :" + graph);
- }
- }
-
-
- public String getTopologicalGraph(TopologicalSortingUtils<ResourceAssignment> topologySorting) {
- StringBuilder s = new StringBuilder();
- if (topologySorting != null) {
- Map<ResourceAssignment, List<ResourceAssignment>> neighbors = topologySorting.getNeighbors();
-
- neighbors.forEach((v, vs) -> {
- if (v == null) {
- s.append("\n * -> [");
- for (ResourceAssignment resourceAssignment : vs) {
- s.append("(" + resourceAssignment.getDictionaryName() + ":" + resourceAssignment.getName()
- + "),");
- }
- s.append("]");
- } else {
- s.append("\n (" + v.getDictionaryName() + ":" + v.getName() + ") -> [");
- for (ResourceAssignment resourceAssignment : vs) {
- s.append("(" + resourceAssignment.getDictionaryName() + ":" + resourceAssignment.getName()
- + "),");
- }
- s.append("]");
- }
- });
- }
- return s.toString();
- }
-}
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 2287c6c8..b4d68cbc 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 6a78ac85..4578aca7 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 00000000..82fbd3ac --- /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 diff --git a/ms/controllerblueprints/modules/resource-dict/src/test/java/org/onap/ccsdk/apps/controllerblueprints/resource/dict/utils/BulkResourceSequencingUtilsTest.java b/ms/controllerblueprints/modules/resource-dict/src/test/java/org/onap/ccsdk/apps/controllerblueprints/resource/dict/utils/BulkResourceSequencingUtilsTest.java new file mode 100644 index 00000000..c7444dba --- /dev/null +++ b/ms/controllerblueprints/modules/resource-dict/src/test/java/org/onap/ccsdk/apps/controllerblueprints/resource/dict/utils/BulkResourceSequencingUtilsTest.java @@ -0,0 +1,37 @@ +/*
+ * 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.junit.Assert;
+import org.junit.Test;
+import org.onap.ccsdk.apps.controllerblueprints.core.utils.JacksonUtils;
+import org.onap.ccsdk.apps.controllerblueprints.resource.dict.ResourceAssignment;
+import java.util.List;
+/**
+ * BulkResourceSequencingUtils.
+ *
+ * @author Brinda Santh
+ */
+public class BulkResourceSequencingUtilsTest {
+
+ @Test
+ public void testProcess(){
+ List<ResourceAssignment> assignments = JacksonUtils.getListFromClassPathFile("validation/success.json", ResourceAssignment.class);
+ Assert.assertNotNull("failed to get ResourceAssignment from validation/success.json ", assignments);
+ BulkResourceSequencingUtils.process(assignments);
+ }
+}
\ No newline at end of file diff --git a/ms/controllerblueprints/modules/resource-dict/src/test/java/org/onap/ccsdk/apps/controllerblueprints/resource/dict/utils/ResourceDictionaryUtilsTest.java b/ms/controllerblueprints/modules/resource-dict/src/test/java/org/onap/ccsdk/apps/controllerblueprints/resource/dict/utils/ResourceDictionaryUtilsTest.java index 5c22f654..5ee56171 100644 --- a/ms/controllerblueprints/modules/resource-dict/src/test/java/org/onap/ccsdk/apps/controllerblueprints/resource/dict/utils/ResourceDictionaryUtilsTest.java +++ b/ms/controllerblueprints/modules/resource-dict/src/test/java/org/onap/ccsdk/apps/controllerblueprints/resource/dict/utils/ResourceDictionaryUtilsTest.java @@ -32,7 +32,11 @@ import org.slf4j.LoggerFactory; import java.util.HashMap;
import java.util.Map;
-
+/**
+ * ResourceDictionaryUtilsTest.
+ *
+ * @author Brinda Santh
+ */
public class ResourceDictionaryUtilsTest {
private static final Logger log = LoggerFactory.getLogger(ResourceDictionaryUtilsTest.class);
|