Upgrade Vulnerable Direct Dependencies [snakeyaml]
[sdc.git] / openecomp-be / lib / openecomp-sdc-validation-lib / openecomp-sdc-validation-impl / src / main / java / org / openecomp / sdc / validation / impl / validators / namingconvention / NeutronPortNamingConventionValidator.java
1 /*
2  * Copyright © 2016-2017 European Support Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.openecomp.sdc.validation.impl.validators.namingconvention;
18
19 import org.apache.commons.collections4.MapUtils;
20 import org.openecomp.core.validation.ErrorMessageCode;
21 import org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder;
22 import org.openecomp.core.validation.types.GlobalValidationContext;
23 import org.openecomp.sdc.common.errors.Messages;
24 import org.openecomp.sdc.datatypes.error.ErrorLevel;
25 import org.openecomp.sdc.heat.datatypes.model.HeatOrchestrationTemplate;
26 import org.openecomp.sdc.heat.datatypes.model.HeatResourcesTypes;
27 import org.openecomp.sdc.heat.datatypes.model.Resource;
28 import org.openecomp.sdc.validation.ResourceValidator;
29 import org.openecomp.sdc.validation.ValidationContext;
30 import org.openecomp.sdc.validation.type.NamingConventionValidationContext;
31 import org.openecomp.sdc.validation.util.ValidationUtil;
32
33 import java.util.Arrays;
34 import java.util.List;
35 import java.util.Map;
36
37 import static java.util.Objects.nonNull;
38
39 public class NeutronPortNamingConventionValidator implements ResourceValidator {
40     private static final ErrorMessageCode ERROR_CODE_NNP1 = new ErrorMessageCode("NNP1");
41     private static final ErrorMessageCode ERROR_CODE_NNP2 = new ErrorMessageCode("NNP2");
42     private static final ErrorMessageCode ERROR_CODE_NNP3 = new ErrorMessageCode("NNP3");
43
44     @Override
45     public void validate(String fileName, Map.Entry<String, Resource> resourceEntry,
46                          GlobalValidationContext globalContext, ValidationContext validationContext) {
47         NamingConventionValidationContext namingConventionValidationContext = (NamingConventionValidationContext) validationContext;
48         validatePortNetworkNamingConvention(fileName, namingConventionValidationContext.getHeatOrchestrationTemplate(), globalContext);
49         validateFixedIpsNamingConvention(fileName, namingConventionValidationContext.getHeatOrchestrationTemplate(), globalContext);
50     }
51
52     private void validatePortNetworkNamingConvention(String fileName,
53                                                      HeatOrchestrationTemplate heatOrchestrationTemplate,
54                                                      GlobalValidationContext globalContext) {
55         if (MapUtils.isEmpty(heatOrchestrationTemplate.getResources())) {
56             return;
57         }
58         String[] regexList = {".*_net_id", ".*_net_name", ".*_net_fqdn"};
59
60         heatOrchestrationTemplate
61                 .getResources()
62                 .entrySet()
63                 .stream()
64                 .filter(entry -> entry.getValue().getType().equals(HeatResourcesTypes.NEUTRON_PORT_RESOURCE_TYPE.getHeatResource()))
65                 .forEach(entry -> entry.getValue()
66                         .getProperties()
67                         .entrySet()
68                         .stream()
69                         .filter(propertyEntry -> ("network").equalsIgnoreCase(propertyEntry.getKey()) || ("network_id").equals(propertyEntry.getKey()))
70                         .forEach(propertyEntry -> validateParamNamingConvention(fileName, entry.getKey(),
71                                 propertyEntry.getValue(), regexList,
72                                 Messages.PARAMETER_NAME_NOT_ALIGNED_WITH_GUIDELINES, globalContext)));
73     }
74
75     private void validateFixedIpsNamingConvention(String fileName,
76                                                   HeatOrchestrationTemplate heatOrchestrationTemplate,
77                                                   GlobalValidationContext globalContext) {
78         if (MapUtils.isEmpty(heatOrchestrationTemplate.getResources())) {
79             return;
80         }
81
82         heatOrchestrationTemplate.getResources()
83                 .entrySet()
84                 .stream()
85                 .filter(entry -> HeatResourcesTypes.findByHeatResource(entry.getValue().getType()) != null)
86                 .filter(entry -> HeatResourcesTypes.findByHeatResource(entry.getValue().getType())
87                         .equals(HeatResourcesTypes.NEUTRON_PORT_RESOURCE_TYPE))
88                 .forEach(entry -> checkNeutronPortFixedIpsName(fileName, entry, globalContext));
89     }
90
91     private void checkNeutronPortFixedIpsName(String fileName,
92                                               Map.Entry<String, Resource> resourceEntry,
93                                               GlobalValidationContext globalContext) {
94         String[] regexList = {"[^_]+_[^_]+_ips", "[^_]+_[^_]+_v6_ips", "[^_]+_[^_]+_ip_(\\d+)",
95                 "[^_]+_[^_]+_v6_ip_(\\d+)", "[^_]+_[^_]+_[^_]+_ips", "[^_]+_[^_]+_[^_]+_v6_ips",
96                 "[^_]+_[^_]+_[^_]+_ip_(\\d+)", "[^_]+_[^_]+_[^_]+_v6_ip_(\\d+)"};
97
98         if (MapUtils.isEmpty(resourceEntry.getValue().getProperties())) {
99             return;
100         }
101
102         Map<String, Object> propertiesMap = resourceEntry.getValue().getProperties();
103         Object fixedIps = propertiesMap.get("fixed_ips");
104         if (nonNull(fixedIps) && fixedIps instanceof List) {
105             List<Object> fixedIpsList = (List<Object>) fixedIps;
106             for (Object fixedIpsObject : fixedIpsList) {
107                 Map.Entry<String, Object> fixedIpsEntry = ((Map<String, Object>) fixedIpsObject).entrySet().iterator().next();
108                 validateFixedIpsName(fileName, resourceEntry, globalContext, regexList, fixedIpsEntry);
109             }
110         }
111     }
112
113     private void validateFixedIpsName(String fileName, Map.Entry<String, Resource> resourceEntry, GlobalValidationContext globalContext,
114                                       String[] regexList, Map.Entry<String, Object> fixedIpsEntry) {
115         if (nonNull(fixedIpsEntry)) {
116             if (fixedIpsEntry.getValue() instanceof Map) {
117                 String fixedIpsName = ValidationUtil.getWantedNameFromPropertyValueGetParam(fixedIpsEntry.getValue());
118                 if (nonNull(fixedIpsName) && !ValidationUtil.evalPattern(fixedIpsName, regexList)) {
119                     globalContext.addMessage(fileName, ErrorLevel.WARNING, ErrorMessagesFormatBuilder
120                             .getErrorWithParameters(ERROR_CODE_NNP1, Messages.PARAMETER_NAME_NOT_ALIGNED_WITH_GUIDELINES.getErrorMessage(),
121                                     "Port", "Fixed_IPS", fixedIpsName, resourceEntry.getKey()));
122                 }
123             } else {
124                 globalContext.addMessage(fileName, ErrorLevel.WARNING, ErrorMessagesFormatBuilder.
125                         getErrorWithParameters(ERROR_CODE_NNP2, Messages.MISSING_GET_PARAM.getErrorMessage(), "fixed_ips", resourceEntry.getKey()));
126             }
127         }
128     }
129
130     private void validateParamNamingConvention(String fileName, String resourceId, Object propertyValue, String[] regexList,
131                                                Messages message, GlobalValidationContext globalContext) {
132         if (propertyValue instanceof Map) {
133             Object paramName = ((Map) propertyValue).get("get_param");
134             if (paramName instanceof String && !ValidationUtil.evalPattern(paramName, regexList)) {
135                 globalContext.addMessage(fileName, ErrorLevel.WARNING, ErrorMessagesFormatBuilder
136                         .getErrorWithParameters(ERROR_CODE_NNP3, message.getErrorMessage(), "Port", "Network", (String) paramName, resourceId));
137             }
138         } else {
139             globalContext.addMessage(fileName, ErrorLevel.WARNING, ErrorMessagesFormatBuilder
140                     .getErrorWithParameters(ERROR_CODE_NNP2, Messages.MISSING_GET_PARAM.getErrorMessage(), "network or network_id", resourceId));
141         }
142     }
143 }