diff options
Diffstat (limited to 'openecomp-be/lib/openecomp-tosca-lib/src')
4 files changed, 295 insertions, 110 deletions
diff --git a/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/errors/ToscaElementTypeNotFoundErrorBuilder.java b/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/errors/ToscaElementTypeNotFoundErrorBuilder.java new file mode 100644 index 0000000000..dd33bf79a2 --- /dev/null +++ b/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/errors/ToscaElementTypeNotFoundErrorBuilder.java @@ -0,0 +1,50 @@ +/* + * Copyright © 2016-2018 European Support Limited + * + * 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.openecomp.sdc.tosca.errors; + +import org.openecomp.sdc.common.errors.ErrorCategory; +import org.openecomp.sdc.common.errors.ErrorCode; + +/** + * The type Tosca data type not found error builder. + */ +public class ToscaElementTypeNotFoundErrorBuilder { + private static final String ENTRY_NOT_FOUND_MSG = + "Entity Type '%s' or one of its derivedFrom type hierarchy, " + + "is not defined in tosca service model"; + private final ErrorCode.ErrorCodeBuilder builder = new ErrorCode.ErrorCodeBuilder(); + + /** + * Instantiates a new Tosca data type not found error builder. + * + * @param elementType element type + */ + public ToscaElementTypeNotFoundErrorBuilder(String elementType) { + builder.withId(ToscaErrorCodes.TOSCA_ENTRY_NOT_FOUND); + builder.withCategory(ErrorCategory.APPLICATION); + builder.withMessage(String.format(ENTRY_NOT_FOUND_MSG, elementType)); + } + + /** + * Build error code. + * + * @return the error code + */ + public ErrorCode build() { + return builder.build(); + } +} diff --git a/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/services/ToscaAnalyzerService.java b/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/services/ToscaAnalyzerService.java index c4c154efb4..d4e7813ea2 100644 --- a/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/services/ToscaAnalyzerService.java +++ b/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/services/ToscaAnalyzerService.java @@ -1,21 +1,17 @@ -/*- - * ============LICENSE_START======================================================= - * SDC - * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. - * ================================================================================ +/* + * Copyright © 2016-2018 European Support Limited + * * 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. - * ============LICENSE_END========================================================= */ package org.openecomp.sdc.tosca.services; @@ -23,6 +19,8 @@ package org.openecomp.sdc.tosca.services; import org.openecomp.sdc.tosca.datatypes.ToscaElementTypes; import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel; import org.openecomp.sdc.tosca.datatypes.model.CapabilityDefinition; +import org.openecomp.sdc.tosca.datatypes.model.DefinitionOfDataType; +import org.openecomp.sdc.tosca.datatypes.model.InterfaceDefinitionType; import org.openecomp.sdc.tosca.datatypes.model.NodeTemplate; import org.openecomp.sdc.tosca.datatypes.model.NodeType; import org.openecomp.sdc.tosca.datatypes.model.PropertyDefinition; @@ -80,14 +78,22 @@ public interface ToscaAnalyzerService { String requirementId, RequirementAssignment requirementAssignment); - public Map<String, PropertyDefinition> manageSubstitutionNodeTypeProperties( + Map<String, PropertyDefinition> manageSubstitutionNodeTypeProperties( ServiceTemplate substitutionServiceTemplate); - public Map<String, CapabilityDefinition> calculateExposedCapabilities( + Map<String, CapabilityDefinition> calculateExposedCapabilities( Map<String, CapabilityDefinition> nodeTypeCapabilitiesDefinition, Map<String, Map<String, RequirementAssignment>> fullFilledRequirementsDefinitionMap); - public List<Map<String, RequirementDefinition>> calculateExposedRequirements( + List<Map<String, RequirementDefinition>> calculateExposedRequirements( List<Map<String, RequirementDefinition>> nodeTypeRequirementsDefinitionList, Map<String, RequirementAssignment> nodeTemplateRequirementsAssignment); + + boolean isTypeOf(InterfaceDefinitionType interfaceDefinition, String interfaceType, + ServiceTemplate + serviceTemplate, ToscaServiceModel toscaServiceModel); + + boolean isTypeOf(DefinitionOfDataType parameterDefinition, String dataType, ServiceTemplate + serviceTemplate, ToscaServiceModel toscaServiceModel); + } diff --git a/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/services/impl/ToscaAnalyzerServiceImpl.java b/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/services/impl/ToscaAnalyzerServiceImpl.java index 9d9e4a42fd..6f514c03fe 100644 --- a/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/services/impl/ToscaAnalyzerServiceImpl.java +++ b/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/services/impl/ToscaAnalyzerServiceImpl.java @@ -1,5 +1,5 @@ /* - * Copyright © 2016-2017 European Support Limited + * Copyright © 2016-2018 European Support Limited * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -22,12 +22,13 @@ import org.apache.commons.lang3.StringUtils; import org.openecomp.core.utilities.CommonMethods; import org.openecomp.sdc.common.errors.CoreException; import org.openecomp.sdc.tosca.datatypes.ToscaElementTypes; -import org.openecomp.sdc.tosca.datatypes.ToscaNodeType; import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel; import org.openecomp.sdc.tosca.datatypes.model.AttributeDefinition; import org.openecomp.sdc.tosca.datatypes.model.CapabilityDefinition; import org.openecomp.sdc.tosca.datatypes.model.CapabilityType; +import org.openecomp.sdc.tosca.datatypes.model.DefinitionOfDataType; import org.openecomp.sdc.tosca.datatypes.model.Import; +import org.openecomp.sdc.tosca.datatypes.model.InterfaceDefinitionType; import org.openecomp.sdc.tosca.datatypes.model.NodeTemplate; import org.openecomp.sdc.tosca.datatypes.model.NodeType; import org.openecomp.sdc.tosca.datatypes.model.ParameterDefinition; @@ -36,16 +37,17 @@ import org.openecomp.sdc.tosca.datatypes.model.PropertyType; import org.openecomp.sdc.tosca.datatypes.model.RequirementAssignment; import org.openecomp.sdc.tosca.datatypes.model.RequirementDefinition; import org.openecomp.sdc.tosca.datatypes.model.ServiceTemplate; +import org.openecomp.sdc.tosca.errors.ToscaElementTypeNotFoundErrorBuilder; import org.openecomp.sdc.tosca.errors.ToscaInvalidEntryNotFoundErrorBuilder; import org.openecomp.sdc.tosca.errors.ToscaInvalidSubstituteNodeTemplatePropertiesErrorBuilder; import org.openecomp.sdc.tosca.errors.ToscaInvalidSubstitutionServiceTemplateErrorBuilder; -import org.openecomp.sdc.tosca.errors.ToscaNodeTypeNotFoundErrorBuilder; import org.openecomp.sdc.tosca.services.DataModelUtil; import org.openecomp.sdc.tosca.services.ToscaAnalyzerService; import org.openecomp.sdc.tosca.services.ToscaConstants; import org.openecomp.sdc.tosca.services.ToscaExtensionYamlUtil; import org.openecomp.sdc.tosca.services.ToscaUtil; +import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; @@ -58,6 +60,13 @@ import java.util.Optional; import java.util.Set; public class ToscaAnalyzerServiceImpl implements ToscaAnalyzerService { + private final String GET_NODE_TYPE_METHOD_NAME = "getNode_types"; + private final String GET_DERIVED_FROM_METHOD_NAME = "getDerived_from"; + private final String GET_TYPE_METHOD_NAME = "getType"; + private final String GET_DATA_TYPE_METHOD_NAME = "getData_types"; + private final String GET_INTERFACE_TYPE_METHOD_NAME = "getInterface_types"; + private final String TOSCA_DOT = "tosca."; + private final String DOT_ROOT = ".Root"; @Override public List<Map<String, RequirementDefinition>> calculateExposedRequirements( @@ -145,7 +154,6 @@ public class ToscaAnalyzerServiceImpl implements ToscaAnalyzerService { for (Map.Entry<String, RequirementAssignment> fullFilledEntry : entry.getValue().entrySet()) { capability = fullFilledEntry.getValue().getCapability(); - fullFilledEntry.getValue().getOccurrences(); node = fullFilledEntry.getValue().getNode(); capabilityKey = capability + "_" + node; CapabilityDefinition capabilityDefinition = nodeTypeCapabilitiesDefinition.get( @@ -230,19 +238,8 @@ public class ToscaAnalyzerServiceImpl implements ToscaAnalyzerService { @Override public boolean isTypeOf(NodeTemplate nodeTemplate, String nodeType, ServiceTemplate serviceTemplate, ToscaServiceModel toscaServiceModel) { - if (nodeTemplate == null) { - return false; - } - - if (isNodeTemplateOfTypeNodeType(nodeTemplate, nodeType)) { - return true; - } - - Optional<Boolean> nodeTypeExistInServiceTemplateHierarchy = - isNodeTypeExistInServiceTemplateHierarchy(nodeType, nodeTemplate.getType(), serviceTemplate, - toscaServiceModel, null); - return nodeTypeExistInServiceTemplateHierarchy.orElseThrow(() -> new CoreException( - new ToscaNodeTypeNotFoundErrorBuilder(nodeTemplate.getType()).build())); + return isTypeOf(nodeTemplate, nodeType, GET_NODE_TYPE_METHOD_NAME, serviceTemplate, + toscaServiceModel); } @Override @@ -410,7 +407,7 @@ public class ToscaAnalyzerServiceImpl implements ToscaAnalyzerService { private boolean isSameCapability(RequirementAssignment requirementAssignment, String capability) { return capability != null && (requirementAssignment.getCapability() == null - || !requirementAssignment.getCapability().equals(capability)); + || !requirementAssignment.getCapability().equals(capability)); } @Override @@ -444,39 +441,46 @@ public class ToscaAnalyzerServiceImpl implements ToscaAnalyzerService { .NODE_TEMPLATE_DIRECTIVE_SUBSTITUTABLE); } - private Optional<Boolean> isNodeTypeExistInServiceTemplateHierarchy( - String nodeTypeToMatch, - String nodeTypeToSearch, - ServiceTemplate serviceTemplate, - ToscaServiceModel toscaServiceModel, - Set<String> analyzedImportFiles) { - Map<String, NodeType> searchableNodeTypes = serviceTemplate.getNode_types(); - if (!MapUtils.isEmpty(searchableNodeTypes)) { - NodeType nodeType = searchableNodeTypes.get(nodeTypeToSearch); - if (Objects.nonNull(nodeType)) { - if (Objects.equals(nodeType.getDerived_from(), nodeTypeToMatch)) { + private <T> Optional<Boolean> isTypeExistInServiceTemplateHierarchy(String typeToMatch, + String typeToSearch, + String getTypesMethodName, + ServiceTemplate serviceTemplate, + ToscaServiceModel toscaServiceModel, + Set<String> analyzedImportFiles) + throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { + Map<String, T> searchableTypes = + (Map<String, T>) serviceTemplate.getClass().getMethod(getTypesMethodName) + .invoke(serviceTemplate); + + if (!MapUtils.isEmpty(searchableTypes)) { + T typeObject = searchableTypes.get(typeToSearch); + if (Objects.nonNull(typeObject)) { + String derivedFromTypeVal = + (String) typeObject.getClass().getMethod(GET_DERIVED_FROM_METHOD_NAME).invoke(typeObject); + if (Objects.equals(derivedFromTypeVal, typeToMatch)) { return Optional.of(true); - } else if (isNodeTypeIsToscaRoot(nodeType)) { + } else if (Objects.isNull(derivedFromTypeVal) || isTypeIsToscaRoot(derivedFromTypeVal)) { return Optional.of(false); } else { - return isNodeTypeExistInServiceTemplateHierarchy(nodeTypeToMatch, - nodeType.getDerived_from(), serviceTemplate, toscaServiceModel, null); + return isTypeExistInServiceTemplateHierarchy(typeToMatch, + derivedFromTypeVal, getTypesMethodName, serviceTemplate, toscaServiceModel, null); } } else { - return isNodeTypeExistInImports(nodeTypeToMatch, nodeTypeToSearch, serviceTemplate, - toscaServiceModel, analyzedImportFiles); + return isTypeExistInImports(typeToMatch, typeToSearch, getTypesMethodName, + serviceTemplate, toscaServiceModel, analyzedImportFiles); } } - return isNodeTypeExistInImports(nodeTypeToMatch, nodeTypeToSearch, serviceTemplate, + return isTypeExistInImports(typeToMatch, typeToSearch, getTypesMethodName, serviceTemplate, toscaServiceModel, analyzedImportFiles); - } - private Optional<Boolean> isNodeTypeExistInImports(String nodeTypeToMatch, - String nodeTypeToSearch, - ServiceTemplate serviceTemplate, - ToscaServiceModel toscaServiceModel, - Set<String> filesScanned) { + private Optional<Boolean> isTypeExistInImports(String typeToMatch, + String typeToSearch, + String getTypesMethodName, + ServiceTemplate serviceTemplate, + ToscaServiceModel toscaServiceModel, + Set<String> filesScanned) + throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { List<Map<String, Import>> imports = serviceTemplate.getImports(); if (CollectionUtils.isEmpty(imports)) { return Optional.empty(); @@ -501,11 +505,11 @@ public class ToscaAnalyzerServiceImpl implements ToscaAnalyzerService { } else { createdFilesScanned.add(ToscaUtil.getServiceTemplateFileName(template)); } - Optional<Boolean> nodeTypeExistInServiceTemplateHierarchy = - isNodeTypeExistInServiceTemplateHierarchy(nodeTypeToMatch, nodeTypeToSearch, template, - toscaServiceModel, createdFilesScanned); - if (nodeTypeExistInServiceTemplateHierarchy.isPresent() - && (nodeTypeExistInServiceTemplateHierarchy.get())) { + Optional<Boolean> typeExistInServiceTemplateHierarchy = + isTypeExistInServiceTemplateHierarchy(typeToMatch, typeToSearch, getTypesMethodName, + template, toscaServiceModel, createdFilesScanned); + if (typeExistInServiceTemplateHierarchy.isPresent() + && (typeExistInServiceTemplateHierarchy.get())) { createdFilesScanned.clear(); return Optional.of(true); } @@ -528,12 +532,8 @@ public class ToscaAnalyzerServiceImpl implements ToscaAnalyzerService { return retFileScanned; } - private boolean isNodeTypeIsToscaRoot(NodeType stNodeType) { - return Objects.equals(stNodeType.getDerived_from(), ToscaNodeType.NATIVE_ROOT); - } - - private boolean isNodeTemplateOfTypeNodeType(NodeTemplate nodeTemplate, String nodeType) { - return Objects.equals(nodeTemplate.getType(), nodeType); + private boolean isTypeIsToscaRoot(String type) { + return (type.contains(TOSCA_DOT) && type.contains(DOT_ROOT)); } private boolean isSubstitutionServiceTemplate(String substituteServiceTemplateFileName, @@ -558,7 +558,7 @@ public class ToscaAnalyzerServiceImpl implements ToscaAnalyzerService { boolean entityFound = enrichEntityFromCurrentServiceTemplate(elementType, typeId, entity, - serviceTemplate,toscaModel, filesScanned, rootScanStartInx); + serviceTemplate, toscaModel, filesScanned, rootScanStartInx); if (!entityFound) { List<Map<String, Import>> imports = serviceTemplate.getImports(); if (CollectionUtils.isEmpty(imports)) { @@ -570,7 +570,7 @@ public class ToscaAnalyzerServiceImpl implements ToscaAnalyzerService { return true; } found = isFlatEntity(importMap, entity, serviceTemplate, filesScanned, - toscaModel,elementType,typeId); + toscaModel, elementType, typeId); } return found; } @@ -841,12 +841,48 @@ public class ToscaAnalyzerServiceImpl implements ToscaAnalyzerService { RequirementAssignment requirementAssignment) { List<Map<String, RequirementAssignment>> nodeTemplateRequirements = nodeTemplate .getRequirements(); - if (nodeTemplateRequirements != null) { - return nodeTemplateRequirements.stream() - .anyMatch(requirement -> requirement.containsKey(requirementId) - && DataModelUtil.compareRequirementAssignment(requirementAssignment, - requirement.get(requirementId))); + return nodeTemplateRequirements != null && nodeTemplateRequirements.stream().anyMatch( + requirement -> requirement.containsKey(requirementId) && DataModelUtil + .compareRequirementAssignment(requirementAssignment, requirement.get(requirementId))); + } + + @Override + public boolean isTypeOf(InterfaceDefinitionType interfaceDefinition, String interfaceType, + ServiceTemplate serviceTemplate, ToscaServiceModel toscaServiceModel) { + return isTypeOf(interfaceDefinition, interfaceType, GET_INTERFACE_TYPE_METHOD_NAME, serviceTemplate, + toscaServiceModel); + } + + @Override + public boolean isTypeOf(DefinitionOfDataType parameterDefinition, String dataType, + ServiceTemplate serviceTemplate, ToscaServiceModel toscaServiceModel) { + return isTypeOf(parameterDefinition, dataType, GET_DATA_TYPE_METHOD_NAME, serviceTemplate, + toscaServiceModel); + } + + private <T> boolean isTypeOf(T object, String type, String getTypesMethodName, + ServiceTemplate serviceTemplate, + ToscaServiceModel toscaServiceModel) { + if (object == null) { + return false; + } + + try { + String objectType = (String) object.getClass().getMethod(GET_TYPE_METHOD_NAME).invoke(object); + if (Objects.equals(objectType, type)) { + return true; + } + + Optional<Boolean> typeExistInServiceTemplateHierarchy = + isTypeExistInServiceTemplateHierarchy(type, objectType, getTypesMethodName, + serviceTemplate, toscaServiceModel, null); + return typeExistInServiceTemplateHierarchy.orElseThrow(() -> new CoreException( + new ToscaElementTypeNotFoundErrorBuilder(objectType).build())); + + } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { + throw new RuntimeException(e); } - return false; } + + } diff --git a/openecomp-be/lib/openecomp-tosca-lib/src/test/java/org/openecomp/sdc/tosca/services/impl/ToscaAnalyzerServiceImplTest.java b/openecomp-be/lib/openecomp-tosca-lib/src/test/java/org/openecomp/sdc/tosca/services/impl/ToscaAnalyzerServiceImplTest.java index c62eb833a3..4e17e50e25 100644 --- a/openecomp-be/lib/openecomp-tosca-lib/src/test/java/org/openecomp/sdc/tosca/services/impl/ToscaAnalyzerServiceImplTest.java +++ b/openecomp-be/lib/openecomp-tosca-lib/src/test/java/org/openecomp/sdc/tosca/services/impl/ToscaAnalyzerServiceImplTest.java @@ -1,5 +1,5 @@ /* - * Copyright © 2016-2017 European Support Limited + * Copyright © 2016-2018 European Support Limited * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -32,10 +32,15 @@ import org.openecomp.sdc.tosca.datatypes.ToscaElementTypes; import org.openecomp.sdc.tosca.datatypes.ToscaNodeType; import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel; import org.openecomp.sdc.tosca.datatypes.model.CapabilityDefinition; -import org.openecomp.sdc.tosca.datatypes.model.CapabilityType; +import org.openecomp.sdc.tosca.datatypes.model.DataType; +import org.openecomp.sdc.tosca.datatypes.model.DefinitionOfDataType; import org.openecomp.sdc.tosca.datatypes.model.Import; +import org.openecomp.sdc.tosca.datatypes.model.InterfaceDefinitionType; +import org.openecomp.sdc.tosca.datatypes.model.InterfaceType; import org.openecomp.sdc.tosca.datatypes.model.NodeTemplate; import org.openecomp.sdc.tosca.datatypes.model.NodeType; +import org.openecomp.sdc.tosca.datatypes.model.ParameterDefinition; +import org.openecomp.sdc.tosca.datatypes.model.PropertyDefinition; import org.openecomp.sdc.tosca.datatypes.model.RequirementAssignment; import org.openecomp.sdc.tosca.datatypes.model.RequirementDefinition; import org.openecomp.sdc.tosca.datatypes.model.ServiceTemplate; @@ -50,7 +55,6 @@ import java.io.InputStream; import java.util.ArrayList; import java.util.HashMap; import java.util.List; -import java.util.ListIterator; import java.util.Map; import java.util.Optional; @@ -58,7 +62,6 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; -import static org.mockito.Matchers.anyObject; import static org.mockito.Mockito.when; @@ -83,6 +86,12 @@ public class ToscaAnalyzerServiceImplTest { @Mock private NodeTemplate nodeTemplateMock; @Mock + private ParameterDefinition parameterDefinitionMock; + @Mock + private PropertyDefinition propertyDefinitionMock; + @Mock + private InterfaceDefinitionType interfaceDefinitionMock; + @Mock private ToscaServiceModel toscaServiceModelMock; @BeforeClass @@ -112,7 +121,7 @@ public class ToscaAnalyzerServiceImplTest { ".cmaui_image", serviceTemplateFromYaml, toscaServiceModel); Assert.assertNotNull(flatEntity); - Assert.assertEquals("org.openecomp.resource.vfc.nodes.heat.nova.Server",flatEntity + Assert.assertEquals("org.openecomp.resource.vfc.nodes.heat.nova.Server", flatEntity .getDerived_from()); } } @@ -134,8 +143,8 @@ public class ToscaAnalyzerServiceImplTest { Object[] occurences1 = new Object[]{1, 1}; rd1.setOccurrences(occurences1); - nodeTypeRequirementDefinition.put("binding",rd1); - nodeTypeRequirementDefinition.put("dependency",rd); + nodeTypeRequirementDefinition.put("binding", rd1); + nodeTypeRequirementDefinition.put("dependency", rd); Map<String, Map<String, RequirementAssignment>> fullFilledRequirementsDefinition = new HashMap<>(); @@ -172,8 +181,8 @@ public class ToscaAnalyzerServiceImplTest { Object[] occurences1 = new Object[]{1, 1}; rd1.setOccurrences(occurences1); - nodeTypeRequirementDefinition.put("binding",rd1); - nodeTypeRequirementDefinition.put("dependency",rd); + nodeTypeRequirementDefinition.put("binding", rd1); + nodeTypeRequirementDefinition.put("dependency", rd); Map<String, Map<String, RequirementAssignment>> fullFilledRequirementsDefinition = new HashMap<>(); @@ -198,7 +207,7 @@ public class ToscaAnalyzerServiceImplTest { Map<String, CapabilityDefinition> nodeTypeCapabilitiesDefinition = new HashMap<>(); CapabilityDefinition cd = new CapabilityDefinition(); cd.setType("tosca.capabilities.Scalable"); - nodeTypeCapabilitiesDefinition.put("tosca.capabilities.network.Bindable_pd_server",cd); + nodeTypeCapabilitiesDefinition.put("tosca.capabilities.network.Bindable_pd_server", cd); Map<String, Map<String, RequirementAssignment>> fullFilledRequirementsDefinition = new HashMap<>(); Map<String, RequirementAssignment> nodeTemplateRequirementsAssignment = new HashMap<>(); @@ -206,7 +215,7 @@ public class ToscaAnalyzerServiceImplTest { ra.setCapability("tosca.capabilities.network.Bindable"); ra.setNode("pd_server"); ra.setRelationship("tosca.relationships.network.BindsTo"); - nodeTemplateRequirementsAssignment.put("binding",ra); + nodeTemplateRequirementsAssignment.put("binding", ra); fullFilledRequirementsDefinition.put("pd_server", nodeTemplateRequirementsAssignment); Map<String, CapabilityDefinition> exposedCapabilities = toscaAnalyzerService.calculateExposedCapabilities(nodeTypeCapabilitiesDefinition, @@ -260,18 +269,18 @@ public class ToscaAnalyzerServiceImplTest { .loadYamlFileIs("/mock/analyzerService/NestedServiceTemplateReqTest.yaml")) { ServiceTemplate - serviceTemplateFromYaml = - toscaExtensionYamlUtil.yamlToObject(yamlFile, ServiceTemplate.class); + serviceTemplateFromYaml = + toscaExtensionYamlUtil.yamlToObject(yamlFile, ServiceTemplate.class); NodeTemplate port_0 = - serviceTemplateFromYaml.getTopology_template().getNode_templates().get("cmaui_port_0"); + serviceTemplateFromYaml.getTopology_template().getNode_templates().get("cmaui_port_0"); List<RequirementAssignment> reqList = - toscaAnalyzerService.getRequirements(port_0, ToscaConstants.BINDING_REQUIREMENT_ID); + toscaAnalyzerService.getRequirements(port_0, ToscaConstants.BINDING_REQUIREMENT_ID); assertEquals(1, reqList.size()); reqList.clear(); NodeTemplate port_1 = - serviceTemplateFromYaml.getTopology_template().getNode_templates().get("cmaui1_port_1"); + serviceTemplateFromYaml.getTopology_template().getNode_templates().get("cmaui1_port_1"); reqList = toscaAnalyzerService.getRequirements(port_1, ToscaConstants.LINK_REQUIREMENT_ID); assertEquals(2, reqList.size()); @@ -334,10 +343,10 @@ public class ToscaAnalyzerServiceImplTest { substitutableNodeTemplate.ifPresent(nodeTemplate -> { Object serviceTemplateFilter = nodeTemplate.getProperties() - .get(ToscaConstants.SERVICE_TEMPLATE_FILTER_PROPERTY_NAME); + .get(ToscaConstants.SERVICE_TEMPLATE_FILTER_PROPERTY_NAME); ((Map) serviceTemplateFilter).clear(); toscaAnalyzerService - .getSubstituteServiceTemplateName("invalid2", nodeTemplate); + .getSubstituteServiceTemplateName("invalid2", nodeTemplate); }); } @@ -349,10 +358,10 @@ public class ToscaAnalyzerServiceImplTest { try (InputStream yamlFile = toscaExtensionYamlUtil .loadYamlFileIs("/mock/analyzerService/ServiceTemplateSubstituteTest.yaml")) { ServiceTemplate serviceTemplateFromYaml = - toscaExtensionYamlUtil.yamlToObject(yamlFile, ServiceTemplate.class); + toscaExtensionYamlUtil.yamlToObject(yamlFile, ServiceTemplate.class); Map<String, NodeTemplate> substitutableNodeTemplates = - toscaAnalyzerService.getSubstitutableNodeTemplates(serviceTemplateFromYaml); + toscaAnalyzerService.getSubstitutableNodeTemplates(serviceTemplateFromYaml); assertEquals(2, substitutableNodeTemplates.size()); assertNotNull(substitutableNodeTemplates.get("test_nested1")); assertNotNull(substitutableNodeTemplates.get("test_nested2")); @@ -360,15 +369,16 @@ public class ToscaAnalyzerServiceImplTest { ServiceTemplate emptyServiceTemplate = new ServiceTemplate(); emptyServiceTemplate.setTopology_template(new TopologyTemplate()); substitutableNodeTemplates = - toscaAnalyzerService.getSubstitutableNodeTemplates(emptyServiceTemplate); + toscaAnalyzerService.getSubstitutableNodeTemplates(emptyServiceTemplate); assertEquals(0, substitutableNodeTemplates.size()); } try (InputStream yamlFile = toscaExtensionYamlUtil - .loadYamlFileIs("/mock/analyzerService/NestedServiceTemplateReqTest.yaml")) { - ServiceTemplate serviceTemplateFromYaml = toscaExtensionYamlUtil.yamlToObject(yamlFile, ServiceTemplate.class); + .loadYamlFileIs("/mock/analyzerService/NestedServiceTemplateReqTest.yaml")) { + ServiceTemplate serviceTemplateFromYaml = + toscaExtensionYamlUtil.yamlToObject(yamlFile, ServiceTemplate.class); Map<String, NodeTemplate> substitutableNodeTemplates = - toscaAnalyzerService.getSubstitutableNodeTemplates(serviceTemplateFromYaml); + toscaAnalyzerService.getSubstitutableNodeTemplates(serviceTemplateFromYaml); assertEquals(0, substitutableNodeTemplates.size()); } } @@ -382,11 +392,11 @@ public class ToscaAnalyzerServiceImplTest { try (InputStream yamlFile = toscaExtensionYamlUtil .loadYamlFileIs("/mock/analyzerService/NestedServiceTemplateReqTest.yaml")) { ServiceTemplate nestedServiceTemplateFromYaml = - toscaExtensionYamlUtil.yamlToObject(yamlFile, ServiceTemplate.class); + toscaExtensionYamlUtil.yamlToObject(yamlFile, ServiceTemplate.class); Optional<Map.Entry<String, NodeTemplate>> mappedNodeTemplate = toscaAnalyzerService - .getSubstitutionMappedNodeTemplateByExposedReq("NestedServiceTemplateSubstituteTest.yaml", - nestedServiceTemplateFromYaml, "local_storage_server_cmaui"); + .getSubstitutionMappedNodeTemplateByExposedReq("NestedServiceTemplateSubstituteTest.yaml", + nestedServiceTemplateFromYaml, "local_storage_server_cmaui"); assertEquals(true, mappedNodeTemplate.isPresent()); mappedNodeTemplate.ifPresent(stringNodeTemplateEntry -> { assertEquals("server_cmaui", stringNodeTemplateEntry.getKey()); @@ -394,8 +404,8 @@ public class ToscaAnalyzerServiceImplTest { }); mappedNodeTemplate = toscaAnalyzerService - .getSubstitutionMappedNodeTemplateByExposedReq("NestedServiceTemplateSubstituteTest.yaml", - nestedServiceTemplateFromYaml, "link_cmaui_port_invalid"); + .getSubstitutionMappedNodeTemplateByExposedReq("NestedServiceTemplateSubstituteTest.yaml", + nestedServiceTemplateFromYaml, "link_cmaui_port_invalid"); assertEquals(true, mappedNodeTemplate.isPresent()); mappedNodeTemplate.ifPresent(stringNodeTemplateEntry -> { assertEquals("server_cmaui", stringNodeTemplateEntry.getKey()); @@ -403,10 +413,10 @@ public class ToscaAnalyzerServiceImplTest { }); ServiceTemplate mainServiceTemplate = toscaServiceModel.getServiceTemplates() - .get(toscaServiceModel.getEntryDefinitionServiceTemplate()); + .get(toscaServiceModel.getEntryDefinitionServiceTemplate()); mappedNodeTemplate = toscaAnalyzerService.getSubstitutionMappedNodeTemplateByExposedReq( - toscaServiceModel.getEntryDefinitionServiceTemplate(), mainServiceTemplate, - "local_storage_server_cmaui"); + toscaServiceModel.getEntryDefinitionServiceTemplate(), mainServiceTemplate, + "local_storage_server_cmaui"); assertEquals(false, mappedNodeTemplate.isPresent()); } } @@ -449,11 +459,11 @@ public class ToscaAnalyzerServiceImplTest { try (InputStream yamlFile = toscaExtensionYamlUtil .loadYamlFileIs("/mock/analyzerService/NestedServiceTemplateReqTest.yaml")) { ServiceTemplate nestedServiceTemplateFromYaml = - toscaExtensionYamlUtil.yamlToObject(yamlFile, ServiceTemplate.class); + toscaExtensionYamlUtil.yamlToObject(yamlFile, ServiceTemplate.class); toscaAnalyzerService - .getSubstitutionMappedNodeTemplateByExposedReq("NestedServiceTemplateSubstituteTest.yaml", - nestedServiceTemplateFromYaml, "link_cmaui_port_invalid"); + .getSubstitutionMappedNodeTemplateByExposedReq("NestedServiceTemplateSubstituteTest.yaml", + nestedServiceTemplateFromYaml, "link_cmaui_port_invalid"); } } @@ -518,8 +528,9 @@ public class ToscaAnalyzerServiceImplTest { @Test public void shouldReturnFalseIfNdTmpIsNull() { + NodeTemplate nodeTemplate = null; assertFalse(toscaAnalyzerService - .isTypeOf(null, ToscaNodeType.NATIVE_NETWORK, new ServiceTemplate(), + .isTypeOf(nodeTemplate, ToscaNodeType.NATIVE_NETWORK, new ServiceTemplate(), toscaServiceModelMock)); } @@ -534,6 +545,26 @@ public class ToscaAnalyzerServiceImplTest { } @Test + public void shouldReturnTrueIfDataTypeIsOfRequestedType() { + PropertyDefinition propertyDefinition = new PropertyDefinition(); + String propertyTypeToSearch = "tosca.datatypes.TimeInterval"; + propertyDefinition.setType(propertyTypeToSearch); + assertTrue(toscaAnalyzerService + .isTypeOf(propertyDefinition, propertyTypeToSearch, new ServiceTemplate(), + toscaServiceModelMock)); + } + + @Test + public void shouldReturnTrueIfInterfaceTypeIsOfRequestedType() { + InterfaceDefinitionType interfaceDefinition = new InterfaceDefinitionType(); + String interfaceTypeToSearch = "test.interface.A"; + interfaceDefinition.setType(interfaceTypeToSearch); + assertTrue(toscaAnalyzerService + .isTypeOf(interfaceDefinition, interfaceTypeToSearch, new ServiceTemplate(), + toscaServiceModelMock)); + } + + @Test public void shouldReturnTrueIfNdTmpTypeIsFoundInSrvTmpNdTyAndNdTyDerivedFromRequestedType() { String typeToMatch = ToscaNodeType.CINDER_VOLUME; when(nodeTemplateMock.getType()).thenReturn(typeToMatch); @@ -550,10 +581,55 @@ public class ToscaAnalyzerServiceImplTest { } @Test + public void dataTypeParameterExistInHierarchy() { + String testedDataTypeKey = "test.dataType.B"; + when(parameterDefinitionMock.getType()).thenReturn(testedDataTypeKey); + dataTypeExistInHierarchy(testedDataTypeKey, parameterDefinitionMock); + + } + + @Test + public void dataTypePropertyExistInHierarchy() { + String testedDataTypeKey = "test.dataType.B"; + when(propertyDefinitionMock.getType()).thenReturn(testedDataTypeKey); + dataTypeExistInHierarchy(testedDataTypeKey, propertyDefinitionMock); + } + + private void dataTypeExistInHierarchy(String testedDataTypeKey, + DefinitionOfDataType testedDefinitionDataType) { + String typeToMatch = "test.dataType.A"; + Map<String, DataType> stDataTypes = new HashMap<>(); + addDataType(stDataTypes, "tosca.datatypes.network.NetworkInfo", new DataType()); + DataType testedDataType = createDataType(typeToMatch); + addDataType(stDataTypes, testedDataTypeKey, testedDataType); + ServiceTemplate serviceTemplate = new ServiceTemplate(); + serviceTemplate.setData_types(stDataTypes); + assertTrue(toscaAnalyzerService + .isTypeOf(testedDefinitionDataType, typeToMatch, serviceTemplate, toscaServiceModelMock)); + } + + @Test + public void interfaceTypeExistInHierarchy() { + String typeToMatch = "test.interfaceType.A"; + String testedInterfaceTypeKey = "test.interfaceType.B"; + when(interfaceDefinitionMock.getType()).thenReturn(testedInterfaceTypeKey); + Map<String, Object> stInterfaceTypes = new HashMap<>(); + stInterfaceTypes.put("tosca.interfaces.network.NetworkInfo", new InterfaceType()); + InterfaceType testedInterfaceType = createInterfaceType(typeToMatch); + stInterfaceTypes.put(testedInterfaceTypeKey, testedInterfaceType); + ServiceTemplate serviceTemplate = new ServiceTemplate(); + serviceTemplate.setInterface_types(stInterfaceTypes); + assertTrue(toscaAnalyzerService + .isTypeOf(interfaceDefinitionMock, "test.interfaceType.A", + serviceTemplate, toscaServiceModelMock)); + } + + @Test public void shouldThrowCoreExceptionForInvalidNodeType() { thrown.expect(CoreException.class); thrown.expectMessage( - "NodeType 'AAA' or one of its derivedFrom node type hierarchy, is not defined in tosca service model"); + "Entity Type 'AAA' or one of its derivedFrom type hierarchy, is not defined in " + + "tosca service model"); when(nodeTemplateMock.getType()).thenReturn("AAA"); Map<String, NodeType> stNodeTypes = new HashMap<>(); addNodeType(stNodeTypes, "notImportant", new NodeType()); @@ -568,7 +644,8 @@ public class ToscaAnalyzerServiceImplTest { public void shouldThrowCoreExceptionForInvalidNodeType2Level() { thrown.expect(CoreException.class); thrown.expectMessage( - "NodeType 'A' or one of its derivedFrom node type hierarchy, is not defined in tosca service model"); + "Entity Type 'A' or one of its derivedFrom type hierarchy, is not defined in tosca " + + "service model"); String typeToMatch = "A"; when(nodeTemplateMock.getType()).thenReturn(typeToMatch); Map<String, NodeType> stNodeTypes = new HashMap<>(); @@ -601,10 +678,26 @@ public class ToscaAnalyzerServiceImplTest { return nodeType; } + private DataType createDataType(String derivedFrom) { + DataType dataType = new DataType(); + dataType.setDerived_from(derivedFrom); + return dataType; + } + + private InterfaceType createInterfaceType(String derivedFrom) { + InterfaceType interfaceType = new InterfaceType(); + interfaceType.setDerived_from(derivedFrom); + return interfaceType; + } + private void addNodeType(Map<String, NodeType> stNodeTypes, String key, NodeType nodeType) { stNodeTypes.put(key, nodeType); } + private void addDataType(Map<String, DataType> stDataTypes, String key, DataType dataType) { + stDataTypes.put(key, dataType); + } + @Test public void shouldReturnTrueIfNdTmpTypeIsFoundInSrvTmpNdTyButRequestedTypeNotMatchButFoundIn1stLevelImports() { String typeToMatch = ToscaNodeType.CINDER_VOLUME; |