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