2 * Copyright © 2017-2018 AT&T Intellectual Property.
3 * Modifications Copyright © 2018 IBM.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 package org.onap.ccsdk.apps.controllerblueprints.resource.dict.service
20 import com.att.eelf.configuration.EELFLogger
21 import org.apache.commons.collections.CollectionUtils
22 import org.apache.commons.lang3.StringUtils
23 import org.apache.commons.lang3.text.StrBuilder
24 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintException
25 import org.onap.ccsdk.apps.controllerblueprints.core.utils.TopologicalSortingUtils
26 import org.onap.ccsdk.apps.controllerblueprints.resource.dict.ResourceAssignment
27 import com.att.eelf.configuration.EELFManager
28 import org.onap.ccsdk.apps.controllerblueprints.core.format
29 import org.onap.ccsdk.apps.controllerblueprints.resource.dict.factory.ResourceSourceMappingFactory
30 import java.io.Serializable
33 * ResourceAssignmentValidationService.
35 * @author Brinda Santh
37 interface ResourceAssignmentValidationService : Serializable {
39 @Throws(BluePrintException::class)
40 fun validate(resourceAssignments: List<ResourceAssignment>): Boolean
44 * ResourceAssignmentValidationDefaultService.
46 * @author Brinda Santh
48 open class ResourceAssignmentValidationDefaultService : ResourceAssignmentValidationService {
49 private val log: EELFLogger = EELFManager.getInstance().getLogger(ResourceAssignmentValidationDefaultService::class.java)
51 open var resourceAssignmentMap: Map<String, ResourceAssignment> = hashMapOf()
52 open val validationMessage = StrBuilder()
54 override fun validate(resourceAssignments: List<ResourceAssignment>): Boolean {
56 validateSources(resourceAssignments)
57 validateTemplateNDictionaryKeys(resourceAssignments)
58 validateCyclicDependency(resourceAssignments)
59 if (StringUtils.isNotBlank(validationMessage)) {
60 throw BluePrintException("Resource Assignment Validation Failure")
62 } catch (e: Exception) {
63 throw BluePrintException("Resource Assignment Validation :" + validationMessage.toString(), e)
68 open fun validateSources(resourceAssignments: List<ResourceAssignment>) {
69 log.info("validating resource assignment sources")
70 resourceAssignments.forEach { resourceAssignment ->
72 ResourceSourceMappingFactory.getRegisterSourceMapping(resourceAssignment.dictionarySource!!)
73 } catch (e: BluePrintException) {
74 validationMessage.appendln(e.message + format(" for resource assignment({})", resourceAssignment.name))
79 open fun validateTemplateNDictionaryKeys(resourceAssignments: List<ResourceAssignment>) {
81 resourceAssignmentMap = resourceAssignments.map { it.name to it }.toMap()
83 val duplicateKeyNames = resourceAssignments.groupBy { it.name }
84 .filter { it.value.size > 1 }
87 if (duplicateKeyNames.isNotEmpty()) {
88 validationMessage.appendln(String.format("Duplicate Assignment Template Keys (%s) is Present", duplicateKeyNames))
91 val duplicateDictionaryKeyNames = resourceAssignments.groupBy { it.dictionaryName }
92 .filter { it.value.size > 1 }
94 if (duplicateDictionaryKeyNames.isNotEmpty()) {
95 validationMessage.appendln(String.format("Duplicate Assignment Dictionary Keys (%s) is Present", duplicateDictionaryKeyNames))
98 val dependenciesNames = resourceAssignments.mapNotNull { it.dependencies }.flatten()
100 val notPresentDictionaries = dependenciesNames.filter { !resourceAssignmentMap.containsKey(it) }.distinct()
101 if (notPresentDictionaries.isNotEmpty()) {
102 validationMessage.appendln(String.format("No assignments for Dictionary Keys (%s)", notPresentDictionaries))
105 if (StringUtils.isNotBlank(validationMessage)) {
106 throw BluePrintException("Resource Assignment Validation Failure")
110 open fun validateCyclicDependency(resourceAssignments: List<ResourceAssignment>) {
111 val startResourceAssignment = ResourceAssignment()
112 startResourceAssignment.name = "*"
114 val topologySorting = TopologicalSortingUtils<ResourceAssignment>()
116 resourceAssignmentMap.map { it.value }.map { resourceAssignment ->
117 if (CollectionUtils.isNotEmpty(resourceAssignment.dependencies)) {
118 resourceAssignment.dependencies!!.map {
119 log.info("Topological Graph link from {} to {}", it, resourceAssignment.name)
120 topologySorting.add(resourceAssignmentMap[it]!!, resourceAssignment)
123 topologySorting.add(startResourceAssignment, resourceAssignment)
127 if (!topologySorting.isDag) {
128 val graph = getTopologicalGraph(topologySorting)
129 validationMessage.appendln("Cyclic Dependency :$graph")
133 open fun getTopologicalGraph(topologySorting: TopologicalSortingUtils<ResourceAssignment>): String {
134 val s = StringBuilder()
135 val neighbors = topologySorting.getNeighbors()
137 neighbors.forEach { v, vs ->
139 s.append("\n * -> [")
140 for (resourceAssignment in vs) {
141 s.append("(" + resourceAssignment.dictionaryName + ":" + resourceAssignment.name
146 s.append("\n (" + v.dictionaryName + ":" + v.name + ") -> [")
147 for (resourceAssignment in vs) {
148 s.append("(" + resourceAssignment.dictionaryName + ":" + resourceAssignment.name