Fix OSGi wiring issues
[ccsdk/features.git] / blueprints-processor / plugin / model-provider / src / main / java / org / onap / ccsdk / features / model / validator / NodeTypeValidator.java
1 /*\r
2  * Copyright © 2017-2018 AT&T Intellectual Property.\r
3  * Modifications Copyright © 2018 IBM.\r
4  * \r
5  * Licensed under the Apache License, Version 2.0 (the "License");\r
6  * you may not use this file except in compliance with the License.\r
7  * You may obtain a copy of the License at\r
8  * \r
9  * http://www.apache.org/licenses/LICENSE-2.0\r
10  * \r
11  * Unless required by applicable law or agreed to in writing, software\r
12  * distributed under the License is distributed on an "AS IS" BASIS,\r
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
14  * See the License for the specific language governing permissions and\r
15  * limitations under the License.\r
16  */\r
17 \r
18 package org.onap.ccsdk.features.model.validator;\r
19 \r
20 import java.util.HashMap;\r
21 import java.util.Map;\r
22 import org.apache.commons.lang3.StringUtils;\r
23 import org.onap.ccsdk.features.model.ConfigModelConstant;\r
24 import org.onap.ccsdk.features.model.ConfigModelException;\r
25 import org.onap.ccsdk.features.model.ValidTypes;\r
26 import org.onap.ccsdk.features.model.data.CapabilityDefinition;\r
27 import org.onap.ccsdk.features.model.data.DataType;\r
28 import org.onap.ccsdk.features.model.data.InterfaceDefinition;\r
29 import org.onap.ccsdk.features.model.data.NodeType;\r
30 import org.onap.ccsdk.features.model.data.OperationDefinition;\r
31 import org.onap.ccsdk.features.model.data.PropertyDefinition;\r
32 import org.onap.ccsdk.features.model.data.RequirementDefinition;\r
33 import org.onap.ccsdk.features.model.data.ServiceTemplate;\r
34 \r
35 import com.att.eelf.configuration.EELFLogger;\r
36 import com.att.eelf.configuration.EELFManager;\r
37 \r
38 /**\r
39  * NodeTypeValidator.java Purpose: Provide Configuration Generator NodeTypeValidator\r
40  *\r
41  * @version 1.0\r
42  */\r
43 public class NodeTypeValidator {\r
44     private static EELFLogger logger = EELFManager.getInstance().getLogger(NodeTypeValidator.class);\r
45     private StringBuilder message;\r
46     private Map<String, DataType> stDataTypes;\r
47     private Map<String, NodeType> stNodeTypes;\r
48     private ServiceTemplate serviceTemplate;\r
49     private PropertyDefinitionValidator propertyDefinitionValidator;\r
50 \r
51     /**\r
52      * This is a NodeTypeValidator\r
53      *\r
54      * @param serviceTemplate\r
55      * @throws ConfigModelException\r
56      */\r
57     public NodeTypeValidator(ServiceTemplate serviceTemplate, StringBuilder message) throws ConfigModelException {\r
58         this.serviceTemplate = serviceTemplate;\r
59         this.message = message;\r
60         propertyDefinitionValidator = new PropertyDefinitionValidator(this.message);\r
61         stDataTypes = new HashMap<>();\r
62         stNodeTypes = new HashMap<>();\r
63         loadInitial();\r
64 \r
65     }\r
66 \r
67     private void loadInitial() {\r
68         if (serviceTemplate != null) {\r
69 \r
70             if (serviceTemplate.getDataTypes() != null) {\r
71                 serviceTemplate.getDataTypes().forEach((dataTypeKey, dataType) -> {\r
72                     stDataTypes.put(dataTypeKey, dataType);\r
73                     logger.trace("Data Type ({}) loaded successfully.", dataTypeKey);\r
74                 });\r
75             }\r
76 \r
77             if (serviceTemplate.getNodeTypes() != null) {\r
78                 serviceTemplate.getNodeTypes().forEach((nodeTypeKey, nodeType) -> {\r
79                     stNodeTypes.put(nodeTypeKey, nodeType);\r
80                     logger.trace("NodeType Type ({}) loaded successfully.", nodeTypeKey);\r
81                 });\r
82             }\r
83 \r
84         }\r
85 \r
86     }\r
87 \r
88     /**\r
89      * This is a validateNodeTypes to validate the Node Type\r
90      *\r
91      * @return boolean\r
92      * @throws ConfigModelException\r
93      */\r
94     @SuppressWarnings({"squid:S00112", "squid:S3776"})\r
95     public boolean validateNodeTypes() {\r
96         if (serviceTemplate != null && serviceTemplate.getNodeTypes() != null) {\r
97             serviceTemplate.getNodeTypes().forEach((nodeTypeKey, nodeType) -> {\r
98                 if (nodeType != null) {\r
99                     message.append("\n ***** Validation Node Type  (" + nodeTypeKey + "), derived from ("\r
100                             + nodeType.getDerivedFrom() + ")");\r
101                     try {\r
102                         validateNodeType(ConfigModelConstant.MODEL_DEFINITION_TYPE_NODE_TYPE,\r
103                                 nodeType.getDerivedFrom());\r
104 \r
105                         if (nodeType.getProperties() != null) {\r
106                             checkValidProperties(nodeType.getProperties());\r
107                         }\r
108 \r
109                         if (nodeType.getCapabilities() != null) {\r
110                             validateNodeTypeCapabilities(nodeType.getCapabilities());\r
111                         }\r
112 \r
113                         if (nodeType.getInterfaces() != null) {\r
114                             validateNodeTypeInterface(nodeType.getInterfaces());\r
115                         }\r
116 \r
117                         if (nodeType.getRequirements() != null) {\r
118                             validateNodeTypeRequirement(nodeType.getRequirements());\r
119                         }\r
120 \r
121                     } catch (ConfigModelException e) {\r
122                         logger.error(e.getMessage());\r
123                         throw new RuntimeException(e.getMessage());\r
124                     }\r
125 \r
126                 }\r
127             });\r
128         }\r
129         return true;\r
130     }\r
131 \r
132     private boolean validateNodeType(String definitionType, String derivedFrom) throws ConfigModelException {\r
133         boolean valid = true;\r
134         if (!ConfigModelConstant.MODEL_DEFINITION_TYPE_DATA_TYPE.equalsIgnoreCase(definitionType)\r
135                 && !ValidTypes.getValidNodeTypes().contains(derivedFrom)) {\r
136             throw new ConfigModelException("Not Valid Model Type (" + derivedFrom + ")");\r
137         }\r
138         return valid;\r
139     }\r
140 \r
141     private boolean checkValidProperties(Map<String, PropertyDefinition> properties) {\r
142         if (properties != null) {\r
143             propertyDefinitionValidator.validatePropertyDefinition(stDataTypes, properties);\r
144         }\r
145         return true;\r
146     }\r
147 \r
148     @SuppressWarnings("squid:S00112")\r
149     private boolean validateNodeTypeCapabilities(Map<String, CapabilityDefinition> capabilities) {\r
150         if (capabilities != null) {\r
151             capabilities.forEach((capabilityKey, capability) -> {\r
152                 if (capability != null) {\r
153                     Map<String, PropertyDefinition> properties = capability.getProperties();\r
154                     message.append("\n Validation Capability (" + capabilityKey + ") properties :");\r
155                     propertyDefinitionValidator.validatePropertyDefinition(stDataTypes, properties);\r
156                 }\r
157             });\r
158         }\r
159         return true;\r
160     }\r
161 \r
162     @SuppressWarnings("squid:S00112")\r
163     private boolean validateNodeTypeInterface(Map<String, InterfaceDefinition> interfaces) {\r
164         if (interfaces != null) {\r
165             interfaces.forEach((interfaceKey, interfaceDefinition) -> {\r
166                 if (interfaceDefinition != null && interfaceDefinition.getOperations() != null) {\r
167                     validateNodeTypeInterfaceOperation(interfaceDefinition.getOperations());\r
168                 }\r
169             });\r
170         }\r
171         return true;\r
172     }\r
173 \r
174     @SuppressWarnings("squid:S00112")\r
175     private boolean validateNodeTypeInterfaceOperation(Map<String, OperationDefinition> operations) {\r
176         if (operations != null) {\r
177             operations.forEach((operationKey, operation) -> {\r
178                 if (operation != null) {\r
179                     Map<String, PropertyDefinition> inputs = operation.getInputs();\r
180                     message.append("\n Validation Operation (" + operationKey + ") Inputs :");\r
181                     propertyDefinitionValidator.validatePropertyDefinition(stDataTypes, inputs);\r
182                     message.append("\n Validation Operation (" + operationKey + ") output :");\r
183                     Map<String, PropertyDefinition> outputs = operation.getOutputs();\r
184                     propertyDefinitionValidator.validatePropertyDefinition(stDataTypes, outputs);\r
185                 }\r
186             });\r
187         }\r
188         return true;\r
189     }\r
190 \r
191     @SuppressWarnings("squid:S00112")\r
192     private boolean validateNodeTypeRequirement(Map<String, RequirementDefinition> requirements) {\r
193         if (requirements != null) {\r
194             requirements.forEach((requirementKey, requirement) -> {\r
195                 if (requirement != null) {\r
196                     String nodeTypeName = requirement.getNode();\r
197                     String capabilityName = requirement.getCapability();\r
198                     try {\r
199                         checkCapabilityPresentInNodeType(nodeTypeName, capabilityName);\r
200                     } catch (ConfigModelException e) {\r
201                         throw new RuntimeException(e);\r
202                     }\r
203                 }\r
204             });\r
205         }\r
206         return true;\r
207     }\r
208 \r
209     private boolean checkCapabilityPresentInNodeType(String nodeTypeName, String capabilityName)\r
210             throws ConfigModelException {\r
211         if (StringUtils.isNotBlank(nodeTypeName) && StringUtils.isNotBlank(capabilityName)) {\r
212 \r
213             if (!stNodeTypes.containsKey(nodeTypeName)) {\r
214                 throw new ConfigModelException(nodeTypeName + " Node Type not Defined.");\r
215             } else {\r
216                 message.append("\n Node Type  (" + nodeTypeName + ") Defined.");\r
217             }\r
218 \r
219             NodeType relationalNodeType = stNodeTypes.get(nodeTypeName);\r
220 \r
221             if (relationalNodeType.getCapabilities() == null) {\r
222                 throw new ConfigModelException(\r
223                         "Node Type  (" + nodeTypeName + "), doesn't have Capability Definitions.");\r
224             }\r
225 \r
226             if (!relationalNodeType.getCapabilities().containsKey(capabilityName)) {\r
227                 throw new ConfigModelException("Node Type (" + nodeTypeName + ") doesn't have  (" + capabilityName\r
228                         + ") Capability Definitions.");\r
229             } else {\r
230                 message.append(\r
231                         "\n Node Type (" + nodeTypeName + ") has (" + capabilityName + ") Capability Definitions.");\r
232             }\r
233 \r
234         }\r
235         return true;\r
236     }\r
237 \r
238 }\r