From a3c95e21a61058ed7bddc6d63be4becd30fe23a2 Mon Sep 17 00:00:00 2001 From: mojahidi Date: Wed, 13 Dec 2017 17:04:17 +0530 Subject: Fixed sonar issues - HeatResourceValidator Done refactoring as per given comment Change-Id: Id1cd659af6ddd9e24d281a1bb0d1ae9431bcaf98 Issue-ID: SDC-343 Signed-off-by: mojahidi --- .../impl/validators/HeatResourceValidator.java | 210 ++++++--------------- 1 file changed, 59 insertions(+), 151 deletions(-) (limited to 'openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-impl/src/main/java') diff --git a/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-impl/src/main/java/org/openecomp/sdc/validation/impl/validators/HeatResourceValidator.java b/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-impl/src/main/java/org/openecomp/sdc/validation/impl/validators/HeatResourceValidator.java index 89e96efa36..ddcc411a6c 100644 --- a/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-impl/src/main/java/org/openecomp/sdc/validation/impl/validators/HeatResourceValidator.java +++ b/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-impl/src/main/java/org/openecomp/sdc/validation/impl/validators/HeatResourceValidator.java @@ -1,24 +1,33 @@ +/* + * 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. + */ + package org.openecomp.sdc.validation.impl.validators; -import org.apache.commons.collections4.CollectionUtils; -import org.apache.commons.collections4.MapUtils; -import org.openecomp.core.validation.ErrorMessageCode; import org.openecomp.core.validation.types.GlobalValidationContext; -import org.openecomp.sdc.heat.datatypes.manifest.ManifestContent; import org.openecomp.sdc.heat.datatypes.model.HeatOrchestrationTemplate; -import org.openecomp.sdc.heat.datatypes.model.HeatResourcesTypes; import org.openecomp.sdc.heat.datatypes.model.Output; import org.openecomp.sdc.heat.datatypes.model.Resource; import org.openecomp.sdc.heat.datatypes.model.ResourceReferenceFunctions; import org.openecomp.sdc.heat.services.HeatStructureUtil; -import org.openecomp.sdc.heat.services.manifest.ManifestUtil; import org.openecomp.sdc.logging.api.Logger; import org.openecomp.sdc.logging.api.LoggerFactory; import org.openecomp.sdc.validation.ValidationContext; import org.openecomp.sdc.validation.base.ResourceBaseValidator; import org.openecomp.sdc.validation.type.ConfigConstants; import org.openecomp.sdc.validation.type.HeatResourceValidationContext; -import org.openecomp.sdc.validation.util.ValidationUtil; import java.util.ArrayList; import java.util.Collection; @@ -31,7 +40,7 @@ import java.util.Set; public class HeatResourceValidator extends ResourceBaseValidator { private static final Logger LOGGER = LoggerFactory.getLogger(ResourceBaseValidator.class); - private static final ErrorMessageCode ERROR_CODE_HTR_1 = new ErrorMessageCode("HTR1"); + private static final String GET_ATTR = "get_attr"; @Override public void init(Map properties) { @@ -43,21 +52,6 @@ public class HeatResourceValidator extends ResourceBaseValidator { String envFileName, HeatOrchestrationTemplate heatOrchestrationTemplate, GlobalValidationContext globalContext) { - ManifestContent manifestContent = new ManifestContent(); - try { - manifestContent = ValidationUtil.validateManifest(globalContext); - } catch (Exception exception) { - LOGGER.debug("", exception); - } - Set baseFiles = ManifestUtil.getBaseFiles(manifestContent); - String baseFileName = CollectionUtils.isEmpty(baseFiles) ? null : baseFiles.iterator().next(); - globalContext.setMessageCode(ERROR_CODE_HTR_1); - HeatOrchestrationTemplate baseHot = - ValidationUtil.checkHeatOrchestrationPreCondition(baseFileName, globalContext); - Set securityGroupsNamesFromBaseFileOutputs = baseFileName == null ? new HashSet<>() - : checkForBaseFilePortsExistenceAndReturnSecurityGroupNamesFromOutputsIfNot - (baseHot); - Map resourcesMap = heatOrchestrationTemplate.getResources() == null ? new HashMap<>() : heatOrchestrationTemplate.getResources(); @@ -68,52 +62,44 @@ public class HeatResourceValidator extends ResourceBaseValidator { Map>>> typeToPointingResourcesMap = new HashMap<>(); - initTypeRelationsMap - (fileName, resourcesMap, outputMap, - securityGroupsNamesFromBaseFileOutputs, typeToPointingResourcesMap, globalContext); + initTypeRelationsMap (fileName, resourcesMap, outputMap, + typeToPointingResourcesMap, globalContext); - return new HeatResourceValidationContext - (heatOrchestrationTemplate, typeToPointingResourcesMap, envFileName); + return new HeatResourceValidationContext (heatOrchestrationTemplate, typeToPointingResourcesMap, + envFileName ); } - private void initTypeRelationsMap(String fileName, - Map resourceMap, - Map outputMap, - Set securityGroupsNamesFromBaseFileOutputs, - Map>>> typeToPointingResourcesMap, - GlobalValidationContext globalContext) { - - initTypeRelationsMapFromResourcesMap - (fileName, resourceMap, + private void initTypeRelationsMap (String fileName, + Map resourceMap, + Map outputMap, + Map>>> typeToPointingResourcesMap, + GlobalValidationContext globalContext ) { + initTypeRelationsMapFromResourcesMap (fileName, resourceMap, typeToPointingResourcesMap, globalContext); - initTypeRelationsMapFromOutputsMap - (fileName, resourceMap, outputMap, + initTypeRelationsMapFromOutputsMap (fileName, resourceMap, outputMap, typeToPointingResourcesMap, globalContext); } - private void initTypeRelationsMapFromOutputsMap(String fileName, + private void initTypeRelationsMapFromOutputsMap (String fileName, Map resourceMap, Map outputMap, Map>>> typeToPointingResourcesMap, - GlobalValidationContext globalContext) { + GlobalValidationContext globalContext ) { for (Map.Entry outputEntry : outputMap.entrySet()) { Object outputValue = outputEntry.getValue().getValue(); Set referencedResources = HeatStructureUtil .getReferencedValuesByFunctionName(fileName, ResourceReferenceFunctions.GET_RESOURCE.getFunction(), outputValue, globalContext); - updateRelationsMapWithOutputsReferences - (outputEntry, resourceMap, referencedResources, typeToPointingResourcesMap); - - + updateRelationsMapWithOutputsReferences (outputEntry, resourceMap, referencedResources, typeToPointingResourcesMap); } } - private void updateRelationsMapWithOutputsReferences(Map.Entry outputEntry, + private void updateRelationsMapWithOutputsReferences (Map.Entry outputEntry, Map resourceMap, Set referencedResources, - Map>>> typeToPointingResourcesMap) { + Map>>> typeToPointingResourcesMap ) { for (String pointedResourceName : referencedResources) { Resource pointedResource = resourceMap.get(pointedResourceName); @@ -149,47 +135,10 @@ public class HeatResourceValidator extends ResourceBaseValidator { referencedResourcesByGetResource.addAll(referencedResourcesByGetAttr); updateRelationsMapWithCurrentResourceReferences - (resourceMap, resourceEntry, referencedResourcesByGetResource, - typeToPointingResourcesMap); + (resourceMap, resourceEntry, referencedResourcesByGetResource, typeToPointingResourcesMap); } } - private void updateRelationsMapWithSecurityGroupsFromBaseFileOutput(String fileName, - Map resourcesMap, - Map.Entry resourceEntry, - Map properties, - Set securityGroupsNamesFromBaseFileOutputs, - Map>>> typeToPointingResourcesMap, - GlobalValidationContext globalContext) { - - Set candidateSecurityGroupUsedFromBaseFile = getResourcesIdsPointedByCurrentResource - (fileName, ResourceReferenceFunctions.GET_PARAM, properties, globalContext); - removeNonSecurityGroupNamesFromList - (candidateSecurityGroupUsedFromBaseFile, securityGroupsNamesFromBaseFileOutputs); - - for (String usedSecurityGroupId : candidateSecurityGroupUsedFromBaseFile) { - updateMapWithRelationsBetweenResources - (usedSecurityGroupId, - HeatResourcesTypes.NEUTRON_SECURITY_GROUP_RESOURCE_TYPE.getHeatResource(), - resourceEntry, typeToPointingResourcesMap); - - } - } - - private void removeNonSecurityGroupNamesFromList( - Set candidateSecurityGroupUsedFromBaseFile, - Set securityGroupsNamesFromBaseFileOutputs) { - - Set nonSecurityGroupNames = new HashSet<>(); - for (String candidateSecurityGroup : candidateSecurityGroupUsedFromBaseFile) { - if (!securityGroupsNamesFromBaseFileOutputs.contains(candidateSecurityGroup)) { - nonSecurityGroupNames.add(candidateSecurityGroup); - } - } - - candidateSecurityGroupUsedFromBaseFile.removeAll(nonSecurityGroupNames); - } - private void updateRelationsMapWithCurrentResourceReferences(Map resourceMap, Map.Entry currentResourceEntry, Set referencedResourcesFromCurrentResource, @@ -200,8 +149,7 @@ public class HeatResourceValidator extends ResourceBaseValidator { if (Objects.nonNull(pointedResource)) { String pointedResourceType = pointedResource.getType(); - updateMapWithRelationsBetweenResources - (pointedResourceName, pointedResourceType, + updateMapWithRelationsBetweenResources (pointedResourceName, pointedResourceType, currentResourceEntry, typeToPointingResourcesMap); } } @@ -215,8 +163,8 @@ public class HeatResourceValidator extends ResourceBaseValidator { initCurrentResourceTypeInMap(pointedResourceName, pointedResourceType, currentResourceEntry.getValue().getType(), typeToPointingResourcesMap); - typeToPointingResourcesMap.get(pointedResourceType).get(pointedResourceName).get - (currentResourceEntry.getValue().getType()).add(currentResourceEntry.getKey()); + typeToPointingResourcesMap.get(pointedResourceType).get(pointedResourceName) + .get(currentResourceEntry.getValue().getType()).add(currentResourceEntry.getKey()); } private void initCurrentResourceTypeInMap(String resourceName, String resourceType, @@ -225,11 +173,11 @@ public class HeatResourceValidator extends ResourceBaseValidator { typeToPointingResourcesMap.putIfAbsent(resourceType, new HashMap<>()); typeToPointingResourcesMap.get(resourceType).putIfAbsent(resourceName, new HashMap<>()); - typeToPointingResourcesMap.get(resourceType).get(resourceName).putIfAbsent - (pointingResourceType, new ArrayList<>()); + typeToPointingResourcesMap.get(resourceType).get(resourceName) + .putIfAbsent (pointingResourceType, new ArrayList<>()); } - private Set handleGetAttrBetweenResources(Map properties) { + private Set handleGetAttrBetweenResources (Map properties) { Set referencedResourcesByGetAttr = new HashSet<>(); for (Map.Entry proprtyEntry : properties.entrySet()) { referencedResourcesByGetAttr.addAll(getGetAttrReferencesInCaseOfContrail(proprtyEntry @@ -241,21 +189,12 @@ public class HeatResourceValidator extends ResourceBaseValidator { private Set getGetAttrReferencesInCaseOfContrail(Object propertyValue) { - Object value; Set getAttrReferences = new HashSet<>(); if (propertyValue instanceof Map) { - if (((Map) propertyValue).containsKey("get_attr")) { - value = ((Map) propertyValue).get("get_attr"); - if (value instanceof List) { - if (((List) value).size() == 2 && ((List) value).get(1).equals("fq_name")) { - if (((List) value).get(0) instanceof String) { - getAttrReferences.add((String) ((List) value).get(0)); - return getAttrReferences; - } else { - LOGGER.warn("invalid format of 'get_attr' function - " + propertyValue.toString()); - } - } + if (((Map) propertyValue).containsKey(GET_ATTR)) { + if (validatePropertyValueAndAddAttrReferencesInSet(propertyValue, getAttrReferences)) { + return getAttrReferences; } } else { Collection valCollection = ((Map) propertyValue).values(); @@ -272,6 +211,22 @@ public class HeatResourceValidator extends ResourceBaseValidator { return getAttrReferences; } + private boolean validatePropertyValueAndAddAttrReferencesInSet(Object propertyValue, + Set getAttrReferences) { + Object value = ((Map) propertyValue).get(GET_ATTR ); + if (value instanceof List && ((List) value).size() == 2 + && ("fq_name").equals(((List) value).get(1))) { + if (((List) value).get(0) instanceof String) { + String attrReference = (((List) value).get(0)).toString(); + getAttrReferences.add(attrReference); + return true; + } else { + LOGGER.warn("invalid format of 'get_attr' function - " + propertyValue.toString()); + } + } + return false; + } + private Set getResourcesIdsPointedByCurrentResource(String fileName, ResourceReferenceFunctions function, @@ -290,51 +245,4 @@ public class HeatResourceValidator extends ResourceBaseValidator { return referencedResources; } - - private Set checkForBaseFilePortsExistenceAndReturnSecurityGroupNamesFromOutputsIfNot( - HeatOrchestrationTemplate heatOrchestrationTemplate) { - Set securityGroupsNamesFromOutputsMap = new HashSet<>(); - - if (heatOrchestrationTemplate != null) { - Map resourceMap = heatOrchestrationTemplate.getResources(); - if (!isPortResourceExistInBaseFile(resourceMap)) { - getSecurityGroupsReferencedResourcesFromOutputs(securityGroupsNamesFromOutputsMap, - heatOrchestrationTemplate.getOutputs(), resourceMap); - } - } - return securityGroupsNamesFromOutputsMap; - } - - private boolean isPortResourceExistInBaseFile(Map resourceMap) { - for (Map.Entry resourceEntry : resourceMap.entrySet()) { - if (resourceEntry.getValue().getType() - .equals(HeatResourcesTypes.NEUTRON_PORT_RESOURCE_TYPE.getHeatResource())) { - return true; - } - } - - return false; - } - - private void getSecurityGroupsReferencedResourcesFromOutputs( - Set securityGroupsNamesFromOutputsMap, Map outputMap, - Map resourceMap) { - - if (MapUtils.isNotEmpty(outputMap)) { - for (Map.Entry outputEntry : outputMap.entrySet()) { - Object outputValue = outputEntry.getValue().getValue(); - if (Objects.nonNull(outputValue) && outputValue instanceof Map) { - String resourceName = (String) ((Map) outputValue) - .get(ResourceReferenceFunctions.GET_RESOURCE.getFunction()); - if (Objects.nonNull(resourceName)) { - Resource resource = resourceMap.get(resourceName); - if (Objects.nonNull(resource) && resource.getType().equals( - HeatResourcesTypes.NEUTRON_SECURITY_GROUP_RESOURCE_TYPE.getHeatResource())) { - securityGroupsNamesFromOutputsMap.add(outputEntry.getKey()); - } - } - } - } - } - } } -- cgit 1.2.3-korg