[SDC-29] Amdocs OnBoard 1707 initial commit.
[sdc.git] / openecomp-be / lib / openecomp-sdc-validation-lib / openecomp-sdc-validation-impl / src / main / java / org / openecomp / sdc / validation / impl / validators / heatresource / NeutronSecurityGroupResourceValidator.java
1 package org.openecomp.sdc.validation.impl.validators.heatresource;
2
3 import org.apache.commons.collections4.MapUtils;
4 import org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder;
5 import org.openecomp.core.validation.types.GlobalValidationContext;
6 import org.openecomp.sdc.common.errors.Messages;
7 import org.openecomp.sdc.datatypes.error.ErrorLevel;
8 import org.openecomp.sdc.heat.datatypes.model.HeatResourcesTypes;
9 import org.openecomp.sdc.heat.datatypes.model.Resource;
10 import org.openecomp.sdc.logging.types.LoggerErrorDescription;
11 import org.openecomp.sdc.logging.types.LoggerTragetServiceName;
12 import org.openecomp.sdc.validation.ResourceValidator;
13 import org.openecomp.sdc.validation.ValidationContext;
14 import org.openecomp.sdc.validation.type.HeatResourceValidationContext;
15 import org.openecomp.sdc.validation.type.ValidatorConstants;
16
17 import java.util.List;
18 import java.util.Map;
19
20 /**
21  * Created by TALIO on 2/27/2017.
22  */
23 public class NeutronSecurityGroupResourceValidator implements ResourceValidator {
24   @Override
25   public void validate(String fileName, Map.Entry<String, Resource> resourceEntry,
26                        GlobalValidationContext globalContext, ValidationContext validationContext) {
27
28     HeatResourceValidationContext heatResourceValidationContext =
29         (HeatResourceValidationContext) validationContext;
30     validateSecurityGroupIsUsed(fileName, resourceEntry, heatResourceValidationContext, globalContext);
31   }
32
33   public void validateSecurityGroupIsUsed(String fileName, Map.Entry<String, Resource> resourceEntry,
34                                           HeatResourceValidationContext
35                                               heatResourceValidationContext,
36                                           GlobalValidationContext globalContext) {
37
38     Map<String, Map<String, List<String>>> securityGroupsPointedByOtherResources =
39         heatResourceValidationContext.getFileLevelResourceDependencies().
40             get(HeatResourcesTypes.NEUTRON_SECURITY_GROUP_RESOURCE_TYPE.getHeatResource());
41
42     if (MapUtils.isEmpty(securityGroupsPointedByOtherResources)) {
43       return;
44     }
45
46     Map<String, List<String>> resourcesPointingCurrSecurityGroup =
47         securityGroupsPointedByOtherResources.get(resourceEntry.getKey());
48
49     if(isSecurityGroupNotInUse(resourcesPointingCurrSecurityGroup)){
50       globalContext.addMessage(
51           fileName,
52           ErrorLevel.WARNING,
53           ErrorMessagesFormatBuilder
54               .getErrorWithParameters(
55                   Messages.RESOURCE_NOT_IN_USE.getErrorMessage(),
56                   ValidatorConstants.Security_Group, resourceEntry.getKey()),
57           LoggerTragetServiceName.VALIDATE_ALL_SERVER_GROUP_OR_SECURITY_GROUP_IN_USE,
58           LoggerErrorDescription.SERVER_GROUP_SECURITY_GROUP_NOT_IN_USE);
59     }
60
61   }
62
63   public boolean isSecurityGroupNotInUse(Map<String, List<String>>
64                                              referencingResourcesToCurrSecurityGroup){
65     return MapUtils.isEmpty(referencingResourcesToCurrSecurityGroup);
66   }
67 }