Remove enter/exit debug #4
[sdc.git] / openecomp-be / lib / openecomp-sdc-validation-lib / openecomp-sdc-validation-impl / src / main / java / org / openecomp / sdc / validation / impl / validators / namingconvention / NovaServerNamingConventionGuideLineValidator.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.apache.commons.lang3.tuple.ImmutablePair;
21 import org.apache.commons.lang3.tuple.Pair;
22 import org.openecomp.core.validation.ErrorMessageCode;
23 import org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder;
24 import org.openecomp.core.validation.types.GlobalValidationContext;
25 import org.openecomp.sdc.common.errors.Messages;
26 import org.openecomp.sdc.datatypes.error.ErrorLevel;
27 import org.openecomp.sdc.heat.datatypes.DefinedHeatParameterTypes;
28 import org.openecomp.sdc.heat.datatypes.model.Environment;
29 import org.openecomp.sdc.heat.datatypes.model.HeatOrchestrationTemplate;
30 import org.openecomp.sdc.heat.datatypes.model.HeatResourcesTypes;
31 import org.openecomp.sdc.heat.datatypes.model.Resource;
32 import org.openecomp.sdc.heat.datatypes.model.ResourceReferenceFunctions;
33 import org.openecomp.sdc.heat.services.HeatStructureUtil;
34 import org.openecomp.sdc.logging.types.LoggerErrorDescription;
35 import org.openecomp.sdc.logging.types.LoggerTragetServiceName;
36 import org.openecomp.sdc.validation.ResourceValidator;
37 import org.openecomp.sdc.validation.ValidationContext;
38 import org.openecomp.sdc.validation.type.NamingConventionValidationContext;
39 import org.openecomp.sdc.validation.util.ValidationUtil;
40
41 import java.util.ArrayList;
42 import java.util.Arrays;
43 import java.util.Collection;
44 import java.util.Comparator;
45 import java.util.HashMap;
46 import java.util.LinkedList;
47 import java.util.List;
48 import java.util.Map;
49 import java.util.Objects;
50 import java.util.TreeMap;
51
52 import static java.util.Objects.nonNull;
53
54 public class NovaServerNamingConventionGuideLineValidator implements ResourceValidator {
55   private static final String AVAILABILITY_ZONE = "availability_zone";
56   private static final String SERVER = "Server";
57   private static final ErrorMessageCode ERROR_CODE_NNS1 = new ErrorMessageCode("NNS1");
58   private static final ErrorMessageCode ERROR_CODE_NNS2 = new ErrorMessageCode("NNS2");
59   private static final ErrorMessageCode ERROR_CODE_NNS3 = new ErrorMessageCode("NNS3");
60   private static final ErrorMessageCode ERROR_CODE_NNS4 = new ErrorMessageCode("NNS4");
61   private static final ErrorMessageCode ERROR_CODE_NNS5 = new ErrorMessageCode("NNS5");
62   private static final ErrorMessageCode ERROR_CODE_NNS6 = new ErrorMessageCode("NNS6");
63   private static final ErrorMessageCode ERROR_CODE_NNS7 = new ErrorMessageCode("NNS7");
64   private static final ErrorMessageCode ERROR_CODE_NNS8 = new ErrorMessageCode("NNS8");
65   private static final ErrorMessageCode ERROR_CODE_NNS9 = new ErrorMessageCode("NNS9");
66   private static final ErrorMessageCode ERROR_CODE_NNS10 = new ErrorMessageCode("NNS10");
67   private static final ErrorMessageCode ERROR_CODE_NNS11 = new ErrorMessageCode("NNS11");
68   private static final ErrorMessageCode ERROR_CODE_NNS12 = new ErrorMessageCode("NNS12");
69   private static final ErrorMessageCode ERROR_CODE_NNS13 = new ErrorMessageCode("NNS13");
70   private static final ErrorMessageCode ERROR_CODE_NNS14 = new ErrorMessageCode("NNS14");
71
72   @Override
73   public void validate(String fileName, Map.Entry<String, Resource> resourceEntry,
74                        GlobalValidationContext globalContext, ValidationContext validationContext) {
75
76     NamingConventionValidationContext namingConventionValidationContext =
77             (NamingConventionValidationContext)validationContext;
78     validateHeatNovaResource(fileName, namingConventionValidationContext.getEnvFileName(),
79             namingConventionValidationContext.getHeatOrchestrationTemplate(),
80             globalContext);
81   }
82
83   private void validateHeatNovaResource(String fileName, String envFileName,
84                                         HeatOrchestrationTemplate heatOrchestrationTemplate,
85                                         GlobalValidationContext globalContext) {
86     Map<String, String> uniqueResourcePortNetworkRole = new HashMap<>();
87     //if no resources exist return
88     if (MapUtils.isEmpty(heatOrchestrationTemplate.getResources())) {
89       return;
90     }
91
92     heatOrchestrationTemplate
93             .getResources()
94             .entrySet()
95             .stream()
96             .filter(entry -> entry.getValue().getType()
97                     .equals(HeatResourcesTypes.NOVA_SERVER_RESOURCE_TYPE.getHeatResource()))
98             .forEach( entry -> validateNovaServerResourceType(entry.getKey(), fileName, envFileName,
99                     entry, uniqueResourcePortNetworkRole, heatOrchestrationTemplate, globalContext));
100   }
101
102   private void validateNovaServerResourceType(String resourceId, String fileName,
103                                               String envFileName,
104                                               Map.Entry<String, Resource> resourceEntry,
105                                               Map<String, String> uniqueResourcePortNetworkRole,
106                                               HeatOrchestrationTemplate heatOrchestrationTemplate,
107                                               GlobalValidationContext globalContext) {
108     validateNovaServerResourceMetaData(fileName, resourceId,
109             heatOrchestrationTemplate.getResources().get(resourceId), globalContext);
110     validateNovaServerResourceNetworkUniqueRole(fileName, resourceId, uniqueResourcePortNetworkRole,
111             heatOrchestrationTemplate, globalContext);
112     validateAvailabilityZoneName(fileName, resourceEntry, globalContext);
113     validateNovaServerNameImageAndFlavor(fileName, envFileName, resourceEntry, globalContext);
114   }
115
116   @SuppressWarnings("unchecked")
117   private void validateNovaServerResourceMetaData(String fileName, String resourceId,
118                                                   Resource resource,
119                                                   GlobalValidationContext globalValidationContext) {
120     Map<String, Object> novaServerProp = resource.getProperties();
121     Object novaServerPropMetadata;
122     if (MapUtils.isNotEmpty(novaServerProp)) {
123       novaServerPropMetadata = novaServerProp.get("metadata");
124       if (novaServerPropMetadata == null) {
125         globalValidationContext.addMessage(
126                 fileName,
127                 ErrorLevel.WARNING,
128                 ErrorMessagesFormatBuilder
129                         .getErrorWithParameters(
130                                 ERROR_CODE_NNS1, Messages.MISSING_NOVA_SERVER_METADATA.getErrorMessage(),
131                                 resourceId),
132                 LoggerTragetServiceName.VALIDATE_NOVA_META_DATA_NAME,
133                 LoggerErrorDescription.MISSING_NOVA_PROPERTIES);
134       } else if (novaServerPropMetadata instanceof Map) {
135         TreeMap<String, Object> propertyMap = new TreeMap((Comparator<String>) String::compareToIgnoreCase);
136         propertyMap.putAll((Map) novaServerPropMetadata);
137         if (!propertyMap.containsKey("vf_module_id")) {
138           globalValidationContext.addMessage(
139                   fileName,
140                   ErrorLevel.WARNING,
141                   ErrorMessagesFormatBuilder.getErrorWithParameters(
142                           ERROR_CODE_NNS2, Messages.MISSING_NOVA_SERVER_VF_MODULE_ID.getErrorMessage(),
143                           resourceId),
144                   LoggerTragetServiceName.VALIDATE_NOVA_META_DATA_NAME,
145                   LoggerErrorDescription.MISSING_NOVA_PROPERTIES);
146         }
147         if (!propertyMap.containsKey("vnf_id")) {
148           globalValidationContext.addMessage(
149                   fileName, ErrorLevel.WARNING,
150                   ErrorMessagesFormatBuilder
151                           .getErrorWithParameters(
152                                   ERROR_CODE_NNS3, Messages.MISSING_NOVA_SERVER_VNF_ID.getErrorMessage(),
153                                   resourceId),
154                   LoggerTragetServiceName.VALIDATE_NOVA_META_DATA_NAME,
155                   LoggerErrorDescription.MISSING_NOVA_PROPERTIES);
156         }
157       }
158     }
159   }
160
161   private void validateNovaServerResourceNetworkUniqueRole(String fileName, String resourceId,
162                                                            Map<String, String> uniqueResourcePortNetworkRole,
163                                                            HeatOrchestrationTemplate heatOrchestrationTemplate,
164                                                            GlobalValidationContext globalValidationContext) {
165     Object propertyNetworkValue =
166             heatOrchestrationTemplate.getResources().get(resourceId).getProperties().get("networks");
167     if (propertyNetworkValue != null && propertyNetworkValue instanceof List) {
168       List<String> portResourceIdList =
169               getNovaNetworkPortResourceList(fileName, (List) propertyNetworkValue,
170                       globalValidationContext);
171       for (String portResourceId : portResourceIdList) {
172         Resource portResource = heatOrchestrationTemplate.getResources().get(portResourceId);
173
174         if (portResource != null && portResource.getType()
175                 .equals(HeatResourcesTypes.NEUTRON_PORT_RESOURCE_TYPE.getHeatResource())) {
176           validateUniqueResourcePortNetworkRole(fileName, resourceId,
177                   uniqueResourcePortNetworkRole, globalValidationContext,
178                   portResourceId, portResource);
179         }
180       }
181     }
182   }
183
184   private void validateUniqueResourcePortNetworkRole(String fileName, String resourceId,
185                                       Map<String, String> uniqueResourcePortNetworkRole,
186                                       GlobalValidationContext globalValidationContext,
187                                       String portResourceId, Resource portResource) {
188     String role = null;
189     Object network;
190     Map portNetwork =
191             getPortNetwork(fileName, resourceId, portResource, globalValidationContext);
192     if (Objects.nonNull(portNetwork)) {
193       network = portNetwork.get("get_param");
194       if (Objects.nonNull(network)) {
195         if (network instanceof String ){
196           role = getNetworkRole((String)network);
197         }else if (network instanceof List){
198           role = getNetworkRole((String)((List) network).get(0));
199         }
200         if (role != null && uniqueResourcePortNetworkRole.containsKey(role)) {
201           globalValidationContext.addMessage(
202                   fileName,
203                   ErrorLevel.WARNING,
204                   ErrorMessagesFormatBuilder.getErrorWithParameters(
205                           ERROR_CODE_NNS12, Messages.RESOURCE_CONNECTED_TO_TWO_EXTERNAL_NETWORKS_WITH_SAME_ROLE
206                                   .getErrorMessage(), resourceId, role),
207                   LoggerTragetServiceName.VALIDATE_RESOURCE_NETWORK_UNIQUE_ROLW,
208                   LoggerErrorDescription.RESOURCE_UNIQUE_NETWORK_ROLE);
209         } else {
210           uniqueResourcePortNetworkRole.put(role, portResourceId);
211         }
212       }
213     }
214   }
215
216   private List<String> getNovaNetworkPortResourceList(String filename, List propertyNetworkValue,
217                                                       GlobalValidationContext globalContext) {
218     globalContext.setMessageCode(ERROR_CODE_NNS14);
219     List<String> portResourceIdList = new ArrayList<>();
220     for (Object propValue : propertyNetworkValue) {
221       Object portPropValue = ((Map) propValue).get("port");
222       Collection<String> portResourceIds = HeatStructureUtil
223               .getReferencedValuesByFunctionName(filename, "get_resource", portPropValue,
224                       globalContext);
225       if (portResourceIds != null) {
226         portResourceIdList.addAll(portResourceIds);
227       }
228     }
229
230     return portResourceIdList;
231   }
232
233   private String getNetworkRole(String network) {
234     if (network == null) {
235       return null;
236     }
237     if (network.contains("_net_id")) {
238       return network.substring(0, network.indexOf("_net_id"));
239     } else if (network.contains("_net_name")) {
240       return network.substring(0, network.indexOf("_net_name"));
241     } else if (network.contains("_net_fqdn")) {
242       return network.substring(0, network.indexOf("_net_fqdn"));
243     }
244     return null;
245   }
246
247   private Map getPortNetwork(String fileName, String resourceId, Resource portResource,
248                              GlobalValidationContext globalValidationContext) {
249     Object portNetwork = portResource.getProperties().get("network_id");
250     if (portNetwork == null) {
251       portNetwork = portResource.getProperties().get("network");
252     }
253     if (!(portNetwork instanceof Map)) {
254       globalValidationContext.addMessage(
255               fileName,
256               ErrorLevel.WARNING,
257               ErrorMessagesFormatBuilder
258                       .getErrorWithParameters(
259                               ERROR_CODE_NNS4, Messages.MISSING_GET_PARAM.getErrorMessage(),
260                               "network or network_id", resourceId),
261               LoggerTragetServiceName.VALIDATE_RESOURCE_NETWORK_UNIQUE_ROLW,
262               LoggerErrorDescription.MISSING_GET_PARAM);
263       return null;
264     }
265     return (Map) portNetwork;
266   }
267
268   private void validateAvailabilityZoneName(String fileName,
269                                             Map.Entry<String, Resource> resourceEntry,
270                                             GlobalValidationContext globalContext) {
271     String[] regexList = new String[]{"availability_zone_(\\d+)"};
272
273     if (MapUtils.isEmpty(resourceEntry.getValue().getProperties())) {
274       return;
275     }
276
277     Object availabilityZoneMap =  resourceEntry.getValue().getProperties()
278             .get(AVAILABILITY_ZONE);
279
280     if (nonNull(availabilityZoneMap)) {
281       if (availabilityZoneMap instanceof Map) {
282         String availabilityZoneName = ValidationUtil.getWantedNameFromPropertyValueGetParam
283                 (availabilityZoneMap);
284
285           if (availabilityZoneName != null && !ValidationUtil
286                   .evalPattern(availabilityZoneName, regexList)) {
287             globalContext.addMessage(
288                     fileName,
289                     ErrorLevel.WARNING, ErrorMessagesFormatBuilder.getErrorWithParameters(
290                             ERROR_CODE_NNS5, Messages.PARAMETER_NAME_NOT_ALIGNED_WITH_GUIDELINES.getErrorMessage(),
291                             ValidationUtil.getMessagePartAccordingToResourceType(resourceEntry),
292                             "Availability Zone", availabilityZoneName, resourceEntry.getKey()),
293                     LoggerTragetServiceName.VALIDATE_AVAILABILITY_ZONE_NAME,
294                     LoggerErrorDescription.NAME_NOT_ALIGNED_WITH_GUIDELINES);
295           }
296       } else {
297         globalContext.addMessage(
298                 fileName,
299                 ErrorLevel.WARNING, ErrorMessagesFormatBuilder
300                         .getErrorWithParameters(
301                                 ERROR_CODE_NNS6, Messages.MISSING_GET_PARAM.getErrorMessage(),
302                                 AVAILABILITY_ZONE, resourceEntry.getKey()),
303                 LoggerTragetServiceName.VALIDATE_AVAILABILITY_ZONE_NAME,
304                 LoggerErrorDescription.MISSING_GET_PARAM);
305       }
306     }
307   }
308
309   private void validateNovaServerNameImageAndFlavor(String fileName, String envFileName,
310                                                     Map.Entry<String, Resource> resourceEntry,
311                                                     GlobalValidationContext globalContext) {
312     String novaName =
313             validateNovaServerNamingConvention(fileName, envFileName, resourceEntry, globalContext);
314     Map<String, String> legalNovaNamingConventionMap =
315             validateImageAndFlavorFromNovaServer(fileName, resourceEntry, globalContext);
316
317     if (Objects.nonNull(novaName)) {
318       legalNovaNamingConventionMap.put("name", novaName);
319     }
320
321     if (legalNovaNamingConventionMap.keySet().size() > 1) {
322       validateNovaServerNameImageAndFlavorSync(fileName, resourceEntry,
323               legalNovaNamingConventionMap, globalContext);
324     }
325   }
326
327   private String validateNovaServerNamingConvention(String fileName, String envFileName,
328                                                     Map.Entry<String, Resource> resourceEntry,
329                                                     GlobalValidationContext globalContext) {
330     if (MapUtils.isEmpty(resourceEntry.getValue().getProperties())) {
331       return null;
332     }
333     return checkIfNovaNameByGuidelines(fileName, envFileName, resourceEntry, globalContext);
334   }
335
336   private Map<String, String> validateImageAndFlavorFromNovaServer(String fileName,
337                                                                    Map.Entry<String, Resource> resourceEntry,
338                                                                    GlobalValidationContext globalContext) {
339     if (MapUtils.isEmpty(resourceEntry.getValue().getProperties())) {
340       return null;
341     }
342
343     Pair<String, String> imagePair = new ImmutablePair<>("image", ".*_image_name");
344     Pair<String, String> flavorPair = new ImmutablePair<>("flavor", ".*_flavor_name");
345     List<Pair<String, String>> imageFlavorPairs = Arrays.asList(imagePair, flavorPair);
346     Map<String, Object> propertiesMap = resourceEntry.getValue().getProperties();
347     Map<String, String> imageAndFlavorLegalNames = new HashMap<>();
348
349     for (Pair<String, String> imageOrFlavor : imageFlavorPairs) {
350       boolean isErrorInImageOrFlavor =
351               isErrorExistWhenValidatingImageOrFlavorNames(fileName, imageOrFlavor, resourceEntry,
352                       propertiesMap, globalContext);
353       if (!isErrorInImageOrFlavor) {
354         Object nameValue = propertiesMap.get(imageOrFlavor.getKey());
355         String imageOrFlavorName = ValidationUtil.getWantedNameFromPropertyValueGetParam
356                 (nameValue);
357         imageAndFlavorLegalNames.put(imageOrFlavor.getKey(), imageOrFlavorName);
358       }
359     }
360     return imageAndFlavorLegalNames;
361   }
362
363   private String checkIfNovaNameByGuidelines(String fileName, String envFileName,
364                                              Map.Entry<String, Resource> resourceEntry,
365                                              GlobalValidationContext globalContext) {
366     if (MapUtils.isEmpty(resourceEntry.getValue().getProperties())) {
367       return null;
368     }
369     Object novaNameGetParam = getNovaServerName(resourceEntry);
370     String novaName = null;
371     if (nonNull(novaNameGetParam)) {
372       novaName =
373               checkNovaNameGetParamValueMap(fileName, novaNameGetParam, resourceEntry, globalContext);
374       checkIfNovaNameParameterInEnvIsStringOrList(fileName, envFileName, novaName, resourceEntry,
375               globalContext);
376     } else {
377       globalContext.addMessage(
378               fileName,
379               ErrorLevel.WARNING, ErrorMessagesFormatBuilder
380                       .getErrorWithParameters(
381                               ERROR_CODE_NNS7, Messages.MISSING_GET_PARAM.getErrorMessage(),
382                               "nova server name", resourceEntry.getKey()),
383               LoggerTragetServiceName.VALIDATE_NOVA_SERVER_NAME,
384               LoggerErrorDescription.MISSING_GET_PARAM);
385     }
386
387     return novaName;
388   }
389
390   private boolean isErrorExistWhenValidatingImageOrFlavorNames(String fileName,
391                                                                Pair<String, String> propertyNameAndRegex,
392                                                                Map.Entry<String, Resource> resourceEntry,
393                                                                Map<String, Object> propertiesMap,
394                                                                GlobalValidationContext globalContext) {
395     String propertyName = propertyNameAndRegex.getKey();
396     Object nameValue = propertiesMap.get(propertyName);
397     String[] regexList = new String[]{propertyNameAndRegex.getValue()};
398
399
400     if (nonNull(nameValue)) {
401       if (nameValue instanceof Map) {
402         globalContext.setMessageCode(ERROR_CODE_NNS13);
403         if (ValidationUtil.validateMapPropertyValue(fileName, resourceEntry, globalContext,
404                 propertyName,
405                 nameValue, regexList)) {
406           return true;
407         }
408       } else {
409         globalContext.addMessage(
410                 fileName,
411                 ErrorLevel.WARNING, ErrorMessagesFormatBuilder
412                         .getErrorWithParameters(
413                                 ERROR_CODE_NNS8, Messages.MISSING_GET_PARAM.getErrorMessage(),
414                                 propertyName, resourceEntry.getKey()),
415                 LoggerTragetServiceName.VALIDATE_IMAGE_AND_FLAVOR_NAME,
416                 LoggerErrorDescription.MISSING_GET_PARAM);
417         return true;
418       }
419
420       return false;
421     }
422     return false;
423   }
424
425   private Object getNovaServerName(Map.Entry<String, Resource> resourceEntry) {
426     Object novaServerName = resourceEntry.getValue().getProperties().get("name");
427     Map novaNameMap;
428       if (nonNull(novaServerName) && novaServerName instanceof Map) {
429         novaNameMap = (Map) novaServerName;
430         return novaNameMap.get(ResourceReferenceFunctions.GET_PARAM.getFunction());
431       }
432
433     return null;
434   }
435
436   @SuppressWarnings("unchecked")
437   private String checkNovaNameGetParamValueMap(String fileName, Object getParamValue,
438                                                Map.Entry<String, Resource> resourceEntry,
439                                                GlobalValidationContext globalContext) {
440     if (getParamValue instanceof List) {
441       List<Object> getParamNameList = (List) getParamValue;
442       String[] regexName = new String[]{".*_names"};
443       return isNovaNameAsListLegal(fileName, regexName, getParamNameList, resourceEntry,
444               globalContext);
445     } else if (getParamValue instanceof String) {
446       String[] regexName = new String[]{".*_name_(\\d+)"};
447       return isNovaNameAsStringLegal(fileName, (String) getParamValue, regexName, resourceEntry,
448               globalContext);
449     }
450
451     return null;
452   }
453
454   private void checkIfNovaNameParameterInEnvIsStringOrList(String fileName,
455                                                            String envFileName,
456                                                            String novaServerName,
457                                                            Map.Entry<String, Resource> resourceEntry,
458                                                            GlobalValidationContext globalContext) {
459     if (nonNull(envFileName)) {
460       Environment environment = ValidationUtil.validateEnvContent(envFileName, globalContext);
461
462       if (environment != null && MapUtils.isNotEmpty(environment.getParameters())) {
463         Object novaServerNameEnvValue = environment.getParameters()
464                         .get(novaServerName);
465           if (Objects.nonNull(novaServerNameEnvValue) && !DefinedHeatParameterTypes
466                   .isNovaServerEnvValueIsFromRightType(novaServerNameEnvValue)) {
467             globalContext.addMessage(
468                     fileName,
469                     ErrorLevel.WARNING, ErrorMessagesFormatBuilder.getErrorWithParameters(
470                             ERROR_CODE_NNS9, Messages.PARAMETER_NAME_NOT_ALIGNED_WITH_GUIDELINES.getErrorMessage(),
471                             SERVER, "Name",
472                             novaServerNameEnvValue.toString(), resourceEntry.getKey()),
473                     LoggerTragetServiceName.VALIDATE_NOVA_SERVER_NAME,
474                     LoggerErrorDescription.NAME_NOT_ALIGNED_WITH_GUIDELINES);
475           }
476       }
477     }
478   }
479
480   private String isNovaNameAsListLegal(String fileName,
481                                        String[] regexName,
482                                        List<Object> getParamNameList,
483                                        Map.Entry<String, Resource> resourceEntry,
484                                        GlobalValidationContext globalContext) {
485
486     if (getParamNameList.size() != 2 || !ValidationUtil.evalPattern(getParamNameList.get(0),
487             regexName)) {
488       globalContext.addMessage(
489               fileName,
490               ErrorLevel.WARNING,
491               ErrorMessagesFormatBuilder.getErrorWithParameters(
492                       ERROR_CODE_NNS10, Messages.PARAMETER_NAME_NOT_ALIGNED_WITH_GUIDELINES.getErrorMessage(),
493                       SERVER,
494                       "name", getParamNameList.toString(), resourceEntry.getKey()),
495               LoggerTragetServiceName.VALIDATE_NOVA_SERVER_NAME,
496               LoggerErrorDescription.NAME_NOT_ALIGNED_WITH_GUIDELINES);
497       return null;
498     }
499
500     return (String) getParamNameList.get(0);
501   }
502
503   private String isNovaNameAsStringLegal(String fileName,
504                                          String novaName,
505                                          String[] regexName,
506                                          Map.Entry<String, Resource> resourceEntry,
507                                          GlobalValidationContext globalContext) {
508     if (!ValidationUtil.evalPattern(novaName, regexName)) {
509       globalContext.addMessage(
510               fileName,
511               ErrorLevel.WARNING,
512               ErrorMessagesFormatBuilder.getErrorWithParameters(
513                       ERROR_CODE_NNS10, Messages.PARAMETER_NAME_NOT_ALIGNED_WITH_GUIDELINES.getErrorMessage(),
514                       SERVER,
515                       "name", novaName, resourceEntry.getKey()),
516               LoggerTragetServiceName.VALIDATE_NOVA_SERVER_NAME,
517               LoggerErrorDescription.NAME_NOT_ALIGNED_WITH_GUIDELINES);
518       return null;
519     }
520     return novaName;
521   }
522
523   private void validateNovaServerNameImageAndFlavorSync(String fileName,
524                                                         Map.Entry<String, Resource> resourceEntry,
525                                                         Map<String, String> legalNovaNamingConventionNames,
526                                                         GlobalValidationContext globalContext) {
527     List<String> vmNames = new LinkedList<>();
528
529     for (Map.Entry<String, String> nameEntry : legalNovaNamingConventionNames.entrySet()) {
530       vmNames.add(getVmName(nameEntry.getValue(), nameEntry.getKey()));
531     }
532
533     vmNames.removeIf(Objects::isNull);
534
535     if (!isVmNameSync(vmNames)) {
536       globalContext.addMessage(
537               fileName,
538               ErrorLevel.WARNING,
539               ErrorMessagesFormatBuilder.getErrorWithParameters(
540                       ERROR_CODE_NNS11, Messages.NOVA_NAME_IMAGE_FLAVOR_NOT_CONSISTENT.getErrorMessage(),
541                       resourceEntry.getKey()),
542               LoggerTragetServiceName.VALIDATE_IMAGE_AND_FLAVOR_NAME,
543               LoggerErrorDescription.NAME_NOT_ALIGNED_WITH_GUIDELINES);
544     }
545   }
546
547   private String getVmName(String nameToGetVmNameFrom, String stringToGetIndexOf) {
548     int vmIndex =
549             nameToGetVmNameFrom == null ? -1 : nameToGetVmNameFrom.indexOf(stringToGetIndexOf);
550     String vmName = null;
551     if (nameToGetVmNameFrom != null) {
552       vmName = vmIndex < 0 ? null
553               : trimNonAlphaNumericCharactersFromEndOfString(nameToGetVmNameFrom.substring(0, vmIndex));
554     }
555     return vmName;
556   }
557
558   private boolean isVmNameSync(List<String> namesToCompare) {
559     int size = namesToCompare.size();
560     for (int i = 0; i < size - 1; i++) {
561       if (!namesToCompare.get(i).equals(namesToCompare.get(i + 1))) {
562         return false;
563       }
564     }
565     return true;
566   }
567
568   private String trimNonAlphaNumericCharactersFromEndOfString(String toTrim) {
569     int stringSize = toTrim.length();
570     int stringLength = stringSize - 1;
571     String[] regexList = new String[]{"[^a-zA-Z0-9]"};
572
573     while (stringLength >= 0) {
574       if (!ValidationUtil.evalPattern(String.valueOf(toTrim.charAt(stringLength)), regexList)) {
575         break;
576       }
577       stringLength--;
578     }
579
580     return toTrim.substring(0, stringLength + 1);
581   }
582 }