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