228b39e29815631cf7472cac13b4930739386d6c
[ccsdk/cds.git] /
1 /*
2  *  Copyright © 2017-2018 AT&T Intellectual Property.
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.onap.ccsdk.apps.controllerblueprints.resource.dict.service
18
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
28
29 /**
30  * ResourceAssignmentValidationService.
31  *
32  * @author Brinda Santh
33  */
34 interface ResourceAssignmentValidationService : Serializable {
35
36     @Throws(BluePrintException::class)
37     fun validate(resourceAssignments: List<ResourceAssignment>): Boolean
38 }
39
40 /**
41  * ResourceAssignmentValidationDefaultService.
42  *
43  * @author Brinda Santh
44  */
45 open class ResourceAssignmentValidationDefaultService : ResourceAssignmentValidationService {
46     private val log: EELFLogger = EELFManager.getInstance().getLogger(ResourceAssignmentValidationDefaultService::class.java)
47
48     open var resourceAssignmentMap: Map<String, ResourceAssignment> = hashMapOf()
49     open val validationMessage = StrBuilder()
50
51     override fun validate(resourceAssignments: List<ResourceAssignment>): Boolean {
52         try {
53             validateSources(resourceAssignments)
54             validateTemplateNDictionaryKeys(resourceAssignments)
55             validateCyclicDependency(resourceAssignments)
56             if (StringUtils.isNotBlank(validationMessage)) {
57                 throw BluePrintException("Resource Assignment Validation Failure")
58             }
59         } catch (e: Exception) {
60             throw BluePrintException("Resource Assignment Validation :" + validationMessage.toString(), e)
61         }
62         return true
63     }
64
65     open fun validateSources(resourceAssignments: List<ResourceAssignment>) {
66         log.info("validating resource assignment sources")
67     }
68
69     open fun validateTemplateNDictionaryKeys(resourceAssignments: List<ResourceAssignment>) {
70
71         resourceAssignmentMap = resourceAssignments.map { it.name to it }.toMap()
72
73         val duplicateKeyNames = resourceAssignments.groupBy { it.name }
74                 .filter { it.value.size > 1 }
75                 .map { it.key }
76
77         if (duplicateKeyNames.isNotEmpty()) {
78             validationMessage.appendln(String.format("Duplicate Assignment Template Keys (%s) is Present", duplicateKeyNames))
79         }
80
81         val duplicateDictionaryKeyNames = resourceAssignments.groupBy { it.dictionaryName }
82                 .filter { it.value.size > 1 }
83                 .map { it.key }
84         if (duplicateDictionaryKeyNames.isNotEmpty()) {
85             validationMessage.appendln(String.format("Duplicate Assignment Dictionary Keys (%s) is Present", duplicateDictionaryKeyNames))
86         }
87
88         val dependenciesNames = resourceAssignments.mapNotNull { it.dependencies }.flatten()
89
90         log.info("Resource assignment definitions : {}", resourceAssignmentMap.keys)
91         log.info("Resource assignment Dictionary dependencies : {}", dependenciesNames)
92
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))
96         }
97
98         if (StringUtils.isNotBlank(validationMessage)) {
99             throw BluePrintException("Resource Assignment Validation Failure")
100         }
101     }
102
103     open fun validateCyclicDependency(resourceAssignments: List<ResourceAssignment>) {
104         val startResourceAssignment = ResourceAssignment()
105         startResourceAssignment.name = "*"
106
107         val topologySorting = TopologicalSortingUtils<ResourceAssignment>()
108
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)
114                 }
115             } else {
116                 topologySorting.add(startResourceAssignment, resourceAssignment)
117             }
118         }
119
120         if (!topologySorting.isDag) {
121             val graph = getTopologicalGraph(topologySorting)
122             validationMessage.appendln("Cyclic Dependency :$graph")
123         }
124     }
125
126     open fun getTopologicalGraph(topologySorting: TopologicalSortingUtils<ResourceAssignment>): String {
127         val s = StringBuilder()
128         val neighbors = topologySorting.getNeighbors()
129
130         neighbors.forEach { v, vs ->
131             if (v.name == "*") {
132                 s.append("\n    * -> [")
133                 for (resourceAssignment in vs) {
134                     s.append("(" + resourceAssignment.dictionaryName + ":" + resourceAssignment.name
135                             + "),")
136                 }
137                 s.append("]")
138             } else {
139                 s.append("\n    (" + v.dictionaryName + ":" + v.name + ") -> [")
140                 for (resourceAssignment in vs) {
141                     s.append("(" + resourceAssignment.dictionaryName + ":" + resourceAssignment.name
142                             + "),")
143                 }
144                 s.append("]")
145             }
146         }
147         return s.toString()
148     }
149
150
151 }