push addional code
[sdc.git] / openecomp-be / lib / openecomp-sdc-validation-lib / openecomp-sdc-validation-impl / src / main / java / org / openecomp / sdc / validation / impl / validators / EcompGuideLineValidator.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.validation.impl.validators;
22
23 import org.apache.commons.collections4.CollectionUtils;
24 import org.apache.commons.collections4.MapUtils;
25 import org.openecomp.core.utilities.CommonMethods;
26 import org.openecomp.core.utilities.yaml.YamlUtil;
27 import org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder;
28 import org.openecomp.core.validation.errors.Messages;
29 import org.openecomp.core.validation.interfaces.Validator;
30 import org.openecomp.core.validation.types.GlobalValidationContext;
31 import org.openecomp.sdc.common.utils.AsdcCommon;
32 import org.openecomp.sdc.datatypes.error.ErrorLevel;
33 import org.openecomp.sdc.datatypes.model.heat.ForbiddenHeatResourceTypes;
34 import org.openecomp.sdc.heat.datatypes.DefinedHeatParameterTypes;
35 import org.openecomp.sdc.heat.datatypes.manifest.FileData;
36 import org.openecomp.sdc.heat.datatypes.manifest.ManifestContent;
37 import org.openecomp.sdc.heat.datatypes.model.Environment;
38 import org.openecomp.sdc.heat.datatypes.model.HeatOrchestrationTemplate;
39 import org.openecomp.sdc.heat.datatypes.model.HeatResourcesTypes;
40 import org.openecomp.sdc.heat.datatypes.model.Resource;
41 import org.openecomp.sdc.heat.datatypes.model.ResourceReferenceFunctions;
42 import org.openecomp.sdc.heat.services.HeatStructureUtil;
43 import org.openecomp.sdc.heat.services.manifest.ManifestUtil;
44
45 import java.util.ArrayList;
46 import java.util.Collection;
47 import java.util.Comparator;
48 import java.util.HashMap;
49 import java.util.HashSet;
50 import java.util.List;
51 import java.util.Map;
52 import java.util.Objects;
53 import java.util.Set;
54 import java.util.TreeMap;
55 import java.util.regex.Pattern;
56
57 public class EcompGuideLineValidator extends HeatValidator implements Validator {
58   @Override
59   public void validate(GlobalValidationContext globalContext) {
60
61     ManifestContent manifestContent;
62     try {
63       manifestContent = checkValidationPreCondition(globalContext);
64     } catch (Exception exception) {
65       return;
66     }
67
68     //global validations
69     Set<String> baseFiles = validateManifest(manifestContent, globalContext);
70
71     Map<String, FileData.Type> fileTypeMap = ManifestUtil.getFileTypeMap(manifestContent);
72     Map<String, FileData> fileEnvMap = ManifestUtil.getFileAndItsEnv(manifestContent);
73     globalContext
74         .getFiles()
75         .stream()
76         .filter(fileName -> FileData
77             .isHeatFile(fileTypeMap.get(fileName)))
78         .forEach(fileName -> validate(fileName,
79             fileEnvMap.get(fileName) != null ? fileEnvMap.get(fileName).getFile() : null,
80             fileTypeMap, baseFiles, globalContext));
81   }
82
83   private void validate(String fileName, String envFileName, Map<String, FileData.Type> fileTypeMap,
84                         Set<String> baseFiles, GlobalValidationContext globalContext) {
85     HeatOrchestrationTemplate heatOrchestrationTemplate =
86         checkHeatOrchestrationPreCondition(fileName, globalContext);
87     if (heatOrchestrationTemplate == null) {
88       return;
89     }
90
91     validateBaseFile(fileName, baseFiles, heatOrchestrationTemplate, globalContext);
92     validateHeatVolumeFile(fileName, fileTypeMap, heatOrchestrationTemplate, globalContext);
93     validateHeatNamingConvention(fileName, heatOrchestrationTemplate, globalContext);
94     validateHeatNovaResource(fileName, envFileName, heatOrchestrationTemplate, globalContext);
95     validateResourceTypeIsForbidden(fileName, heatOrchestrationTemplate, globalContext);
96     validateFixedIpsNamingConvention(fileName, heatOrchestrationTemplate, globalContext);
97   }
98
99   private void validateHeatNovaResource(String fileName, String envFileName,
100                                         HeatOrchestrationTemplate heatOrchestrationTemplate,
101                                         GlobalValidationContext globalContext) {
102     Map<String, String> uniqueResourcePortNetworkRole = new HashMap<>();
103     //if no resources exist return
104     if (heatOrchestrationTemplate.getResources() == null
105         || heatOrchestrationTemplate.getResources().size() == 0) {
106       return;
107     }
108
109     heatOrchestrationTemplate
110         .getResources()
111         .entrySet()
112         .stream()
113         .filter(entry -> entry.getValue().getType()
114             .equals(HeatResourcesTypes.NOVA_SERVER_RESOURCE_TYPE.getHeatResource()))
115         .forEach(entry -> validateNovaServerResourceType(entry.getKey(), fileName, envFileName,
116             entry, uniqueResourcePortNetworkRole, heatOrchestrationTemplate, globalContext));
117   }
118
119   private void validateNovaServerResourceType(String resourceId, String fileName,
120                                               String envFileName,
121                                               Map.Entry<String, Resource> resourceEntry,
122                                               Map<String, String> uniqueResourcePortNetworkRole,
123                                               HeatOrchestrationTemplate heatOrchestrationTemplate,
124                                               GlobalValidationContext globalValidationContext) {
125     validateNovaServerResourceMetaData(fileName, resourceId,
126         heatOrchestrationTemplate.getResources().get(resourceId), globalValidationContext);
127     validateNovaServerResourceNetworkUniqueRole(fileName, resourceId, heatOrchestrationTemplate,
128         globalValidationContext);
129     validateNovaServerNamingConvention(fileName, envFileName, resourceEntry,
130         globalValidationContext);
131     validateNovaServerAvailabilityZoneName(fileName, resourceEntry, globalValidationContext);
132     validateImageAndFlavorFromNovaServer(fileName, resourceEntry, globalValidationContext);
133   }
134
135   @SuppressWarnings("unchecked")
136   private void validateNovaServerResourceMetaData(String fileName, String resourceId,
137                                                   Resource resource,
138                                                   GlobalValidationContext globalValidationContext) {
139     Map<String, Object> novaServerProp = resource.getProperties();
140     Object novaServerPropMetadata;
141     if (MapUtils.isNotEmpty(novaServerProp)) {
142       novaServerPropMetadata = novaServerProp.get("metadata");
143       if (novaServerPropMetadata == null) {
144         globalValidationContext.addMessage(
145             fileName,
146             ErrorLevel.WARNING,
147             ErrorMessagesFormatBuilder
148                 .getErrorWithParameters(Messages.MISSING_NOVA_SERVER_METADATA.getErrorMessage(),
149                     resourceId));
150       } else if (novaServerPropMetadata instanceof Map) {
151         TreeMap<String, Object> propertyMap = new TreeMap(new Comparator<String>() {
152
153           @Override
154           public int compare(String o1, String o2) {
155             return o1.compareToIgnoreCase(o2);
156           }
157
158           @Override
159           public boolean equals(Object obj) {
160             return false;
161           }
162         });
163         propertyMap.putAll((Map) novaServerPropMetadata);
164         if (!propertyMap.containsKey("vf_module_id")) {
165           globalValidationContext.addMessage(fileName, ErrorLevel.WARNING,
166               ErrorMessagesFormatBuilder.getErrorWithParameters(
167                   Messages.MISSING_NOVA_SERVER_VF_MODULE_ID.getErrorMessage(), resourceId));
168         }
169         if (!propertyMap.containsKey("vnf_id")) {
170           globalValidationContext.addMessage(fileName, ErrorLevel.WARNING,
171               ErrorMessagesFormatBuilder
172                   .getErrorWithParameters(Messages.MISSING_NOVA_SERVER_VNF_ID.getErrorMessage(),
173                       resourceId));
174         }
175       }
176     }
177   }
178
179   private void validateNovaServerResourceNetworkUniqueRole(String fileName, String resourceId,
180                                                            HeatOrchestrationTemplate
181                                                                heatOrchestrationTemplate,
182                                                            GlobalValidationContext
183                                                                globalValidationContext) {
184
185     String network;
186     String role;
187     Map<String, String> uniqueResourcePortNetworkRole = new HashMap<>();
188
189     Object propertyNetworkValue =
190         heatOrchestrationTemplate.getResources().get(resourceId).getProperties().get("networks");
191     if (propertyNetworkValue != null && propertyNetworkValue instanceof List) {
192       List<String> portResourceIdList =
193           getNovaNetworkPortResourceList(fileName, (List) propertyNetworkValue,
194               globalValidationContext);
195       for (String portResourceId : portResourceIdList) {
196         Resource portResource = heatOrchestrationTemplate.getResources().get(portResourceId);
197         if (portResource != null && portResource.getType()
198             .equals(HeatResourcesTypes.NEUTRON_PORT_RESOURCE_TYPE.getHeatResource())) {
199           Map portNetwork =
200               getPortNetwork(fileName, resourceId, portResource, globalValidationContext);
201           if (Objects.nonNull(portNetwork)) {
202             network = (String) portNetwork.get("get_param");
203             if (Objects.nonNull(network)) {
204               role = getNetworkRole(network);
205               if (role != null && uniqueResourcePortNetworkRole.containsKey(role)) {
206                 globalValidationContext.addMessage(fileName, ErrorLevel.WARNING,
207                     ErrorMessagesFormatBuilder.getErrorWithParameters(
208                         Messages.RESOURCE_CONNECTED_TO_TWO_EXTERNAL_NETWORKS_WITH_SAME_ROLE
209                             .getErrorMessage(), resourceId, role));
210               } else {
211                 uniqueResourcePortNetworkRole.put(role, portResourceId);
212               }
213             }
214           }
215         }
216       }
217     }
218   }
219
220
221   private Map getPortNetwork(String fileName, String resourceId, Resource portResource,
222                              GlobalValidationContext globalValidationContext) {
223     Object portNetwork = portResource.getProperties().get("network_id");
224     if (portNetwork == null) {
225       portNetwork = portResource.getProperties().get("network");
226     }
227     if (!(portNetwork instanceof Map)) {
228       globalValidationContext.addMessage(fileName, ErrorLevel.WARNING, ErrorMessagesFormatBuilder
229           .getErrorWithParameters(Messages.MISSING_GET_PARAM.getErrorMessage(),
230               "network or network_id", resourceId));
231       return null;
232     }
233     return (Map) portNetwork;
234   }
235
236   private List<String> getNovaNetworkPortResourceList(String filename, List propertyNetworkValue,
237                                                       GlobalValidationContext globalContext) {
238     List<String> portResourceIdList = new ArrayList<>();
239     for (Object propValue : propertyNetworkValue) {
240       Object portPropValue = ((Map) propValue).get("port");
241       Collection<String> portResourceIds = HeatStructureUtil
242           .getReferencedValuesByFunctionName(filename, "get_resource", portPropValue,
243               globalContext);
244       if (portResourceIds != null) {
245         portResourceIdList.addAll(portResourceIds);
246       }
247     }
248
249     return portResourceIdList;
250   }
251
252   private String getNetworkRole(String network) {
253     if (network == null) {
254       return null;
255     }
256     if (network.contains("_net_id")) {
257       return network.substring(0, network.indexOf("_net_id"));
258     } else if (network.contains("net_name")) {
259       return network.substring(0, network.indexOf("_net_name"));
260     } else if (network.contains("net_fqdn")) {
261       return network.substring(0, network.indexOf("_net_fqdn"));
262     }
263     return null;
264   }
265
266   private void validateHeatNamingConvention(String fileName,
267                                             HeatOrchestrationTemplate heatOrchestrationTemplate,
268                                             GlobalValidationContext globalContext) {
269     validatePortNetworkNamingConvention(fileName, heatOrchestrationTemplate, globalContext);
270   }
271
272   private void validatePortNetworkNamingConvention(String fileName,
273                                                    HeatOrchestrationTemplate
274                                                        heatOrchestrationTemplate,
275                                                    GlobalValidationContext globalContext) {
276     if (MapUtils.isEmpty(heatOrchestrationTemplate.getResources())) {
277       return;
278     }
279     String[] regexList = new String[]{".*_net_id", ".*_net_name", ".*_net_fqdn"};
280
281     heatOrchestrationTemplate
282         .getResources()
283         .entrySet()
284         .stream()
285         .filter(entry -> entry.getValue().getType() != null && entry.getValue().getType()
286             .equals(HeatResourcesTypes.NEUTRON_PORT_RESOURCE_TYPE.getHeatResource()))
287         .forEach(entry -> entry.getValue()
288             .getProperties()
289             .entrySet()
290             .stream()
291             .filter(propertyEntry -> propertyEntry != null
292                 && (propertyEntry.getKey().toLowerCase().equals("network".toLowerCase())
293                 ||
294                 propertyEntry.getKey().equals("network_id")))
295             .forEach(propertyEntry -> validateParamNamingConvention(fileName, entry.getKey(),
296                 propertyEntry.getValue(), regexList,
297                 Messages.NETWORK_PARAM_NOT_ALIGNED_WITH_GUIDE_LINE, globalContext)));
298   }
299
300   private void validateParamNamingConvention(String fileName, String resourceId,
301                                              Object propertyValue, String[] regexList,
302                                              Messages message,
303                                              GlobalValidationContext globalContext) {
304     Object paramName;
305     if (propertyValue instanceof Map) {
306       paramName = ((Map) propertyValue).get("get_param");
307       if (paramName instanceof String) {
308         if (!evalPattern((String) paramName, regexList)) {
309           globalContext.addMessage(fileName, ErrorLevel.WARNING, ErrorMessagesFormatBuilder
310               .getErrorWithParameters(message.getErrorMessage(), (String) paramName, resourceId));
311         }
312       }
313     } else {
314       globalContext.addMessage(fileName, ErrorLevel.WARNING, ErrorMessagesFormatBuilder
315           .getErrorWithParameters(Messages.MISSING_GET_PARAM.getErrorMessage(),
316               "network or network_id", resourceId));
317     }
318   }
319
320   private boolean evalPattern(Object paramVal, String[] regexList) {
321     String value = "";
322     if (paramVal instanceof String) {
323       value = ((String) paramVal);
324     }
325     if (paramVal instanceof Integer) {
326       value = paramVal.toString();
327     }
328     return evalPattern(value, regexList);
329   }
330
331   private boolean evalPattern(String paramVal, String[] regexList) {
332
333     for (String regex : regexList) {
334       if (Pattern.matches(regex, paramVal)) {
335         return true;
336       }
337     }
338
339     return false;
340   }
341
342
343   private void validateHeatVolumeFile(String fileName, Map<String, FileData.Type> fileTypeMap,
344                                       HeatOrchestrationTemplate heatOrchestrationTemplate,
345                                       GlobalValidationContext globalContext) {
346     //if not heat volume return
347     if (!fileTypeMap.get(fileName).equals(FileData.Type.HEAT_VOL)) {
348       return;
349     }
350
351     //if no resources exist return
352     if (heatOrchestrationTemplate.getResources() == null
353         || heatOrchestrationTemplate.getResources().size() == 0) {
354       return;
355     }
356
357     Set<String> expectedExposedResources = new HashSet<>();
358     Set<String> actualExposedResources = new HashSet<>();
359     heatOrchestrationTemplate.getResources()
360         .entrySet()
361         .stream()
362         .filter(entry -> entry.getValue().getType()
363             .equals(HeatResourcesTypes.CINDER_VOLUME_RESOURCE_TYPE.getHeatResource()))
364         .forEach(entry -> expectedExposedResources.add(entry.getKey()));
365
366     if (heatOrchestrationTemplate.getOutputs() != null) {
367
368       heatOrchestrationTemplate.getOutputs().entrySet()
369           .stream()
370           .filter(entry -> isPropertyValueGetResource(fileName, entry.getValue().getValue(),
371               globalContext))
372           .forEach(entry -> actualExposedResources.add(
373               getResourceIdFromPropertyValue(fileName, entry.getValue().getValue(),
374                   globalContext)));
375     }
376
377     actualExposedResources.stream().forEach(expectedExposedResources::remove);
378
379     if (expectedExposedResources.size() > 0) {
380       expectedExposedResources
381           .stream()
382           .forEach(name -> globalContext.addMessage(fileName, ErrorLevel.WARNING,
383               ErrorMessagesFormatBuilder
384                   .getErrorWithParameters(Messages.VOLUME_HEAT_NOT_EXPOSED.getErrorMessage(),
385                       name)));
386     }
387   }
388
389   private void validateBaseFile(String fileName, Set<String> baseFiles,
390                                 HeatOrchestrationTemplate heatOrchestrationTemplate,
391                                 GlobalValidationContext globalContext) {
392
393     //if not base return
394     if (baseFiles == null || !baseFiles.contains(fileName)) {
395       return;
396     }
397
398     //if no resources exist return
399     if (heatOrchestrationTemplate.getResources() == null
400         || heatOrchestrationTemplate.getResources().size() == 0) {
401       return;
402     }
403
404     Set<String> expectedExposedResources = new HashSet<>();
405     Set<String> actualExposedResources = new HashSet<>();
406     heatOrchestrationTemplate.getResources()
407         .entrySet()
408         .stream()
409         .filter(entry -> isExpectedToBeExposed(entry.getValue().getType()))
410         .forEach(entry -> expectedExposedResources.add(entry.getKey()));
411
412     if (heatOrchestrationTemplate.getOutputs() != null) {
413
414       heatOrchestrationTemplate.getOutputs().entrySet()
415           .stream()
416           .filter(entry -> isPropertyValueGetResource(fileName, entry.getValue().getValue(),
417               globalContext))
418           .forEach(entry -> actualExposedResources.add(
419               getResourceIdFromPropertyValue(fileName, entry.getValue().getValue(),
420                   globalContext)));
421     }
422     actualExposedResources.stream().forEach(expectedExposedResources::remove);
423
424     if (expectedExposedResources.size() > 0) {
425       expectedExposedResources
426           .stream()
427           .forEach(name -> globalContext.addMessage(fileName, ErrorLevel.WARNING,
428               ErrorMessagesFormatBuilder
429                   .getErrorWithParameters(Messages.RESOURCE_NOT_DEFINED_IN_OUTPUT.getErrorMessage(),
430                       name)));
431     }
432   }
433
434   private void validateResourceTypeIsForbidden(String fileName,
435                                                HeatOrchestrationTemplate heatOrchestrationTemplate,
436                                                GlobalValidationContext globalContext) {
437     if (MapUtils.isEmpty(heatOrchestrationTemplate.getResources())) {
438       return;
439     }
440
441     heatOrchestrationTemplate.getResources()
442         .entrySet()
443         .stream()
444         .filter(entry ->
445             ForbiddenHeatResourceTypes.findByForbiddenHeatResource(entry.getValue().getType())
446                 != null)
447         .filter(entry -> ForbiddenHeatResourceTypes
448             .findByForbiddenHeatResource(entry.getValue().getType())
449             .equals(ForbiddenHeatResourceTypes.HEAT_FLOATING_IP_TYPE))
450         .forEach(entry -> globalContext.addMessage(fileName, ErrorLevel.WARNING,
451             ErrorMessagesFormatBuilder
452                 .getErrorWithParameters(Messages.FLOATING_IP_NOT_IN_USE.getErrorMessage(),
453                     entry.getKey())));
454   }
455
456
457   private void validateFixedIpsNamingConvention(String fileName,
458                                                 HeatOrchestrationTemplate heatOrchestrationTemplate,
459                                                 GlobalValidationContext globalContext) {
460     if (MapUtils.isEmpty(heatOrchestrationTemplate.getResources())) {
461       return;
462     }
463
464     heatOrchestrationTemplate.getResources()
465         .entrySet()
466         .stream()
467         .filter(entry -> HeatResourcesTypes.findByHeatResource(entry.getValue().getType()) != null)
468         .filter(entry -> HeatResourcesTypes.findByHeatResource(entry.getValue().getType())
469             .equals(HeatResourcesTypes.NEUTRON_PORT_RESOURCE_TYPE))
470         .forEach(entry -> checkNeutronPortFixedIpsName(fileName, entry, globalContext));
471   }
472
473   private void validateImageAndFlavorFromNovaServer(String fileName,
474                                                     Map.Entry<String, Resource> resourceEntry,
475                                                     GlobalValidationContext globalContext) {
476     if (MapUtils.isEmpty(resourceEntry.getValue().getProperties())) {
477       return;
478     }
479
480     String[] imageOrFlavorAsParameters = new String[]{"image", "flavor"};
481     Map<String, Object> propertiesMap = resourceEntry.getValue().getProperties();
482
483     for (String imageOrFlavor : imageOrFlavorAsParameters) {
484       checkImageAndFlavorNames(fileName, imageOrFlavor, resourceEntry.getKey(), propertiesMap,
485           globalContext);
486     }
487   }
488
489   private void checkImageAndFlavorNames(String fileName, String imageOrFlavor, String resourceId,
490                                         Map<String, Object> propertiesMap,
491                                         GlobalValidationContext globalContext) {
492     Object nameValue =
493         propertiesMap.get(imageOrFlavor) == null ? null : propertiesMap.get(imageOrFlavor);
494     String[] regexList = new String[]{".*_" + imageOrFlavor + "_name"};
495
496     if (Objects.nonNull(nameValue)) {
497       if (nameValue instanceof Map) {
498         String imageOrFlavorName = getWantedNameFromPropertyValueGetParam(nameValue);
499         if (Objects.nonNull(imageOrFlavorName)) {
500           if (!evalPattern(imageOrFlavorName, regexList)) {
501             globalContext.addMessage(fileName, ErrorLevel.WARNING, ErrorMessagesFormatBuilder
502                 .getErrorWithParameters(
503                     Messages.WRONG_IMAGE_OR_FLAVOR_NAME_NOVA_SERVER.getErrorMessage(),
504                     imageOrFlavor, resourceId));
505           }
506         }
507       } else {
508         globalContext.addMessage(fileName, ErrorLevel.WARNING, ErrorMessagesFormatBuilder
509             .getErrorWithParameters(Messages.MISSING_GET_PARAM.getErrorMessage(), imageOrFlavor,
510                 resourceId));
511       }
512     }
513   }
514
515
516   @SuppressWarnings("unchecked")
517   private void checkNeutronPortFixedIpsName(String fileName,
518                                             Map.Entry<String, Resource> resourceEntry,
519                                             GlobalValidationContext globalContext) {
520     String[] regexList =
521         new String[]{"[^_]+_[^_]+_ips", "[^_]+_[^_]+_v6_ips", "[^_]+_[^_]+_ip_(\\d+)",
522             "[^_]+_[^_]+_v6_ip_(\\d+)"};
523
524     if (MapUtils.isEmpty(resourceEntry.getValue().getProperties())) {
525       return;
526     }
527
528     Map<String, Object> propertiesMap = resourceEntry.getValue().getProperties();
529     Object fixedIps = propertiesMap.get("fixed_ips");
530     if (Objects.nonNull(fixedIps) && fixedIps instanceof List) {
531       List<Object> fixedIpsList = (List<Object>) fixedIps;
532       for (Object fixedIpsObject : fixedIpsList) {
533         Map.Entry<String, Object> fixedIpsEntry =
534             ((Map<String, Object>) fixedIpsObject).entrySet().iterator().next();
535         if (Objects.nonNull(fixedIpsEntry)) {
536           if (fixedIpsEntry.getValue() instanceof Map) {
537             String fixedIpsName = getWantedNameFromPropertyValueGetParam(fixedIpsEntry.getValue());
538             if (Objects.nonNull(fixedIpsName)) {
539               if (!evalPattern(fixedIpsName, regexList)) {
540                 globalContext.addMessage(fileName, ErrorLevel.WARNING, ErrorMessagesFormatBuilder
541                     .getErrorWithParameters(
542                         Messages.FIXED_IPS_NOT_ALIGNED_WITH_GUIDE_LINES.getErrorMessage(),
543                         resourceEntry.getKey()));
544               }
545             }
546           } else {
547             globalContext.addMessage(fileName, ErrorLevel.WARNING, ErrorMessagesFormatBuilder
548                 .getErrorWithParameters(Messages.MISSING_GET_PARAM.getErrorMessage(), "fixed_ips",
549                     resourceEntry.getKey()));
550           }
551         }
552       }
553     }
554   }
555
556
557   private void validateNovaServerNamingConvention(String fileName, String envFileName,
558                                                   Map.Entry<String, Resource> resourceEntry,
559                                                   GlobalValidationContext globalContext) {
560     if (MapUtils.isEmpty(resourceEntry.getValue().getProperties())) {
561       return;
562     }
563
564     checkIfNovaNameByGuidelines(fileName, envFileName, resourceEntry, globalContext);
565   }
566
567   private void checkIfNovaNameByGuidelines(String fileName, String envFileName,
568                                            Map.Entry<String, Resource> resourceEntry,
569                                            GlobalValidationContext globalContext) {
570     if (MapUtils.isEmpty(resourceEntry.getValue().getProperties())) {
571       return;
572     }
573
574     Object novaServerName = resourceEntry.getValue().getProperties().get("name");
575     Map novaNameMap;
576     String novaName;
577     if (Objects.nonNull(novaServerName)) {
578       if (novaServerName instanceof Map) {
579         novaNameMap = (Map) novaServerName;
580         Object novaNameGetParam =
581             novaNameMap.get(ResourceReferenceFunctions.GET_PARAM.getFunction()) == null ? null
582                 : novaNameMap.get(ResourceReferenceFunctions.GET_PARAM.getFunction());
583         if (Objects.nonNull(novaNameGetParam)) {
584           checkNovaNameGetParamValueMap(fileName, novaNameGetParam, resourceEntry, globalContext);
585           novaName = novaNameGetParam instanceof List ? (String) ((List) novaNameGetParam).get(0)
586               : (String) novaNameGetParam;
587           checkIfNovaNameParameterInEnvIsStringOrList(fileName, envFileName, resourceEntry,
588               novaName, globalContext);
589         }
590       } else {
591         globalContext.addMessage(fileName, ErrorLevel.WARNING, ErrorMessagesFormatBuilder
592             .getErrorWithParameters(Messages.MISSING_GET_PARAM.getErrorMessage(),
593                 "nova server name", resourceEntry.getKey()));
594       }
595     }
596
597   }
598
599   private void checkIfNovaNameParameterInEnvIsStringOrList(String fileName, String envFileName,
600                                                            Map.Entry<String, Resource>
601                                                                resourceEntry,
602                                                            String novaServerName,
603                                                            GlobalValidationContext globalContext) {
604     if (Objects.nonNull(envFileName)) {
605       Environment environment = validateEnvContent(envFileName, globalContext);
606
607       if (environment != null && MapUtils.isNotEmpty(environment.getParameters())) {
608         Object novaServerNameEnvValue =
609             environment.getParameters().containsKey(novaServerName) ? environment.getParameters()
610                 .get(novaServerName) : null;
611         if (Objects.nonNull(novaServerNameEnvValue)) {
612           if (!DefinedHeatParameterTypes
613               .isNovaServerEnvValueIsFromRightType(novaServerNameEnvValue)) {
614             globalContext.addMessage(fileName, ErrorLevel.WARNING, ErrorMessagesFormatBuilder
615                 .getErrorWithParameters(
616                     Messages.NOVA_SERVER_NAME_NOT_ALIGNED_WITH_GUIDE_LINES.getErrorMessage(),
617                     resourceEntry.getKey()));
618           }
619         }
620       }
621     }
622   }
623
624
625   private void validateNovaServerAvailabilityZoneName(String fileName,
626                                                       Map.Entry<String, Resource> resourceEntry,
627                                                       GlobalValidationContext globalContext) {
628     String[] regexList = new String[]{"availability_zone_(\\d+)"};
629
630     if (MapUtils.isEmpty(resourceEntry.getValue().getProperties())) {
631       return;
632     }
633
634     Object availabilityZoneMap =
635         resourceEntry.getValue().getProperties().containsKey("availability_zone") ? resourceEntry
636             .getValue().getProperties().get("availability_zone") : null;
637
638     if (Objects.nonNull(availabilityZoneMap)) {
639       if (availabilityZoneMap instanceof Map) {
640         String availabilityZoneName = getWantedNameFromPropertyValueGetParam(availabilityZoneMap);
641
642         if (availabilityZoneName != null) {
643           if (!evalPattern(availabilityZoneName, regexList)) {
644             globalContext.addMessage(fileName, ErrorLevel.WARNING, ErrorMessagesFormatBuilder
645                 .getErrorWithParameters(
646                     Messages.AVAILABILITY_ZONE_NOT_ALIGNED_WITH_GUIDE_LINES.getErrorMessage(),
647                     resourceEntry.getKey()));
648           }
649         }
650       } else {
651         globalContext.addMessage(fileName, ErrorLevel.WARNING, ErrorMessagesFormatBuilder
652             .getErrorWithParameters(Messages.MISSING_GET_PARAM.getErrorMessage(),
653                 "availability_zone", resourceEntry.getKey()));
654       }
655     }
656
657   }
658
659   @SuppressWarnings("unchecked")
660   private void checkNovaNameGetParamValueMap(String fileName, Object getParamValue,
661                                              Map.Entry<String, Resource> resourceEntry,
662                                              GlobalValidationContext globalContext) {
663     if (getParamValue instanceof List) {
664       List<Object> getParamNameList = (List) getParamValue;
665       String[] regexName = new String[]{".*_names"};
666       isNovaNameAsListLegal(fileName, getParamNameList, regexName, resourceEntry, globalContext);
667     } else if (getParamValue instanceof String) {
668       String[] regexName = new String[]{".*_name_(\\d+)"};
669       isNovaNameAsStringLegal(fileName, (String) getParamValue, regexName, resourceEntry,
670           globalContext);
671     }
672
673   }
674
675
676   private void isNovaNameAsListLegal(String fileName, List<Object> getParamNameList,
677                                      String[] regexName, Map.Entry<String, Resource> resourceEntry,
678                                      GlobalValidationContext globalContext) {
679
680     if (getParamNameList.size() != 2 || !evalPattern(getParamNameList.get(0), regexName)) {
681       globalContext.addMessage(fileName, ErrorLevel.WARNING, ErrorMessagesFormatBuilder
682           .getErrorWithParameters(
683               Messages.NOVA_SERVER_NAME_NOT_ALIGNED_WITH_GUIDE_LINES.getErrorMessage(),
684               resourceEntry.getKey()));
685     }
686   }
687
688   private boolean isNovaNameAsStringLegal(String fileName, String novaName, String[] regexName,
689                                           Map.Entry<String, Resource> resourceEntry,
690                                           GlobalValidationContext globalContext) {
691     if (!evalPattern(novaName, regexName)) {
692       globalContext.addMessage(fileName, ErrorLevel.WARNING, ErrorMessagesFormatBuilder
693           .getErrorWithParameters(
694               Messages.NOVA_SERVER_NAME_NOT_ALIGNED_WITH_GUIDE_LINES.getErrorMessage(),
695               resourceEntry.getKey()));
696       return false;
697     }
698     return true;
699   }
700
701   private String getWantedNameFromPropertyValueGetParam(Object value) {
702     Set<String> paramName = HeatStructureUtil
703         .getReferencedValuesByFunctionName(null, ResourceReferenceFunctions.GET_PARAM.getFunction(),
704             value, null);
705     if (paramName != null && CollectionUtils.isNotEmpty(paramName)) {
706       return (String) paramName.toArray()[0];
707     }
708     return null;
709   }
710
711   private String getResourceIdFromPropertyValue(String filename, Object value,
712                                                 GlobalValidationContext globalContext) {
713     Set<String> referenceValues = HeatStructureUtil.getReferencedValuesByFunctionName(filename,
714         ResourceReferenceFunctions.GET_RESOURCE.getFunction(), value, globalContext);
715     if (referenceValues != null && CollectionUtils.isNotEmpty(referenceValues)) {
716       return (String) referenceValues.toArray()[0];
717     }
718     return null;
719   }
720
721   private boolean isPropertyValueGetResource(String filename, Object value,
722                                              GlobalValidationContext globalContext) {
723     Set<String> referenceValues = HeatStructureUtil.getReferencedValuesByFunctionName(filename,
724         ResourceReferenceFunctions.GET_RESOURCE.getFunction(), value, globalContext);
725     return referenceValues != null && (referenceValues.size() > 0);
726   }
727
728   private boolean isExpectedToBeExposed(String type) {
729     return HeatResourcesTypes.isResourceExpectedToBeExposed(type);
730   }
731
732   private Set<String> validateManifest(ManifestContent manifestContent,
733                                        GlobalValidationContext globalContext) {
734     Set<String> baseFiles = ManifestUtil.getBaseFiles(manifestContent);
735     if (baseFiles == null || baseFiles.size() == 0) {
736       globalContext.addMessage(
737           AsdcCommon.MANIFEST_NAME,
738           ErrorLevel.WARNING,
739           ErrorMessagesFormatBuilder
740               .getErrorWithParameters(Messages.MISSIN_BASE_HEAT_FILE.getErrorMessage()));
741     } else if (baseFiles.size() > 1) {
742       String baseFileList = getElementListAsString(baseFiles);
743       globalContext.addMessage(
744           AsdcCommon.MANIFEST_NAME,
745           ErrorLevel.WARNING,
746           ErrorMessagesFormatBuilder
747               .getErrorWithParameters(Messages.MULTI_BASE_HEAT_FILE.getErrorMessage(),
748                   baseFileList));
749     }
750     return baseFiles;
751   }
752
753   private String getElementListAsString(Set<String> elementCollection) {
754
755     return "[" + CommonMethods.collectionToCommaSeparatedString(elementCollection)  + "]";
756   }
757
758
759   private Environment validateEnvContent(String envFileName,
760                                          GlobalValidationContext globalContext) {
761     Environment envContent;
762     try {
763       envContent =
764           new YamlUtil().yamlToObject(globalContext.getFileContent(envFileName), Environment.class);
765     } catch (Exception exception) {
766       return null;
767     }
768     return envContent;
769   }
770
771   private HeatOrchestrationTemplate checkHeatOrchestrationPreCondition(String fileName,
772                                                                        GlobalValidationContext
773                                                                            globalContext) {
774     HeatOrchestrationTemplate heatOrchestrationTemplate;
775     try {
776       heatOrchestrationTemplate = new YamlUtil()
777           .yamlToObject(globalContext.getFileContent(fileName), HeatOrchestrationTemplate.class);
778
779     } catch (Exception exception) {
780       return null;
781     }
782     return heatOrchestrationTemplate;
783   }
784 }