1 package org.openecomp.sdc.validation.impl.validators.namingconvention;
3 import org.apache.commons.collections4.MapUtils;
4 import org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder;
5 import org.openecomp.core.validation.types.GlobalValidationContext;
6 import org.openecomp.sdc.common.errors.Messages;
7 import org.openecomp.sdc.datatypes.error.ErrorLevel;
8 import org.openecomp.sdc.heat.datatypes.model.HeatOrchestrationTemplate;
9 import org.openecomp.sdc.heat.datatypes.model.HeatResourcesTypes;
10 import org.openecomp.sdc.heat.datatypes.model.Resource;
11 import org.openecomp.sdc.logging.context.impl.MdcDataDebugMessage;
12 import org.openecomp.sdc.logging.types.LoggerErrorDescription;
13 import org.openecomp.sdc.logging.types.LoggerTragetServiceName;
14 import org.openecomp.sdc.validation.ResourceValidator;
15 import org.openecomp.sdc.validation.ValidationContext;
16 import org.openecomp.sdc.validation.type.NamingConventionValidationContext;
17 import org.openecomp.sdc.validation.util.ValidationUtil;
19 import java.util.List;
22 import static java.util.Objects.nonNull;
25 * Created by TALIO on 2/23/2017.
27 public class NeutronPortNamingConventionValidator implements ResourceValidator {
28 private static MdcDataDebugMessage mdcDataDebugMessage = new MdcDataDebugMessage();
31 public void validate(String fileName, Map.Entry<String, Resource> resourceEntry,
32 GlobalValidationContext globalContext, ValidationContext validationContext) {
34 NamingConventionValidationContext namingConventionValidationContext =
35 (NamingConventionValidationContext)validationContext;
36 validatePortNetworkNamingConvention(fileName, namingConventionValidationContext.getHeatOrchestrationTemplate(), globalContext);
37 validateFixedIpsNamingConvention(fileName, namingConventionValidationContext.getHeatOrchestrationTemplate(), globalContext);
40 private void validatePortNetworkNamingConvention(String fileName,
41 HeatOrchestrationTemplate heatOrchestrationTemplate,
42 GlobalValidationContext globalContext) {
44 mdcDataDebugMessage.debugEntryMessage("file", fileName);
46 if (MapUtils.isEmpty(heatOrchestrationTemplate.getResources())) {
47 mdcDataDebugMessage.debugExitMessage("file", fileName);
50 String[] regexList = new String[]{".*_net_id", ".*_net_name", ".*_net_fqdn"};
52 heatOrchestrationTemplate
56 .filter(entry -> entry.getValue().getType()
57 .equals(HeatResourcesTypes.NEUTRON_PORT_RESOURCE_TYPE.getHeatResource()))
58 .forEach(entry -> entry.getValue()
62 .filter(propertyEntry ->
63 propertyEntry.getKey().toLowerCase().equals("network".toLowerCase())
64 || propertyEntry.getKey().equals("network_id"))
65 .forEach(propertyEntry -> validateParamNamingConvention(fileName, entry.getKey(),
66 propertyEntry.getValue(), "Port", "Network", regexList,
67 Messages.PARAMETER_NAME_NOT_ALIGNED_WITH_GUIDELINES, globalContext)));
69 mdcDataDebugMessage.debugExitMessage("file", fileName);
72 private void validateFixedIpsNamingConvention(String fileName,
73 HeatOrchestrationTemplate heatOrchestrationTemplate,
74 GlobalValidationContext globalContext) {
76 mdcDataDebugMessage.debugEntryMessage("file", fileName);
78 if (MapUtils.isEmpty(heatOrchestrationTemplate.getResources())) {
79 mdcDataDebugMessage.debugExitMessage("file", fileName);
83 heatOrchestrationTemplate.getResources()
86 .filter(entry -> HeatResourcesTypes.findByHeatResource(entry.getValue().getType()) != null)
87 .filter(entry -> HeatResourcesTypes.findByHeatResource(entry.getValue().getType())
88 .equals(HeatResourcesTypes.NEUTRON_PORT_RESOURCE_TYPE))
89 .forEach(entry -> checkNeutronPortFixedIpsName(fileName, entry, globalContext));
91 mdcDataDebugMessage.debugExitMessage("file", fileName);
94 private void checkNeutronPortFixedIpsName(String fileName,
95 Map.Entry<String, Resource> resourceEntry,
96 GlobalValidationContext globalContext) {
98 new String[]{"[^_]+_[^_]+_ips", "[^_]+_[^_]+_v6_ips", "[^_]+_[^_]+_ip_(\\d+)",
99 "[^_]+_[^_]+_v6_ip_(\\d+)"};
101 if (MapUtils.isEmpty(resourceEntry.getValue().getProperties())) {
105 Map<String, Object> propertiesMap = resourceEntry.getValue().getProperties();
106 Object fixedIps = propertiesMap.get("fixed_ips");
107 if (nonNull(fixedIps) && fixedIps instanceof List) {
108 List<Object> fixedIpsList = (List<Object>) fixedIps;
109 for (Object fixedIpsObject : fixedIpsList) {
110 Map.Entry<String, Object> fixedIpsEntry =
111 ((Map<String, Object>) fixedIpsObject).entrySet().iterator().next();
112 if (nonNull(fixedIpsEntry)) {
113 if (fixedIpsEntry.getValue() instanceof Map) {
114 String fixedIpsName = ValidationUtil.getWantedNameFromPropertyValueGetParam
117 if (nonNull(fixedIpsName)) {
118 if (!ValidationUtil.evalPattern(fixedIpsName, regexList)) {
119 globalContext.addMessage(
121 ErrorLevel.WARNING, ErrorMessagesFormatBuilder.getErrorWithParameters(
122 Messages.PARAMETER_NAME_NOT_ALIGNED_WITH_GUIDELINES.getErrorMessage(),
123 "Port", "Fixed_IPS", fixedIpsName, resourceEntry.getKey()),
124 LoggerTragetServiceName.VALIDATE_FIXED_IPS_NAME,
125 LoggerErrorDescription.NAME_NOT_ALIGNED_WITH_GUIDELINES);
129 globalContext.addMessage(
131 ErrorLevel.WARNING, ErrorMessagesFormatBuilder
132 .getErrorWithParameters(Messages.MISSING_GET_PARAM.getErrorMessage(),
133 "fixed_ips", resourceEntry.getKey()),
134 LoggerTragetServiceName.VALIDATE_FIXED_IPS_NAME,
135 LoggerErrorDescription.MISSING_GET_PARAM);
142 private void validateParamNamingConvention(String fileName, String resourceId,
143 Object propertyValue, String resourceType,
144 String wrongPropertyFormat, String[] regexList,
146 GlobalValidationContext globalContext) {
148 mdcDataDebugMessage.debugEntryMessage("file", fileName);
151 if (propertyValue instanceof Map) {
152 paramName = ((Map) propertyValue).get("get_param");
153 if (paramName instanceof String) {
154 if (!ValidationUtil.evalPattern((String) paramName, regexList)) {
155 globalContext.addMessage(
157 ErrorLevel.WARNING, ErrorMessagesFormatBuilder
158 .getErrorWithParameters(message.getErrorMessage(), resourceType,
159 wrongPropertyFormat, (String) paramName, resourceId),
160 LoggerTragetServiceName.VALIDATE_PORT_NETWORK_NAME,
161 LoggerErrorDescription.NAME_NOT_ALIGNED_WITH_GUIDELINES);
165 globalContext.addMessage(
168 ErrorMessagesFormatBuilder
169 .getErrorWithParameters(Messages.MISSING_GET_PARAM.getErrorMessage(),
170 "network or network_id", resourceId),
171 LoggerTragetServiceName.VALIDATE_PORT_NETWORK_NAME,
172 LoggerErrorDescription.MISSING_GET_PARAM);
175 mdcDataDebugMessage.debugExitMessage("file", fileName);