Added oparent to sdc main
[sdc.git] / openecomp-be / lib / openecomp-sdc-validation-lib / openecomp-sdc-validation-impl / src / main / java / org / openecomp / sdc / validation / impl / validators / heatresource / NeutronSecurityGroupResourceValidator.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20 package org.openecomp.sdc.validation.impl.validators.heatresource;
21
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;
34
35 import java.util.List;
36 import java.util.Map;
37
38 /**
39  * Created by TALIO on 2/27/2017.
40  */
41 public class NeutronSecurityGroupResourceValidator implements ResourceValidator {
42   private static final ErrorMessageCode ERROR_CODE_HSG1 = new ErrorMessageCode("HSG1");
43
44   @Override
45   public void validate(String fileName, Map.Entry<String, Resource> resourceEntry,
46                        GlobalValidationContext globalContext, ValidationContext validationContext) {
47
48     HeatResourceValidationContext heatResourceValidationContext =
49             (HeatResourceValidationContext) validationContext;
50     validateSecurityGroupIsUsed(fileName, resourceEntry, heatResourceValidationContext,
51             globalContext);
52   }
53
54   private void validateSecurityGroupIsUsed(String fileName,
55                                            Map.Entry<String, Resource> resourceEntry,
56                                            HeatResourceValidationContext
57                                                    heatResourceValidationContext,
58                                            GlobalValidationContext globalContext) {
59
60     Map<String, Map<String, List<String>>> securityGroupsPointedByOtherResources =
61             heatResourceValidationContext.getFileLevelResourceDependencies()
62                     .get(HeatResourcesTypes.NEUTRON_SECURITY_GROUP_RESOURCE_TYPE.getHeatResource());
63
64     if (MapUtils.isEmpty(securityGroupsPointedByOtherResources)) {
65       return;
66     }
67
68     Map<String, List<String>> resourcesPointingCurrSecurityGroup =
69             securityGroupsPointedByOtherResources.get(resourceEntry.getKey());
70
71     if (isSecurityGroupNotInUse(resourcesPointingCurrSecurityGroup)) {
72       globalContext.addMessage(
73               fileName,
74               ErrorLevel.WARNING,
75               ErrorMessagesFormatBuilder
76                       .getErrorWithParameters(
77                               ERROR_CODE_HSG1, Messages.RESOURCE_NOT_IN_USE.getErrorMessage(),
78                               ValidatorConstants.Security_Group, resourceEntry.getKey()));
79     }
80
81   }
82
83   private boolean isSecurityGroupNotInUse(Map<String, List<String>>
84                                                   referencingResourcesToCurrSecurityGroup) {
85     return MapUtils.isEmpty(referencingResourcesToCurrSecurityGroup);
86   }
87 }