2 * Copyright © 2016-2017 European Support Limited
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.openecomp.sdc.validation.impl.validators.namingconvention;
19 import static java.util.Objects.nonNull;
20 import org.apache.commons.collections4.MapUtils;
21 import org.openecomp.core.validation.ErrorMessageCode;
22 import org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder;
23 import org.openecomp.core.validation.types.GlobalValidationContext;
24 import org.openecomp.sdc.common.errors.Messages;
25 import org.openecomp.sdc.datatypes.error.ErrorLevel;
26 import org.openecomp.sdc.heat.datatypes.model.HeatOrchestrationTemplate;
27 import org.openecomp.sdc.heat.datatypes.model.HeatResourcesTypes;
28 import org.openecomp.sdc.heat.datatypes.model.Resource;
29 import org.openecomp.sdc.logging.context.impl.MdcDataDebugMessage;
30 import org.openecomp.sdc.logging.types.LoggerErrorDescription;
31 import org.openecomp.sdc.logging.types.LoggerTragetServiceName;
32 import org.openecomp.sdc.validation.ResourceValidator;
33 import org.openecomp.sdc.validation.ValidationContext;
34 import org.openecomp.sdc.validation.type.NamingConventionValidationContext;
35 import org.openecomp.sdc.validation.util.ValidationUtil;
37 import java.util.List;
40 public class NeutronPortNamingConventionValidator implements ResourceValidator {
41 private static final MdcDataDebugMessage MDC_DATA_DEBUG_MESSAGE = new MdcDataDebugMessage();
42 private static final ErrorMessageCode ERROR_CODE_NNP1 = new ErrorMessageCode("NNP1");
43 private static final ErrorMessageCode ERROR_CODE_NNP2 = new ErrorMessageCode("NNP2");
44 private static final ErrorMessageCode ERROR_CODE_NNP3 = new ErrorMessageCode("NNP3");
47 public void validate(String fileName, Map.Entry<String, Resource> resourceEntry,
48 GlobalValidationContext globalContext, ValidationContext validationContext) {
50 NamingConventionValidationContext namingConventionValidationContext =
51 (NamingConventionValidationContext)validationContext;
52 validatePortNetworkNamingConvention(fileName, namingConventionValidationContext.getHeatOrchestrationTemplate(), globalContext);
53 validateFixedIpsNamingConvention(fileName, namingConventionValidationContext.getHeatOrchestrationTemplate(), globalContext);
56 private void validatePortNetworkNamingConvention(String fileName,
57 HeatOrchestrationTemplate heatOrchestrationTemplate,
58 GlobalValidationContext globalContext) {
60 MDC_DATA_DEBUG_MESSAGE.debugEntryMessage("file", fileName);
62 if (MapUtils.isEmpty(heatOrchestrationTemplate.getResources())) {
63 MDC_DATA_DEBUG_MESSAGE.debugExitMessage("file", fileName);
66 String[] regexList = new String[]{".*_net_id", ".*_net_name", ".*_net_fqdn"};
68 heatOrchestrationTemplate
72 .filter(entry -> entry.getValue().getType()
73 .equals(HeatResourcesTypes.NEUTRON_PORT_RESOURCE_TYPE.getHeatResource()))
74 .forEach(entry -> entry.getValue()
78 .filter(propertyEntry ->
79 ("network").equalsIgnoreCase(propertyEntry.getKey())
80 || ("network_id").equals(propertyEntry.getKey()))
81 .forEach(propertyEntry -> validateParamNamingConvention(fileName, entry.getKey(),
82 propertyEntry.getValue(), regexList,
83 Messages.PARAMETER_NAME_NOT_ALIGNED_WITH_GUIDELINES, globalContext)));
85 MDC_DATA_DEBUG_MESSAGE.debugExitMessage("file", fileName);
88 private void validateFixedIpsNamingConvention(String fileName,
89 HeatOrchestrationTemplate heatOrchestrationTemplate,
90 GlobalValidationContext globalContext) {
92 MDC_DATA_DEBUG_MESSAGE.debugEntryMessage("file", fileName);
94 if (MapUtils.isEmpty(heatOrchestrationTemplate.getResources())) {
95 MDC_DATA_DEBUG_MESSAGE.debugExitMessage("file", fileName);
99 heatOrchestrationTemplate.getResources()
102 .filter(entry -> HeatResourcesTypes.findByHeatResource(entry.getValue().getType()) != null)
103 .filter(entry -> HeatResourcesTypes.findByHeatResource(entry.getValue().getType())
104 .equals(HeatResourcesTypes.NEUTRON_PORT_RESOURCE_TYPE))
105 .forEach(entry -> checkNeutronPortFixedIpsName(fileName, entry, globalContext));
107 MDC_DATA_DEBUG_MESSAGE.debugExitMessage("file", fileName);
110 private void checkNeutronPortFixedIpsName(String fileName,
111 Map.Entry<String, Resource> resourceEntry,
112 GlobalValidationContext globalContext) {
114 new String[]{"[^_]+_[^_]+_ips", "[^_]+_[^_]+_v6_ips", "[^_]+_[^_]+_ip_(\\d+)",
115 "[^_]+_[^_]+_v6_ip_(\\d+)"};
117 if (MapUtils.isEmpty(resourceEntry.getValue().getProperties())) {
121 Map<String, Object> propertiesMap = resourceEntry.getValue().getProperties();
122 Object fixedIps = propertiesMap.get("fixed_ips");
123 if (nonNull(fixedIps) && fixedIps instanceof List) {
124 List<Object> fixedIpsList = (List<Object>) fixedIps;
125 for (Object fixedIpsObject : fixedIpsList) {
126 Map.Entry<String, Object> fixedIpsEntry =
127 ((Map<String, Object>) fixedIpsObject).entrySet().iterator().next();
129 validateFixedIpsName(fileName, resourceEntry, globalContext, regexList, fixedIpsEntry);
136 private void validateFixedIpsName(String fileName, Map.Entry<String, Resource> resourceEntry,
137 GlobalValidationContext globalContext,
138 String[] regexList, Map.Entry<String, Object> fixedIpsEntry) {
139 if (nonNull(fixedIpsEntry)) {
140 if (fixedIpsEntry.getValue() instanceof Map) {
142 String fixedIpsName = ValidationUtil
143 .getWantedNameFromPropertyValueGetParam(fixedIpsEntry.getValue());
144 if (nonNull(fixedIpsName) && !ValidationUtil
145 .evalPattern(fixedIpsName, regexList)) {
146 globalContext.addMessage(
148 ErrorLevel.WARNING, ErrorMessagesFormatBuilder.getErrorWithParameters(
150 Messages.PARAMETER_NAME_NOT_ALIGNED_WITH_GUIDELINES.getErrorMessage(),
151 "Port", "Fixed_IPS", fixedIpsName, resourceEntry.getKey()),
152 LoggerTragetServiceName.VALIDATE_FIXED_IPS_NAME,
153 LoggerErrorDescription.NAME_NOT_ALIGNED_WITH_GUIDELINES);
158 globalContext.addMessage(
160 ErrorLevel.WARNING, ErrorMessagesFormatBuilder
161 .getErrorWithParameters(
162 ERROR_CODE_NNP2, Messages.MISSING_GET_PARAM.getErrorMessage(),
163 "fixed_ips", resourceEntry.getKey()),
164 LoggerTragetServiceName.VALIDATE_FIXED_IPS_NAME,
165 LoggerErrorDescription.MISSING_GET_PARAM);
170 private void validateParamNamingConvention(String fileName, String resourceId,
171 Object propertyValue,
174 GlobalValidationContext globalContext) {
176 MDC_DATA_DEBUG_MESSAGE.debugEntryMessage("file", fileName);
179 if (propertyValue instanceof Map) {
180 paramName = ((Map) propertyValue).get("get_param");
181 if (paramName instanceof String && !ValidationUtil
182 .evalPattern((String) paramName, regexList)) {
183 globalContext.addMessage(
185 ErrorLevel.WARNING, ErrorMessagesFormatBuilder
186 .getErrorWithParameters(ERROR_CODE_NNP3, message.getErrorMessage(), "Port",
187 "Network", (String) paramName, resourceId),
188 LoggerTragetServiceName.VALIDATE_PORT_NETWORK_NAME,
189 LoggerErrorDescription.NAME_NOT_ALIGNED_WITH_GUIDELINES);
193 globalContext.addMessage(
196 ErrorMessagesFormatBuilder
197 .getErrorWithParameters(
198 ERROR_CODE_NNP2, Messages.MISSING_GET_PARAM.getErrorMessage(),
199 "network or network_id", resourceId),
200 LoggerTragetServiceName.VALIDATE_PORT_NETWORK_NAME,
201 LoggerErrorDescription.MISSING_GET_PARAM);
204 MDC_DATA_DEBUG_MESSAGE.debugExitMessage("file", fileName);