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