1 package org.openecomp.sdc.validation.impl.validators.heatresource;
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;
18 import java.util.List;
22 * Created by TALIO on 2/28/2017.
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");
29 public void validate(String fileName, Map.Entry<String, Resource> resourceEntry,
30 GlobalValidationContext globalContext, ValidationContext validationContext) {
31 validateNetworkPolicyIsUsed(fileName, resourceEntry, globalContext,
32 (HeatResourceValidationContext) validationContext);
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());
44 if (MapUtils.isEmpty(referencedNetworkAttachPoliciesResources)) {
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);
58 handleNetworkAttachPolicyReferences(fileName, resourceEntry,
59 referencedNetworkAttachPoliciesResources, globalContext);
62 private static void handleNetworkAttachPolicyReferences(String fileName,
63 Map.Entry<String, Resource> resourceEntry,
64 Map<String, Map<String, List<String>>> pointedNetworkAttachPolicies,
65 GlobalValidationContext globalContext) {
67 Map<String, List<String>> resourcesPointingToCurrNetworkAttachPolicy =
68 pointedNetworkAttachPolicies.get(resourceEntry.getKey());
69 if (isNetworkAttachPolicyNotInUse(resourcesPointingToCurrNetworkAttachPolicy)) {
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);
83 private static boolean isNetworkAttachPolicyNotInUse(
84 Map<String, List<String>> resourcesPointingToCurrNetworkAttachPolicy) {
85 return MapUtils.isEmpty(resourcesPointingToCurrNetworkAttachPolicy);