diff options
author | Singal, Kapil (ks220y) <ks220y@att.com> | 2018-09-04 22:23:23 -0400 |
---|---|---|
committer | Singal, Kapil (ks220y) <ks220y@att.com> | 2018-09-04 22:23:23 -0400 |
commit | 1a990c52d7269a35052c874c0d971d848f86fac5 (patch) | |
tree | d90e9c9f50e6446979dc898fb1b3609239873485 /blueprints-processor/plugin | |
parent | 1ebe1fde1c94bc8c3bdf794465c2244fda38c6b0 (diff) |
SDNC Blueprints Processor Model Validator
Creating SDN Controller Blueprints Processor Model Validator API
Change-Id: I0df5ade443cacf92fac22c180e5e3159447b02d8
Issue-ID: CCSDK-499
Signed-off-by: Singal, Kapil (ks220y) <ks220y@att.com>
Diffstat (limited to 'blueprints-processor/plugin')
6 files changed, 930 insertions, 0 deletions
diff --git a/blueprints-processor/plugin/model-provider/src/main/java/org/onap/ccsdk/config/model/validator/DataTypeValidator.java b/blueprints-processor/plugin/model-provider/src/main/java/org/onap/ccsdk/config/model/validator/DataTypeValidator.java new file mode 100644 index 000000000..c603040a3 --- /dev/null +++ b/blueprints-processor/plugin/model-provider/src/main/java/org/onap/ccsdk/config/model/validator/DataTypeValidator.java @@ -0,0 +1,109 @@ +/*
+ * Copyright © 2017-2018 AT&T Intellectual Property.
+ * Modifications Copyright © 2018 IBM.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onap.ccsdk.config.model.validator;
+
+import java.util.HashMap;
+import java.util.Map;
+import org.apache.commons.lang3.StringUtils;
+import org.onap.ccsdk.config.model.ConfigModelException;
+import org.onap.ccsdk.config.model.ValidTypes;
+import org.onap.ccsdk.config.model.data.DataType;
+import org.onap.ccsdk.config.model.data.PropertyDefinition;
+import org.onap.ccsdk.config.model.data.ServiceTemplate;
+
+/**
+ * DataTypeValidator.java Purpose: Provide Configuration Generator DataTypeValidator
+ *
+ * @version 1.0
+ */
+public class DataTypeValidator {
+
+ private StringBuilder message;
+ private Map<String, DataType> stDataTypes;
+ private ServiceTemplate serviceTemplate;
+ private PropertyDefinitionValidator propertyDefinitionValidator;
+
+ /**
+ * This is a DataTypeValidator
+ *
+ * @param serviceTemplate
+ * @throws ConfigModelException
+ */
+ public DataTypeValidator(ServiceTemplate serviceTemplate, StringBuilder message) throws ConfigModelException {
+ this.serviceTemplate = serviceTemplate;
+ this.message = message;
+ propertyDefinitionValidator = new PropertyDefinitionValidator(this.message);
+ stDataTypes = new HashMap<>();
+ loadInitial();
+
+ }
+
+ private void loadInitial() {
+ if (serviceTemplate != null && serviceTemplate.getDataTypes() != null) {
+ message.append("\n DataTypes" + serviceTemplate.getDataTypes());
+ serviceTemplate.getDataTypes().forEach((dataTypeKey, dataType) -> {
+ stDataTypes.put(dataTypeKey, dataType);
+ message.append("\n Data Type (" + dataTypeKey + ") loaded successfully.");
+ });
+ }
+ }
+
+ /**
+ * This is a validateDataTypes
+ *
+ * @return boolean
+ * @throws ConfigModelException
+ */
+ @SuppressWarnings("squid:S00112")
+ public boolean validateDataTypes() {
+ if (serviceTemplate != null && serviceTemplate.getDataTypes() != null) {
+
+ serviceTemplate.getDataTypes().forEach((dataTypeKey, dataType) -> {
+ if (dataType != null) {
+ try {
+ String derivedFrom = dataType.getDerivedFrom();
+ checkValidDerivedType(dataTypeKey, derivedFrom);
+ checkValidProperties(dataTypeKey, dataType.getProperties());
+ } catch (ConfigModelException e) {
+ throw new RuntimeException(e);
+ }
+
+ }
+
+ });
+ }
+ return true;
+ }
+
+ private boolean checkValidDerivedType(String dataTypeName, String derivedFrom) throws ConfigModelException {
+
+ if (StringUtils.isBlank(derivedFrom) || !ValidTypes.getValidDataTypeDerivedFrom().contains(derivedFrom)) {
+ throw new ConfigModelException(derivedFrom + " is not a valid derived type for Data type " + dataTypeName);
+ }
+ return true;
+ }
+
+ private boolean checkValidProperties(String dataTypeName, Map<String, PropertyDefinition> properties) {
+ if (properties != null) {
+ message.append("\n validation Data Type (" + dataTypeName + ") Property.");
+ propertyDefinitionValidator.validatePropertyDefinition(stDataTypes, properties);
+ }
+ return true;
+ }
+
+}
diff --git a/blueprints-processor/plugin/model-provider/src/main/java/org/onap/ccsdk/config/model/validator/NodeTypeValidator.java b/blueprints-processor/plugin/model-provider/src/main/java/org/onap/ccsdk/config/model/validator/NodeTypeValidator.java new file mode 100644 index 000000000..f46d6483c --- /dev/null +++ b/blueprints-processor/plugin/model-provider/src/main/java/org/onap/ccsdk/config/model/validator/NodeTypeValidator.java @@ -0,0 +1,237 @@ +/*
+ * Copyright © 2017-2018 AT&T Intellectual Property.
+ * Modifications Copyright © 2018 IBM.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onap.ccsdk.config.model.validator;
+
+import java.util.HashMap;
+import java.util.Map;
+import org.apache.commons.lang3.StringUtils;
+import org.onap.ccsdk.config.model.ConfigModelConstant;
+import org.onap.ccsdk.config.model.ConfigModelException;
+import org.onap.ccsdk.config.model.ValidTypes;
+import org.onap.ccsdk.config.model.data.CapabilityDefinition;
+import org.onap.ccsdk.config.model.data.DataType;
+import org.onap.ccsdk.config.model.data.InterfaceDefinition;
+import org.onap.ccsdk.config.model.data.NodeType;
+import org.onap.ccsdk.config.model.data.OperationDefinition;
+import org.onap.ccsdk.config.model.data.PropertyDefinition;
+import org.onap.ccsdk.config.model.data.RequirementDefinition;
+import org.onap.ccsdk.config.model.data.ServiceTemplate;
+import com.att.eelf.configuration.EELFLogger;
+import com.att.eelf.configuration.EELFManager;
+
+/**
+ * NodeTypeValidator.java Purpose: Provide Configuration Generator NodeTypeValidator
+ *
+ * @version 1.0
+ */
+public class NodeTypeValidator {
+ private static EELFLogger logger = EELFManager.getInstance().getLogger(NodeTypeValidator.class);
+ private StringBuilder message;
+ private Map<String, DataType> stDataTypes;
+ private Map<String, NodeType> stNodeTypes;
+ private ServiceTemplate serviceTemplate;
+ private PropertyDefinitionValidator propertyDefinitionValidator;
+
+ /**
+ * This is a NodeTypeValidator
+ *
+ * @param serviceTemplate
+ * @throws ConfigModelException
+ */
+ public NodeTypeValidator(ServiceTemplate serviceTemplate, StringBuilder message) throws ConfigModelException {
+ this.serviceTemplate = serviceTemplate;
+ this.message = message;
+ propertyDefinitionValidator = new PropertyDefinitionValidator(this.message);
+ stDataTypes = new HashMap<>();
+ stNodeTypes = new HashMap<>();
+ loadInitial();
+
+ }
+
+ private void loadInitial() {
+ if (serviceTemplate != null) {
+
+ if (serviceTemplate.getDataTypes() != null) {
+ serviceTemplate.getDataTypes().forEach((dataTypeKey, dataType) -> {
+ stDataTypes.put(dataTypeKey, dataType);
+ logger.trace("Data Type ({}) loaded successfully.", dataTypeKey);
+ });
+ }
+
+ if (serviceTemplate.getNodeTypes() != null) {
+ serviceTemplate.getNodeTypes().forEach((nodeTypeKey, nodeType) -> {
+ stNodeTypes.put(nodeTypeKey, nodeType);
+ logger.trace("NodeType Type ({}) loaded successfully.", nodeTypeKey);
+ });
+ }
+
+ }
+
+ }
+
+ /**
+ * This is a validateNodeTypes to validate the Node Type
+ *
+ * @return boolean
+ * @throws ConfigModelException
+ */
+ @SuppressWarnings({"squid:S00112", "squid:S3776"})
+ public boolean validateNodeTypes() {
+ if (serviceTemplate != null && serviceTemplate.getNodeTypes() != null) {
+ serviceTemplate.getNodeTypes().forEach((nodeTypeKey, nodeType) -> {
+ if (nodeType != null) {
+ message.append("\n ***** Validation Node Type (" + nodeTypeKey + "), derived from ("
+ + nodeType.getDerivedFrom() + ")");
+ try {
+ validateNodeType(ConfigModelConstant.MODEL_DEFINITION_TYPE_NODE_TYPE,
+ nodeType.getDerivedFrom());
+
+ if (nodeType.getProperties() != null) {
+ checkValidProperties(nodeType.getProperties());
+ }
+
+ if (nodeType.getCapabilities() != null) {
+ validateNodeTypeCapabilities(nodeType.getCapabilities());
+ }
+
+ if (nodeType.getInterfaces() != null) {
+ validateNodeTypeInterface(nodeType.getInterfaces());
+ }
+
+ if (nodeType.getRequirements() != null) {
+ validateNodeTypeRequirement(nodeType.getRequirements());
+ }
+
+ } catch (ConfigModelException e) {
+ logger.error(e.getMessage());
+ throw new RuntimeException(e.getMessage());
+ }
+
+ }
+ });
+ }
+ return true;
+ }
+
+ private boolean validateNodeType(String definitionType, String derivedFrom) throws ConfigModelException {
+ boolean valid = true;
+ if (!ConfigModelConstant.MODEL_DEFINITION_TYPE_DATA_TYPE.equalsIgnoreCase(definitionType)
+ && !ValidTypes.getValidNodeTypes().contains(derivedFrom)) {
+ throw new ConfigModelException("Not Valid Model Type (" + derivedFrom + ")");
+ }
+ return valid;
+ }
+
+ private boolean checkValidProperties(Map<String, PropertyDefinition> properties) {
+ if (properties != null) {
+ propertyDefinitionValidator.validatePropertyDefinition(stDataTypes, properties);
+ }
+ return true;
+ }
+
+ @SuppressWarnings("squid:S00112")
+ private boolean validateNodeTypeCapabilities(Map<String, CapabilityDefinition> capabilities) {
+ if (capabilities != null) {
+ capabilities.forEach((capabilityKey, capability) -> {
+ if (capability != null) {
+ Map<String, PropertyDefinition> properties = capability.getProperties();
+ message.append("\n Validation Capability (" + capabilityKey + ") properties :");
+ propertyDefinitionValidator.validatePropertyDefinition(stDataTypes, properties);
+ }
+ });
+ }
+ return true;
+ }
+
+ @SuppressWarnings("squid:S00112")
+ private boolean validateNodeTypeInterface(Map<String, InterfaceDefinition> interfaces) {
+ if (interfaces != null) {
+ interfaces.forEach((interfaceKey, interfaceDefinition) -> {
+ if (interfaceDefinition != null && interfaceDefinition.getOperations() != null) {
+ validateNodeTypeInterfaceOperation(interfaceDefinition.getOperations());
+ }
+ });
+ }
+ return true;
+ }
+
+ @SuppressWarnings("squid:S00112")
+ private boolean validateNodeTypeInterfaceOperation(Map<String, OperationDefinition> operations) {
+ if (operations != null) {
+ operations.forEach((operationKey, operation) -> {
+ if (operation != null) {
+ Map<String, PropertyDefinition> inputs = operation.getInputs();
+ message.append("\n Validation Operation (" + operationKey + ") Inputs :");
+ propertyDefinitionValidator.validatePropertyDefinition(stDataTypes, inputs);
+ message.append("\n Validation Operation (" + operationKey + ") output :");
+ Map<String, PropertyDefinition> outputs = operation.getOutputs();
+ propertyDefinitionValidator.validatePropertyDefinition(stDataTypes, outputs);
+ }
+ });
+ }
+ return true;
+ }
+
+ @SuppressWarnings("squid:S00112")
+ private boolean validateNodeTypeRequirement(Map<String, RequirementDefinition> requirements) {
+ if (requirements != null) {
+ requirements.forEach((requirementKey, requirement) -> {
+ if (requirement != null) {
+ String nodeTypeName = requirement.getNode();
+ String capabilityName = requirement.getCapability();
+ try {
+ checkCapabilityPresentInNodeType(nodeTypeName, capabilityName);
+ } catch (ConfigModelException e) {
+ throw new RuntimeException(e);
+ }
+ }
+ });
+ }
+ return true;
+ }
+
+ private boolean checkCapabilityPresentInNodeType(String nodeTypeName, String capabilityName)
+ throws ConfigModelException {
+ if (StringUtils.isNotBlank(nodeTypeName) && StringUtils.isNotBlank(capabilityName)) {
+
+ if (!stNodeTypes.containsKey(nodeTypeName)) {
+ throw new ConfigModelException(nodeTypeName + " Node Type not Defined.");
+ } else {
+ message.append("\n Node Type (" + nodeTypeName + ") Defined.");
+ }
+
+ NodeType relationalNodeType = stNodeTypes.get(nodeTypeName);
+
+ if (relationalNodeType.getCapabilities() == null) {
+ throw new ConfigModelException(
+ "Node Type (" + nodeTypeName + "), doesn't have Capability Definitions.");
+ }
+
+ if (!relationalNodeType.getCapabilities().containsKey(capabilityName)) {
+ throw new ConfigModelException("Node Type (" + nodeTypeName + ") doesn't have (" + capabilityName
+ + ") Capability Definitions.");
+ } else {
+ message.append(
+ "\n Node Type (" + nodeTypeName + ") has (" + capabilityName + ") Capability Definitions.");
+ }
+
+ }
+ return true;
+ }
+
+}
diff --git a/blueprints-processor/plugin/model-provider/src/main/java/org/onap/ccsdk/config/model/validator/PropertyDefinitionValidator.java b/blueprints-processor/plugin/model-provider/src/main/java/org/onap/ccsdk/config/model/validator/PropertyDefinitionValidator.java new file mode 100644 index 000000000..dc8638349 --- /dev/null +++ b/blueprints-processor/plugin/model-provider/src/main/java/org/onap/ccsdk/config/model/validator/PropertyDefinitionValidator.java @@ -0,0 +1,89 @@ +/*
+ * Copyright © 2017-2018 AT&T Intellectual Property.
+ * Modifications Copyright © 2018 IBM.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onap.ccsdk.config.model.validator;
+
+import java.util.Map;
+import org.apache.commons.lang3.StringUtils;
+import org.onap.ccsdk.config.model.ConfigModelException;
+import org.onap.ccsdk.config.model.ValidTypes;
+import org.onap.ccsdk.config.model.data.DataType;
+import org.onap.ccsdk.config.model.data.PropertyDefinition;
+
+/**
+ * PropertyDefinitionValidator.java Purpose: Provide Configuration Generator
+ * PropertyDefinitionValidator
+ *
+ * @version 1.0
+ */
+public class PropertyDefinitionValidator {
+
+ StringBuilder message = new StringBuilder();
+
+ public PropertyDefinitionValidator(StringBuilder message) {
+ this.message = message;
+
+ }
+
+ /**
+ * This is a validatePropertyDefinition stored in database
+ *
+ * @param stDataTypes
+ * @param properties
+ * @return boolean
+ * @throws ConfigModelException
+ */
+ @SuppressWarnings({"squid:S00112", "squid:S3776", "squid:S1192"})
+ public boolean validatePropertyDefinition(Map<String, DataType> stDataTypes,
+ Map<String, PropertyDefinition> properties) {
+
+ if (stDataTypes != null && properties != null) {
+ properties.forEach((propertyKey, prop) -> {
+ if (propertyKey != null && prop != null) {
+ try {
+ String propertType = prop.getType();
+ message.append("\n Validating (" + propertyKey + ") " + prop);
+
+ if (!ValidTypes.getValidPropertType().contains(propertType)
+ && !stDataTypes.containsKey(propertType)) {
+ throw new ConfigModelException("Data Type (" + propertyKey + ") -> type(" + propertType
+ + ") is not a valid type.");
+ } else if (ValidTypes.getListPropertType().contains(propertType)) {
+ if (prop.getEntrySchema() == null || StringUtils.isBlank(prop.getEntrySchema().getType())) {
+ throw new ConfigModelException("Data Type (" + propertyKey + ") -> type (" + propertType
+ + ") Entity Schema is not defined.");
+ }
+
+ String entitySchemaType = prop.getEntrySchema().getType();
+
+ if (!ValidTypes.getValidPropertType().contains(entitySchemaType)
+ && !stDataTypes.containsKey(entitySchemaType)) {
+ message.append("\n Present Data Type " + stDataTypes);
+ throw new ConfigModelException("Data Type (" + propertyKey + ") -> type(" + propertType
+ + ") -> entitySchema(" + entitySchemaType + ") is not defined.");
+ }
+ }
+ } catch (ConfigModelException e) {
+ throw new RuntimeException(e);
+ }
+ }
+ });
+ }
+ return true;
+ }
+
+}
diff --git a/blueprints-processor/plugin/model-provider/src/main/java/org/onap/ccsdk/config/model/validator/ResourceAssignmentValidator.java b/blueprints-processor/plugin/model-provider/src/main/java/org/onap/ccsdk/config/model/validator/ResourceAssignmentValidator.java new file mode 100644 index 000000000..cd486cf7f --- /dev/null +++ b/blueprints-processor/plugin/model-provider/src/main/java/org/onap/ccsdk/config/model/validator/ResourceAssignmentValidator.java @@ -0,0 +1,163 @@ +/*
+ * Copyright © 2017-2018 AT&T Intellectual Property.
+ * Modifications Copyright © 2018 IBM.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onap.ccsdk.config.model.validator;
+
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import org.apache.commons.lang3.StringUtils;
+import org.onap.ccsdk.config.model.ConfigModelConstant;
+import org.onap.ccsdk.config.model.ConfigModelException;
+import org.onap.ccsdk.config.model.data.CapabilityAssignment;
+import org.onap.ccsdk.config.model.data.NodeTemplate;
+import org.onap.ccsdk.config.model.data.ResourceAssignment;
+import org.onap.ccsdk.config.model.utils.TopologicalSortingUtils;
+import org.onap.ccsdk.config.model.utils.TransformationUtils;
+import com.att.eelf.configuration.EELFLogger;
+import com.att.eelf.configuration.EELFManager;
+
+public class ResourceAssignmentValidator {
+ private static EELFLogger logger = EELFManager.getInstance().getLogger(ResourceAssignmentValidator.class);
+ private List<ResourceAssignment> assignments;
+ private Map<String, ResourceAssignment> resourceAssignmentMap = new HashMap<>();
+ private StringBuilder validationMessage = new StringBuilder();
+
+ public ResourceAssignmentValidator(NodeTemplate nodeTemplate) throws ConfigModelException {
+
+ if (nodeTemplate != null && nodeTemplate.getCapabilities() != null) {
+ CapabilityAssignment capabilityAssignment =
+ nodeTemplate.getCapabilities().get(ConfigModelConstant.CAPABILITY_PROPERTY_MAPPING);
+ if (capabilityAssignment != null && capabilityAssignment.getProperties() != null) {
+ Object mappingObject =
+ capabilityAssignment.getProperties().get(ConfigModelConstant.CAPABILITY_PROPERTY_MAPPING);
+ if (mappingObject != null) {
+ String mappingContent = TransformationUtils.getJson(mappingObject);
+ if (StringUtils.isNotBlank(mappingContent)) {
+ this.assignments =
+ TransformationUtils.getListfromJson(mappingContent, ResourceAssignment.class);
+ } else {
+ validationMessage
+ .append(String.format("Failed to transform Mapping Content (%s) ", mappingContent));
+ throw new ConfigModelException(
+ String.format("Failed to transform Mapping Content (%s) ", mappingContent));
+ }
+ }
+ }
+ }
+ }
+
+ public ResourceAssignmentValidator(List<ResourceAssignment> assignments) {
+ this.assignments = assignments;
+ }
+
+ /**
+ * This is a validateResourceAssignment to validate the Topology Template
+ *
+ * @return boolean
+ * @throws ConfigModelException
+ */
+ public boolean validateResourceAssignment() throws ConfigModelException {
+ if (assignments != null && !assignments.isEmpty()) {
+ validateDuplicateDictionaryKeys();
+ validateCyclicDependencty();
+ if (validationMessage.length() > 0) {
+ logger.error("Resourece Assignment Validation : {}", validationMessage);
+ throw new ConfigModelException("Resourece Assignment Validation :" + validationMessage.toString());
+ }
+ }
+ return true;
+ }
+
+ @SuppressWarnings("squid:S3776")
+ private void validateDuplicateDictionaryKeys() {
+ this.assignments.forEach(resourceMapping -> {
+ if (resourceMapping != null) {
+ if (!resourceAssignmentMap.containsKey(resourceMapping.getName())) {
+ resourceAssignmentMap.put(resourceMapping.getName(), resourceMapping);
+ } else {
+ validationMessage.append(String.format("Duplicate Assignment Template Key (%s) is Present",
+ resourceMapping.getName()));
+ }
+ }
+ });
+
+ if (!assignments.isEmpty()) {
+ Set<String> uniqueSet = new HashSet<>();
+ for (ResourceAssignment resourceAssignment : assignments) {
+ if (resourceAssignment != null) {
+ boolean added = uniqueSet.add(resourceAssignment.getDictionaryName());
+ if (!added) {
+ validationMessage.append(
+ String.format("Duplicate Assignment Dictionary Key (%s) present with Template Key (%s)",
+ resourceAssignment.getDictionaryName(), resourceAssignment.getName()));
+ }
+ }
+ }
+ }
+ }
+
+ private void validateCyclicDependencty() {
+ TopologicalSortingUtils<ResourceAssignment> topologySorting = new TopologicalSortingUtils<>();
+ this.resourceAssignmentMap.forEach((mappingKey, mapping) -> {
+ if (mapping != null) {
+ if (mapping.getDependencies() != null && !mapping.getDependencies().isEmpty()) {
+ for (String dependency : mapping.getDependencies()) {
+ topologySorting.add(resourceAssignmentMap.get(dependency), mapping);
+ }
+ } else {
+ topologySorting.add(null, mapping);
+ }
+ }
+ });
+
+ if (!topologySorting.isDag()) {
+ String graph = getTopologicalGraph(topologySorting);
+ validationMessage.append("Cyclic Dependency :" + graph);
+ }
+ }
+
+ public String getTopologicalGraph(TopologicalSortingUtils<ResourceAssignment> topologySorting) {
+ StringBuilder s = new StringBuilder();
+ if (topologySorting != null) {
+ Map<ResourceAssignment, List<ResourceAssignment>> neighbors = topologySorting.getNeighbors();
+
+ neighbors.forEach((v, vs) -> {
+ if (v == null) {
+ s.append("\n * -> [");
+ List<ResourceAssignment> links = vs;
+ for (ResourceAssignment resourceAssignment : links) {
+ s.append("(" + resourceAssignment.getDictionaryName() + ":" + resourceAssignment.getName()
+ + "),");
+ }
+ s.append("]");
+ } else {
+ s.append("\n (" + v.getDictionaryName() + ":" + v.getName() + ") -> [");
+ List<ResourceAssignment> links = vs;
+ for (ResourceAssignment resourceAssignment : links) {
+ s.append("(" + resourceAssignment.getDictionaryName() + ":" + resourceAssignment.getName()
+ + "),");
+ }
+ s.append("]");
+ }
+ });
+ }
+ return s.toString();
+ }
+}
diff --git a/blueprints-processor/plugin/model-provider/src/main/java/org/onap/ccsdk/config/model/validator/ServiceTemplateValidator.java b/blueprints-processor/plugin/model-provider/src/main/java/org/onap/ccsdk/config/model/validator/ServiceTemplateValidator.java new file mode 100644 index 000000000..c329c7fc1 --- /dev/null +++ b/blueprints-processor/plugin/model-provider/src/main/java/org/onap/ccsdk/config/model/validator/ServiceTemplateValidator.java @@ -0,0 +1,129 @@ +/*
+ * Copyright © 2017-2018 AT&T Intellectual Property.
+ * Modifications Copyright © 2018 IBM.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onap.ccsdk.config.model.validator;
+
+import java.util.HashMap;
+import java.util.Map;
+import org.apache.commons.lang3.StringUtils;
+import org.onap.ccsdk.config.model.ConfigModelConstant;
+import org.onap.ccsdk.config.model.ConfigModelException;
+import org.onap.ccsdk.config.model.data.ServiceTemplate;
+import org.onap.ccsdk.config.model.utils.TransformationUtils;
+import com.att.eelf.configuration.EELFLogger;
+import com.att.eelf.configuration.EELFManager;
+
+/**
+ * ServiceTemplateValidator.java Purpose: Provide Configuration Generator ServiceTemplateValidator
+ *
+ * @version 1.0
+ */
+
+public class ServiceTemplateValidator {
+
+ private static EELFLogger logger = EELFManager.getInstance().getLogger(ServiceTemplateValidator.class);
+
+ private Map<String, String> metaData = new HashMap<>();
+
+ StringBuilder message = new StringBuilder();
+
+ /**
+ * This is a validateServiceTemplate
+ *
+ * @param serviceTemplateContent
+ * @return boolean
+ * @throws ConfigModelException
+ */
+ @SuppressWarnings("squid:S00112")
+ public boolean validateServiceTemplate(String serviceTemplateContent) throws ConfigModelException {
+ if (StringUtils.isNotBlank(serviceTemplateContent)) {
+ ServiceTemplate serviceTemplate =
+ TransformationUtils.readValue(serviceTemplateContent, ServiceTemplate.class);
+ return validateServiceTemplate(serviceTemplate);
+ } else {
+ throw new ConfigModelException(
+ "Service Template Content is (" + serviceTemplateContent + ") not Defined.");
+ }
+ }
+
+ /**
+ * This is a validateServiceTemplate
+ *
+ * @param serviceTemplate
+ * @return boolean
+ * @throws ConfigModelException
+ */
+ @SuppressWarnings("squid:S00112")
+ public boolean validateServiceTemplate(ServiceTemplate serviceTemplate) throws ConfigModelException {
+ if (serviceTemplate != null) {
+ try {
+ validateMetaData(serviceTemplate);
+ (new DataTypeValidator(serviceTemplate, message)).validateDataTypes();
+ (new NodeTypeValidator(serviceTemplate, message)).validateNodeTypes();
+ (new TopologyTemplateValidator(serviceTemplate, message)).validateTopologyTemplate();
+ logger.debug("Validation Message : {}", message);
+ } catch (Exception e) {
+ throw new ConfigModelException(
+ "Validation Failed " + e.toString() + ",Message Trace : \n" + message.toString());
+ }
+
+ } else {
+ throw new ConfigModelException("Service Template is not defined.");
+ }
+ return true;
+ }
+
+ /**
+ * This is a getMetaData to get the key information during the
+ *
+ * @return Map<String , String>
+ */
+ public Map<String, String> getMetaData() {
+ return metaData;
+ }
+
+ private void validateMetaData(ServiceTemplate serviceTemplate) throws ConfigModelException {
+ if (serviceTemplate.getMetadata() != null) {
+ this.metaData.putAll(serviceTemplate.getMetadata());
+
+ String author = serviceTemplate.getMetadata().get(ConfigModelConstant.SERVICE_TEMPLATE_KEY_ARTIFACT_AUTHOR);
+ String serviceTemplateName =
+ serviceTemplate.getMetadata().get(ConfigModelConstant.SERVICE_TEMPLATE_KEY_ARTIFACT_NAME);
+ String serviceTemplateVersion =
+ serviceTemplate.getMetadata().get(ConfigModelConstant.SERVICE_TEMPLATE_KEY_ARTIFACT_VERSION);
+
+ if (StringUtils.isBlank(author)) {
+ throw new ConfigModelException("Service Template Metadata (author) Information is missing.");
+ }
+
+ if (StringUtils.isBlank(serviceTemplateName)) {
+ throw new ConfigModelException(
+ "Service Template Metadata (service-template-name) Information is missing.");
+ }
+
+ if (StringUtils.isBlank(serviceTemplateVersion)) {
+ throw new ConfigModelException(
+ "Service Template Metadata (service-template-version) Information is missing.");
+ }
+
+ } else {
+ throw new ConfigModelException("Service Template Metadata Information is missing.");
+ }
+
+ }
+
+}
diff --git a/blueprints-processor/plugin/model-provider/src/main/java/org/onap/ccsdk/config/model/validator/TopologyTemplateValidator.java b/blueprints-processor/plugin/model-provider/src/main/java/org/onap/ccsdk/config/model/validator/TopologyTemplateValidator.java new file mode 100644 index 000000000..dc1e314ea --- /dev/null +++ b/blueprints-processor/plugin/model-provider/src/main/java/org/onap/ccsdk/config/model/validator/TopologyTemplateValidator.java @@ -0,0 +1,203 @@ +/*
+ * Copyright © 2017-2018 AT&T Intellectual Property.
+ * Modifications Copyright © 2018 IBM.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onap.ccsdk.config.model.validator;
+
+import java.util.HashMap;
+import java.util.Map;
+import org.apache.commons.lang3.StringUtils;
+import org.onap.ccsdk.config.model.ConfigModelConstant;
+import org.onap.ccsdk.config.model.ConfigModelException;
+import org.onap.ccsdk.config.model.data.DataType;
+import org.onap.ccsdk.config.model.data.NodeTemplate;
+import org.onap.ccsdk.config.model.data.NodeType;
+import org.onap.ccsdk.config.model.data.PropertyDefinition;
+import org.onap.ccsdk.config.model.data.RequirementAssignment;
+import org.onap.ccsdk.config.model.data.ServiceTemplate;
+import org.onap.ccsdk.config.model.data.TopologyTemplate;
+import com.att.eelf.configuration.EELFLogger;
+import com.att.eelf.configuration.EELFManager;
+
+/**
+ * TopologyTemplateValidator.java Purpose: Provide Configuration Generator TopologyTemplateValidator
+ *
+ * @version 1.0
+ */
+public class TopologyTemplateValidator {
+ private static EELFLogger logger = EELFManager.getInstance().getLogger(TopologyTemplateValidator.class);
+ private StringBuilder message;
+ private Map<String, DataType> stDataTypes;
+ private Map<String, NodeType> stNodeTypes;
+ private Map<String, NodeTemplate> stNodeTemplates;
+ private ServiceTemplate serviceTemplate;
+ private PropertyDefinitionValidator propertyDefinitionValidator;
+
+ /**
+ * This is a TopologyTemplateValidator
+ *
+ * @param serviceTemplate
+ * @throws ConfigModelException
+ */
+ public TopologyTemplateValidator(ServiceTemplate serviceTemplate, StringBuilder message) {
+ this.serviceTemplate = serviceTemplate;
+ this.message = message;
+ propertyDefinitionValidator = new PropertyDefinitionValidator(this.message);
+ stDataTypes = new HashMap<>();
+ stNodeTypes = new HashMap<>();
+ stNodeTemplates = new HashMap<>();
+ loadInitial();
+ }
+
+ private void loadInitial() {
+ if (serviceTemplate != null) {
+
+ if (serviceTemplate.getDataTypes() != null) {
+ serviceTemplate.getDataTypes().forEach((dataTypeKey, dataType) -> {
+ stDataTypes.put(dataTypeKey, dataType);
+ logger.trace("Data Type ({}) loaded successfully.", dataTypeKey);
+ });
+ }
+
+ if (serviceTemplate.getNodeTypes() != null) {
+ serviceTemplate.getNodeTypes().forEach((nodeTypeKey, nodeType) -> {
+ stNodeTypes.put(nodeTypeKey, nodeType);
+ logger.trace("Node Type ({}) loaded successfully.", nodeTypeKey);
+ });
+ }
+
+ if (serviceTemplate.getTopologyTemplate() != null) {
+ TopologyTemplate topologyTemplate = serviceTemplate.getTopologyTemplate();
+
+ if (topologyTemplate.getNodeTemplates() != null) {
+ topologyTemplate.getNodeTemplates().forEach((nodeTemplateKey, nodeTemplate) -> {
+ stNodeTemplates.put(nodeTemplateKey, nodeTemplate);
+ logger.trace("Node Template ({}) Type loaded successfully.", nodeTemplateKey);
+ });
+ }
+ }
+ }
+
+ }
+
+ /**
+ * This is a validateTopologyTemplate to validate the Topology Template
+ *
+ * @return boolean
+ * @throws ConfigModelException
+ */
+ public boolean validateTopologyTemplate() {
+ if (serviceTemplate != null && serviceTemplate.getTopologyTemplate() != null) {
+
+ checkValidInputProperties(serviceTemplate.getTopologyTemplate().getInputs());
+
+ validateNodeTemplates(serviceTemplate.getTopologyTemplate().getNodeTemplates());
+ }
+ return true;
+ }
+
+ private boolean checkValidInputProperties(Map<String, PropertyDefinition> properties) {
+ if (properties != null) {
+ message.append("\n Validation topology template input properties :");
+ propertyDefinitionValidator.validatePropertyDefinition(stDataTypes, properties);
+ }
+ return true;
+ }
+
+ @SuppressWarnings({"squid:S00112", "squid:S3776"})
+ private boolean validateNodeTemplates(Map<String, NodeTemplate> nodeTemplates) {
+ if (nodeTemplates != null) {
+ nodeTemplates.forEach((nodeTemplateKey, nodeTemplate) -> {
+ if (nodeTemplate != null) {
+ message.append("\n ##### Validation Node Template (" + nodeTemplateKey + "), of type ("
+ + nodeTemplate.getType() + ")");
+
+ String nodeTypeName = nodeTemplate.getType();
+ if (!stNodeTypes.containsKey(nodeTypeName)) {
+ throw new RuntimeException("Node Type (" + nodeTypeName + ")not Defined.");
+ }
+
+ if (nodeTemplate.getRequirements() != null) {
+ validateNodeTemplateRequirement(nodeTemplate.getRequirements());
+ }
+
+ // Validate Resource Assignments
+ NodeType nodeType = stNodeTypes.get(nodeTypeName);
+ if (nodeType != null
+ && ConfigModelConstant.MODEL_TYPE_NODE_ARTIFACT.equals(nodeType.getDerivedFrom())) {
+ logger.info("Validating Resource Assignment NodeTemplate ({}).", nodeTemplateKey);
+ ResourceAssignmentValidator resourceAssignmentValidator;
+ try {
+ resourceAssignmentValidator = new ResourceAssignmentValidator(nodeTemplate);
+ resourceAssignmentValidator.validateResourceAssignment();
+ } catch (ConfigModelException e) {
+ throw new RuntimeException(e);
+ }
+
+ }
+ }
+ });
+ }
+ return true;
+ }
+
+ @SuppressWarnings("squid:S00112")
+ private boolean validateNodeTemplateRequirement(Map<String, RequirementAssignment> requirements) {
+ if (requirements != null) {
+ requirements.forEach((requirementKey, requirement) -> {
+ if (requirement != null) {
+ String requirementnodeTypeName = requirement.getNode();
+ String capabilityName = requirement.getCapability();
+ try {
+ checkCapabilityPresentInNodeTemplate(requirementnodeTypeName, capabilityName);
+ } catch (ConfigModelException e) {
+ throw new RuntimeException(e);
+ }
+ }
+ });
+ }
+ return true;
+ }
+
+ private boolean checkCapabilityPresentInNodeTemplate(String nodeTemplateName, String capabilityName)
+ throws ConfigModelException {
+ if (StringUtils.isNotBlank(nodeTemplateName) && StringUtils.isNotBlank(capabilityName)) {
+
+ if (!stNodeTemplates.containsKey(nodeTemplateName)) {
+ throw new ConfigModelException(nodeTemplateName + " Node Template not Defined.");
+ } else {
+ message.append("\n Node Template (" + nodeTemplateName + ") Defined.");
+ }
+
+ NodeTemplate relationalNodeType = stNodeTemplates.get(nodeTemplateName);
+
+ if (relationalNodeType.getCapabilities() == null) {
+ throw new ConfigModelException(
+ "Node Template (" + nodeTemplateName + "), doesn't have Capability Definitions.");
+ }
+
+ if (!relationalNodeType.getCapabilities().containsKey(capabilityName)) {
+ throw new ConfigModelException("Node Type (" + nodeTemplateName + ") doesn't have (" + capabilityName
+ + ") Capability Definitions.");
+ } else {
+ message.append("\n Node Template (" + nodeTemplateName + ") has (" + capabilityName
+ + ") Capability Definitions.");
+ }
+
+ }
+ return true;
+ }
+}
|