0a066f5c387c9a1dda0eae5f486d88dd586e975f
[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.logging.types.LoggerErrorDescription;
12 import org.openecomp.sdc.logging.types.LoggerTragetServiceName;
13 import org.openecomp.sdc.validation.ResourceValidator;
14 import org.openecomp.sdc.validation.ValidationContext;
15 import org.openecomp.sdc.validation.type.HeatResourceValidationContext;
16 import org.openecomp.sdc.validation.type.ValidatorConstants;
17
18 import java.util.List;
19 import java.util.Map;
20
21 /**
22  * Created by TALIO on 2/27/2017.
23  */
24 public class NeutronSecurityGroupResourceValidator implements ResourceValidator {
25   private static final ErrorMessageCode ERROR_CODE_HSG1 = new ErrorMessageCode("HSG1");
26
27   @Override
28   public void validate(String fileName, Map.Entry<String, Resource> resourceEntry,
29                        GlobalValidationContext globalContext, ValidationContext validationContext) {
30
31     HeatResourceValidationContext heatResourceValidationContext =
32             (HeatResourceValidationContext) validationContext;
33     validateSecurityGroupIsUsed(fileName, resourceEntry, heatResourceValidationContext,
34             globalContext);
35   }
36
37   private void validateSecurityGroupIsUsed(String fileName,
38                                            Map.Entry<String, Resource> resourceEntry,
39                                            HeatResourceValidationContext
40                                                    heatResourceValidationContext,
41                                            GlobalValidationContext globalContext) {
42
43     Map<String, Map<String, List<String>>> securityGroupsPointedByOtherResources =
44             heatResourceValidationContext.getFileLevelResourceDependencies()
45                     .get(HeatResourcesTypes.NEUTRON_SECURITY_GROUP_RESOURCE_TYPE.getHeatResource());
46
47     if (MapUtils.isEmpty(securityGroupsPointedByOtherResources)) {
48       return;
49     }
50
51     Map<String, List<String>> resourcesPointingCurrSecurityGroup =
52             securityGroupsPointedByOtherResources.get(resourceEntry.getKey());
53
54     if (isSecurityGroupNotInUse(resourcesPointingCurrSecurityGroup)) {
55       globalContext.addMessage(
56               fileName,
57               ErrorLevel.WARNING,
58               ErrorMessagesFormatBuilder
59                       .getErrorWithParameters(
60                               ERROR_CODE_HSG1, Messages.RESOURCE_NOT_IN_USE.getErrorMessage(),
61                               ValidatorConstants.Security_Group, resourceEntry.getKey()),
62               LoggerTragetServiceName.VALIDATE_ALL_SERVER_GROUP_OR_SECURITY_GROUP_IN_USE,
63               LoggerErrorDescription.SERVER_GROUP_SECURITY_GROUP_NOT_IN_USE);
64     }
65
66   }
67
68   private boolean isSecurityGroupNotInUse(Map<String, List<String>>
69                                                   referencingResourcesToCurrSecurityGroup) {
70     return MapUtils.isEmpty(referencingResourcesToCurrSecurityGroup);
71   }
72 }