d71fbbf806781d2e25e8736d1069a8bcc254a4cc
[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 com.att.eelf.configuration.EELFManager
22 import org.apache.commons.collections.CollectionUtils
23 import org.apache.commons.lang3.StringUtils
24 import org.apache.commons.lang3.text.StrBuilder
25 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintException
26 import org.onap.ccsdk.apps.controllerblueprints.core.format
27 import org.onap.ccsdk.apps.controllerblueprints.core.utils.TopologicalSortingUtils
28 import org.onap.ccsdk.apps.controllerblueprints.resource.dict.ResourceAssignment
29 import org.onap.ccsdk.apps.controllerblueprints.resource.dict.factory.ResourceSourceMappingFactory
30 import java.io.Serializable
31
32 /**
33  * ResourceAssignmentValidationService.
34  *
35  * @author Brinda Santh
36  */
37 interface ResourceAssignmentValidationService : Serializable {
38
39     @Throws(BluePrintException::class)
40     fun validate(resourceAssignments: List<ResourceAssignment>): Boolean
41 }
42
43 /**
44  * ResourceAssignmentValidationServiceImpl.
45  *
46  * @author Brinda Santh
47  */
48 open class ResourceAssignmentValidationServiceImpl : ResourceAssignmentValidationService {
49     private val log: EELFLogger = EELFManager.getInstance().getLogger(ResourceAssignmentValidationServiceImpl::class.java)
50
51     open var resourceAssignmentMap: Map<String, ResourceAssignment> = hashMapOf()
52     open val validationMessage = StrBuilder()
53
54     override fun validate(resourceAssignments: List<ResourceAssignment>): Boolean {
55         try {
56             validateSources(resourceAssignments)
57             validateTemplateNDictionaryKeys(resourceAssignments)
58             validateCyclicDependency(resourceAssignments)
59             if (StringUtils.isNotBlank(validationMessage)) {
60                 throw BluePrintException("Resource Assignment Validation Failure")
61             }
62         } catch (e: Exception) {
63             throw BluePrintException("Resource Assignment Validation :" + validationMessage.toString(), e)
64         }
65         return true
66     }
67
68     open fun validateSources(resourceAssignments: List<ResourceAssignment>) {
69         log.info("validating resource assignment sources")
70         // Check the Resource Assignment Source(Dynamic Instance) is valid.
71         resourceAssignments.forEach { resourceAssignment ->
72             try {
73                 ResourceSourceMappingFactory.getRegisterSourceMapping(resourceAssignment.dictionarySource!!)
74             } catch (e: BluePrintException) {
75                 validationMessage.appendln(e.message + format(" for resource assignment({})", resourceAssignment.name))
76             }
77         }
78     }
79
80     open fun validateTemplateNDictionaryKeys(resourceAssignments: List<ResourceAssignment>) {
81
82         resourceAssignmentMap = resourceAssignments.map { it.name to it }.toMap()
83
84         // Check the Resource Assignment has Duplicate Key Names
85         val duplicateKeyNames = resourceAssignments.groupBy { it.name }
86                 .filter { it.value.size > 1 }
87                 .map { it.key }
88
89         if (duplicateKeyNames.isNotEmpty()) {
90             validationMessage.appendln(String.format("Duplicate Assignment Template Keys (%s) is Present", duplicateKeyNames))
91         }
92
93         // Check the Resource Assignment has Duplicate Dictionary Names
94         val duplicateDictionaryKeyNames = resourceAssignments.groupBy { it.dictionaryName }
95                 .filter { it.value.size > 1 }
96                 .map { it.key }
97         if (duplicateDictionaryKeyNames.isNotEmpty()) {
98             validationMessage.appendln(String.format("Duplicate Assignment Dictionary Keys (%s) is Present", duplicateDictionaryKeyNames))
99         }
100
101         // Collect all the dependencies as a single list
102         val dependenciesNames = resourceAssignments.mapNotNull { it.dependencies }.flatten()
103
104         // Check all the dependencies keys have Resource Assignment mappings.
105         val notPresentDictionaries = dependenciesNames.filter { !resourceAssignmentMap.containsKey(it) }.distinct()
106         if (notPresentDictionaries.isNotEmpty()) {
107             validationMessage.appendln(String.format("No assignments for Dictionary Keys (%s)", notPresentDictionaries))
108         }
109
110         if (StringUtils.isNotBlank(validationMessage)) {
111             throw BluePrintException("Resource Assignment Validation Failure")
112         }
113     }
114
115     open fun validateCyclicDependency(resourceAssignments: List<ResourceAssignment>) {
116         val startResourceAssignment = ResourceAssignment()
117         startResourceAssignment.name = "*"
118
119         val topologySorting = TopologicalSortingUtils<ResourceAssignment>()
120
121         resourceAssignmentMap.map { it.value }.map { resourceAssignment ->
122             if (CollectionUtils.isNotEmpty(resourceAssignment.dependencies)) {
123                 resourceAssignment.dependencies!!.map {
124                     log.info("Topological Graph link from {} to {}", it, resourceAssignment.name)
125                     topologySorting.add(resourceAssignmentMap[it]!!, resourceAssignment)
126                 }
127             } else {
128                 topologySorting.add(startResourceAssignment, resourceAssignment)
129             }
130         }
131
132         if (!topologySorting.isDag) {
133             val graph = getTopologicalGraph(topologySorting)
134             validationMessage.appendln("Cyclic Dependency :$graph")
135         }
136     }
137
138     open fun getTopologicalGraph(topologySorting: TopologicalSortingUtils<ResourceAssignment>): String {
139         val s = StringBuilder()
140         val neighbors = topologySorting.getNeighbors()
141
142         neighbors.forEach { v, vs ->
143             if (v.name == "*") {
144                 s.append("\n    * -> [")
145                 for (resourceAssignment in vs) {
146                     s.append("(" + resourceAssignment.dictionaryName + ":" + resourceAssignment.name
147                             + "),")
148                 }
149                 s.append("]")
150             } else {
151                 s.append("\n    (" + v.dictionaryName + ":" + v.name + ") -> [")
152                 for (resourceAssignment in vs) {
153                     s.append("(" + resourceAssignment.dictionaryName + ":" + resourceAssignment.name
154                             + "),")
155                 }
156                 s.append("]")
157             }
158         }
159         return s.toString()
160     }
161
162
163 }