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.apps.controllerblueprints.resource.dict.service
19 import com.att.eelf.configuration.EELFLogger
20 import org.apache.commons.collections.CollectionUtils
21 import org.apache.commons.lang3.StringUtils
22 import org.apache.commons.lang3.text.StrBuilder
23 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintException
24 import org.onap.ccsdk.apps.controllerblueprints.core.utils.TopologicalSortingUtils
25 import org.onap.ccsdk.apps.controllerblueprints.resource.dict.ResourceAssignment
26 import com.att.eelf.configuration.EELFManager
27 import java.io.Serializable
30 * ResourceAssignmentValidationService.
32 * @author Brinda Santh
34 interface ResourceAssignmentValidationService : Serializable {
36 @Throws(BluePrintException::class)
37 fun validate(resourceAssignments: List<ResourceAssignment>): Boolean
41 * ResourceAssignmentValidationDefaultService.
43 * @author Brinda Santh
45 open class ResourceAssignmentValidationDefaultService : ResourceAssignmentValidationService {
46 private val log: EELFLogger = EELFManager.getInstance().getLogger(ResourceAssignmentValidationDefaultService::class.java)
48 open var resourceAssignmentMap: Map<String, ResourceAssignment> = hashMapOf()
49 open val validationMessage = StrBuilder()
51 override fun validate(resourceAssignments: List<ResourceAssignment>): Boolean {
53 validateSources(resourceAssignments)
54 validateTemplateNDictionaryKeys(resourceAssignments)
55 validateCyclicDependency(resourceAssignments)
56 if (StringUtils.isNotBlank(validationMessage)) {
57 throw BluePrintException("Resource Assignment Validation Failure")
59 } catch (e: Exception) {
60 throw BluePrintException("Resource Assignment Validation :" + validationMessage.toString(), e)
65 open fun validateSources(resourceAssignments: List<ResourceAssignment>) {
66 log.info("validating resource assignment sources")
69 open fun validateTemplateNDictionaryKeys(resourceAssignments: List<ResourceAssignment>) {
71 resourceAssignmentMap = resourceAssignments.map { it.name to it }.toMap()
73 val duplicateKeyNames = resourceAssignments.groupBy { it.name }
74 .filter { it.value.size > 1 }
77 if (duplicateKeyNames.isNotEmpty()) {
78 validationMessage.appendln(String.format("Duplicate Assignment Template Keys (%s) is Present", duplicateKeyNames))
81 val duplicateDictionaryKeyNames = resourceAssignments.groupBy { it.dictionaryName }
82 .filter { it.value.size > 1 }
84 if (duplicateDictionaryKeyNames.isNotEmpty()) {
85 validationMessage.appendln(String.format("Duplicate Assignment Dictionary Keys (%s) is Present", duplicateDictionaryKeyNames))
88 val dependenciesNames = resourceAssignments.mapNotNull { it.dependencies }.flatten()
90 log.info("Resource assignment definitions : {}", resourceAssignmentMap.keys)
91 log.info("Resource assignment Dictionary dependencies : {}", dependenciesNames)
93 val notPresentDictionaries = dependenciesNames.filter { !resourceAssignmentMap.containsKey(it) }.distinct()
94 if (notPresentDictionaries.isNotEmpty()) {
95 validationMessage.appendln(String.format("No assignments for Dictionary Keys (%s)", notPresentDictionaries))
98 if (StringUtils.isNotBlank(validationMessage)) {
99 throw BluePrintException("Resource Assignment Validation Failure")
103 open fun validateCyclicDependency(resourceAssignments: List<ResourceAssignment>) {
104 val startResourceAssignment = ResourceAssignment()
105 startResourceAssignment.name = "*"
107 val topologySorting = TopologicalSortingUtils<ResourceAssignment>()
109 resourceAssignmentMap.map { it.value }.map { resourceAssignment ->
110 if (CollectionUtils.isNotEmpty(resourceAssignment.dependencies)) {
111 resourceAssignment.dependencies!!.map {
112 log.info("Topological Graph link from {} to {}", it, resourceAssignment.name)
113 topologySorting.add(resourceAssignmentMap[it]!!, resourceAssignment)
116 topologySorting.add(startResourceAssignment, resourceAssignment)
120 if (!topologySorting.isDag) {
121 val graph = getTopologicalGraph(topologySorting)
122 validationMessage.appendln("Cyclic Dependency :$graph")
126 open fun getTopologicalGraph(topologySorting: TopologicalSortingUtils<ResourceAssignment>): String {
127 val s = StringBuilder()
128 val neighbors = topologySorting.getNeighbors()
130 neighbors.forEach { v, vs ->
132 s.append("\n * -> [")
133 for (resourceAssignment in vs) {
134 s.append("(" + resourceAssignment.dictionaryName + ":" + resourceAssignment.name
139 s.append("\n (" + v.dictionaryName + ":" + v.name + ") -> [")
140 for (resourceAssignment in vs) {
141 s.append("(" + resourceAssignment.dictionaryName + ":" + resourceAssignment.name