Removed log constants from translator core
[sdc.git] / openecomp-be / lib / openecomp-sdc-translator-lib / openecomp-sdc-translator-core / src / main / java / org / openecomp / sdc / translator / services / heattotosca / HeatToToscaUtil.java
1 /*
2  * Copyright © 2016-2017 European Support Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.openecomp.sdc.translator.services.heattotosca;
18
19 import org.apache.commons.collections4.CollectionUtils;
20 import org.apache.commons.collections4.MapUtils;
21 import org.apache.commons.io.FilenameUtils;
22 import org.openecomp.core.translator.api.HeatToToscaTranslator;
23 import org.openecomp.core.translator.datatypes.TranslatorOutput;
24 import org.openecomp.core.translator.factory.HeatToToscaTranslatorFactory;
25 import org.openecomp.core.utilities.file.FileContentHandler;
26 import org.openecomp.core.utilities.file.FileUtils;
27 import org.openecomp.core.utilities.orchestration.OnboardingTypesEnum;
28 import org.openecomp.core.validation.util.MessageContainerUtil;
29 import org.openecomp.sdc.common.errors.CoreException;
30 import org.openecomp.sdc.common.togglz.ToggleableFeature;
31 import org.openecomp.sdc.common.utils.SdcCommon;
32 import org.openecomp.sdc.datatypes.error.ErrorLevel;
33 import org.openecomp.sdc.datatypes.error.ErrorMessage;
34 import org.openecomp.sdc.heat.datatypes.HeatBoolean;
35 import org.openecomp.sdc.heat.datatypes.manifest.FileData;
36 import org.openecomp.sdc.heat.datatypes.model.HeatOrchestrationTemplate;
37 import org.openecomp.sdc.heat.datatypes.model.HeatResourcesTypes;
38 import org.openecomp.sdc.heat.datatypes.model.Resource;
39 import org.openecomp.sdc.heat.datatypes.structure.HeatStructureTree;
40 import org.openecomp.sdc.heat.datatypes.structure.ValidationStructureList;
41 import org.openecomp.sdc.heat.services.HeatConstants;
42 import org.openecomp.sdc.heat.services.tree.HeatTreeManager;
43 import org.openecomp.sdc.heat.services.tree.HeatTreeManagerUtil;
44 import org.openecomp.sdc.logging.api.Logger;
45 import org.openecomp.sdc.logging.api.LoggerFactory;
46 import org.openecomp.sdc.tosca.datatypes.ToscaCapabilityType;
47 import org.openecomp.sdc.tosca.datatypes.ToscaElementTypes;
48 import org.openecomp.sdc.tosca.datatypes.ToscaNodeType;
49 import org.openecomp.sdc.tosca.datatypes.ToscaRelationshipType;
50 import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel;
51 import org.openecomp.sdc.tosca.datatypes.model.AttributeDefinition;
52 import org.openecomp.sdc.tosca.datatypes.model.CapabilityDefinition;
53 import org.openecomp.sdc.tosca.datatypes.model.Import;
54 import org.openecomp.sdc.tosca.datatypes.model.NodeTemplate;
55 import org.openecomp.sdc.tosca.datatypes.model.NodeType;
56 import org.openecomp.sdc.tosca.datatypes.model.ParameterDefinition;
57 import org.openecomp.sdc.tosca.datatypes.model.PropertyDefinition;
58 import org.openecomp.sdc.tosca.datatypes.model.PropertyType;
59 import org.openecomp.sdc.tosca.datatypes.model.RequirementAssignment;
60 import org.openecomp.sdc.tosca.datatypes.model.RequirementDefinition;
61 import org.openecomp.sdc.tosca.datatypes.model.ServiceTemplate;
62 import org.openecomp.sdc.tosca.datatypes.model.Template;
63 import org.openecomp.sdc.tosca.datatypes.model.TopologyTemplate;
64 import org.openecomp.sdc.tosca.services.DataModelUtil;
65 import org.openecomp.sdc.tosca.services.ToscaAnalyzerService;
66 import org.openecomp.sdc.tosca.services.ToscaConstants;
67 import org.openecomp.sdc.tosca.services.ToscaExtensionYamlUtil;
68 import org.openecomp.sdc.tosca.services.ToscaUtil;
69 import org.openecomp.sdc.tosca.services.YamlUtil;
70 import org.openecomp.sdc.tosca.services.impl.ToscaAnalyzerServiceImpl;
71 import org.openecomp.sdc.translator.datatypes.heattotosca.AttachedPropertyVal;
72 import org.openecomp.sdc.translator.datatypes.heattotosca.AttachedResourceId;
73 import org.openecomp.sdc.translator.datatypes.heattotosca.ReferenceType;
74 import org.openecomp.sdc.translator.datatypes.heattotosca.TranslationContext;
75 import org.openecomp.sdc.translator.datatypes.heattotosca.to.FileDataCollection;
76 import org.openecomp.sdc.translator.datatypes.heattotosca.to.TranslateTo;
77 import org.openecomp.sdc.translator.services.heattotosca.errors.ResourceNotFoundInHeatFileErrorBuilder;
78 import org.openecomp.sdc.translator.services.heattotosca.globaltypes.GlobalTypesGenerator;
79 import org.openecomp.sdc.translator.services.heattotosca.helper.FunctionTranslationHelper;
80 import org.openecomp.sdc.translator.services.heattotosca.mapping.TranslatorHeatToToscaPropertyConverter;
81
82 import java.io.IOException;
83 import java.io.InputStream;
84 import java.util.ArrayList;
85 import java.util.Collection;
86 import java.util.HashMap;
87 import java.util.HashSet;
88 import java.util.List;
89 import java.util.Map;
90 import java.util.Objects;
91 import java.util.Optional;
92 import java.util.Set;
93 import java.util.stream.Collectors;
94
95 /**
96  * The type Heat to tosca util.
97  */
98 public class HeatToToscaUtil {
99
100   private static final Logger LOGGER = LoggerFactory.getLogger(HeatToToscaUtil.class);
101   public static final String FQ_NAME = "fq_name";
102   public static final String GET_PARAM = "get_param";
103   private static final String FORWARDER = "forwarder";
104   private static final String GET_ATTR = "get_attr";
105   private static final String GET_RESOURCE = "get_resource";
106
107   /**
108    * Load and translate template data translator output.
109    *
110    * @param fileNameContentMap the file name content map
111    * @return the translator output
112    */
113   public static TranslatorOutput loadAndTranslateTemplateData(
114       FileContentHandler fileNameContentMap) {
115     HeatToToscaTranslator heatToToscaTranslator =
116         HeatToToscaTranslatorFactory.getInstance().createInterface();
117
118     try (InputStream fileContent = fileNameContentMap.getFileContent(SdcCommon.MANIFEST_NAME)) {
119       heatToToscaTranslator.addManifest(SdcCommon.MANIFEST_NAME, FileUtils.toByteArray(fileContent));
120     } catch (IOException e) {
121       throw new RuntimeException("Failed to read manifest", e);
122     }
123
124     fileNameContentMap.getFileList().stream()
125         .filter(fileName -> !(fileName.equals(SdcCommon.MANIFEST_NAME))).forEach(
126             fileName -> heatToToscaTranslator
127                 .addFile(fileName, FileUtils.toByteArray
128                     (fileNameContentMap.getFileContent(fileName))));
129
130     Map<String, List<ErrorMessage>> errors = heatToToscaTranslator.validate();
131     if (MapUtils.isNotEmpty(MessageContainerUtil.getMessageByLevel(ErrorLevel.ERROR, errors))) {
132       TranslatorOutput translatorOutput = new TranslatorOutput();
133       translatorOutput.setErrorMessages(errors);
134       return translatorOutput;
135     }
136
137     try (InputStream structureFile = getHeatStructureTreeFile(fileNameContentMap)) {
138       heatToToscaTranslator.addExternalArtifacts(SdcCommon.HEAT_META, structureFile);
139       return heatToToscaTranslator.translate();
140     } catch (IOException e) {
141       // rethrow as a RuntimeException to keep the signature backward compatible
142       throw new RuntimeException("Failed to read Heat template tree", e);
143     }
144   }
145
146
147   private static InputStream getHeatStructureTreeFile(FileContentHandler fileNameContentMap) {
148     HeatTreeManager heatTreeManager = HeatTreeManagerUtil.initHeatTreeManager(fileNameContentMap);
149     heatTreeManager.createTree();
150     HeatStructureTree tree = heatTreeManager.getTree();
151     ValidationStructureList validationStructureList = new ValidationStructureList(tree);
152     return FileUtils.convertToInputStream(validationStructureList, FileUtils.FileExtension.JSON);
153   }
154
155   /**
156    * Build list of files to search optional.
157    *
158    * @param heatFileName  the heat file name
159    * @param filesDataList the files data list
160    * @param types         the types
161    * @return the optional
162    */
163   public static Optional<List<FileData>> buildListOfFilesToSearch(String heatFileName,
164                                                                   List<FileData> filesDataList,
165                                                                   FileData.Type... types) {
166     List<FileData> list = new ArrayList<>(filesDataList);
167     Optional<FileData> resourceFileData = HeatToToscaUtil.getFileData(heatFileName, filesDataList);
168     if (resourceFileData.isPresent() && Objects.nonNull(resourceFileData.get().getData())) {
169       list.addAll(resourceFileData.get().getData());
170     }
171     return Optional.ofNullable(HeatToToscaUtil.getFilteredListOfFileDataByTypes(list, types));
172   }
173
174   /**
175    * Gets filtered list of file data by types.
176    *
177    * @param filesToSearch the files to search
178    * @param types         the types
179    * @return the filtered list of file data by types
180    */
181   public static List<FileData> getFilteredListOfFileDataByTypes(List<FileData> filesToSearch,
182                                                                 FileData.Type... types) {
183     return filesToSearch.stream().filter(FileData.buildFileDataPredicateByType(types))
184         .collect(Collectors.toList());
185   }
186
187   /**
188    * Gets file data from the list according to the input heat file name.
189    *
190    * @param heatFileName the heat file name
191    * @param fileDataList the file data list
192    * @return the file data
193    */
194   public static Optional<FileData> getFileData(String heatFileName,
195                                                Collection<FileData> fileDataList) {
196     for (FileData file : fileDataList) {
197       if (file.getFile().equals(heatFileName)) {
198         return Optional.of(file);
199       }
200     }
201     return Optional.empty();
202   }
203
204   /**
205    * Gets file data which is supported by the translator, from the context according the input heat
206    * file name.
207    *
208    * @param heatFileName the heat file name
209    * @param context      the translation context
210    * @return the file data
211    */
212   public static FileData getFileData(String heatFileName, TranslationContext context) {
213
214     List<FileData> fileDataList = context.getManifest().getContent().getData();
215     for (FileData fileData : fileDataList) {
216       if (TranslationService.getTypesToProcessByTranslator().contains(fileData.getType())
217           && fileData.getFile().equals(heatFileName)) {
218         return fileData;
219       }
220     }
221     return null;
222   }
223
224   static FileDataCollection getFileCollectionsByFilter(List<FileData> fileDataList,
225                                                        Set<FileData.Type> typeFilter,
226                                                        TranslationContext translationContext) {
227     FileDataCollection fileDataCollection = new FileDataCollection();
228     Map<String, FileData> filteredFiles = filterFileDataListByType(fileDataList, typeFilter);
229     Set<String> referenced = new HashSet<>();
230
231     for (FileData fileData : filteredFiles.values()) {
232       String fileName = fileData.getFile();
233
234       if (FileData.isHeatFile(fileData.getType())) {
235         if (fileData.getBase() != null && fileData.getBase()) {
236           fileDataCollection.addBaseFiles(fileData);
237         }
238         HeatOrchestrationTemplate heatOrchestrationTemplate = new YamlUtil()
239             .yamlToObject(translationContext.getFileContent(fileName),
240                 HeatOrchestrationTemplate.class);
241         if (MapUtils.isNotEmpty(heatOrchestrationTemplate.getResources())) {
242           applyFilterOnFileCollection(heatOrchestrationTemplate, translationContext,
243               fileDataCollection, filteredFiles, referenced);
244         }
245
246       } else {
247         fileDataCollection.addArtifactFiles(fileData);
248         filteredFiles.remove(fileData.getFile());
249       }
250     }
251
252     referenced.forEach(filteredFiles::remove);
253     if (!CollectionUtils.isEmpty(fileDataCollection.getBaseFile())) {
254       for (FileData fileData : fileDataCollection.getBaseFile()) {
255         filteredFiles.remove(fileData.getFile());
256       }
257     }
258     fileDataCollection.setAddOnFiles(filteredFiles.values());
259     return fileDataCollection;
260   }
261
262   private static void applyFilterOnFileCollection(
263       HeatOrchestrationTemplate heatOrchestrationTemplate,
264       TranslationContext translationContext,
265       FileDataCollection fileDataCollection, Map<String, FileData> filteredFiles,
266       Set<String> referenced) {
267     List<String> filenames = extractFilenamesFromFileDataList(filteredFiles.values());
268
269     for (Resource resource : heatOrchestrationTemplate.getResources().values()) {
270       if (filenames.contains(resource.getType())) {
271         handleNestedFile(translationContext, fileDataCollection, filteredFiles, referenced,
272             resource.getType());
273       } else if (resource.getType()
274           .equals(HeatResourcesTypes.RESOURCE_GROUP_RESOURCE_TYPE.getHeatResource())) {
275         handleResourceGrpNestedFile(resource, translationContext, fileDataCollection,
276             filteredFiles, filenames, referenced);
277       }
278     }
279   }
280
281   private static void handleResourceGrpNestedFile(Resource resource,
282                                                   TranslationContext translationContext,
283                                                   FileDataCollection fileDataCollection,
284                                                   Map<String, FileData> filteredFiles,
285                                                   List<String> filenames,
286                                                   Set<String> referenced) {
287     Object resourceDef = resource.getProperties().get(HeatConstants.RESOURCE_DEF_PROPERTY_NAME);
288     Object innerTypeDef = ((Map) resourceDef).get("type");
289     if (innerTypeDef instanceof String) {
290       String internalResourceType = (String) innerTypeDef;
291       if (filenames.contains(internalResourceType)) {
292         handleNestedFile(translationContext, fileDataCollection, filteredFiles,
293             referenced, internalResourceType);
294       }
295     }
296   }
297
298   private static void handleNestedFile(TranslationContext translationContext,
299                                        FileDataCollection fileDataCollection,
300                                        Map<String, FileData> filteredFiles, Set<String> referenced,
301                                        String nestedFileName) {
302     referenced.add(nestedFileName);
303     fileDataCollection.addNestedFiles(filteredFiles.get(nestedFileName));
304     translationContext.getNestedHeatsFiles().add(nestedFileName);
305   }
306
307   private static Map<String, FileData> filterFileDataListByType(List<FileData> fileDataList,
308                                                                 Set<FileData.Type> typesToGet) {
309     Map<String, FileData> filtered = new HashMap<>();
310     fileDataList.stream().filter(file -> typesToGet.contains(file.getType()))
311         .forEach(file -> filtered.put(file.getFile(), file));
312     return filtered;
313   }
314
315   private static List<String> extractFilenamesFromFileDataList(Collection<FileData> fileDataList) {
316     return fileDataList.stream().map(FileData::getFile).collect(Collectors.toList());
317   }
318
319   /**
320    * Extract attached resource id optional.
321    *
322    * @param translateTo  the translate to
323    * @param propertyName the property name
324    * @return the optional
325    */
326   public static Optional<AttachedResourceId> extractAttachedResourceId(TranslateTo translateTo,
327                                                                        String propertyName) {
328     Object propertyValue = translateTo.getResource().getProperties().get(propertyName);
329     if (propertyValue == null) {
330       return Optional.empty();
331     }
332     return extractAttachedResourceId(translateTo.getHeatFileName(),
333         translateTo.getHeatOrchestrationTemplate(), translateTo.getContext(), propertyValue);
334   }
335
336   /**
337    * Extract attached resource id optional.
338    *
339    * @param heatFileName              the heat file name
340    * @param heatOrchestrationTemplate the heat orchestration template
341    * @param context                   the context
342    * @param propertyValue             the property value
343    * @return the optional
344    */
345   public static Optional<AttachedResourceId> extractAttachedResourceId(
346       String heatFileName,
347       HeatOrchestrationTemplate heatOrchestrationTemplate,
348       TranslationContext context,
349       Object propertyValue) {
350
351     Object entity;
352     Object translatedId;
353
354     if (Objects.isNull(propertyValue)) {
355       return Optional.empty();
356     }
357
358     ReferenceType referenceType = ReferenceType.OTHER;
359     if (propertyValue instanceof Map && !((Map) propertyValue).isEmpty()) {
360       Map<String, Object> propMap = (Map) propertyValue;
361       Map.Entry<String, Object> entry = propMap.entrySet().iterator().next();
362       entity = entry.getValue();
363       String key = entry.getKey();
364       referenceType = getReferenceTypeFromAttachedResouce(key);
365
366       if (!FunctionTranslationFactory.getInstance(entry.getKey()).isPresent()) {
367         translatedId = null;
368       } else {
369         translatedId = FunctionTranslationFactory.getInstance(entry.getKey()).get()
370             .translateFunction(null, null, null, entry.getKey(), entry.getValue(), heatFileName,
371                 heatOrchestrationTemplate, null, context);
372       }
373       if (translatedId instanceof String
374           && !FunctionTranslationHelper.isResourceSupported((String) translatedId)) {
375         translatedId = null;
376       }
377
378     } else {
379       translatedId = propertyValue;
380       entity = propertyValue;
381     }
382
383     return Optional.of(new AttachedResourceId(translatedId, entity, referenceType));
384   }
385
386   private static ReferenceType getReferenceTypeFromAttachedResouce(String key) {
387     ReferenceType referenceType;
388     switch (key) {
389       case GET_RESOURCE:
390         referenceType = ReferenceType.GET_RESOURCE;
391         break;
392       case GET_PARAM:
393         referenceType = ReferenceType.GET_PARAM;
394         break;
395       case GET_ATTR:
396         referenceType = ReferenceType.GET_ATTR;
397         break;
398       default:
399         referenceType = ReferenceType.OTHER;
400         break;
401     }
402
403     return referenceType;
404   }
405
406   /**
407    * Gets contrail attached heat resource id.
408    *
409    * @param attachedResource the attached resource
410    * @return the contrail attached heat resource id
411    */
412   public static Optional<String> getContrailAttachedHeatResourceId(
413       AttachedResourceId attachedResource) {
414     if (attachedResource == null) {
415       return Optional.empty();
416     }
417
418     if (attachedResource.isGetResource()) {
419       return Optional.of((String) attachedResource.getEntityId());
420     }
421
422     if (attachedResource.isGetAttr()) {
423       return getResourceId(attachedResource.getEntityId());
424     }
425     return Optional.empty();
426   }
427
428   /**
429    * Extract property optional.
430    *
431    * @param propertyValue the property value
432    * @return the optional
433    */
434   public static Optional<AttachedPropertyVal> extractProperty(Object propertyValue) {
435     Object attachedPropertyVal;
436     if (Objects.isNull(propertyValue)) {
437       return Optional.empty();
438     }
439
440     ReferenceType referenceType = ReferenceType.OTHER;
441     if (propertyValue instanceof Map && !((Map) propertyValue).isEmpty()) {
442       Map<String, Object> propMap = (Map) propertyValue;
443       Map.Entry<String, Object> entry = propMap.entrySet().iterator().next();
444       attachedPropertyVal = entry.getValue();
445       String key = entry.getKey();
446       switch (key) {
447         case GET_RESOURCE:
448           referenceType = ReferenceType.GET_RESOURCE;
449           break;
450         case GET_PARAM:
451           referenceType = ReferenceType.GET_PARAM;
452           break;
453         case GET_ATTR:
454           referenceType = ReferenceType.GET_ATTR;
455           break;
456         default:
457           break;
458       }
459
460     } else {
461       attachedPropertyVal = propertyValue;
462     }
463     return Optional.of(new AttachedPropertyVal(attachedPropertyVal, referenceType));
464   }
465
466   /**
467    * Map boolean.
468    *
469    * @param nodeTemplate the node template
470    * @param propertyKey  the property key
471    */
472   public static void mapBoolean(NodeTemplate nodeTemplate, String propertyKey) {
473     Object value = nodeTemplate.getProperties().get(propertyKey);
474     if (value != null && !(value instanceof Map)) {
475       nodeTemplate.getProperties().put(propertyKey, HeatBoolean.eval(value));
476     }
477   }
478
479   /**
480    * Map boolean list.
481    *
482    * @param nodeTemplate    the node template
483    * @param propertyListKey the property list key
484    */
485   public static void mapBooleanList(NodeTemplate nodeTemplate, String propertyListKey) {
486     Object listValue = nodeTemplate.getProperties().get(propertyListKey);
487     if (listValue instanceof List) {
488       List booleanList = (List) listValue;
489       for (int i = 0; i < booleanList.size(); i++) {
490         Object value = booleanList.get(i);
491         if (value != null && !(value instanceof Map)) {
492           booleanList.set(i, HeatBoolean.eval(value));
493         }
494       }
495     }
496   }
497
498
499   /**
500    * Is yml file type boolean.
501    *
502    * @param filename the filename
503    * @return the boolean
504    */
505   public static boolean isYmlFileType(String filename) {
506     String extension = FilenameUtils.getExtension(filename);
507     return "yaml".equalsIgnoreCase(extension)
508         || "yml".equalsIgnoreCase(extension);
509   }
510
511   /**
512    * Is nested resource boolean.
513    *
514    * @param resource the resource
515    * @return the boolean
516    */
517   public static boolean isNestedResource(Resource resource) {
518     String resourceType = resource.getType();
519
520     if (resourceType.equals(HeatResourcesTypes.RESOURCE_GROUP_RESOURCE_TYPE.getHeatResource())) {
521       Object resourceDef = resource.getProperties().get(HeatConstants.RESOURCE_DEF_PROPERTY_NAME);
522       if (!(((Map) resourceDef).get("type") instanceof String)) {
523         //currently only resource group which is poinitng to nested heat file is supported
524         //dynamic type is currently not supported
525         return false;
526       }
527       String internalResourceType = (String) ((Map) resourceDef).get("type");
528       if (isYamlFile(internalResourceType)) {
529         return true;
530       }
531     } else if (isYamlFile(resourceType)) {
532       return true;
533     }
534     return false;
535   }
536
537   /**
538    * Checks if the nested resource represents a VFC or a complex VFC (Heat file should contain at
539    * least one or more compute nodes).
540    *
541    * @param resource the resource
542    * @param context the context
543    * @return true if the resource represents a VFC and false otherwise.
544    */
545   public static boolean isNestedVfcResource(Resource resource, TranslationContext context) {
546     Optional<String> nestedHeatFileName = HeatToToscaUtil.getNestedHeatFileName(resource);
547     HeatOrchestrationTemplate nestedHeatOrchestrationTemplate = new YamlUtil()
548         .yamlToObject(context.getFileContent(nestedHeatFileName.get()),
549             HeatOrchestrationTemplate.class);
550     if (Objects.nonNull(nestedHeatOrchestrationTemplate.getResources())) {
551       for (String innerResourceId : nestedHeatOrchestrationTemplate.getResources().keySet()) {
552         if (ConsolidationDataUtil
553             .isComputeResource(nestedHeatOrchestrationTemplate, innerResourceId)) {
554           return true;
555         }
556       }
557     }
558     return false;
559   }
560
561   /**
562    * Get nested heat file name in case of nested resource.
563    *
564    * @param resource the resource
565    * @return the nested heat file name
566    */
567   public static Optional<String> getNestedHeatFileName(Resource resource) {
568     if (!isNestedResource(resource)) {
569       return Optional.empty();
570     }
571
572     String resourceType = resource.getType();
573
574     if (resourceType.equals(HeatResourcesTypes.RESOURCE_GROUP_RESOURCE_TYPE.getHeatResource())) {
575       Object resourceDef = resource.getProperties().get(HeatConstants.RESOURCE_DEF_PROPERTY_NAME);
576       String internalResourceType = (String) ((Map) resourceDef).get("type");
577       return Optional.of(internalResourceType);
578     }
579     return Optional.of(resourceType);
580   }
581
582   /**
583    * Gets nested file.
584    *
585    * @param resource the resource
586    * @return the nested file
587    */
588   public static Optional<String> getNestedFile(Resource resource) {
589     if (!isNestedResource(resource)) {
590       return Optional.empty();
591     }
592     String resourceType = resource.getType();
593     if (resourceType.equals(HeatResourcesTypes.RESOURCE_GROUP_RESOURCE_TYPE.getHeatResource())) {
594       Object resourceDef = resource.getProperties().get(HeatConstants.RESOURCE_DEF_PROPERTY_NAME);
595       String internalResourceType = (String) ((Map) resourceDef).get("type");
596       return Optional.of(internalResourceType);
597     } else {
598       return Optional.of(resourceType);
599     }
600   }
601
602   private static boolean isYamlFile(String fileName) {
603     return fileName.endsWith(".yaml") || fileName.endsWith(".yml");
604   }
605
606   /**
607    * Gets resource.
608    *
609    * @param heatOrchestrationTemplate the heat orchestration template
610    * @param resourceId                the resource id
611    * @param heatFileName              the heat file name
612    * @return the resource
613    */
614   public static Resource getResource(HeatOrchestrationTemplate heatOrchestrationTemplate,
615                                      String resourceId, String heatFileName) {
616     Resource resource = heatOrchestrationTemplate.getResources().get(resourceId);
617     if (resource == null) {
618       throw new CoreException(
619           new ResourceNotFoundInHeatFileErrorBuilder(resourceId, heatFileName).build());
620     }
621     return resource;
622   }
623
624
625   /**
626    * Get resource type.
627    *
628    * @param resourceId                the resource id
629    * @param heatOrchestrationTemplate heat orchestration template
630    * @param heatFileName              heat file name
631    * @return resource type
632    */
633   public static String getResourceType(String resourceId,
634                                        HeatOrchestrationTemplate heatOrchestrationTemplate,
635                                        String heatFileName) {
636     return HeatToToscaUtil.getResource(heatOrchestrationTemplate, resourceId, heatFileName)
637         .getType();
638   }
639
640   /**
641    * Is heat file nested boolean.
642    *
643    * @param translateTo  the translate to
644    * @param heatFileName the heat file name
645    * @return the boolean
646    */
647   public static boolean isHeatFileNested(TranslateTo translateTo, String heatFileName) {
648     return translateTo.getContext().getNestedHeatsFiles().contains(heatFileName);
649   }
650
651   /**
652    * Extract contrail get resource attached heat resource id optional.
653    *
654    * @param propertyValue the property value
655    * @return the optional
656    */
657   public static Optional<String> extractContrailGetResourceAttachedHeatResourceId(
658       Object propertyValue) {
659     if (propertyValue instanceof Map) {
660       if (((Map) propertyValue).containsKey(GET_ATTR)) {
661         return getResourceId(((Map) propertyValue).get(GET_ATTR));
662       } else if (((Map) propertyValue).containsKey(GET_RESOURCE)) {
663         return getHeatResourceIdFromResource((Map) propertyValue);
664       } else {
665         Collection valCollection = ((Map) propertyValue).values();
666         return evaluateHeatResourceId(valCollection);
667       }
668     } else if (propertyValue instanceof List) {
669       return evaluateHeatResourceId((List) propertyValue);
670     }
671     return Optional.empty();
672   }
673
674   private static Optional<String> getResourceId(Object data) {
675     if (data instanceof List && CollectionUtils.size(data) > 1
676         && FQ_NAME.equals(((List) data).get(1))
677         && ((List) data).get(0) instanceof String) {
678       return Optional.of((String) ((List) data).get(0));
679     } else {
680       LOGGER.warn("invalid format of 'get_attr' function - " + data.toString());
681       return Optional.empty();
682     }
683   }
684
685   private static Optional<String> getHeatResourceIdFromResource(Map propertyValue) {
686     Object value = propertyValue.get(GET_RESOURCE);
687     if (value instanceof String) {
688       return Optional.of((String) value);
689     } else {
690       LOGGER.warn("invalid format of 'get_resource' function - " + propertyValue.toString());
691       return Optional.empty();
692     }
693   }
694
695   private static Optional<String> evaluateHeatResourceId(Collection propertyValue) {
696     for (Object prop : propertyValue) {
697       Optional<String> ret = extractContrailGetResourceAttachedHeatResourceId(prop);
698       if (ret.isPresent()) {
699         return ret;
700       }
701     }
702     return Optional.empty();
703   }
704   /**
705    * Gets tosca service model.
706    *
707    * @param context translation context
708    * @return the tosca service model
709    */
710   public static ToscaServiceModel getToscaServiceModel(TranslationContext context) {
711     Map<String, String> metadata = new HashMap<>();
712     metadata.put(ToscaConstants.ST_METADATA_TEMPLATE_NAME, Constants.MAIN_TEMPLATE_NAME);
713     return getToscaServiceModel(context, metadata);
714   }
715
716   /**
717    * Gets tosca service model.
718    *
719    * @param context                 translation context
720    * @param entryDefinitionMetadata template name of the entry definition servie template
721    * @return the tosca service model
722    */
723   public static ToscaServiceModel getToscaServiceModel(
724       TranslationContext context,
725       Map<String, String> entryDefinitionMetadata) {
726     Map<String, ServiceTemplate> serviceTemplates =
727         new HashMap<>(context.getGlobalServiceTemplates());
728     Collection<ServiceTemplate> tmpServiceTemplates =
729         context.getTranslatedServiceTemplates().values();
730     for (ServiceTemplate serviceTemplate : tmpServiceTemplates) {
731       ToscaUtil.addServiceTemplateToMapWithKeyFileName(serviceTemplates, serviceTemplate);
732     }
733     return new ToscaServiceModel(null, serviceTemplates,
734         ToscaUtil.getServiceTemplateFileName(entryDefinitionMetadata));
735   }
736
737   /**
738    * Gets service template from context.
739    *
740    * @param serviceTemplateFileName the service template file name
741    * @param context                 the context
742    * @return the service template from context
743    */
744   public static Optional<ServiceTemplate> getServiceTemplateFromContext(
745       String serviceTemplateFileName, TranslationContext context) {
746     for (ServiceTemplate serviceTemplate : context.getTranslatedServiceTemplates().values()) {
747       if (ToscaUtil.getServiceTemplateFileName(serviceTemplate).equals(serviceTemplateFileName)) {
748         return Optional.of(serviceTemplate);
749       }
750     }
751     return Optional.empty();
752   }
753
754   /**
755    * Adding link requerment from port node template to network node template.
756    *
757    * @param portNodeTemplate    port node template
758    * @param networkTranslatedId network node template id
759    */
760   public static RequirementAssignment addLinkReqFromPortToNetwork(NodeTemplate portNodeTemplate,
761                                                                   String networkTranslatedId) {
762     RequirementAssignment requirement = new RequirementAssignment();
763     requirement.setCapability(ToscaCapabilityType.NATIVE_NETWORK_LINKABLE);
764     requirement.setRelationship(ToscaRelationshipType.NATIVE_NETWORK_LINK_TO);
765     requirement.setNode(networkTranslatedId);
766     DataModelUtil.addRequirementAssignment(portNodeTemplate,
767         ToscaConstants.LINK_REQUIREMENT_ID, requirement);
768     return requirement;
769   }
770
771   /**
772    * Adding binding requerment from sub interface node template to interface (port) node template.
773    *
774    * @param subInterfaceNodeTemplate sub interface template
775    * @param interfaceTranslatedId    interface node template id
776    */
777   public static void addBindingReqFromSubInterfaceToInterface(
778       NodeTemplate subInterfaceNodeTemplate, String interfaceTranslatedId) {
779     RequirementAssignment requirement = new RequirementAssignment();
780     requirement.setCapability(ToscaCapabilityType.NATIVE_NETWORK_BINDABLE);
781     requirement.setRelationship(ToscaRelationshipType.NATIVE_NETWORK_BINDS_TO);
782     requirement.setNode(interfaceTranslatedId);
783     DataModelUtil
784         .addRequirementAssignment(subInterfaceNodeTemplate,
785             ToscaConstants.BINDING_REQUIREMENT_ID, requirement);
786   }
787
788   /**
789    * Get property Parameter Name Value.
790    *
791    * @param property property
792    * @return Parameter name in case the property include "get_param" function
793    */
794   public static Optional<String> getPropertyParameterNameValue(Object property) {
795     if (Objects.isNull(property)) {
796       return Optional.empty();
797     }
798     Optional<AttachedPropertyVal> extractedProperty = extractProperty(property);
799     return extractedProperty.flatMap(HeatToToscaUtil::getParameterName);
800   }
801
802   private static Optional<String> getParameterName(AttachedPropertyVal extractedProperty) {
803     if (!extractedProperty.isGetParam()) {
804       return Optional.empty();
805     }
806     Object getParamFuncValue = extractedProperty.getPropertyValue();
807     if (getParamFuncValue instanceof String) {
808       return Optional.of((String) getParamFuncValue);
809     } else {
810       return Optional.of((String) ((List) getParamFuncValue).get(0));
811     }
812   }
813
814   public static String getToscaPropertyName(TranslationContext context, String heatResourceType,
815                                             String heatPropertyName) {
816     return context.getElementMapping(heatResourceType, Constants.PROP, heatPropertyName);
817   }
818
819   /**
820    * Gets tosca property name.
821    *
822    * @param translateTo      the translate to
823    * @param heatPropertyName the heat property name
824    * @return the tosca property name
825    */
826   public static String getToscaPropertyName(TranslateTo translateTo, String heatPropertyName) {
827     return translateTo.getContext()
828         .getElementMapping(translateTo.getResource().getType(), Constants.PROP, heatPropertyName);
829   }
830
831   /**
832    * Gets tosca attribute name.
833    *
834    * @param context          the context
835    * @param heatResourceType the heat resource type
836    * @param heatAttrName     the heat attr name
837    * @return the tosca attribute name
838    */
839   public static String getToscaAttributeName(TranslationContext context, String heatResourceType,
840                                              String heatAttrName) {
841     return context.getElementMapping(heatResourceType, Constants.ATTR, heatAttrName);
842   }
843
844   /**
845    * Gets tosca attribute name.
846    *
847    * @param translateTo  the translate to
848    * @param heatAttrName the heat attr name
849    * @return the tosca attribute name
850    */
851   public static String getToscaAttributeName(TranslateTo translateTo, String heatAttrName) {
852     return translateTo.getContext()
853         .getElementMapping(translateTo.getResource().getType(), Constants.ATTR, heatAttrName);
854   }
855
856   /**
857    * Create init substitution service template service template.
858    *
859    * @param templateName the template name
860    * @return the service template
861    */
862   public static ServiceTemplate createInitSubstitutionServiceTemplate(String templateName) {
863     ServiceTemplate nestedSubstitutionServiceTemplate = new ServiceTemplate();
864     Map<String, String> templateMetadata = new HashMap<>();
865     templateMetadata.put(ToscaConstants.ST_METADATA_TEMPLATE_NAME, templateName);
866     nestedSubstitutionServiceTemplate.setMetadata(templateMetadata);
867     nestedSubstitutionServiceTemplate
868         .setTosca_definitions_version(ToscaConstants.TOSCA_DEFINITIONS_VERSION);
869     nestedSubstitutionServiceTemplate.setTopology_template(new TopologyTemplate());
870     List<Map<String, Import>> globalTypesImportList =
871         GlobalTypesGenerator.getGlobalTypesImportList();
872     globalTypesImportList.addAll(
873         HeatToToscaUtil.createImportList(Constants.GLOBAL_SUBSTITUTION_TYPES_TEMPLATE_NAME));
874     nestedSubstitutionServiceTemplate.setImports(globalTypesImportList);
875     return nestedSubstitutionServiceTemplate;
876   }
877
878   /**
879    * Create init global substitution service template service template.
880    *
881    * @return the service template
882    */
883   public static ServiceTemplate createInitGlobalSubstitutionServiceTemplate() {
884     ServiceTemplate globalSubstitutionServiceTemplate = new ServiceTemplate();
885     Map<String, String> templateMetadata = new HashMap<>();
886     templateMetadata.put(ToscaConstants.ST_METADATA_TEMPLATE_NAME,
887         Constants.GLOBAL_SUBSTITUTION_TYPES_TEMPLATE_NAME);
888     globalSubstitutionServiceTemplate.setMetadata(templateMetadata);
889     globalSubstitutionServiceTemplate
890         .setImports(GlobalTypesGenerator.getGlobalTypesImportList());
891     globalSubstitutionServiceTemplate
892         .setTosca_definitions_version(ToscaConstants.TOSCA_DEFINITIONS_VERSION);
893     return globalSubstitutionServiceTemplate;
894   }
895
896   /**
897    * Create substitution node type node type.
898    *
899    * @param substitutionServiceTemplate the substitution service template
900    * @return the node type
901    */
902   public NodeType createSubstitutionNodeType(ServiceTemplate substitutionServiceTemplate) {
903     NodeType substitutionNodeType = new NodeType();
904     substitutionNodeType.setDerived_from(ToscaNodeType.ABSTRACT_SUBSTITUTE);
905     substitutionNodeType.setDescription(substitutionServiceTemplate.getDescription());
906     substitutionNodeType
907         .setProperties(manageSubstitutionNodeTypeProperties(substitutionServiceTemplate));
908     substitutionNodeType
909         .setAttributes(manageSubstitutionNodeTypeAttributes(substitutionServiceTemplate));
910     return substitutionNodeType;
911   }
912
913   private Map<String, PropertyDefinition> manageSubstitutionNodeTypeProperties(
914       ServiceTemplate substitutionServiceTemplate) {
915     Map<String, PropertyDefinition> substitutionNodeTypeProperties = new HashMap<>();
916     Map<String, ParameterDefinition> properties =
917         substitutionServiceTemplate.getTopology_template().getInputs();
918     if (properties == null) {
919       return null;
920     }
921
922     PropertyDefinition propertyDefinition;
923     String toscaPropertyName;
924     for (Map.Entry<String, ParameterDefinition> entry : properties.entrySet()) {
925       toscaPropertyName = entry.getKey();
926       propertyDefinition = new PropertyDefinition();
927       ParameterDefinition parameterDefinition =
928           substitutionServiceTemplate.getTopology_template().getInputs().get(toscaPropertyName);
929       propertyDefinition.setType(parameterDefinition.getType());
930       propertyDefinition.setDescription(parameterDefinition.getDescription());
931       propertyDefinition.setRequired(parameterDefinition.getRequired());
932       propertyDefinition.set_default(parameterDefinition.get_default());
933       propertyDefinition.setConstraints(parameterDefinition.getConstraints());
934       propertyDefinition.setEntry_schema(parameterDefinition.getEntry_schema());
935       propertyDefinition.setStatus(parameterDefinition.getStatus());
936       substitutionNodeTypeProperties.put(toscaPropertyName, propertyDefinition);
937     }
938     return substitutionNodeTypeProperties;
939   }
940
941   private Map<String, AttributeDefinition> manageSubstitutionNodeTypeAttributes(
942       ServiceTemplate substitutionServiceTemplate) {
943     Map<String, AttributeDefinition> substitutionNodeTypeAttributes = new HashMap<>();
944     Map<String, ParameterDefinition> attributes =
945         substitutionServiceTemplate.getTopology_template().getOutputs();
946     if (attributes == null) {
947       return null;
948     }
949     AttributeDefinition attributeDefinition;
950     String toscaAttributeName;
951
952     for (Map.Entry<String, ParameterDefinition> entry : attributes.entrySet()) {
953       attributeDefinition = new AttributeDefinition();
954       toscaAttributeName = entry.getKey();
955       ParameterDefinition parameterDefinition =
956           substitutionServiceTemplate.getTopology_template().getOutputs().get(toscaAttributeName);
957       if (parameterDefinition.getType() != null && !parameterDefinition.getType().isEmpty()) {
958         attributeDefinition.setType(parameterDefinition.getType());
959       } else {
960         attributeDefinition.setType(PropertyType.STRING.getDisplayName());
961       }
962       attributeDefinition.setDescription(parameterDefinition.getDescription());
963       attributeDefinition.set_default(parameterDefinition.get_default());
964       attributeDefinition.setEntry_schema(parameterDefinition.getEntry_schema());
965       attributeDefinition.setStatus(parameterDefinition.getStatus());
966       substitutionNodeTypeAttributes.put(toscaAttributeName, attributeDefinition);
967     }
968     return substitutionNodeTypeAttributes;
969   }
970
971   /**
972    * .
973    * Create and add substitution mapping to the nested substitution service template, and update
974    * the subtitution node type accordingly with the exposed requerments and capabilities
975    *
976    * @param context                           the translation context
977    * @param substitutionNodeTypeKey           the substitution node type key
978    * @param nestedSubstitutionServiceTemplate the nested substitution service template
979    * @param substitutionNodeType              the substitution node type
980    */
981   public static void handleSubstitutionMapping(
982       TranslationContext context,
983       String substitutionNodeTypeKey,
984       ServiceTemplate nestedSubstitutionServiceTemplate,
985       NodeType substitutionNodeType) {
986     Map<String, Map<String, List<String>>> substitutionMapping =
987         getSubstitutionNodeTypeExposedConnectionPoints(substitutionNodeType,
988             nestedSubstitutionServiceTemplate, context);
989     //add substitution mapping after capability and requirement expose calculation
990     nestedSubstitutionServiceTemplate.getTopology_template().setSubstitution_mappings(
991         DataModelUtil.createSubstitutionTemplateSubMapping(substitutionNodeTypeKey,
992             substitutionNodeType, substitutionMapping));
993   }
994
995   /**
996    * Gets node type with flat hierarchy.
997    *
998    * @param nodeTypeId      the node type id
999    * @param serviceTemplate the service template
1000    * @param context         the context
1001    * @return the node type with flat hierarchy
1002    */
1003   public static NodeType getNodeTypeWithFlatHierarchy(String nodeTypeId,
1004                                                       ServiceTemplate serviceTemplate,
1005                                                       TranslationContext context) {
1006     ToscaAnalyzerService toscaAnalyzerService = new ToscaAnalyzerServiceImpl();
1007     ToscaServiceModel toscaServiceModel = HeatToToscaUtil
1008         .getToscaServiceModel(context, serviceTemplate.getMetadata());
1009     return (NodeType) toscaAnalyzerService
1010         .getFlatEntity(ToscaElementTypes.NODE_TYPE, nodeTypeId, serviceTemplate, toscaServiceModel);
1011   }
1012
1013   /**
1014    * Create substitution node template node template.
1015    *
1016    * @param translateTo             the translate to
1017    * @param templateName            the template name
1018    * @param substitutionNodeTypeKey the substitution node type key
1019    * @return the node template
1020    */
1021   public NodeTemplate createSubstitutionNodeTemplate(TranslateTo translateTo, String templateName,
1022                                                      String substitutionNodeTypeKey) {
1023     NodeTemplate substitutionNodeTemplate = new NodeTemplate();
1024     List<String> directiveList = new ArrayList<>();
1025     directiveList.add(ToscaConstants.NODE_TEMPLATE_DIRECTIVE_SUBSTITUTABLE);
1026     substitutionNodeTemplate.setDirectives(directiveList);
1027     substitutionNodeTemplate.setType(substitutionNodeTypeKey);
1028     substitutionNodeTemplate.setProperties(
1029         managerSubstitutionNodeTemplateProperties(translateTo, substitutionNodeTemplate,
1030             templateName));
1031     return substitutionNodeTemplate;
1032   }
1033
1034   /**
1035    * Create abstract substitution node template.
1036    *
1037    * @param translateTo             the translate to
1038    * @param templateName            the template name
1039    * @param substitutionNodeTypeKey the substitution node type key
1040    * @return the abstract substitute node template
1041    */
1042   public static NodeTemplate createAbstractSubstitutionNodeTemplate(
1043       TranslateTo translateTo,
1044       String templateName,
1045       String substitutionNodeTypeKey) {
1046     NodeTemplate substitutionNodeTemplate = new NodeTemplate();
1047     List<String> directiveList = new ArrayList<>();
1048     directiveList.add(ToscaConstants.NODE_TEMPLATE_DIRECTIVE_SUBSTITUTABLE);
1049     substitutionNodeTemplate.setDirectives(directiveList);
1050     substitutionNodeTemplate.setType(substitutionNodeTypeKey);
1051     substitutionNodeTemplate.setProperties(
1052         managerSubstitutionNodeTemplateProperties(translateTo, substitutionNodeTemplate,
1053             templateName));
1054     return substitutionNodeTemplate;
1055   }
1056
1057
1058   /**
1059    * Checks if the source and target resource is a valid candidate for adding tosca dependency
1060    * relationship.
1061    *
1062    * @param heatOrchestrationTemplate the heat orchestration template
1063    * @param sourceResource          the source resource
1064    * @param targetResource          the target resource
1065    * @param dependencyEntity        the dependency entity
1066    * @return true if the candidate resources are a valid combination for the dependency relationship
1067    * and false otherwise
1068    */
1069   public static boolean isValidDependsOnCandidate(HeatOrchestrationTemplate
1070                                                       heatOrchestrationTemplate,
1071                                                   Resource sourceResource,
1072                                                   Resource targetResource,
1073                                                   ConsolidationEntityType dependencyEntity,
1074                                                   TranslationContext context) {
1075     dependencyEntity
1076         .setEntityType(heatOrchestrationTemplate, sourceResource, targetResource, context);
1077     ConsolidationEntityType sourceEntityType = dependencyEntity.getSourceEntityType();
1078     ConsolidationEntityType targetEntityType = dependencyEntity.getTargetEntityType();
1079
1080     return ConsolidationTypesConnectivity.isDependsOnRelationshipValid(sourceEntityType, targetEntityType);
1081   }
1082
1083   private static Map<String, Object> managerSubstitutionNodeTemplateProperties(
1084       TranslateTo translateTo,
1085       Template template,
1086       String templateName) {
1087     Map<String, Object> substitutionProperties = new HashMap<>();
1088     Map<String, Object> heatProperties = translateTo.getResource().getProperties();
1089     if (Objects.nonNull(heatProperties)) {
1090       for (Map.Entry<String, Object> entry : heatProperties.entrySet()) {
1091         Object property = TranslatorHeatToToscaPropertyConverter
1092             .getToscaPropertyValue(translateTo.getServiceTemplate(),
1093                 translateTo.getTranslatedId(), entry.getKey(),
1094                 entry.getValue(), null, translateTo.getHeatFileName(),
1095                 translateTo.getHeatOrchestrationTemplate(), template, translateTo.getContext());
1096         substitutionProperties.put(entry.getKey(), property);
1097       }
1098     }
1099     return addAbstractSubstitutionProperty(templateName, substitutionProperties);
1100   }
1101
1102   private static Map<String, Object> addAbstractSubstitutionProperty(String templateName,
1103                                                                      Map<String, Object>
1104                                                                          substitutionProperties) {
1105     Map<String, Object> innerProps = new HashMap<>();
1106     innerProps.put(ToscaConstants.SUBSTITUTE_SERVICE_TEMPLATE_PROPERTY_NAME,
1107         ToscaUtil.getServiceTemplateFileName(templateName));
1108     substitutionProperties.put(ToscaConstants.SERVICE_TEMPLATE_FILTER_PROPERTY_NAME, innerProps);
1109     return substitutionProperties;
1110   }
1111
1112   private static Map<String, Map<String, List<String>>>
1113   getSubstitutionNodeTypeExposedConnectionPoints(NodeType substitutionNodeType,
1114                                                  ServiceTemplate substitutionServiceTemplate,
1115                                                  TranslationContext context) {
1116     Map<String, NodeTemplate> nodeTemplates =
1117         substitutionServiceTemplate.getTopology_template().getNode_templates();
1118     String nodeTemplateId;
1119     NodeTemplate nodeTemplate;
1120     String nodeType;
1121     Map<String, Map<String, List<String>>> substitutionMapping = new HashMap<>();
1122     if (nodeTemplates == null) {
1123       return substitutionMapping;
1124     }
1125
1126     Map<String, List<String>> capabilitySubstitutionMapping = new HashMap<>();
1127     Map<String, List<String>> requirementSubstitutionMapping = new HashMap<>();
1128     substitutionMapping.put("capability", capabilitySubstitutionMapping);
1129     substitutionMapping.put("requirement", requirementSubstitutionMapping);
1130     List<Map<String, RequirementDefinition>> nodeTypeRequirementsDefinition;
1131     Map<String, RequirementAssignment> nodeTemplateRequirementsAssignment;
1132     List<Map<String, RequirementDefinition>> exposedRequirementsDefinition;
1133     Map<String, Map<String, RequirementAssignment>> fullFilledRequirementsDefinition =
1134         new HashMap<>();
1135     Map<String, CapabilityDefinition> nodeTypeCapabilitiesDefinition = new HashMap<>();
1136     Map<String, CapabilityDefinition> exposedCapabilitiesDefinition;
1137     ToscaAnalyzerService toscaAnalyzerService = new ToscaAnalyzerServiceImpl();
1138
1139     for (Map.Entry<String, NodeTemplate> entry : nodeTemplates.entrySet()) {
1140       nodeTemplateId = entry.getKey();
1141       nodeTemplate = entry.getValue();
1142       nodeType = nodeTemplate.getType();
1143
1144       // get requirements
1145       nodeTypeRequirementsDefinition =
1146           getNodeTypeReqs(nodeType, nodeTemplateId, substitutionServiceTemplate,
1147               requirementSubstitutionMapping, context);
1148       nodeTemplateRequirementsAssignment = DataModelUtil.getNodeTemplateRequirements(nodeTemplate);
1149       fullFilledRequirementsDefinition.put(nodeTemplateId, nodeTemplateRequirementsAssignment);
1150       //set substitution node type requirements
1151       exposedRequirementsDefinition =
1152           toscaAnalyzerService.calculateExposedRequirements(nodeTypeRequirementsDefinition,
1153           nodeTemplateRequirementsAssignment);
1154       DataModelUtil
1155           .addSubstitutionNodeTypeRequirements(substitutionNodeType, exposedRequirementsDefinition,
1156           nodeTemplateId);
1157
1158       //get capabilities
1159       addNodeTypeCapabilitiesToSubMapping(nodeTypeCapabilitiesDefinition,
1160           capabilitySubstitutionMapping, nodeType,
1161           nodeTemplateId, substitutionServiceTemplate, context);
1162     }
1163
1164     exposedCapabilitiesDefinition =
1165         toscaAnalyzerService.calculateExposedCapabilities(nodeTypeCapabilitiesDefinition,
1166         fullFilledRequirementsDefinition);
1167     DataModelUtil.addNodeTypeCapabilitiesDef(substitutionNodeType, exposedCapabilitiesDefinition);
1168     return substitutionMapping;
1169   }
1170
1171   private static void addNodeTypeCapabilitiesToSubMapping(
1172       Map<String, CapabilityDefinition> nodeTypeCapabilitiesDefinition,
1173       Map<String, List<String>> capabilitySubstitutionMapping, String type, String templateName,
1174       ServiceTemplate serviceTemplate, TranslationContext context) {
1175     NodeType flatNodeType =
1176         getNodeTypeWithFlatHierarchy(type, serviceTemplate, context);
1177
1178     if (flatNodeType.getCapabilities() != null) {
1179       flatNodeType.getCapabilities()
1180           .entrySet()
1181           .stream()
1182           .filter(capabilityNodeEntry -> shouldCapabilityNeedsToBeAdded(capabilityNodeEntry.getKey()))
1183           .forEach(capabilityNodeEntry ->
1184               addCapabilityToSubMapping(
1185               templateName, capabilityNodeEntry, nodeTypeCapabilitiesDefinition, capabilitySubstitutionMapping));
1186     }
1187   }
1188
1189   private static boolean shouldCapabilityNeedsToBeAdded(String capabilityKey) {
1190     return !capabilityKey.contains(FORWARDER) || ToggleableFeature.FORWARDER_CAPABILITY.isActive();
1191   }
1192
1193   private static void addCapabilityToSubMapping(String templateName,
1194                                                 Map.Entry<String, CapabilityDefinition> capabilityNodeEntry,
1195                                                 Map<String, CapabilityDefinition> nodeTypeCapabilitiesDefinition,
1196                                                 Map<String, List<String>> capabilitySubstitutionMapping) {
1197     String capabilityKey;
1198     List<String> capabilityMapping;
1199     capabilityKey = capabilityNodeEntry.getKey() + "_" + templateName;
1200     nodeTypeCapabilitiesDefinition.put(capabilityKey, capabilityNodeEntry.getValue().clone());
1201     capabilityMapping = new ArrayList<>();
1202     capabilityMapping.add(templateName);
1203     capabilityMapping.add(capabilityNodeEntry.getKey());
1204     capabilitySubstitutionMapping.put(capabilityKey, capabilityMapping);
1205   }
1206
1207   private static List<Map<String, RequirementDefinition>> getNodeTypeReqs(
1208       String type,
1209       String templateName,
1210       ServiceTemplate serviceTemplate,
1211       Map<String, List<String>> requirementSubstitutionMapping,
1212       TranslationContext context) {
1213     List<Map<String, RequirementDefinition>> requirementList = new ArrayList<>();
1214     NodeType flatNodeType = getNodeTypeWithFlatHierarchy(type, serviceTemplate, context);
1215     List<String> requirementMapping;
1216
1217     if (flatNodeType.getRequirements() == null) {
1218       return requirementList;
1219     }
1220
1221     for (Map<String, RequirementDefinition> requirementMap : flatNodeType.getRequirements()) {
1222       for (Map.Entry<String, RequirementDefinition> requirementNodeEntry : requirementMap
1223           .entrySet()) {
1224         ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil();
1225         RequirementDefinition requirementNodeEntryValue = toscaExtensionYamlUtil
1226             .yamlToObject(toscaExtensionYamlUtil.objectToYaml(requirementNodeEntry.getValue()),
1227                 RequirementDefinition.class);
1228         if (Objects.isNull(requirementNodeEntryValue.getOccurrences())) {
1229           requirementNodeEntryValue.setOccurrences(new Object[]{1, 1});
1230         }
1231         Map<String, RequirementDefinition> requirementDef = new HashMap<>();
1232         requirementDef.put(requirementNodeEntry.getKey(), requirementNodeEntryValue);
1233         DataModelUtil.addRequirementToList(requirementList, requirementDef);
1234         requirementMapping = new ArrayList<>();
1235         requirementMapping.add(templateName);
1236         requirementMapping.add(requirementNodeEntry.getKey());
1237         requirementSubstitutionMapping
1238             .put(requirementNodeEntry.getKey() + "_" + templateName, requirementMapping);
1239         if (Objects.isNull(requirementNodeEntryValue.getNode())) {
1240           requirementNodeEntryValue.setOccurrences(new Object[]{1, 1});
1241         }
1242       }
1243     }
1244     return requirementList;
1245   }
1246
1247   /**
1248    * Fetch global substitution service template service template.
1249    *
1250    * @param serviceTemplate the service template
1251    * @param context         the context
1252    * @return the service template
1253    */
1254   public static ServiceTemplate fetchGlobalSubstitutionServiceTemplate(
1255       ServiceTemplate serviceTemplate,
1256       TranslationContext context) {
1257     ServiceTemplate globalSubstitutionServiceTemplate =
1258         context.getTranslatedServiceTemplates()
1259             .get(Constants.GLOBAL_SUBSTITUTION_TYPES_TEMPLATE_NAME);
1260     if (globalSubstitutionServiceTemplate == null) {
1261       globalSubstitutionServiceTemplate =
1262           HeatToToscaUtil.createInitGlobalSubstitutionServiceTemplate();
1263       context.getTranslatedServiceTemplates()
1264           .put(Constants.GLOBAL_SUBSTITUTION_TYPES_TEMPLATE_NAME,
1265               globalSubstitutionServiceTemplate);
1266     }
1267     boolean isImportAddedToServiceTemplate =
1268         DataModelUtil.isImportAddedToServiceTemplate(serviceTemplate.getImports(), Constants
1269             .GLOBAL_SUBSTITUTION_TYPES_TEMPLATE_NAME);
1270     if (!isImportAddedToServiceTemplate) {
1271       serviceTemplate.getImports()
1272           .addAll(
1273               HeatToToscaUtil.createImportList(Constants.GLOBAL_SUBSTITUTION_TYPES_TEMPLATE_NAME));
1274     }
1275     return globalSubstitutionServiceTemplate;
1276   }
1277
1278   public static List<Map<String, Import>> createImportList(String templateName) {
1279     List<Map<String, Import>> imports = new ArrayList<>();
1280     Map<String, Import> importsMap = new HashMap<>();
1281     importsMap.put(templateName, HeatToToscaUtil.createServiceTemplateImport(templateName));
1282     imports.add(importsMap);
1283     return imports;
1284   }
1285
1286   /**
1287    * Create service template import import.
1288    *
1289    * @param serviceTemplate the service template
1290    * @return the import
1291    */
1292   public static Import createServiceTemplateImport(ServiceTemplate serviceTemplate) {
1293     Import serviceTemplateImport = new Import();
1294     serviceTemplateImport.setFile(ToscaUtil.getServiceTemplateFileName(serviceTemplate));
1295     return serviceTemplateImport;
1296   }
1297
1298   /**
1299    * Create service template import import.
1300    *
1301    * @param metadataTemplateName the service template name
1302    * @return the import
1303    */
1304   public static Import createServiceTemplateImport(String metadataTemplateName) {
1305     Import serviceTemplateImport = new Import();
1306     serviceTemplateImport.setFile(ToscaUtil.getServiceTemplateFileName(metadataTemplateName));
1307     return serviceTemplateImport;
1308   }
1309
1310   public static ToscaServiceModel createToscaServiceModel(ServiceTemplate
1311                                                               entryDefinitionServiceTemplate,
1312                                                           TranslationContext translationContext) {
1313     return new ToscaServiceModel(getCsarArtifactFiles(translationContext),
1314         getServiceTemplates(translationContext),
1315         ToscaUtil.getServiceTemplateFileName(entryDefinitionServiceTemplate));
1316   }
1317
1318   private static FileContentHandler getCsarArtifactFiles(TranslationContext translationContext) {
1319     FileContentHandler artifactFiles = new FileContentHandler();
1320     artifactFiles.setFiles(translationContext.getFiles());
1321     artifactFiles.setFiles(translationContext.getExternalArtifacts());
1322
1323     HeatTreeManager heatTreeManager =
1324         HeatTreeManagerUtil.initHeatTreeManager(translationContext.getFiles());
1325     heatTreeManager.createTree();
1326     ValidationStructureList validationStructureList =
1327         new ValidationStructureList(heatTreeManager.getTree());
1328     byte[] validationStructureFile =
1329         FileUtils.convertToBytes(validationStructureList, FileUtils.FileExtension.JSON);
1330     artifactFiles.addFile("HEAT.meta", validationStructureFile);
1331     return artifactFiles;
1332   }
1333
1334
1335   private static Map<String, ServiceTemplate> getServiceTemplates(TranslationContext
1336                                                                       translationContext) {
1337     List<ServiceTemplate> serviceTemplates = new ArrayList<>();
1338     serviceTemplates.addAll(GlobalTypesGenerator
1339         .getGlobalTypesServiceTemplate(OnboardingTypesEnum.ZIP).values());
1340     serviceTemplates.addAll(translationContext.getTranslatedServiceTemplates().values());
1341     Map<String, ServiceTemplate> serviceTemplatesMap = new HashMap<>();
1342
1343     for (ServiceTemplate template : serviceTemplates) {
1344       serviceTemplatesMap.put(ToscaUtil.getServiceTemplateFileName(template), template);
1345     }
1346     return serviceTemplatesMap;
1347   }
1348
1349 }