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.features.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.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
35 import com.att.eelf.configuration.EELFLogger;
\r
36 import com.att.eelf.configuration.EELFManager;
\r
39 * NodeTypeValidator.java Purpose: Provide Configuration Generator NodeTypeValidator
\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
52 * This is a NodeTypeValidator
\r
54 * @param serviceTemplate
\r
55 * @throws ConfigModelException
\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
67 private void loadInitial() {
\r
68 if (serviceTemplate != null) {
\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
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
89 * This is a validateNodeTypes to validate the Node Type
\r
92 * @throws ConfigModelException
\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
102 validateNodeType(ConfigModelConstant.MODEL_DEFINITION_TYPE_NODE_TYPE,
\r
103 nodeType.getDerivedFrom());
\r
105 if (nodeType.getProperties() != null) {
\r
106 checkValidProperties(nodeType.getProperties());
\r
109 if (nodeType.getCapabilities() != null) {
\r
110 validateNodeTypeCapabilities(nodeType.getCapabilities());
\r
113 if (nodeType.getInterfaces() != null) {
\r
114 validateNodeTypeInterface(nodeType.getInterfaces());
\r
117 if (nodeType.getRequirements() != null) {
\r
118 validateNodeTypeRequirement(nodeType.getRequirements());
\r
121 } catch (ConfigModelException e) {
\r
122 logger.error(e.getMessage());
\r
123 throw new RuntimeException(e.getMessage());
\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
141 private boolean checkValidProperties(Map<String, PropertyDefinition> properties) {
\r
142 if (properties != null) {
\r
143 propertyDefinitionValidator.validatePropertyDefinition(stDataTypes, properties);
\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
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
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
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
199 checkCapabilityPresentInNodeType(nodeTypeName, capabilityName);
\r
200 } catch (ConfigModelException e) {
\r
201 throw new RuntimeException(e);
\r
209 private boolean checkCapabilityPresentInNodeType(String nodeTypeName, String capabilityName)
\r
210 throws ConfigModelException {
\r
211 if (StringUtils.isNotBlank(nodeTypeName) && StringUtils.isNotBlank(capabilityName)) {
\r
213 if (!stNodeTypes.containsKey(nodeTypeName)) {
\r
214 throw new ConfigModelException(nodeTypeName + " Node Type not Defined.");
\r
216 message.append("\n Node Type (" + nodeTypeName + ") Defined.");
\r
219 NodeType relationalNodeType = stNodeTypes.get(nodeTypeName);
\r
221 if (relationalNodeType.getCapabilities() == null) {
\r
222 throw new ConfigModelException(
\r
223 "Node Type (" + nodeTypeName + "), doesn't have Capability Definitions.");
\r
226 if (!relationalNodeType.getCapabilities().containsKey(capabilityName)) {
\r
227 throw new ConfigModelException("Node Type (" + nodeTypeName + ") doesn't have (" + capabilityName
\r
228 + ") Capability Definitions.");
\r
231 "\n Node Type (" + nodeTypeName + ") has (" + capabilityName + ") Capability Definitions.");
\r