2 * Copyright © 2017-2018 AT&T Intellectual Property.
\r
3 * Modifications Copyright © 2018 IBM.
\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
9 * http://www.apache.org/licenses/LICENSE-2.0
\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
18 package org.onap.ccsdk.config.model.validator;
\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.config.model.ConfigModelConstant;
\r
24 import org.onap.ccsdk.config.model.ConfigModelException;
\r
25 import org.onap.ccsdk.config.model.ValidTypes;
\r
26 import org.onap.ccsdk.config.model.data.CapabilityDefinition;
\r
27 import org.onap.ccsdk.config.model.data.DataType;
\r
28 import org.onap.ccsdk.config.model.data.InterfaceDefinition;
\r
29 import org.onap.ccsdk.config.model.data.NodeType;
\r
30 import org.onap.ccsdk.config.model.data.OperationDefinition;
\r
31 import org.onap.ccsdk.config.model.data.PropertyDefinition;
\r
32 import org.onap.ccsdk.config.model.data.RequirementDefinition;
\r
33 import org.onap.ccsdk.config.model.data.ServiceTemplate;
\r
34 import com.att.eelf.configuration.EELFLogger;
\r
35 import com.att.eelf.configuration.EELFManager;
\r
38 * NodeTypeValidator.java Purpose: Provide Configuration Generator NodeTypeValidator
\r
42 public class NodeTypeValidator {
\r
43 private static EELFLogger logger = EELFManager.getInstance().getLogger(NodeTypeValidator.class);
\r
44 private StringBuilder message;
\r
45 private Map<String, DataType> stDataTypes;
\r
46 private Map<String, NodeType> stNodeTypes;
\r
47 private ServiceTemplate serviceTemplate;
\r
48 private PropertyDefinitionValidator propertyDefinitionValidator;
\r
51 * This is a NodeTypeValidator
\r
53 * @param serviceTemplate
\r
54 * @throws ConfigModelException
\r
56 public NodeTypeValidator(ServiceTemplate serviceTemplate, StringBuilder message) throws ConfigModelException {
\r
57 this.serviceTemplate = serviceTemplate;
\r
58 this.message = message;
\r
59 propertyDefinitionValidator = new PropertyDefinitionValidator(this.message);
\r
60 stDataTypes = new HashMap<>();
\r
61 stNodeTypes = new HashMap<>();
\r
66 private void loadInitial() {
\r
67 if (serviceTemplate != null) {
\r
69 if (serviceTemplate.getDataTypes() != null) {
\r
70 serviceTemplate.getDataTypes().forEach((dataTypeKey, dataType) -> {
\r
71 stDataTypes.put(dataTypeKey, dataType);
\r
72 logger.trace("Data Type ({}) loaded successfully.", dataTypeKey);
\r
76 if (serviceTemplate.getNodeTypes() != null) {
\r
77 serviceTemplate.getNodeTypes().forEach((nodeTypeKey, nodeType) -> {
\r
78 stNodeTypes.put(nodeTypeKey, nodeType);
\r
79 logger.trace("NodeType Type ({}) loaded successfully.", nodeTypeKey);
\r
88 * This is a validateNodeTypes to validate the Node Type
\r
91 * @throws ConfigModelException
\r
93 @SuppressWarnings({"squid:S00112", "squid:S3776"})
\r
94 public boolean validateNodeTypes() {
\r
95 if (serviceTemplate != null && serviceTemplate.getNodeTypes() != null) {
\r
96 serviceTemplate.getNodeTypes().forEach((nodeTypeKey, nodeType) -> {
\r
97 if (nodeType != null) {
\r
98 message.append("\n ***** Validation Node Type (" + nodeTypeKey + "), derived from ("
\r
99 + nodeType.getDerivedFrom() + ")");
\r
101 validateNodeType(ConfigModelConstant.MODEL_DEFINITION_TYPE_NODE_TYPE,
\r
102 nodeType.getDerivedFrom());
\r
104 if (nodeType.getProperties() != null) {
\r
105 checkValidProperties(nodeType.getProperties());
\r
108 if (nodeType.getCapabilities() != null) {
\r
109 validateNodeTypeCapabilities(nodeType.getCapabilities());
\r
112 if (nodeType.getInterfaces() != null) {
\r
113 validateNodeTypeInterface(nodeType.getInterfaces());
\r
116 if (nodeType.getRequirements() != null) {
\r
117 validateNodeTypeRequirement(nodeType.getRequirements());
\r
120 } catch (ConfigModelException e) {
\r
121 logger.error(e.getMessage());
\r
122 throw new RuntimeException(e.getMessage());
\r
131 private boolean validateNodeType(String definitionType, String derivedFrom) throws ConfigModelException {
\r
132 boolean valid = true;
\r
133 if (!ConfigModelConstant.MODEL_DEFINITION_TYPE_DATA_TYPE.equalsIgnoreCase(definitionType)
\r
134 && !ValidTypes.getValidNodeTypes().contains(derivedFrom)) {
\r
135 throw new ConfigModelException("Not Valid Model Type (" + derivedFrom + ")");
\r
140 private boolean checkValidProperties(Map<String, PropertyDefinition> properties) {
\r
141 if (properties != null) {
\r
142 propertyDefinitionValidator.validatePropertyDefinition(stDataTypes, properties);
\r
147 @SuppressWarnings("squid:S00112")
\r
148 private boolean validateNodeTypeCapabilities(Map<String, CapabilityDefinition> capabilities) {
\r
149 if (capabilities != null) {
\r
150 capabilities.forEach((capabilityKey, capability) -> {
\r
151 if (capability != null) {
\r
152 Map<String, PropertyDefinition> properties = capability.getProperties();
\r
153 message.append("\n Validation Capability (" + capabilityKey + ") properties :");
\r
154 propertyDefinitionValidator.validatePropertyDefinition(stDataTypes, properties);
\r
161 @SuppressWarnings("squid:S00112")
\r
162 private boolean validateNodeTypeInterface(Map<String, InterfaceDefinition> interfaces) {
\r
163 if (interfaces != null) {
\r
164 interfaces.forEach((interfaceKey, interfaceDefinition) -> {
\r
165 if (interfaceDefinition != null && interfaceDefinition.getOperations() != null) {
\r
166 validateNodeTypeInterfaceOperation(interfaceDefinition.getOperations());
\r
173 @SuppressWarnings("squid:S00112")
\r
174 private boolean validateNodeTypeInterfaceOperation(Map<String, OperationDefinition> operations) {
\r
175 if (operations != null) {
\r
176 operations.forEach((operationKey, operation) -> {
\r
177 if (operation != null) {
\r
178 Map<String, PropertyDefinition> inputs = operation.getInputs();
\r
179 message.append("\n Validation Operation (" + operationKey + ") Inputs :");
\r
180 propertyDefinitionValidator.validatePropertyDefinition(stDataTypes, inputs);
\r
181 message.append("\n Validation Operation (" + operationKey + ") output :");
\r
182 Map<String, PropertyDefinition> outputs = operation.getOutputs();
\r
183 propertyDefinitionValidator.validatePropertyDefinition(stDataTypes, outputs);
\r
190 @SuppressWarnings("squid:S00112")
\r
191 private boolean validateNodeTypeRequirement(Map<String, RequirementDefinition> requirements) {
\r
192 if (requirements != null) {
\r
193 requirements.forEach((requirementKey, requirement) -> {
\r
194 if (requirement != null) {
\r
195 String nodeTypeName = requirement.getNode();
\r
196 String capabilityName = requirement.getCapability();
\r
198 checkCapabilityPresentInNodeType(nodeTypeName, capabilityName);
\r
199 } catch (ConfigModelException e) {
\r
200 throw new RuntimeException(e);
\r
208 private boolean checkCapabilityPresentInNodeType(String nodeTypeName, String capabilityName)
\r
209 throws ConfigModelException {
\r
210 if (StringUtils.isNotBlank(nodeTypeName) && StringUtils.isNotBlank(capabilityName)) {
\r
212 if (!stNodeTypes.containsKey(nodeTypeName)) {
\r
213 throw new ConfigModelException(nodeTypeName + " Node Type not Defined.");
\r
215 message.append("\n Node Type (" + nodeTypeName + ") Defined.");
\r
218 NodeType relationalNodeType = stNodeTypes.get(nodeTypeName);
\r
220 if (relationalNodeType.getCapabilities() == null) {
\r
221 throw new ConfigModelException(
\r
222 "Node Type (" + nodeTypeName + "), doesn't have Capability Definitions.");
\r
225 if (!relationalNodeType.getCapabilities().containsKey(capabilityName)) {
\r
226 throw new ConfigModelException("Node Type (" + nodeTypeName + ") doesn't have (" + capabilityName
\r
227 + ") Capability Definitions.");
\r
230 "\n Node Type (" + nodeTypeName + ") has (" + capabilityName + ") Capability Definitions.");
\r