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.types.LoggerErrorDescription;
30 import org.openecomp.sdc.logging.types.LoggerTragetServiceName;
31 import org.openecomp.sdc.validation.ResourceValidator;
32 import org.openecomp.sdc.validation.ValidationContext;
33 import org.openecomp.sdc.validation.type.NamingConventionValidationContext;
34 import org.openecomp.sdc.validation.util.ValidationUtil;
36 import java.util.List;
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");
45 public void validate(String fileName, Map.Entry<String, Resource> resourceEntry,
46 GlobalValidationContext globalContext, ValidationContext validationContext) {
48 NamingConventionValidationContext namingConventionValidationContext =
49 (NamingConventionValidationContext)validationContext;
50 validatePortNetworkNamingConvention(fileName, namingConventionValidationContext.getHeatOrchestrationTemplate(), globalContext);
51 validateFixedIpsNamingConvention(fileName, namingConventionValidationContext.getHeatOrchestrationTemplate(), globalContext);
54 private void validatePortNetworkNamingConvention(String fileName,
55 HeatOrchestrationTemplate heatOrchestrationTemplate,
56 GlobalValidationContext globalContext) {
57 if (MapUtils.isEmpty(heatOrchestrationTemplate.getResources())) {
60 String[] regexList = new String[]{".*_net_id", ".*_net_name", ".*_net_fqdn"};
62 heatOrchestrationTemplate
66 .filter(entry -> entry.getValue().getType()
67 .equals(HeatResourcesTypes.NEUTRON_PORT_RESOURCE_TYPE.getHeatResource()))
68 .forEach(entry -> entry.getValue()
72 .filter(propertyEntry ->
73 ("network").equalsIgnoreCase(propertyEntry.getKey())
74 || ("network_id").equals(propertyEntry.getKey()))
75 .forEach(propertyEntry -> validateParamNamingConvention(fileName, entry.getKey(),
76 propertyEntry.getValue(), regexList,
77 Messages.PARAMETER_NAME_NOT_ALIGNED_WITH_GUIDELINES, globalContext)));
80 private void validateFixedIpsNamingConvention(String fileName,
81 HeatOrchestrationTemplate heatOrchestrationTemplate,
82 GlobalValidationContext globalContext) {
83 if (MapUtils.isEmpty(heatOrchestrationTemplate.getResources())) {
87 heatOrchestrationTemplate.getResources()
90 .filter(entry -> HeatResourcesTypes.findByHeatResource(entry.getValue().getType()) != null)
91 .filter(entry -> HeatResourcesTypes.findByHeatResource(entry.getValue().getType())
92 .equals(HeatResourcesTypes.NEUTRON_PORT_RESOURCE_TYPE))
93 .forEach(entry -> checkNeutronPortFixedIpsName(fileName, entry, globalContext));
96 private void checkNeutronPortFixedIpsName(String fileName,
97 Map.Entry<String, Resource> resourceEntry,
98 GlobalValidationContext globalContext) {
100 new String[]{"[^_]+_[^_]+_ips", "[^_]+_[^_]+_v6_ips", "[^_]+_[^_]+_ip_(\\d+)",
101 "[^_]+_[^_]+_v6_ip_(\\d+)"};
103 if (MapUtils.isEmpty(resourceEntry.getValue().getProperties())) {
107 Map<String, Object> propertiesMap = resourceEntry.getValue().getProperties();
108 Object fixedIps = propertiesMap.get("fixed_ips");
109 if (nonNull(fixedIps) && fixedIps instanceof List) {
110 List<Object> fixedIpsList = (List<Object>) fixedIps;
111 for (Object fixedIpsObject : fixedIpsList) {
112 Map.Entry<String, Object> fixedIpsEntry =
113 ((Map<String, Object>) fixedIpsObject).entrySet().iterator().next();
115 validateFixedIpsName(fileName, resourceEntry, globalContext, regexList, fixedIpsEntry);
122 private void validateFixedIpsName(String fileName, Map.Entry<String, Resource> resourceEntry,
123 GlobalValidationContext globalContext,
124 String[] regexList, Map.Entry<String, Object> fixedIpsEntry) {
125 if (nonNull(fixedIpsEntry)) {
126 if (fixedIpsEntry.getValue() instanceof Map) {
128 String fixedIpsName = ValidationUtil
129 .getWantedNameFromPropertyValueGetParam(fixedIpsEntry.getValue());
130 if (nonNull(fixedIpsName) && !ValidationUtil
131 .evalPattern(fixedIpsName, regexList)) {
132 globalContext.addMessage(
134 ErrorLevel.WARNING, ErrorMessagesFormatBuilder.getErrorWithParameters(
136 Messages.PARAMETER_NAME_NOT_ALIGNED_WITH_GUIDELINES.getErrorMessage(),
137 "Port", "Fixed_IPS", fixedIpsName, resourceEntry.getKey()),
138 LoggerTragetServiceName.VALIDATE_FIXED_IPS_NAME,
139 LoggerErrorDescription.NAME_NOT_ALIGNED_WITH_GUIDELINES);
144 globalContext.addMessage(
146 ErrorLevel.WARNING, ErrorMessagesFormatBuilder
147 .getErrorWithParameters(
148 ERROR_CODE_NNP2, Messages.MISSING_GET_PARAM.getErrorMessage(),
149 "fixed_ips", resourceEntry.getKey()),
150 LoggerTragetServiceName.VALIDATE_FIXED_IPS_NAME,
151 LoggerErrorDescription.MISSING_GET_PARAM);
156 private void validateParamNamingConvention(String fileName, String resourceId,
157 Object propertyValue,
160 GlobalValidationContext globalContext) {
162 if (propertyValue instanceof Map) {
163 paramName = ((Map) propertyValue).get("get_param");
164 if (paramName instanceof String && !ValidationUtil
165 .evalPattern((String) paramName, regexList)) {
166 globalContext.addMessage(
168 ErrorLevel.WARNING, ErrorMessagesFormatBuilder
169 .getErrorWithParameters(ERROR_CODE_NNP3, message.getErrorMessage(), "Port",
170 "Network", (String) paramName, resourceId),
171 LoggerTragetServiceName.VALIDATE_PORT_NETWORK_NAME,
172 LoggerErrorDescription.NAME_NOT_ALIGNED_WITH_GUIDELINES);
176 globalContext.addMessage(
179 ErrorMessagesFormatBuilder
180 .getErrorWithParameters(
181 ERROR_CODE_NNP2, Messages.MISSING_GET_PARAM.getErrorMessage(),
182 "network or network_id", resourceId),
183 LoggerTragetServiceName.VALIDATE_PORT_NETWORK_NAME,
184 LoggerErrorDescription.MISSING_GET_PARAM);