2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
20 package org.openecomp.sdc.validation.impl.validators.heatresource;
22 import org.apache.commons.collections4.MapUtils;
23 import org.openecomp.core.validation.ErrorMessageCode;
24 import org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder;
25 import org.openecomp.core.validation.types.GlobalValidationContext;
26 import org.openecomp.sdc.common.errors.Messages;
27 import org.openecomp.sdc.datatypes.error.ErrorLevel;
28 import org.openecomp.sdc.heat.datatypes.model.HeatResourcesTypes;
29 import org.openecomp.sdc.heat.datatypes.model.Resource;
30 import org.openecomp.sdc.validation.ResourceValidator;
31 import org.openecomp.sdc.validation.ValidationContext;
32 import org.openecomp.sdc.validation.type.HeatResourceValidationContext;
33 import org.openecomp.sdc.validation.type.ValidatorConstants;
35 import java.util.List;
39 * Created by TALIO on 2/28/2017.
41 public class ContrailNetworkPolicyResourceValidator implements ResourceValidator {
42 private static final ErrorMessageCode ERROR_CODE_HNP1 = new ErrorMessageCode("HNP1");
43 private static final ErrorMessageCode ERROR_CODE_HNP2 = new ErrorMessageCode("HNP2");
46 public void validate(String fileName, Map.Entry<String, Resource> resourceEntry,
47 GlobalValidationContext globalContext, ValidationContext validationContext) {
48 validateNetworkPolicyIsUsed(fileName, resourceEntry, globalContext,
49 (HeatResourceValidationContext) validationContext);
53 private static void validateNetworkPolicyIsUsed(String fileName,
54 Map.Entry<String, Resource> resourceEntry,
55 GlobalValidationContext globalContext,
56 HeatResourceValidationContext validationContext) {
57 Map<String, Map<String, List<String>>> referencedNetworkAttachPoliciesResources =
58 validationContext.getFileLevelResourceDependencies()
59 .get(HeatResourcesTypes.CONTRAIL_NETWORK_RULE_RESOURCE_TYPE.getHeatResource());
61 if (MapUtils.isEmpty(referencedNetworkAttachPoliciesResources)) {
66 ErrorMessagesFormatBuilder
67 .getErrorWithParameters(ERROR_CODE_HNP1,
68 Messages.RESOURCE_NOT_IN_USE.getErrorMessage(),
69 ValidatorConstants.Network_Policy, resourceEntry.getKey()));
73 handleNetworkAttachPolicyReferences(fileName, resourceEntry,
74 referencedNetworkAttachPoliciesResources, globalContext);
77 private static void handleNetworkAttachPolicyReferences(String fileName,
78 Map.Entry<String, Resource> resourceEntry,
79 Map<String, Map<String, List<String>>> pointedNetworkAttachPolicies,
80 GlobalValidationContext globalContext) {
82 Map<String, List<String>> resourcesPointingToCurrNetworkAttachPolicy =
83 pointedNetworkAttachPolicies.get(resourceEntry.getKey());
84 if (isNetworkAttachPolicyNotInUse(resourcesPointingToCurrNetworkAttachPolicy)) {
89 ErrorMessagesFormatBuilder
90 .getErrorWithParameters(ERROR_CODE_HNP2,
91 Messages.RESOURCE_NOT_IN_USE.getErrorMessage(),
92 ValidatorConstants.Network_Policy, resourceEntry.getKey()));
96 private static boolean isNetworkAttachPolicyNotInUse(
97 Map<String, List<String>> resourcesPointingToCurrNetworkAttachPolicy) {
98 return MapUtils.isEmpty(resourcesPointingToCurrNetworkAttachPolicy);