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