diff options
Diffstat (limited to 'openecomp-be/lib/openecomp-tosca-lib')
3 files changed, 408 insertions, 136 deletions
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 9e0e77ecc3..898025c44d 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 @@ -54,6 +54,7 @@ import org.openecomp.sdc.tosca.services.ToscaUtil; import java.util.ArrayList; import java.util.Collection; +import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.List; @@ -63,11 +64,14 @@ import java.util.Optional; import java.util.Set; public class ToscaAnalyzerServiceImpl implements ToscaAnalyzerService { + + @Override public List<Map<String, RequirementDefinition>> calculateExposedRequirements( List<Map<String, RequirementDefinition>> nodeTypeRequirementsDefinitionList, Map<String, RequirementAssignment> nodeTemplateRequirementsAssignment) { + if (nodeTypeRequirementsDefinitionList == null) { - return null; + return Collections.emptyList(); } for (Map.Entry<String, RequirementAssignment> entry : nodeTemplateRequirementsAssignment .entrySet()) { @@ -78,30 +82,44 @@ public class ToscaAnalyzerServiceImpl implements ToscaAnalyzerService { RequirementDefinition cloneRequirementDefinition; if (requirementDefinition.isPresent()) { cloneRequirementDefinition = requirementDefinition.get().clone(); - if (!evaluateRequirementFulfillment(cloneRequirementDefinition)) { - CommonMethods.mergeEntryInList(entry.getKey(), cloneRequirementDefinition, - nodeTypeRequirementsDefinitionList); - } else { - DataModelUtil.removeRequirementsDefinition(nodeTypeRequirementsDefinitionList, entry - .getKey()); - } + updateRequirementDefinition(nodeTypeRequirementsDefinitionList, entry, + cloneRequirementDefinition); } } else { for (Map<String, RequirementDefinition> nodeTypeRequirementsMap : nodeTypeRequirementsDefinitionList) { - Object max = nodeTypeRequirementsMap.get(entry.getKey()).getOccurrences() != null - && nodeTypeRequirementsMap.get(entry.getKey()).getOccurrences().length > 0 - ? nodeTypeRequirementsMap.get(entry.getKey()).getOccurrences()[1] : 1; - Object min = nodeTypeRequirementsMap.get(entry.getKey()).getOccurrences() != null - && nodeTypeRequirementsMap.get(entry.getKey()).getOccurrences().length > 0 - ? nodeTypeRequirementsMap.get(entry.getKey()).getOccurrences()[0] : 1; - nodeTypeRequirementsMap.get(entry.getKey()).setOccurrences(new Object[]{min, max}); + updateMinMaxOccurencesForNodeTypeRequirement(entry, nodeTypeRequirementsMap); } } } return nodeTypeRequirementsDefinitionList; } + private void updateMinMaxOccurencesForNodeTypeRequirement( + Map.Entry<String, RequirementAssignment> entry, + Map<String, RequirementDefinition> nodeTypeRequirementsMap) { + Object max = nodeTypeRequirementsMap.get(entry.getKey()).getOccurrences() != null + && nodeTypeRequirementsMap.get(entry.getKey()).getOccurrences().length > 0 + ? nodeTypeRequirementsMap.get(entry.getKey()).getOccurrences()[1] : 1; + Object min = nodeTypeRequirementsMap.get(entry.getKey()).getOccurrences() != null + && nodeTypeRequirementsMap.get(entry.getKey()).getOccurrences().length > 0 + ? nodeTypeRequirementsMap.get(entry.getKey()).getOccurrences()[0] : 1; + nodeTypeRequirementsMap.get(entry.getKey()).setOccurrences(new Object[]{min, max}); + } + + private void updateRequirementDefinition( + List<Map<String, RequirementDefinition>> nodeTypeRequirementsDefinitionList, + Map.Entry<String, RequirementAssignment> entry, + RequirementDefinition cloneRequirementDefinition) { + if (!evaluateRequirementFulfillment(cloneRequirementDefinition)) { + CommonMethods.mergeEntryInList(entry.getKey(), cloneRequirementDefinition, + nodeTypeRequirementsDefinitionList); + } else { + DataModelUtil.removeRequirementsDefinition(nodeTypeRequirementsDefinitionList, entry + .getKey()); + } + } + private static boolean evaluateRequirementFulfillment(RequirementDefinition requirementDefinition) { Object[] occurrences = requirementDefinition.getOccurrences(); @@ -120,9 +138,11 @@ public class ToscaAnalyzerServiceImpl implements ToscaAnalyzerService { return false; } + @Override public Map<String, CapabilityDefinition> calculateExposedCapabilities( Map<String, CapabilityDefinition> nodeTypeCapabilitiesDefinition, Map<String, Map<String, RequirementAssignment>> fullFilledRequirementsDefinitionMap) { + String capabilityKey; String capability; String node; @@ -139,11 +159,8 @@ public class ToscaAnalyzerServiceImpl implements ToscaAnalyzerService { if (capabilityDefinition != null) { CapabilityDefinition clonedCapabilityDefinition = capabilityDefinition.clone(); nodeTypeCapabilitiesDefinition.put(capabilityKey, capabilityDefinition.clone()); - if (evaluateCapabilityFulfillment(clonedCapabilityDefinition)) { - nodeTypeCapabilitiesDefinition.remove(capabilityKey); - } else { - nodeTypeCapabilitiesDefinition.put(capabilityKey, clonedCapabilityDefinition); - } + updateNodeTypeCapabilitiesDefinition(nodeTypeCapabilitiesDefinition, capabilityKey, + clonedCapabilityDefinition); } } } @@ -156,6 +173,16 @@ public class ToscaAnalyzerServiceImpl implements ToscaAnalyzerService { return exposedCapabilitiesDefinition; } + private void updateNodeTypeCapabilitiesDefinition( + Map<String, CapabilityDefinition> nodeTypeCapabilitiesDefinition, String capabilityKey, + CapabilityDefinition clonedCapabilityDefinition) { + if (evaluateCapabilityFulfillment(clonedCapabilityDefinition)) { + nodeTypeCapabilitiesDefinition.remove(capabilityKey); + } else { + nodeTypeCapabilitiesDefinition.put(capabilityKey, clonedCapabilityDefinition); + } + } + private static boolean evaluateCapabilityFulfillment(CapabilityDefinition capabilityDefinition) { Object[] occurrences = capabilityDefinition.getOccurrences(); @@ -273,14 +300,7 @@ public class ToscaAnalyzerServiceImpl implements ToscaAnalyzerService { if (serviceTemplateFilter != null && serviceTemplateFilter instanceof Map) { Object substituteServiceTemplate = ((Map) serviceTemplateFilter) .get(ToscaConstants.SUBSTITUTE_SERVICE_TEMPLATE_PROPERTY_NAME); - if (substituteServiceTemplate == null) { - MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB, - LoggerTragetServiceName.ADD_ENTITIES_TO_TOSCA, ErrorLevel.ERROR.name(), - LoggerErrorCode.DATA_ERROR.getErrorCode(), LoggerErrorDescription.INVALID_PROPERTY); - throw new CoreException( - new ToscaInvalidSubstituteNodeTemplatePropertiesErrorBuilder(substituteNodeTemplateId) - .build()); - } + handleNoSubstituteServiceTemplate(substituteNodeTemplateId, substituteServiceTemplate); return Optional.of(substituteServiceTemplate.toString()); } } @@ -292,6 +312,18 @@ public class ToscaAnalyzerServiceImpl implements ToscaAnalyzerService { .build()); } + private void handleNoSubstituteServiceTemplate(String substituteNodeTemplateId, + Object substituteServiceTemplate) { + if (substituteServiceTemplate == null) { + MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB, + LoggerTragetServiceName.ADD_ENTITIES_TO_TOSCA, ErrorLevel.ERROR.name(), + LoggerErrorCode.DATA_ERROR.getErrorCode(), LoggerErrorDescription.INVALID_PROPERTY); + throw new CoreException( + new ToscaInvalidSubstituteNodeTemplatePropertiesErrorBuilder(substituteNodeTemplateId) + .build()); + } + } + @Override public Map<String, NodeTemplate> getSubstitutableNodeTemplates(ServiceTemplate serviceTemplate) { Map<String, NodeTemplate> substitutableNodeTemplates = new HashMap<>(); @@ -304,8 +336,9 @@ public class ToscaAnalyzerServiceImpl implements ToscaAnalyzerService { Map<String, NodeTemplate> nodeTemplates = serviceTemplate.getTopology_template().getNode_templates(); - for (String nodeTemplateId : nodeTemplates.keySet()) { - NodeTemplate nodeTemplate = nodeTemplates.get(nodeTemplateId); + for (Map.Entry<String, NodeTemplate> entry : nodeTemplates.entrySet()) { + String nodeTemplateId = entry.getKey(); + NodeTemplate nodeTemplate = entry.getValue(); if (isSubstitutableNodeTemplate(nodeTemplate)) { substitutableNodeTemplates.put(nodeTemplateId, nodeTemplate); } @@ -363,31 +396,38 @@ public class ToscaAnalyzerServiceImpl implements ToscaAnalyzerService { public boolean isDesiredRequirementAssignment(RequirementAssignment requirementAssignment, String capability, String node, String relationship) { - if (capability != null) { - if (requirementAssignment.getCapability() == null - || !requirementAssignment.getCapability().equals(capability)) { - return false; - } + if (isSameCapability(requirementAssignment, capability)) { + return false; } - if (node != null) { - if (requirementAssignment.getNode() == null - || !requirementAssignment.getNode().equals(node)) { - return false; - } + if (isSameRequirement(requirementAssignment, node)) { + return false; } - if (relationship != null) { - if (requirementAssignment.getRelationship() == null - || !requirementAssignment.getRelationship().equals(relationship)) { - return false; - } + if (isSameRelationship(requirementAssignment, relationship)) { + return false; } return !(capability == null && node == null && relationship == null); } + private boolean isSameRelationship(RequirementAssignment requirementAssignment, + String relationship) { + return relationship != null && (requirementAssignment.getRelationship() == null + || !requirementAssignment.getRelationship().equals(relationship)); + } + + private boolean isSameRequirement(RequirementAssignment requirementAssignment, String node) { + return node != null && (requirementAssignment.getNode() == null + || !requirementAssignment.getNode().equals(node)); + } + + private boolean isSameCapability(RequirementAssignment requirementAssignment, String capability) { + return capability != null && (requirementAssignment.getCapability() == null + || !requirementAssignment.getCapability().equals(capability)); + } + @Override public Object getFlatEntity(ToscaElementTypes elementType, String typeId, ServiceTemplate serviceTemplate, ToscaServiceModel toscaModel) { @@ -408,7 +448,8 @@ public class ToscaAnalyzerServiceImpl implements ToscaAnalyzerService { "Entity[" + elementType + "] id[" + typeId + "] flat not supported"); } - scanAnFlatEntity(elementType, typeId, returnEntity, serviceTemplate, toscaModel, new ArrayList<>(), 0); + scanAnFlatEntity(elementType, typeId, returnEntity, serviceTemplate, toscaModel, + new ArrayList<>(), 0); return returnEntity; @@ -459,46 +500,50 @@ public class ToscaAnalyzerServiceImpl implements ToscaAnalyzerService { return Optional.empty(); } - filesScanned = createFilesScannedSet(filesScanned); + Set<String> createdFilesScanned = createFilesScannedSet(filesScanned); for (Map<String, Import> map : imports) { ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil(); Import anImport = toscaExtensionYamlUtil .yamlToObject(toscaExtensionYamlUtil.objectToYaml(map.values().iterator().next()), Import.class); - if (Objects.isNull(anImport) || Objects.isNull(anImport.getFile())) { - throw new RuntimeException("import without file entry"); - } + handleImportWithNoFileEntry(anImport); String importFile = anImport.getFile(); ServiceTemplate template = toscaServiceModel.getServiceTemplates().get(fetchFileNameForImport(importFile, serviceTemplate.getMetadata() == null ? null : serviceTemplate.getMetadata().get("filename"))); - if (Objects.isNull(template) || - filesScanned.contains(ToscaUtil.getServiceTemplateFileName(template))) { + if (Objects.isNull(template) + || createdFilesScanned.contains(ToscaUtil.getServiceTemplateFileName(template))) { continue; } else { - filesScanned.add(ToscaUtil.getServiceTemplateFileName(template)); + createdFilesScanned.add(ToscaUtil.getServiceTemplateFileName(template)); } Optional<Boolean> nodeTypeExistInServiceTemplateHierarchy = isNodeTypeExistInServiceTemplateHierarchy(nodeTypeToMatch, nodeTypeToSearch, template, - toscaServiceModel, filesScanned); - if (nodeTypeExistInServiceTemplateHierarchy.isPresent()) { - if (nodeTypeExistInServiceTemplateHierarchy.get()) { - filesScanned.clear(); - return Optional.of(true); - } + toscaServiceModel, createdFilesScanned); + if (nodeTypeExistInServiceTemplateHierarchy.isPresent() + && (nodeTypeExistInServiceTemplateHierarchy.get())) { + createdFilesScanned.clear(); + return Optional.of(true); } } return Optional.of(false); } + private void handleImportWithNoFileEntry(Import anImport) { + if (Objects.isNull(anImport) || Objects.isNull(anImport.getFile())) { + throw new RuntimeException("import without file entry"); + } + } + private Set<String> createFilesScannedSet(Set<String> filesScanned) { - if (Objects.isNull(filesScanned)) { - filesScanned = new HashSet<>(); + Set<String> retFileScanned = filesScanned; + if (Objects.isNull(retFileScanned)) { + retFileScanned = new HashSet<>(); } - return filesScanned; + return retFileScanned; } private boolean isNodeTypeIsToscaRoot(NodeType stNodeType) { @@ -530,54 +575,59 @@ public class ToscaAnalyzerServiceImpl implements ToscaAnalyzerService { List<String> filesScanned, int rootScanStartInx) { - boolean entityFound = - enrichEntityFromCurrentServiceTemplate(elementType, typeId, entity, serviceTemplate, - toscaModel, filesScanned, rootScanStartInx); + boolean entityFound = enrichEntityFromCurrentServiceTemplate(elementType, typeId, entity, + serviceTemplate,toscaModel, filesScanned, rootScanStartInx); if (!entityFound) { List<Map<String, Import>> imports = serviceTemplate.getImports(); if (CollectionUtils.isEmpty(imports)) { return false; } - ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil(); boolean found = false; for (Map<String, Import> importMap : imports) { if (found) { return true; } - String filename; - for (Object importObject : importMap.values()) { - Import importServiceTemplate = toscaExtensionYamlUtil - .yamlToObject(toscaExtensionYamlUtil.objectToYaml(importObject), Import.class); - filename = fetchFileNameForImport(importServiceTemplate.getFile(), - serviceTemplate.getMetadata() == null ? null : serviceTemplate.getMetadata().get - ("filename")); - if (filesScanned.contains(filename)) { - return false; - } else { - filesScanned.add(filename); - } - ServiceTemplate template = - toscaModel.getServiceTemplates() - .get(filename); - found = - scanAnFlatEntity(elementType, typeId, entity, template, toscaModel, filesScanned, - filesScanned.size()); - } + found = isFlatEntity(importMap, entity, serviceTemplate, filesScanned, + toscaModel,elementType,typeId); } return found; } return true; } + private boolean isFlatEntity(Map<String, Import> importMap, + Object entity, + ServiceTemplate serviceTemplate, + List<String> filesScanned, + ToscaServiceModel toscaModel, + ToscaElementTypes elementType, String typeId) { + boolean found = false; + ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil(); + for (Object importObject : importMap.values()) { + Import importServiceTemplate = toscaExtensionYamlUtil + .yamlToObject(toscaExtensionYamlUtil.objectToYaml(importObject), Import.class); + String fileName = fetchFileNameForImport(importServiceTemplate.getFile(), + serviceTemplate.getMetadata() == null ? null : serviceTemplate.getMetadata().get( + "filename")); + if (filesScanned.contains(fileName)) { + return false; + } else { + filesScanned.add(fileName); + } + ServiceTemplate template = toscaModel.getServiceTemplates().get(fileName); + found = scanAnFlatEntity(elementType, typeId, entity, template, toscaModel, + filesScanned, filesScanned.size()); + } + return found; + } + private String fetchFileNameForImport(String importServiceTemplateFile, String currentMetadatafileName) { if (importServiceTemplateFile.contains("../")) { return importServiceTemplateFile.replace("../", ""); - } else if (importServiceTemplateFile.contains("/")) { - return importServiceTemplateFile; } else if (currentMetadatafileName != null) { - return currentMetadatafileName.substring(0, currentMetadatafileName.indexOf("/")) + "/" + - importServiceTemplateFile; + return currentMetadatafileName.substring(0, currentMetadatafileName.indexOf('/')) + "/" + + importServiceTemplateFile; } else { return importServiceTemplateFile; } @@ -590,39 +640,16 @@ public class ToscaAnalyzerServiceImpl implements ToscaAnalyzerService { ToscaServiceModel toscaModel, List<String> filesScanned, int rootScanStartInx) { - String derivedFrom; switch (elementType) { case CAPABILITY_TYPE: - if (serviceTemplate.getCapability_types() != null - && serviceTemplate.getCapability_types().containsKey(typeId)) { - - filesScanned.clear(); - CapabilityType targetCapabilityType = ((CapabilityType) entity); - CapabilityType sourceCapabilityType = serviceTemplate.getCapability_types().get(typeId); - derivedFrom = sourceCapabilityType.getDerived_from(); - if (derivedFrom != null) { - scanAnFlatEntity(elementType, derivedFrom, entity, serviceTemplate, toscaModel, - filesScanned, rootScanStartInx); - } - combineCapabilityTypeInfo(sourceCapabilityType, targetCapabilityType); - } else { + if (enrichCapabilityType(elementType, typeId, entity, serviceTemplate, toscaModel, + filesScanned, rootScanStartInx)) { return false; } break; case NODE_TYPE: - if (serviceTemplate.getNode_types() != null - && serviceTemplate.getNode_types().containsKey(typeId)) { - - filesScanned.clear(); - NodeType targetNodeType = ((NodeType) entity); - NodeType sourceNodeType = serviceTemplate.getNode_types().get(typeId); - derivedFrom = sourceNodeType.getDerived_from(); - if (derivedFrom != null) { - scanAnFlatEntity(elementType, derivedFrom, entity, serviceTemplate, toscaModel, - filesScanned, rootScanStartInx); - } - combineNodeTypeInfo(sourceNodeType, targetNodeType); - } else { + if (enrichNodeTypeInfo(elementType, typeId, entity, serviceTemplate, toscaModel, + filesScanned, rootScanStartInx)) { return false; } break; @@ -639,6 +666,51 @@ public class ToscaAnalyzerServiceImpl implements ToscaAnalyzerService { } + private boolean enrichNodeTypeInfo(ToscaElementTypes elementType, String typeId, Object entity, + ServiceTemplate serviceTemplate, ToscaServiceModel toscaModel, + List<String> filesScanned, int rootScanStartInx) { + String derivedFrom; + if (serviceTemplate.getNode_types() != null + && serviceTemplate.getNode_types().containsKey(typeId)) { + + filesScanned.clear(); + NodeType targetNodeType = (NodeType) entity; + NodeType sourceNodeType = serviceTemplate.getNode_types().get(typeId); + derivedFrom = sourceNodeType.getDerived_from(); + if (derivedFrom != null) { + scanAnFlatEntity(elementType, derivedFrom, entity, serviceTemplate, toscaModel, + filesScanned, rootScanStartInx); + } + combineNodeTypeInfo(sourceNodeType, targetNodeType); + } else { + return true; + } + return false; + } + + private boolean enrichCapabilityType(ToscaElementTypes elementType, String typeId, Object entity, + ServiceTemplate serviceTemplate, + ToscaServiceModel toscaModel, List<String> filesScanned, + int rootScanStartInx) { + String derivedFrom; + if (serviceTemplate.getCapability_types() != null + && serviceTemplate.getCapability_types().containsKey(typeId)) { + + filesScanned.clear(); + CapabilityType targetCapabilityType = (CapabilityType) entity; + CapabilityType sourceCapabilityType = serviceTemplate.getCapability_types().get(typeId); + derivedFrom = sourceCapabilityType.getDerived_from(); + if (derivedFrom != null) { + scanAnFlatEntity(elementType, derivedFrom, entity, serviceTemplate, toscaModel, + filesScanned, rootScanStartInx); + } + combineCapabilityTypeInfo(sourceCapabilityType, targetCapabilityType); + } else { + return true; + } + return false; + } + private void combineNodeTypeInfo(NodeType sourceNodeType, NodeType targetNodeType) { targetNodeType.setDerived_from(sourceNodeType.getDerived_from()); targetNodeType.setDescription(sourceNodeType.getDescription()); @@ -705,6 +777,7 @@ public class ToscaAnalyzerServiceImpl implements ToscaAnalyzerService { return substitutionNodeType; } + @Override public Map<String, PropertyDefinition> manageSubstitutionNodeTypeProperties( ServiceTemplate substitutionServiceTemplate) { Map<String, PropertyDefinition> substitutionNodeTypeProperties = new HashMap<>(); @@ -742,6 +815,7 @@ public class ToscaAnalyzerServiceImpl implements ToscaAnalyzerService { return substitutionNodeTypeProperties; } + private Map<String, AttributeDefinition> manageSubstitutionNodeTypeAttributes( ServiceTemplate substitutionServiceTemplate) { Map<String, AttributeDefinition> substitutionNodeTypeAttributes = new HashMap<>(); @@ -782,23 +856,18 @@ public class ToscaAnalyzerServiceImpl implements ToscaAnalyzerService { * @param requirementAssignment the requirement assignment * @return true if the requirement already exists and false otherwise */ + @Override public boolean isRequirementExistInNodeTemplate(NodeTemplate nodeTemplate, String requirementId, RequirementAssignment requirementAssignment) { - boolean result = false; List<Map<String, RequirementAssignment>> nodeTemplateRequirements = nodeTemplate .getRequirements(); if (nodeTemplateRequirements != null) { - for (Map<String, RequirementAssignment> requirement : nodeTemplateRequirements) { - if (requirement.containsKey(requirementId)) { - result = DataModelUtil.compareRequirementAssignment(requirementAssignment, - requirement.get(requirementId)); - if (result) { - break; - } - } - } + return nodeTemplateRequirements.stream() + .anyMatch(requirement -> requirement.containsKey(requirementId) + && DataModelUtil.compareRequirementAssignment(requirementAssignment, + requirement.get(requirementId))); } - return result; + return false; } } diff --git a/openecomp-be/lib/openecomp-tosca-lib/src/test/java/org/openecomp/sdc/tosca/TestUtil.java b/openecomp-be/lib/openecomp-tosca-lib/src/test/java/org/openecomp/sdc/tosca/TestUtil.java index ca0c0ea506..ef82e6f354 100644 --- a/openecomp-be/lib/openecomp-tosca-lib/src/test/java/org/openecomp/sdc/tosca/TestUtil.java +++ b/openecomp-be/lib/openecomp-tosca-lib/src/test/java/org/openecomp/sdc/tosca/TestUtil.java @@ -23,6 +23,8 @@ package org.openecomp.sdc.tosca; import org.openecomp.sdc.logging.api.Logger; import org.openecomp.sdc.logging.api.LoggerFactory; import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel; +import org.openecomp.sdc.tosca.datatypes.model.NodeTemplate; +import org.openecomp.sdc.tosca.datatypes.model.RequirementAssignment; import org.openecomp.sdc.tosca.datatypes.model.ServiceTemplate; import org.openecomp.sdc.tosca.services.ToscaExtensionYamlUtil; @@ -33,7 +35,10 @@ import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.nio.file.NotDirectoryException; +import java.util.ArrayList; import java.util.HashMap; +import java.util.List; +import java.util.ListIterator; import java.util.Map; public class TestUtil { @@ -97,4 +102,43 @@ public class TestUtil { } } } + + public static void createConcreteRequirementObjectsInServiceTemplate( + ServiceTemplate serviceTemplateFromYaml, + ToscaExtensionYamlUtil toscaExtensionYamlUtil){ + + if (serviceTemplateFromYaml == null + || serviceTemplateFromYaml.getTopology_template() == null + || serviceTemplateFromYaml.getTopology_template().getNode_templates() == null) { + return; + } + + //Creating concrete objects + Map<String, NodeTemplate> nodeTemplates = + serviceTemplateFromYaml.getTopology_template().getNode_templates(); + for (Map.Entry<String, NodeTemplate> entry : nodeTemplates.entrySet()) { + NodeTemplate nodeTemplate = entry.getValue(); + List<Map<String, RequirementAssignment>> requirements = nodeTemplate.getRequirements(); + List<Map<String, RequirementAssignment>> concreteRequirementList = new ArrayList<>(); + if (requirements != null) { + ListIterator<Map<String, RequirementAssignment>> reqListIterator = requirements + .listIterator(); + while (reqListIterator.hasNext()){ + Map<String, RequirementAssignment> requirement = reqListIterator.next(); + Map<String, RequirementAssignment> concreteRequirement = new HashMap<>(); + for (Map.Entry<String, RequirementAssignment> reqEntry : requirement.entrySet()) { + RequirementAssignment requirementAssignment = (toscaExtensionYamlUtil + .yamlToObject(toscaExtensionYamlUtil.objectToYaml(reqEntry.getValue()), + RequirementAssignment.class)); + concreteRequirement.put(reqEntry.getKey(), requirementAssignment); + concreteRequirementList.add(concreteRequirement); + reqListIterator.remove(); + } + } + requirements.clear(); + requirements.addAll(concreteRequirementList); + nodeTemplate.setRequirements(requirements); + } + } + } } 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 d082d2febf..c62eb833a3 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,25 +1,22 @@ -/*- - * ============LICENSE_START======================================================= - * SDC - * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. - * ================================================================================ +/* + * Copyright © 2016-2017 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.impl; +import org.junit.Assert; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Rule; @@ -31,12 +28,16 @@ import org.mockito.MockitoAnnotations; import org.mockito.runners.MockitoJUnitRunner; import org.openecomp.sdc.common.errors.CoreException; import org.openecomp.sdc.tosca.TestUtil; +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.Import; import org.openecomp.sdc.tosca.datatypes.model.NodeTemplate; import org.openecomp.sdc.tosca.datatypes.model.NodeType; 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.datatypes.model.SubstitutionMapping; import org.openecomp.sdc.tosca.datatypes.model.TopologyTemplate; @@ -49,6 +50,7 @@ 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; @@ -56,6 +58,7 @@ 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; @@ -95,6 +98,162 @@ public class ToscaAnalyzerServiceImplTest { } @Test + public void testGetFlatEntity() throws Exception { + ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil(); + try (InputStream yamlFile = toscaExtensionYamlUtil + .loadYamlFileIs("/mock/analyzerService/NestedServiceTemplateReqTest.yaml")) { + + ServiceTemplate + serviceTemplateFromYaml = + toscaExtensionYamlUtil.yamlToObject(yamlFile, ServiceTemplate.class); + + final NodeType flatEntity = (NodeType) toscaAnalyzerService + .getFlatEntity(ToscaElementTypes.NODE_TYPE, "org.openecomp.resource.vfc.nodes.heat" + + ".cmaui_image", serviceTemplateFromYaml, toscaServiceModel); + + Assert.assertNotNull(flatEntity); + Assert.assertEquals("org.openecomp.resource.vfc.nodes.heat.nova.Server",flatEntity + .getDerived_from()); + } + } + + @Test + public void testCalculateExposedRequirements() throws Exception { + Map<String, RequirementDefinition> nodeTypeRequirementDefinition = new HashMap<>(); + RequirementDefinition rd = new RequirementDefinition(); + rd.setCapability("tosca.capabilities.Node"); + rd.setNode("tosca.nodes.Root"); + rd.setRelationship("tosca.relationships.DependsOn"); + Object[] occurences = new Object[]{0, "UNBOUNDED"}; + rd.setOccurrences(occurences); + + RequirementDefinition rd1 = new RequirementDefinition(); + rd.setCapability("tosca.capabilities.network.Bindable"); + rd.setNode(null); + rd.setRelationship("tosca.relationships.network.BindsTo"); + Object[] occurences1 = new Object[]{1, 1}; + rd1.setOccurrences(occurences1); + + nodeTypeRequirementDefinition.put("binding",rd1); + nodeTypeRequirementDefinition.put("dependency",rd); + + Map<String, Map<String, RequirementAssignment>> fullFilledRequirementsDefinition = + new HashMap<>(); + Map<String, RequirementAssignment> nodeTemplateRequirementsAssignment = new HashMap<>(); + RequirementAssignment ra = new RequirementAssignment(); + ra.setCapability("tosca.capabilities.network.Bindable"); + ra.setNode("pd_server"); + ra.setRelationship("tosca.relationships.network.BindsTo"); + nodeTemplateRequirementsAssignment.put("binding", ra); + + List<Map<String, RequirementDefinition>> nodeTypeRequirementsDefinition = new ArrayList<>(); + nodeTypeRequirementsDefinition.add(nodeTypeRequirementDefinition); + + List<Map<String, RequirementDefinition>> exposedRequirements = toscaAnalyzerService + .calculateExposedRequirements(nodeTypeRequirementsDefinition, + nodeTemplateRequirementsAssignment); + Assert.assertEquals(1, exposedRequirements.size()); + } + + @Test + public void testCalExpReqWithNullNodeInReqAssignment() throws Exception { + Map<String, RequirementDefinition> nodeTypeRequirementDefinition = new HashMap<>(); + RequirementDefinition rd = new RequirementDefinition(); + rd.setCapability("tosca.capabilities.Node"); + rd.setNode("tosca.nodes.Root"); + rd.setRelationship("tosca.relationships.DependsOn"); + Object[] occurences = new Object[]{0, "UNBOUNDED"}; + rd.setOccurrences(occurences); + + RequirementDefinition rd1 = new RequirementDefinition(); + rd.setCapability("tosca.capabilities.network.Bindable"); + rd.setNode(null); + rd.setRelationship("tosca.relationships.network.BindsTo"); + Object[] occurences1 = new Object[]{1, 1}; + rd1.setOccurrences(occurences1); + + nodeTypeRequirementDefinition.put("binding",rd1); + nodeTypeRequirementDefinition.put("dependency",rd); + + Map<String, Map<String, RequirementAssignment>> fullFilledRequirementsDefinition = + new HashMap<>(); + Map<String, RequirementAssignment> nodeTemplateRequirementsAssignment = new HashMap<>(); + RequirementAssignment ra = new RequirementAssignment(); + ra.setCapability("tosca.capabilities.network.Bindable"); + ra.setNode(null); + ra.setRelationship("tosca.relationships.network.BindsTo"); + nodeTemplateRequirementsAssignment.put("binding", ra); + + List<Map<String, RequirementDefinition>> nodeTypeRequirementsDefinition = new ArrayList<>(); + nodeTypeRequirementsDefinition.add(nodeTypeRequirementDefinition); + + List<Map<String, RequirementDefinition>> exposedRequirements = toscaAnalyzerService + .calculateExposedRequirements(nodeTypeRequirementsDefinition, + nodeTemplateRequirementsAssignment); + Assert.assertEquals(1, exposedRequirements.size()); + } + + @Test + public void testCalculateExposedCapabilities() throws Exception { + Map<String, CapabilityDefinition> nodeTypeCapabilitiesDefinition = new HashMap<>(); + CapabilityDefinition cd = new CapabilityDefinition(); + cd.setType("tosca.capabilities.Scalable"); + nodeTypeCapabilitiesDefinition.put("tosca.capabilities.network.Bindable_pd_server",cd); + Map<String, Map<String, RequirementAssignment>> fullFilledRequirementsDefinition = + new HashMap<>(); + Map<String, RequirementAssignment> nodeTemplateRequirementsAssignment = new HashMap<>(); + RequirementAssignment ra = new RequirementAssignment(); + ra.setCapability("tosca.capabilities.network.Bindable"); + ra.setNode("pd_server"); + ra.setRelationship("tosca.relationships.network.BindsTo"); + nodeTemplateRequirementsAssignment.put("binding",ra); + fullFilledRequirementsDefinition.put("pd_server", nodeTemplateRequirementsAssignment); + Map<String, CapabilityDefinition> exposedCapabilities = + toscaAnalyzerService.calculateExposedCapabilities(nodeTypeCapabilitiesDefinition, + fullFilledRequirementsDefinition); + Assert.assertEquals(1, exposedCapabilities.size()); + } + + @Test + public void testIsRequirementExistsWithInvalidReqId() throws Exception { + ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil(); + try (InputStream yamlFile = toscaExtensionYamlUtil + .loadYamlFileIs("/mock/analyzerService/NestedServiceTemplateReqTest.yaml")) { + + ServiceTemplate + serviceTemplateFromYaml = + toscaExtensionYamlUtil.yamlToObject(yamlFile, ServiceTemplate.class); + + TestUtil.createConcreteRequirementObjectsInServiceTemplate(serviceTemplateFromYaml, + toscaExtensionYamlUtil); + + NodeTemplate port_0 = + serviceTemplateFromYaml.getTopology_template().getNode_templates().get("cmaui_port_0"); + + RequirementAssignment ra = new RequirementAssignment(); + ra.setCapability("tosca.capabilities.network.Bindable"); + ra.setNode("server_cmaui"); + ra.setRelationship("tosca.relationships.network.BindsTo"); + + //Test With Empty requirementId + Assert.assertEquals(false, + toscaAnalyzerService.isRequirementExistInNodeTemplate(port_0, "", ra)); + + //Test With valid requirementId + Assert.assertEquals(true, + toscaAnalyzerService.isRequirementExistInNodeTemplate(port_0, "binding", ra)); + + //Test With invalid requirement assignment + RequirementAssignment ra1 = new RequirementAssignment(); + ra1.setCapability("tosca.capabilities.network.Bindable1"); + ra1.setNode("server_cmaui1"); + ra1.setRelationship("tosca.relationships.network.BindsTo1"); + Assert.assertEquals(false, + toscaAnalyzerService.isRequirementExistInNodeTemplate(port_0, "binding", ra1)); + } + } + + @Test public void testGetRequirements() throws Exception { ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil(); try (InputStream yamlFile = toscaExtensionYamlUtil |