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