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