2d7b5ada48b8b4004a614f2c213064e1eddde795
[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/28/2017.
21  */
22 public class ContrailNetworkPolicyResourceValidator implements ResourceValidator {
23   private static final ErrorMessageCode ERROR_CODE_HNP1 = new ErrorMessageCode("HNP1");
24   private static final ErrorMessageCode ERROR_CODE_HNP2 = new ErrorMessageCode("HNP2");
25
26   @Override
27   public void validate(String fileName, Map.Entry<String, Resource> resourceEntry,
28                        GlobalValidationContext globalContext, ValidationContext validationContext) {
29     validateNetworkPolicyIsUsed(fileName, resourceEntry, globalContext,
30             (HeatResourceValidationContext) validationContext);
31
32   }
33
34   private static void validateNetworkPolicyIsUsed(String fileName,
35                                                   Map.Entry<String, Resource> resourceEntry,
36                                                   GlobalValidationContext globalContext,
37                                                   HeatResourceValidationContext validationContext) {
38     Map<String, Map<String, List<String>>> referencedNetworkAttachPoliciesResources =
39             validationContext.getFileLevelResourceDependencies()
40                     .get(HeatResourcesTypes.CONTRAIL_NETWORK_RULE_RESOURCE_TYPE.getHeatResource());
41
42     if (MapUtils.isEmpty(referencedNetworkAttachPoliciesResources)) {
43       globalContext
44               .addMessage(
45                       fileName,
46                       ErrorLevel.WARNING,
47                       ErrorMessagesFormatBuilder
48                               .getErrorWithParameters(ERROR_CODE_HNP1,
49                                       Messages.RESOURCE_NOT_IN_USE.getErrorMessage(),
50                                       ValidatorConstants.Network_Policy, resourceEntry.getKey()));
51       return;
52     }
53
54     handleNetworkAttachPolicyReferences(fileName, resourceEntry,
55             referencedNetworkAttachPoliciesResources, globalContext);
56   }
57
58   private static void handleNetworkAttachPolicyReferences(String fileName,
59                                                           Map.Entry<String, Resource> resourceEntry,
60                                                           Map<String, Map<String, List<String>>> pointedNetworkAttachPolicies,
61                                                           GlobalValidationContext globalContext) {
62
63     Map<String, List<String>> resourcesPointingToCurrNetworkAttachPolicy =
64             pointedNetworkAttachPolicies.get(resourceEntry.getKey());
65     if (isNetworkAttachPolicyNotInUse(resourcesPointingToCurrNetworkAttachPolicy)) {
66       globalContext
67               .addMessage(
68                       fileName,
69                       ErrorLevel.WARNING,
70                       ErrorMessagesFormatBuilder
71                               .getErrorWithParameters(ERROR_CODE_HNP2,
72                                       Messages.RESOURCE_NOT_IN_USE.getErrorMessage(),
73                                       ValidatorConstants.Network_Policy, resourceEntry.getKey()));
74     }
75   }
76
77   private static boolean isNetworkAttachPolicyNotInUse(
78           Map<String, List<String>> resourcesPointingToCurrNetworkAttachPolicy) {
79     return MapUtils.isEmpty(resourcesPointingToCurrNetworkAttachPolicy);
80   }
81
82 }