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 / ContrailNetworkPolicyResourceValidator.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/28/2017.
40  */
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");
44
45   @Override
46   public void validate(String fileName, Map.Entry<String, Resource> resourceEntry,
47                        GlobalValidationContext globalContext, ValidationContext validationContext) {
48     validateNetworkPolicyIsUsed(fileName, resourceEntry, globalContext,
49             (HeatResourceValidationContext) validationContext);
50
51   }
52
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());
60
61     if (MapUtils.isEmpty(referencedNetworkAttachPoliciesResources)) {
62       globalContext
63               .addMessage(
64                       fileName,
65                       ErrorLevel.WARNING,
66                       ErrorMessagesFormatBuilder
67                               .getErrorWithParameters(ERROR_CODE_HNP1,
68                                       Messages.RESOURCE_NOT_IN_USE.getErrorMessage(),
69                                       ValidatorConstants.Network_Policy, resourceEntry.getKey()));
70       return;
71     }
72
73     handleNetworkAttachPolicyReferences(fileName, resourceEntry,
74             referencedNetworkAttachPoliciesResources, globalContext);
75   }
76
77   private static void handleNetworkAttachPolicyReferences(String fileName,
78                                                           Map.Entry<String, Resource> resourceEntry,
79                                                           Map<String, Map<String, List<String>>> pointedNetworkAttachPolicies,
80                                                           GlobalValidationContext globalContext) {
81
82     Map<String, List<String>> resourcesPointingToCurrNetworkAttachPolicy =
83             pointedNetworkAttachPolicies.get(resourceEntry.getKey());
84     if (isNetworkAttachPolicyNotInUse(resourcesPointingToCurrNetworkAttachPolicy)) {
85       globalContext
86               .addMessage(
87                       fileName,
88                       ErrorLevel.WARNING,
89                       ErrorMessagesFormatBuilder
90                               .getErrorWithParameters(ERROR_CODE_HNP2,
91                                       Messages.RESOURCE_NOT_IN_USE.getErrorMessage(),
92                                       ValidatorConstants.Network_Policy, resourceEntry.getKey()));
93     }
94   }
95
96   private static boolean isNetworkAttachPolicyNotInUse(
97           Map<String, List<String>> resourcesPointingToCurrNetworkAttachPolicy) {
98     return MapUtils.isEmpty(resourcesPointingToCurrNetworkAttachPolicy);
99   }
100
101 }