aboutsummaryrefslogtreecommitdiffstats
path: root/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-impl/src/main/java/org/openecomp/sdc/validation/impl/validators/namingconvention/NovaServerNamingConventionGuideLineValidator.java
blob: cf2751bef72730902693c90f491539f8a7f38ade (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
package org.openecomp.sdc.validation.impl.validators.namingconvention;

import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.apache.commons.lang3.tuple.Pair;
import org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder;
import org.openecomp.core.validation.types.GlobalValidationContext;
import org.openecomp.sdc.common.errors.Messages;
import org.openecomp.sdc.datatypes.error.ErrorLevel;
import org.openecomp.sdc.heat.datatypes.DefinedHeatParameterTypes;
import org.openecomp.sdc.heat.datatypes.model.Environment;
import org.openecomp.sdc.heat.datatypes.model.HeatOrchestrationTemplate;
import org.openecomp.sdc.heat.datatypes.model.HeatResourcesTypes;
import org.openecomp.sdc.heat.datatypes.model.Resource;
import org.openecomp.sdc.heat.datatypes.model.ResourceReferenceFunctions;
import org.openecomp.sdc.heat.services.HeatStructureUtil;
import org.openecomp.sdc.logging.context.impl.MdcDataDebugMessage;
import org.openecomp.sdc.logging.types.LoggerErrorDescription;
import org.openecomp.sdc.logging.types.LoggerTragetServiceName;
import org.openecomp.sdc.validation.ResourceValidator;
import org.openecomp.sdc.validation.ValidationContext;
import org.openecomp.sdc.validation.type.NamingConventionValidationContext;
import org.openecomp.sdc.validation.util.ValidationUtil;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.TreeMap;

import static java.util.Objects.nonNull;

/**
 * Created by TALIO on 2/23/2017.
 */
public class NovaServerNamingConventionGuideLineValidator implements ResourceValidator {
  private static MdcDataDebugMessage mdcDataDebugMessage = new MdcDataDebugMessage();

  @Override
  public void validate(String fileName, Map.Entry<String, Resource> resourceEntry,
                       GlobalValidationContext globalContext, ValidationContext validationContext) {

    NamingConventionValidationContext namingConventionValidationContext =
        (NamingConventionValidationContext)validationContext;
    validateHeatNovaResource(fileName, namingConventionValidationContext.getEnvFileName(),
        namingConventionValidationContext.getHeatOrchestrationTemplate(),
        globalContext);
  }

  private void validateHeatNovaResource(String fileName, String envFileName,
                                        HeatOrchestrationTemplate heatOrchestrationTemplate,
                                        GlobalValidationContext globalContext) {


    mdcDataDebugMessage.debugEntryMessage("file", fileName);

    Map<String, String> uniqueResourcePortNetworkRole = new HashMap<>();
    //if no resources exist return
    if (MapUtils.isEmpty(heatOrchestrationTemplate.getResources())) {
      return;
    }

    heatOrchestrationTemplate
        .getResources()
        .entrySet()
        .stream()
        .filter(entry -> entry.getValue().getType()
            .equals(HeatResourcesTypes.NOVA_SERVER_RESOURCE_TYPE.getHeatResource()))
        .forEach( entry -> validateNovaServerResourceType(entry.getKey(), fileName, envFileName,
            entry, uniqueResourcePortNetworkRole, heatOrchestrationTemplate, globalContext));

    mdcDataDebugMessage.debugExitMessage("file", fileName);
  }

  private void validateNovaServerResourceType(String resourceId, String fileName,
                                              String envFileName,
                                              Map.Entry<String, Resource> resourceEntry,
                                              Map<String, String> uniqueResourcePortNetworkRole,
                                              HeatOrchestrationTemplate heatOrchestrationTemplate,
                                              GlobalValidationContext globalContext) {

    mdcDataDebugMessage.debugEntryMessage("file", fileName);

    validateNovaServerResourceMetaData(fileName, resourceId,
        heatOrchestrationTemplate.getResources().get(resourceId), globalContext);
    validateNovaServerResourceNetworkUniqueRole(fileName, resourceId, uniqueResourcePortNetworkRole,
        heatOrchestrationTemplate, globalContext);
    validateAvailabilityZoneName(fileName, resourceEntry, globalContext);
    validateNovaServerNameImageAndFlavor(fileName, envFileName, resourceEntry, globalContext);

    mdcDataDebugMessage.debugExitMessage("file", fileName);
  }

  @SuppressWarnings("unchecked")
  private void validateNovaServerResourceMetaData(String fileName, String resourceId,
                                                  Resource resource,
                                                  GlobalValidationContext globalValidationContext) {

    mdcDataDebugMessage.debugEntryMessage("file", fileName);

    Map<String, Object> novaServerProp = resource.getProperties();
    Object novaServerPropMetadata;
    if (MapUtils.isNotEmpty(novaServerProp)) {
      novaServerPropMetadata = novaServerProp.get("metadata");
      if (novaServerPropMetadata == null) {
        globalValidationContext.addMessage(
            fileName,
            ErrorLevel.WARNING,
            ErrorMessagesFormatBuilder
                .getErrorWithParameters(Messages.MISSING_NOVA_SERVER_METADATA.getErrorMessage(),
                    resourceId),
            LoggerTragetServiceName.VALIDATE_NOVA_META_DATA_NAME,
            LoggerErrorDescription.MISSING_NOVA_PROPERTIES);
      } else if (novaServerPropMetadata instanceof Map) {
        TreeMap<String, Object> propertyMap = new TreeMap(new Comparator<String>() {

          @Override
          public int compare(String o1, String o2) {
            return o1.compareToIgnoreCase(o2);
          }

          @Override
          public boolean equals(Object obj) {
            return false;
          }
        });
        propertyMap.putAll((Map) novaServerPropMetadata);
        if (!propertyMap.containsKey("vf_module_id")) {
          globalValidationContext.addMessage(
              fileName,
              ErrorLevel.WARNING,
              ErrorMessagesFormatBuilder.getErrorWithParameters(
                  Messages.MISSING_NOVA_SERVER_VF_MODULE_ID.getErrorMessage(), resourceId),
              LoggerTragetServiceName.VALIDATE_NOVA_META_DATA_NAME,
              LoggerErrorDescription.MISSING_NOVA_PROPERTIES);
        }
        if (!propertyMap.containsKey("vnf_id")) {
          globalValidationContext.addMessage(
              fileName, ErrorLevel.WARNING,
              ErrorMessagesFormatBuilder
                  .getErrorWithParameters(Messages.MISSING_NOVA_SERVER_VNF_ID.getErrorMessage(),
                      resourceId),
              LoggerTragetServiceName.VALIDATE_NOVA_META_DATA_NAME,
              LoggerErrorDescription.MISSING_NOVA_PROPERTIES);
        }
      }
    }

    mdcDataDebugMessage.debugExitMessage("file", fileName);
  }

  private void validateNovaServerResourceNetworkUniqueRole(String fileName, String resourceId,
                                                           Map<String, String> uniqueResourcePortNetworkRole,
                                                           HeatOrchestrationTemplate heatOrchestrationTemplate,
                                                           GlobalValidationContext globalValidationContext) {


    mdcDataDebugMessage.debugEntryMessage("file", fileName);

    Object network;
    String role = null;

    Object propertyNetworkValue =
        heatOrchestrationTemplate.getResources().get(resourceId).getProperties().get("networks");
    if (propertyNetworkValue != null && propertyNetworkValue instanceof List) {
      List<String> portResourceIdList =
          getNovaNetworkPortResourceList(fileName, (List) propertyNetworkValue,
              globalValidationContext);
      for (String portResourceId : portResourceIdList) {
        Resource portResource = heatOrchestrationTemplate.getResources().get(portResourceId);
        if (portResource != null && portResource.getType()
            .equals(HeatResourcesTypes.NEUTRON_PORT_RESOURCE_TYPE.getHeatResource())) {
          Map portNetwork =
              getPortNetwork(fileName, resourceId, portResource, globalValidationContext);
          if (Objects.nonNull(portNetwork)) {
            network = portNetwork.get("get_param");
            if (Objects.nonNull(network)) {
              if (network instanceof String ){
                role = getNetworkRole((String)network);
              }else if (network instanceof List){
                role = getNetworkRole((String)((List) network).get(0));
              }
              if (role != null && uniqueResourcePortNetworkRole.containsKey(role)) {
                globalValidationContext.addMessage(
                    fileName,
                    ErrorLevel.WARNING,
                    ErrorMessagesFormatBuilder.getErrorWithParameters(
                        Messages.RESOURCE_CONNECTED_TO_TWO_EXTERNAL_NETWORKS_WITH_SAME_ROLE
                            .getErrorMessage(), resourceId, role),
                    LoggerTragetServiceName.VALIDATE_RESOURCE_NETWORK_UNIQUE_ROLW,
                    LoggerErrorDescription.RESOURCE_UNIQUE_NETWORK_ROLE);
              } else {
                uniqueResourcePortNetworkRole.put(role, portResourceId);
              }
            }
          }
        }
      }
    }

    mdcDataDebugMessage.debugExitMessage("file", fileName);
  }

  private List<String> getNovaNetworkPortResourceList(String filename, List propertyNetworkValue,
                                                      GlobalValidationContext globalContext) {
    List<String> portResourceIdList = new ArrayList<>();
    for (Object propValue : propertyNetworkValue) {
      Object portPropValue = ((Map) propValue).get("port");
      Collection<String> portResourceIds = HeatStructureUtil
          .getReferencedValuesByFunctionName(filename, "get_resource", portPropValue,
              globalContext);
      if (portResourceIds != null) {
        portResourceIdList.addAll(portResourceIds);
      }
    }

    return portResourceIdList;
  }

  private String getNetworkRole(String network) {
    if (network == null) {
      return null;
    }
    if (network.contains("_net_id")) {
      return network.substring(0, network.indexOf("_net_id"));
    } else if (network.contains("net_name")) {
      return network.substring(0, network.indexOf("_net_name"));
    } else if (network.contains("net_fqdn")) {
      return network.substring(0, network.indexOf("_net_fqdn"));
    }
    return null;
  }

  private Map getPortNetwork(String fileName, String resourceId, Resource portResource,
                             GlobalValidationContext globalValidationContext) {
    Object portNetwork = portResource.getProperties().get("network_id");
    if (portNetwork == null) {
      portNetwork = portResource.getProperties().get("network");
    }
    if (!(portNetwork instanceof Map)) {
      globalValidationContext.addMessage(
          fileName,
          ErrorLevel.WARNING,
          ErrorMessagesFormatBuilder
              .getErrorWithParameters(Messages.MISSING_GET_PARAM.getErrorMessage(),
                  "network or network_id", resourceId),
          LoggerTragetServiceName.VALIDATE_RESOURCE_NETWORK_UNIQUE_ROLW,
          LoggerErrorDescription.MISSING_GET_PARAM);
      return null;
    }
    return (Map) portNetwork;
  }

  private void validateAvailabilityZoneName(String fileName,
                                            Map.Entry<String, Resource> resourceEntry,
                                            GlobalValidationContext globalContext) {


    mdcDataDebugMessage.debugEntryMessage("file", fileName);

    String[] regexList = new String[]{"availability_zone_(\\d+)"};

    if (MapUtils.isEmpty(resourceEntry.getValue().getProperties())) {
      mdcDataDebugMessage.debugExitMessage("file", fileName);
      return;
    }

    Object availabilityZoneMap =
        resourceEntry.getValue().getProperties().containsKey("availability_zone") ? resourceEntry
            .getValue().getProperties().get("availability_zone") : null;

    if (nonNull(availabilityZoneMap)) {
      if (availabilityZoneMap instanceof Map) {
        String availabilityZoneName = ValidationUtil.getWantedNameFromPropertyValueGetParam
            (availabilityZoneMap);

        if (availabilityZoneName != null) {
          if (!ValidationUtil.evalPattern(availabilityZoneName, regexList)) {
            globalContext.addMessage(
                fileName,
                ErrorLevel.WARNING, ErrorMessagesFormatBuilder.getErrorWithParameters(
                    Messages.PARAMETER_NAME_NOT_ALIGNED_WITH_GUIDELINES.getErrorMessage(),
                    ValidationUtil.getMessagePartAccordingToResourceType(resourceEntry),
                    "Availability Zone",
                    availabilityZoneName, resourceEntry.getKey()),
                LoggerTragetServiceName.VALIDATE_AVAILABILITY_ZONE_NAME,
                LoggerErrorDescription.NAME_NOT_ALIGNED_WITH_GUIDELINES);
          }
        }
      } else {
        globalContext.addMessage(
            fileName,
            ErrorLevel.WARNING, ErrorMessagesFormatBuilder
                .getErrorWithParameters(Messages.MISSING_GET_PARAM.getErrorMessage(),
                    "availability_zone", resourceEntry.getKey()),
            LoggerTragetServiceName.VALIDATE_AVAILABILITY_ZONE_NAME,
            LoggerErrorDescription.MISSING_GET_PARAM);
      }
    }
    mdcDataDebugMessage.debugExitMessage("file", fileName);
  }

  private void validateNovaServerNameImageAndFlavor(String fileName, String envFileName,
                                                    Map.Entry<String, Resource> resourceEntry,
                                                    GlobalValidationContext globalContext) {

    mdcDataDebugMessage.debugEntryMessage("file", fileName);

    String novaName =
        validateNovaServerNamingConvention(fileName, envFileName, resourceEntry, globalContext);
    Map<String, String> legalNovaNamingConventionMap =
        validateImageAndFlavorFromNovaServer(fileName, resourceEntry, globalContext);

    if (Objects.nonNull(novaName)) {
      legalNovaNamingConventionMap.put("name", novaName);
    }

    if (legalNovaNamingConventionMap.keySet().size() > 1) {
      validateNovaServerNameImageAndFlavorSync(fileName, resourceEntry,
          legalNovaNamingConventionMap, globalContext);
    }

    mdcDataDebugMessage.debugExitMessage("file", fileName);
  }

  private String validateNovaServerNamingConvention(String fileName, String envFileName,
                                                    Map.Entry<String, Resource> resourceEntry,
                                                    GlobalValidationContext globalContext) {

    mdcDataDebugMessage.debugEntryMessage("file", fileName);

    if (MapUtils.isEmpty(resourceEntry.getValue().getProperties())) {
      mdcDataDebugMessage.debugExitMessage("file", fileName);
      return null;
    }

    mdcDataDebugMessage.debugExitMessage("file", fileName);
    return checkIfNovaNameByGuidelines(fileName, envFileName, resourceEntry, globalContext);
  }

  private Map<String, String> validateImageAndFlavorFromNovaServer(String fileName,
                                                                   Map.Entry<String, Resource> resourceEntry,
                                                                   GlobalValidationContext globalContext) {

    mdcDataDebugMessage.debugEntryMessage("file", fileName);

    if (MapUtils.isEmpty(resourceEntry.getValue().getProperties())) {
      mdcDataDebugMessage.debugExitMessage("file", fileName);
      return null;
    }

    Pair<String, String> imagePair = new ImmutablePair<>("image", ".*_image_name");
    Pair<String, String> flavorPair = new ImmutablePair<>("flavor", ".*_flavor_name");
    List<Pair<String, String>> imageFlavorPairs = Arrays.asList(imagePair, flavorPair);
    Map<String, Object> propertiesMap = resourceEntry.getValue().getProperties();
    Map<String, String> imageAndFlavorLegalNames = new HashMap<>();

    for (Pair<String, String> imageOrFlavor : imageFlavorPairs) {
      boolean isErrorInImageOrFlavor =
          isErrorExistWhenValidatingImageOrFlavorNames(fileName, imageOrFlavor, resourceEntry,
              propertiesMap, globalContext);
      if (!isErrorInImageOrFlavor) {
        Object nameValue = propertiesMap.get(imageOrFlavor.getKey()) == null ? null
            : propertiesMap.get(imageOrFlavor.getKey());
        String imageOrFlavorName = ValidationUtil.getWantedNameFromPropertyValueGetParam(nameValue);
        imageAndFlavorLegalNames.put(imageOrFlavor.getKey(), imageOrFlavorName);
      }
    }

    mdcDataDebugMessage.debugExitMessage("file", fileName);
    return imageAndFlavorLegalNames;
  }

  private String checkIfNovaNameByGuidelines(String fileName, String envFileName,
                                             Map.Entry<String, Resource> resourceEntry,
                                             GlobalValidationContext globalContext) {
    if (MapUtils.isEmpty(resourceEntry.getValue().getProperties())) {
      return null;
    }
    Object novaNameGetParam = getNovaServerName(resourceEntry);
    String novaName = null;
    if (nonNull(novaNameGetParam)) {
      novaName =
          checkNovaNameGetParamValueMap(fileName, novaNameGetParam, resourceEntry, globalContext);
      checkIfNovaNameParameterInEnvIsStringOrList(fileName, envFileName, resourceEntry, novaName,
          globalContext);
    } else {
      globalContext.addMessage(
          fileName,
          ErrorLevel.WARNING, ErrorMessagesFormatBuilder
              .getErrorWithParameters(Messages.MISSING_GET_PARAM.getErrorMessage(),
                  "nova server name", resourceEntry.getKey()),
          LoggerTragetServiceName.VALIDATE_NOVA_SERVER_NAME,
          LoggerErrorDescription.MISSING_GET_PARAM);
    }

    return novaName;
  }

  private boolean isErrorExistWhenValidatingImageOrFlavorNames(String fileName,
                                                               Pair<String, String> propertyNameAndRegex,
                                                               Map.Entry<String, Resource> resourceEntry,
                                                               Map<String, Object> propertiesMap,
                                                               GlobalValidationContext globalContext) {
    String propertyName = propertyNameAndRegex.getKey();
    Object nameValue =
        propertiesMap.get(propertyName) == null ? null : propertiesMap.get(propertyName);
    String[] regexList = new String[]{propertyNameAndRegex.getValue()};

    if (nonNull(nameValue)) {
      if (nameValue instanceof Map) {
        if (ValidationUtil.validateMapPropertyValue(fileName, resourceEntry, globalContext,
            propertyName,
            nameValue, regexList)) {
          return true;
        }
      } else {
        globalContext.addMessage(
            fileName,
            ErrorLevel.WARNING, ErrorMessagesFormatBuilder
                .getErrorWithParameters(Messages.MISSING_GET_PARAM.getErrorMessage(), propertyName,
                    resourceEntry.getKey()),
            LoggerTragetServiceName.VALIDATE_IMAGE_AND_FLAVOR_NAME,
            LoggerErrorDescription.MISSING_GET_PARAM);
        return true;
      }

      return false;
    }
    return false;
  }

  private Object getNovaServerName(Map.Entry<String, Resource> resourceEntry) {
    Object novaServerName = resourceEntry.getValue().getProperties().get("name");
    Map novaNameMap;
    if (nonNull(novaServerName)) {
      if (novaServerName instanceof Map) {
        novaNameMap = (Map) novaServerName;
        return novaNameMap.get(ResourceReferenceFunctions.GET_PARAM.getFunction()) == null ? null
            : novaNameMap.get(ResourceReferenceFunctions.GET_PARAM.getFunction());
      }
    }
    return null;
  }

  @SuppressWarnings("unchecked")
  private String checkNovaNameGetParamValueMap(String fileName, Object getParamValue,
                                               Map.Entry<String, Resource> resourceEntry,
                                               GlobalValidationContext globalContext) {
    if (getParamValue instanceof List) {
      List<Object> getParamNameList = (List) getParamValue;
      String[] regexName = new String[]{".*_names"};
      return isNovaNameAsListLegal(fileName, getParamNameList, regexName, resourceEntry,
          globalContext);
    } else if (getParamValue instanceof String) {
      String[] regexName = new String[]{".*_name_(\\d+)"};
      return isNovaNameAsStringLegal(fileName, (String) getParamValue, regexName, resourceEntry,
          globalContext);
    }

    return null;
  }

  private void checkIfNovaNameParameterInEnvIsStringOrList(String fileName, String envFileName,
                                                           Map.Entry<String, Resource> resourceEntry,
                                                           String novaServerName,
                                                           GlobalValidationContext globalContext) {
    if (nonNull(envFileName)) {
      Environment environment = ValidationUtil.validateEnvContent(envFileName, globalContext);

      if (environment != null && MapUtils.isNotEmpty(environment.getParameters())) {
        Object novaServerNameEnvValue =
            environment.getParameters().containsKey(novaServerName) ? environment.getParameters()
                .get(novaServerName) : null;
        if (Objects.nonNull(novaServerNameEnvValue)) {
          if (!DefinedHeatParameterTypes
              .isNovaServerEnvValueIsFromRightType(novaServerNameEnvValue)) {
            globalContext.addMessage(
                fileName,
                ErrorLevel.WARNING, ErrorMessagesFormatBuilder.getErrorWithParameters(
                    Messages.PARAMETER_NAME_NOT_ALIGNED_WITH_GUIDELINES.getErrorMessage(), "Server",
                    "Name", novaServerNameEnvValue.toString(), resourceEntry.getKey()),
                LoggerTragetServiceName.VALIDATE_NOVA_SERVER_NAME,
                LoggerErrorDescription.NAME_NOT_ALIGNED_WITH_GUIDELINES);
          }
        }
      }
    }
  }

  private String isNovaNameAsListLegal(String fileName, List<Object> getParamNameList,
                                       String[] regexName,
                                       Map.Entry<String, Resource> resourceEntry,
                                       GlobalValidationContext globalContext) {

    if (getParamNameList.size() != 2 || !ValidationUtil.evalPattern(getParamNameList.get(0),
        regexName)) {
      globalContext.addMessage(
          fileName,
          ErrorLevel.WARNING,
          ErrorMessagesFormatBuilder.getErrorWithParameters(
              Messages.PARAMETER_NAME_NOT_ALIGNED_WITH_GUIDELINES.getErrorMessage(), "Server",
              "name", getParamNameList.toString(), resourceEntry.getKey()),
          LoggerTragetServiceName.VALIDATE_NOVA_SERVER_NAME,
          LoggerErrorDescription.NAME_NOT_ALIGNED_WITH_GUIDELINES);
      return null;
    }

    return (String) getParamNameList.get(0);
  }

  private String isNovaNameAsStringLegal(String fileName, String novaName, String[] regexName,
                                         Map.Entry<String, Resource> resourceEntry,
                                         GlobalValidationContext globalContext) {
    if (!ValidationUtil.evalPattern(novaName, regexName)) {
      globalContext.addMessage(
          fileName,
          ErrorLevel.WARNING,
          ErrorMessagesFormatBuilder.getErrorWithParameters(
              Messages.PARAMETER_NAME_NOT_ALIGNED_WITH_GUIDELINES.getErrorMessage(), "Server",
              "name", novaName, resourceEntry.getKey()),
          LoggerTragetServiceName.VALIDATE_NOVA_SERVER_NAME,
          LoggerErrorDescription.NAME_NOT_ALIGNED_WITH_GUIDELINES);
      return null;
    }
    return novaName;
  }

  private void validateNovaServerNameImageAndFlavorSync(String fileName,
                                                        Map.Entry<String, Resource> resourceEntry,
                                                        Map<String, String> legalNovaNamingConventionNames,
                                                        GlobalValidationContext globalContext) {

    mdcDataDebugMessage.debugEntryMessage("file", fileName);

    List<String> vmNames = new LinkedList<>();

    for (Map.Entry<String, String> nameEntry : legalNovaNamingConventionNames.entrySet()) {
      vmNames.add(getVmName(nameEntry.getValue(), nameEntry.getKey()));
    }

    vmNames.removeIf(VMName -> VMName == null);

    if (!isVmNameSync(vmNames)) {
      globalContext.addMessage(
          fileName,
          ErrorLevel.WARNING,
          ErrorMessagesFormatBuilder.getErrorWithParameters(
              Messages.NOVA_NAME_IMAGE_FLAVOR_NOT_CONSISTENT.getErrorMessage(),
              resourceEntry.getKey()),
          LoggerTragetServiceName.VALIDATE_IMAGE_AND_FLAVOR_NAME,
          LoggerErrorDescription.NAME_NOT_ALIGNED_WITH_GUIDELINES);
    }
    mdcDataDebugMessage.debugExitMessage("file", fileName);
  }

  private String getVmName(String nameToGetVmNameFrom, String stringToGetIndexOf) {
    int vmIndex =
        nameToGetVmNameFrom == null ? -1 : nameToGetVmNameFrom.indexOf(stringToGetIndexOf);
    String vmName = vmIndex < 0 ? null
        : trimNonAlphaNumericCharactersFromEndOfString(nameToGetVmNameFrom.substring(0, vmIndex));

    return vmName;

  }

  private boolean isVmNameSync(List<String> namesToCompare) {
    int size = namesToCompare.size();
    for (int i = 0; i < size - 1; i++) {
      if (!namesToCompare.get(i).equals(namesToCompare.get(i + 1))) {
        return false;
      }
    }
    return true;
  }

  private String trimNonAlphaNumericCharactersFromEndOfString(String toTrim) {
    int stringSize = toTrim.length();
    int stringLength = stringSize - 1;
    String[] regexList = new String[]{"[^a-zA-Z0-9]"};

    while (stringLength >= 0) {
      if (!ValidationUtil.evalPattern(String.valueOf(toTrim.charAt(stringLength)), regexList)) {
        break;
      }
      stringLength--;
    }

    return toTrim.substring(0, stringLength + 1);
  }
}