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 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;
33 import java.util.List;
36 import static java.util.Objects.nonNull;
38 public class NeutronPortNamingConventionValidator implements ResourceValidator {
39 private static final ErrorMessageCode ERROR_CODE_NNP1 = new ErrorMessageCode("NNP1");
40 private static final ErrorMessageCode ERROR_CODE_NNP2 = new ErrorMessageCode("NNP2");
41 private static final ErrorMessageCode ERROR_CODE_NNP3 = new ErrorMessageCode("NNP3");
44 public void validate(String fileName, Map.Entry<String, Resource> resourceEntry,
45 GlobalValidationContext globalContext, ValidationContext validationContext) {
47 NamingConventionValidationContext namingConventionValidationContext =
48 (NamingConventionValidationContext)validationContext;
49 validatePortNetworkNamingConvention(fileName, namingConventionValidationContext.getHeatOrchestrationTemplate(),
51 validateFixedIpsNamingConvention(fileName, namingConventionValidationContext.getHeatOrchestrationTemplate(),
55 private void validatePortNetworkNamingConvention(String fileName,
56 HeatOrchestrationTemplate heatOrchestrationTemplate,
57 GlobalValidationContext globalContext) {
58 if (MapUtils.isEmpty(heatOrchestrationTemplate.getResources())) {
61 String[] regexList = new String[]{".*_net_id", ".*_net_name", ".*_net_fqdn"};
63 heatOrchestrationTemplate
67 .filter(entry -> entry.getValue().getType()
68 .equals(HeatResourcesTypes.NEUTRON_PORT_RESOURCE_TYPE.getHeatResource()))
69 .forEach(entry -> entry.getValue()
73 .filter(propertyEntry ->
74 ("network").equalsIgnoreCase(propertyEntry.getKey())
75 || ("network_id").equals(propertyEntry.getKey()))
76 .forEach(propertyEntry -> validateParamNamingConvention(fileName, entry.getKey(),
77 propertyEntry.getValue(), regexList,
78 Messages.PARAMETER_NAME_NOT_ALIGNED_WITH_GUIDELINES, globalContext)));
81 private void validateFixedIpsNamingConvention(String fileName,
82 HeatOrchestrationTemplate heatOrchestrationTemplate,
83 GlobalValidationContext globalContext) {
84 if (MapUtils.isEmpty(heatOrchestrationTemplate.getResources())) {
88 heatOrchestrationTemplate.getResources()
91 .filter(entry -> HeatResourcesTypes.findByHeatResource(entry.getValue().getType()) != null)
92 .filter(entry -> HeatResourcesTypes.findByHeatResource(entry.getValue().getType())
93 .equals(HeatResourcesTypes.NEUTRON_PORT_RESOURCE_TYPE))
94 .forEach(entry -> checkNeutronPortFixedIpsName(fileName, entry, globalContext));
97 private void checkNeutronPortFixedIpsName(String fileName,
98 Map.Entry<String, Resource> resourceEntry,
99 GlobalValidationContext globalContext) {
101 new String[]{"[^_]+_[^_]+_ips", "[^_]+_[^_]+_v6_ips", "[^_]+_[^_]+_ip_(\\d+)",
102 "[^_]+_[^_]+_v6_ip_(\\d+)"};
104 if (MapUtils.isEmpty(resourceEntry.getValue().getProperties())) {
108 Map<String, Object> propertiesMap = resourceEntry.getValue().getProperties();
109 Object fixedIps = propertiesMap.get("fixed_ips");
110 if (nonNull(fixedIps) && fixedIps instanceof List) {
111 List<Object> fixedIpsList = (List<Object>) fixedIps;
112 for (Object fixedIpsObject : fixedIpsList) {
113 Map.Entry<String, Object> fixedIpsEntry =
114 ((Map<String, Object>) fixedIpsObject).entrySet().iterator().next();
116 validateFixedIpsName(fileName, resourceEntry, globalContext, regexList, fixedIpsEntry);
123 private void validateFixedIpsName(String fileName, Map.Entry<String, Resource> resourceEntry,
124 GlobalValidationContext globalContext,
125 String[] regexList, Map.Entry<String, Object> fixedIpsEntry) {
126 if (nonNull(fixedIpsEntry)) {
127 if (fixedIpsEntry.getValue() instanceof Map) {
129 String fixedIpsName = ValidationUtil
130 .getWantedNameFromPropertyValueGetParam(fixedIpsEntry.getValue());
131 if (nonNull(fixedIpsName) && !ValidationUtil
132 .evalPattern(fixedIpsName, regexList)) {
133 globalContext.addMessage(
135 ErrorLevel.WARNING, ErrorMessagesFormatBuilder.getErrorWithParameters(
137 Messages.PARAMETER_NAME_NOT_ALIGNED_WITH_GUIDELINES.getErrorMessage(),
138 "Port", "Fixed_IPS", fixedIpsName, resourceEntry.getKey()));
143 globalContext.addMessage(
145 ErrorLevel.WARNING, ErrorMessagesFormatBuilder
146 .getErrorWithParameters(
147 ERROR_CODE_NNP2, Messages.MISSING_GET_PARAM.getErrorMessage(),
148 "fixed_ips", resourceEntry.getKey()));
153 private void validateParamNamingConvention(String fileName, String resourceId,
154 Object propertyValue,
157 GlobalValidationContext globalContext) {
159 if (propertyValue instanceof Map) {
160 paramName = ((Map) propertyValue).get("get_param");
161 if (paramName instanceof String && !ValidationUtil.evalPattern(paramName, regexList)) {
162 globalContext.addMessage(
164 ErrorLevel.WARNING, ErrorMessagesFormatBuilder
165 .getErrorWithParameters(ERROR_CODE_NNP3, message.getErrorMessage(), "Port",
166 "Network", (String) paramName, resourceId));
170 globalContext.addMessage(
173 ErrorMessagesFormatBuilder
174 .getErrorWithParameters(
175 ERROR_CODE_NNP2, Messages.MISSING_GET_PARAM.getErrorMessage(),
176 "network or network_id", resourceId));