8aa5714f2c69caa9ef4f674a80c2e843248e53ad
[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.context.impl.MdcDataDebugMessage;
12 import org.openecomp.sdc.logging.types.LoggerErrorDescription;
13 import org.openecomp.sdc.logging.types.LoggerTragetServiceName;
14 import org.openecomp.sdc.validation.ResourceValidator;
15 import org.openecomp.sdc.validation.ValidationContext;
16 import org.openecomp.sdc.validation.type.HeatResourceValidationContext;
17 import org.openecomp.sdc.validation.type.ValidatorConstants;
18
19 import java.util.List;
20 import java.util.Map;
21
22 /**
23  * Created by TALIO on 2/28/2017.
24  */
25 public class ContrailNetworkPolicyResourceValidator implements ResourceValidator {
26   private static MdcDataDebugMessage mdcDataDebugMessage = new MdcDataDebugMessage();
27   private static final ErrorMessageCode ERROR_CODE_HNP1 = new ErrorMessageCode("HNP1");
28   private static final ErrorMessageCode ERROR_CODE_HNP2 = new ErrorMessageCode("HNP2");
29
30   @Override
31   public void validate(String fileName, Map.Entry<String, Resource> resourceEntry,
32                        GlobalValidationContext globalContext, ValidationContext validationContext) {
33     validateNetworkPolicyIsUsed(fileName, resourceEntry, globalContext,
34             (HeatResourceValidationContext) validationContext);
35
36   }
37
38   private static void validateNetworkPolicyIsUsed(String fileName,
39                                                   Map.Entry<String, Resource> resourceEntry,
40                                                   GlobalValidationContext globalContext,
41                                                   HeatResourceValidationContext validationContext) {
42     mdcDataDebugMessage.debugEntryMessage("file", fileName);
43
44     Map<String, Map<String, List<String>>> referencedNetworkAttachPoliciesResources =
45             validationContext.getFileLevelResourceDependencies()
46                     .get(HeatResourcesTypes.CONTRAIL_NETWORK_RULE_RESOURCE_TYPE.getHeatResource());
47
48     if (MapUtils.isEmpty(referencedNetworkAttachPoliciesResources)) {
49       globalContext
50               .addMessage(
51                       fileName,
52                       ErrorLevel.WARNING,
53                       ErrorMessagesFormatBuilder
54                               .getErrorWithParameters(ERROR_CODE_HNP1,
55                                       Messages.RESOURCE_NOT_IN_USE.getErrorMessage(),
56                                       ValidatorConstants.Network_Policy, resourceEntry.getKey()),
57                       LoggerTragetServiceName.VALIDATE_ATTACH_POLICY_IN_USE,
58                       LoggerErrorDescription.NETWORK_ATTACH_POLICY_NOT_IN_USE);
59       return;
60     }
61
62     handleNetworkAttachPolicyReferences(fileName, resourceEntry,
63             referencedNetworkAttachPoliciesResources, globalContext);
64
65     mdcDataDebugMessage.debugExitMessage("file", fileName);
66
67   }
68
69   private static void handleNetworkAttachPolicyReferences(String fileName,
70                                                           Map.Entry<String, Resource> resourceEntry,
71                                                           Map<String, Map<String, List<String>>> pointedNetworkAttachPolicies,
72                                                           GlobalValidationContext globalContext) {
73
74     Map<String, List<String>> resourcesPointingToCurrNetworkAttachPolicy =
75             pointedNetworkAttachPolicies.get(resourceEntry.getKey());
76     if (isNetworkAttachPolicyNotInUse(resourcesPointingToCurrNetworkAttachPolicy)) {
77       globalContext
78               .addMessage(
79                       fileName,
80                       ErrorLevel.WARNING,
81                       ErrorMessagesFormatBuilder
82                               .getErrorWithParameters(ERROR_CODE_HNP2,
83                                       Messages.RESOURCE_NOT_IN_USE.getErrorMessage(),
84                                       ValidatorConstants.Network_Policy, resourceEntry.getKey()),
85                       LoggerTragetServiceName.VALIDATE_ATTACH_POLICY_IN_USE,
86                       LoggerErrorDescription.NETWORK_ATTACH_POLICY_NOT_IN_USE);
87     }
88   }
89
90   private static boolean isNetworkAttachPolicyNotInUse(
91           Map<String, List<String>> resourcesPointingToCurrNetworkAttachPolicy) {
92     return MapUtils.isEmpty(resourcesPointingToCurrNetworkAttachPolicy);
93   }
94
95 }