From f928f5983a4722b728ff4366f1dd3eb3453e02d8 Mon Sep 17 00:00:00 2001 From: shiria Date: Wed, 21 Mar 2018 09:04:07 +0200 Subject: Add support in TOSCA isTypeOf Add support for InterfaceDefinitionType entity type Add support for PropertyDefinition entity type Add support for ParameterDefinition entity type Change-Id: I10ba0f6c3f16a0d476e254d40b4fcd463392cb52 Issue-ID: SDC-1153 Signed-off-by: shiria --- .../ToscaElementTypeNotFoundErrorBuilder.java | 50 +++++++ .../sdc/tosca/services/ToscaAnalyzerService.java | 30 ++-- .../services/impl/ToscaAnalyzerServiceImpl.java | 156 +++++++++++++-------- 3 files changed, 164 insertions(+), 72 deletions(-) create mode 100644 openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/errors/ToscaElementTypeNotFoundErrorBuilder.java (limited to 'openecomp-be/lib/openecomp-tosca-lib/src/main') 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 manageSubstitutionNodeTypeProperties( + Map manageSubstitutionNodeTypeProperties( ServiceTemplate substitutionServiceTemplate); - public Map calculateExposedCapabilities( + Map calculateExposedCapabilities( Map nodeTypeCapabilitiesDefinition, Map> fullFilledRequirementsDefinitionMap); - public List> calculateExposedRequirements( + List> calculateExposedRequirements( List> nodeTypeRequirementsDefinitionList, Map 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> calculateExposedRequirements( @@ -145,7 +154,6 @@ public class ToscaAnalyzerServiceImpl implements ToscaAnalyzerService { for (Map.Entry 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 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 isNodeTypeExistInServiceTemplateHierarchy( - String nodeTypeToMatch, - String nodeTypeToSearch, - ServiceTemplate serviceTemplate, - ToscaServiceModel toscaServiceModel, - Set analyzedImportFiles) { - Map 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 Optional isTypeExistInServiceTemplateHierarchy(String typeToMatch, + String typeToSearch, + String getTypesMethodName, + ServiceTemplate serviceTemplate, + ToscaServiceModel toscaServiceModel, + Set analyzedImportFiles) + throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { + Map searchableTypes = + (Map) 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 isNodeTypeExistInImports(String nodeTypeToMatch, - String nodeTypeToSearch, - ServiceTemplate serviceTemplate, - ToscaServiceModel toscaServiceModel, - Set filesScanned) { + private Optional isTypeExistInImports(String typeToMatch, + String typeToSearch, + String getTypesMethodName, + ServiceTemplate serviceTemplate, + ToscaServiceModel toscaServiceModel, + Set filesScanned) + throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { List> 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 nodeTypeExistInServiceTemplateHierarchy = - isNodeTypeExistInServiceTemplateHierarchy(nodeTypeToMatch, nodeTypeToSearch, template, - toscaServiceModel, createdFilesScanned); - if (nodeTypeExistInServiceTemplateHierarchy.isPresent() - && (nodeTypeExistInServiceTemplateHierarchy.get())) { + Optional 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> 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> 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 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 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; } + + } -- cgit 1.2.3-korg