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/27/2017.
41 public class NeutronSecurityGroupResourceValidator implements ResourceValidator {
42 private static final ErrorMessageCode ERROR_CODE_HSG1 = new ErrorMessageCode("HSG1");
45 public void validate(String fileName, Map.Entry<String, Resource> resourceEntry,
46 GlobalValidationContext globalContext, ValidationContext validationContext) {
48 HeatResourceValidationContext heatResourceValidationContext =
49 (HeatResourceValidationContext) validationContext;
50 validateSecurityGroupIsUsed(fileName, resourceEntry, heatResourceValidationContext,
54 private void validateSecurityGroupIsUsed(String fileName,
55 Map.Entry<String, Resource> resourceEntry,
56 HeatResourceValidationContext
57 heatResourceValidationContext,
58 GlobalValidationContext globalContext) {
60 Map<String, Map<String, List<String>>> securityGroupsPointedByOtherResources =
61 heatResourceValidationContext.getFileLevelResourceDependencies()
62 .get(HeatResourcesTypes.NEUTRON_SECURITY_GROUP_RESOURCE_TYPE.getHeatResource());
64 if (MapUtils.isEmpty(securityGroupsPointedByOtherResources)) {
68 Map<String, List<String>> resourcesPointingCurrSecurityGroup =
69 securityGroupsPointedByOtherResources.get(resourceEntry.getKey());
71 if (isSecurityGroupNotInUse(resourcesPointingCurrSecurityGroup)) {
72 globalContext.addMessage(
75 ErrorMessagesFormatBuilder
76 .getErrorWithParameters(
77 ERROR_CODE_HSG1, Messages.RESOURCE_NOT_IN_USE.getErrorMessage(),
78 ValidatorConstants.Security_Group, resourceEntry.getKey()));
83 private boolean isSecurityGroupNotInUse(Map<String, List<String>>
84 referencingResourcesToCurrSecurityGroup) {
85 return MapUtils.isEmpty(referencingResourcesToCurrSecurityGroup);