2d283d301ae47ed32a1d82de77e864a05c3eb153
[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 String getSubInterfaceResourceType(Resource resource) {
608         if (!HeatToToscaUtil.isYamlFile(resource.getType())) {
609             return ((Map) resource.getProperties().get(HeatConstants.RESOURCE_DEF_PROPERTY_NAME))
610                            .get(HeatConstants.RESOURCE_DEF_TYPE_PROPERTY_NAME).toString();
611         }
612         return resource.getType();
613     }
614
615     private static Optional<String> getParentNodeTemplateIdFromPropertyValue(Object toscaPropertyValue,
616                                                                                     TranslateTo subInterfaceTo) {
617         if (toscaPropertyValue instanceof List && ((List) toscaPropertyValue).get(0) instanceof Map) {
618             Resource subInterfaceResource = subInterfaceTo.getResource();
619             Map<String, String> toscaPropertyValueMap = (Map) ((List) toscaPropertyValue).get(0);
620             String parentPortPropertyInput = toscaPropertyValueMap.get(ToscaFunctions.GET_INPUT.getDisplayName());
621             Map<String, Object> resourceDefPropertiesMap;
622             if (!isYamlFile(subInterfaceResource.getType())) {
623                 resourceDefPropertiesMap =
624                         (Map) ((Map) subInterfaceResource.getProperties().get(HeatConstants.RESOURCE_DEF_PROPERTY_NAME))
625                                       .get(HeatConstants.RESOURCE_DEF_PROPERTIES);
626             } else {
627                 resourceDefPropertiesMap = subInterfaceResource.getProperties();
628             }
629             Object parentPortObj = resourceDefPropertiesMap.get(parentPortPropertyInput);
630             if (parentPortObj instanceof Map) {
631                 Map<String, String> parentPortPropertyValue = (Map) parentPortObj;
632                 if (parentPortPropertyValue.keySet().contains(ResourceReferenceFunctions.GET_RESOURCE.getFunction())) {
633                     return ResourceTranslationBase.getResourceTranslatedId(subInterfaceTo.getHeatFileName(),
634                             subInterfaceTo.getHeatOrchestrationTemplate(),
635                             parentPortPropertyValue.get(ResourceReferenceFunctions.GET_RESOURCE.getFunction()),
636                             subInterfaceTo.getContext());
637                 }
638             }
639         }
640         return Optional.empty();
641     }
642
643     /**
644      * Checks if the nested resource represents a VFC or a complex VFC (Heat file should contain at
645      * least one or more compute nodes).
646      *
647      * @param resource the resource
648      * @param context  the context
649      * @return true if the resource represents a VFC and false otherwise.
650      */
651     public static boolean isNestedVfcResource(Resource resource, TranslationContext context) {
652         Optional<String> nestedHeatFileName = HeatToToscaUtil.getNestedHeatFileName(resource);
653         HeatOrchestrationTemplate nestedHeatOrchestrationTemplate = new YamlUtil().yamlToObject(
654                 context.getFileContent(nestedHeatFileName.get()), HeatOrchestrationTemplate.class);
655         Map<String, Resource> resources = nestedHeatOrchestrationTemplate.getResources();
656         if (Objects.nonNull(resources)) {
657             for (Resource innerResource : resources.values()) {
658                 if (ConsolidationDataUtil.isComputeResource(innerResource)) {
659                     return true;
660                 }
661             }
662         }
663         return false;
664     }
665
666     /**
667      * Get nested heat file name in case of nested resource.
668      *
669      * @param resource the resource
670      * @return the nested heat file name
671      */
672     private static Optional<String> getNestedHeatFileName(Resource resource) {
673         if (!isNestedResource(resource)) {
674             return Optional.empty();
675         }
676
677         String resourceType = resource.getType();
678
679         if (resourceType.equals(HeatResourcesTypes.RESOURCE_GROUP_RESOURCE_TYPE.getHeatResource())) {
680             Object resourceDef = resource.getProperties().get(HeatConstants.RESOURCE_DEF_PROPERTY_NAME);
681             String internalResourceType =
682                     (String) ((Map) resourceDef).get(HeatConstants.RESOURCE_DEF_TYPE_PROPERTY_NAME);
683             return Optional.of(internalResourceType);
684         }
685         return Optional.of(resourceType);
686     }
687
688     /**
689      * Gets nested file.
690      *
691      * @param resource the resource
692      * @return the nested file
693      */
694     public static Optional<String> getNestedFile(Resource resource) {
695         if (!isNestedResource(resource)) {
696             return Optional.empty();
697         }
698         String resourceType = resource.getType();
699         if (resourceType.equals(HeatResourcesTypes.RESOURCE_GROUP_RESOURCE_TYPE.getHeatResource())) {
700             Object resourceDef = resource.getProperties().get(HeatConstants.RESOURCE_DEF_PROPERTY_NAME);
701             String internalResourceType =
702                     (String) ((Map) resourceDef).get(HeatConstants.RESOURCE_DEF_TYPE_PROPERTY_NAME);
703             return Optional.of(internalResourceType);
704         } else {
705             return Optional.of(resourceType);
706         }
707     }
708
709     public static boolean isYamlFile(String fileName) {
710         return fileName.endsWith(".yaml") || fileName.endsWith(".yml");
711     }
712
713     /**
714      * Gets resource.
715      *
716      * @param heatOrchestrationTemplate the heat orchestration template
717      * @param resourceId                the resource id
718      * @param heatFileName              the heat file name
719      * @return the resource
720      */
721     public static Resource getResource(HeatOrchestrationTemplate heatOrchestrationTemplate, String resourceId,
722                                               String heatFileName) {
723         Resource resource = heatOrchestrationTemplate.getResources().get(resourceId);
724         if (resource == null) {
725             throw new CoreException(new ResourceNotFoundInHeatFileErrorBuilder(resourceId, heatFileName).build());
726         }
727         return resource;
728     }
729
730
731     /**
732      * Get resource type.
733      *
734      * @param resourceId                the resource id
735      * @param heatOrchestrationTemplate heat orchestration template
736      * @param heatFileName              heat file name
737      * @return resource type
738      */
739     public static String getResourceType(String resourceId, HeatOrchestrationTemplate heatOrchestrationTemplate,
740                                                 String heatFileName) {
741         return HeatToToscaUtil.getResource(heatOrchestrationTemplate, resourceId, heatFileName).getType();
742     }
743
744     /**
745      * Is heat file nested boolean.
746      *
747      * @param translateTo  the translate to
748      * @param heatFileName the heat file name
749      * @return the boolean
750      */
751     public static boolean isHeatFileNested(TranslateTo translateTo, String heatFileName) {
752         return isHeatFileNested(translateTo.getContext(), heatFileName);
753     }
754
755     public static boolean isHeatFileNested(TranslationContext context, String heatFileName) {
756         return context.getNestedHeatsFiles().contains(heatFileName);
757     }
758
759     /**
760      * Extract contrail get resource attached heat resource id optional.
761      *
762      * @param propertyValue the property value
763      * @return the optional
764      */
765     public static Optional<String> extractContrailGetResourceAttachedHeatResourceId(Object propertyValue) {
766         if (propertyValue instanceof Map) {
767             if (((Map) propertyValue).containsKey(GET_ATTR)) {
768                 return getResourceId(((Map) propertyValue).get(GET_ATTR));
769             } else if (((Map) propertyValue).containsKey(GET_RESOURCE)) {
770                 return getHeatResourceIdFromResource((Map) propertyValue);
771             } else {
772                 Collection valCollection = ((Map) propertyValue).values();
773                 return evaluateHeatResourceId(valCollection);
774             }
775         } else if (propertyValue instanceof List) {
776             return evaluateHeatResourceId((List) propertyValue);
777         }
778         return Optional.empty();
779     }
780
781     private static Optional<String> getResourceId(Object data) {
782         if (data instanceof List && CollectionUtils.size(data) > 1 && FQ_NAME.equals(((List) data).get(1))
783                     && ((List) data).get(0) instanceof String) {
784             return Optional.of((String) ((List) data).get(0));
785         } else {
786             LOGGER.warn("invalid format of 'get_attr' function - " + data.toString());
787             return Optional.empty();
788         }
789     }
790
791     private static Optional<String> getHeatResourceIdFromResource(Map propertyValue) {
792         Object value = propertyValue.get(GET_RESOURCE);
793         if (value instanceof String) {
794             return Optional.of((String) value);
795         } else {
796             LOGGER.warn("invalid format of 'get_resource' function - " + propertyValue.toString());
797             return Optional.empty();
798         }
799     }
800
801     private static Optional<String> evaluateHeatResourceId(Collection propertyValue) {
802         for (Object prop : propertyValue) {
803             Optional<String> ret = extractContrailGetResourceAttachedHeatResourceId(prop);
804             if (ret.isPresent()) {
805                 return ret;
806             }
807         }
808         return Optional.empty();
809     }
810
811     /**
812      * Gets tosca service model.
813      *
814      * @param context translation context
815      * @return the tosca service model
816      */
817     public static ToscaServiceModel getToscaServiceModel(TranslationContext context) {
818         Map<String, String> metadata = new HashMap<>();
819         metadata.put(ToscaConstants.ST_METADATA_TEMPLATE_NAME, Constants.MAIN_TEMPLATE_NAME);
820         return getToscaServiceModel(context, metadata);
821     }
822
823     /**
824      * Gets tosca service model.
825      *
826      * @param context                 translation context
827      * @param entryDefinitionMetadata template name of the entry definition servie template
828      * @return the tosca service model
829      */
830     private static ToscaServiceModel getToscaServiceModel(TranslationContext context,
831                                                                  Map<String, String> entryDefinitionMetadata) {
832         Map<String, ServiceTemplate> serviceTemplates = new HashMap<>(context.getGlobalServiceTemplates());
833         Collection<ServiceTemplate> tmpServiceTemplates = context.getTranslatedServiceTemplates().values();
834         for (ServiceTemplate serviceTemplate : tmpServiceTemplates) {
835             ToscaUtil.addServiceTemplateToMapWithKeyFileName(serviceTemplates, serviceTemplate);
836         }
837         return new ToscaServiceModel(null, serviceTemplates,
838                                             ToscaUtil.getServiceTemplateFileName(entryDefinitionMetadata));
839     }
840
841     /**
842      * Gets service template from context.
843      *
844      * @param serviceTemplateFileName the service template file name
845      * @param context                 the context
846      * @return the service template from context
847      */
848     public static Optional<ServiceTemplate> getServiceTemplateFromContext(String serviceTemplateFileName,
849                                                                                  TranslationContext context) {
850         for (ServiceTemplate serviceTemplate : context.getTranslatedServiceTemplates().values()) {
851             if (ToscaUtil.getServiceTemplateFileName(serviceTemplate).equals(serviceTemplateFileName)) {
852                 return Optional.of(serviceTemplate);
853             }
854         }
855         return Optional.empty();
856     }
857
858     /**
859      * Adding link requerment from port node template to network node template.
860      *
861      * @param portNodeTemplate    port node template
862      * @param networkTranslatedId network node template id
863      */
864     public static RequirementAssignment addLinkReqFromPortToNetwork(NodeTemplate portNodeTemplate,
865                                                                            String networkTranslatedId) {
866         RequirementAssignment requirement = new RequirementAssignment();
867         requirement.setCapability(ToscaCapabilityType.NATIVE_NETWORK_LINKABLE);
868         requirement.setRelationship(ToscaRelationshipType.NATIVE_NETWORK_LINK_TO);
869         requirement.setNode(networkTranslatedId);
870         DataModelUtil.addRequirementAssignment(portNodeTemplate, ToscaConstants.LINK_REQUIREMENT_ID, requirement);
871         return requirement;
872     }
873
874     /**
875      * Adding binding requerment from sub interface node template to interface (port) node template.
876      *
877      * @param subInterfaceNodeTemplate sub interface template
878      * @param interfaceTranslatedId    interface node template id
879      */
880     public static void addBindingReqFromSubInterfaceToInterface(NodeTemplate subInterfaceNodeTemplate,
881                                                                        String interfaceTranslatedId) {
882         RequirementAssignment requirement = new RequirementAssignment();
883         requirement.setCapability(ToscaCapabilityType.NATIVE_NETWORK_BINDABLE);
884         requirement.setRelationship(ToscaRelationshipType.NATIVE_NETWORK_BINDS_TO);
885         requirement.setNode(interfaceTranslatedId);
886         DataModelUtil
887                 .addRequirementAssignment(subInterfaceNodeTemplate, ToscaConstants.BINDING_REQUIREMENT_ID, requirement);
888     }
889
890     /**
891      * Get property Parameter Name Value.
892      *
893      * @param property property
894      * @return Parameter name in case the property include "get_param" function
895      */
896     public static Optional<String> getPropertyParameterNameValue(Object property) {
897         if (Objects.isNull(property)) {
898             return Optional.empty();
899         }
900         Optional<AttachedPropertyVal> extractedProperty = extractProperty(property);
901         if (extractedProperty.isPresent()) {
902             return getParameterName(extractedProperty.get());
903         }
904         return Optional.empty();
905     }
906
907     private static Optional<String> getParameterName(AttachedPropertyVal extractedProperty) {
908         if (!extractedProperty.isGetParam()) {
909             return Optional.empty();
910         }
911         Object getParamFuncValue = extractedProperty.getPropertyValue();
912         if (getParamFuncValue instanceof String) {
913             return Optional.of((String) getParamFuncValue);
914         } else {
915             return Optional.of((String) ((List) getParamFuncValue).get(0));
916         }
917     }
918
919     public static String getToscaPropertyName(TranslationContext context, String heatResourceType,
920                                                      String heatPropertyName) {
921         return context.getElementMapping(heatResourceType, Constants.PROP, heatPropertyName);
922     }
923
924     /**
925      * Gets tosca property name.
926      *
927      * @param translateTo      the translate to
928      * @param heatPropertyName the heat property name
929      * @return the tosca property name
930      */
931     public static String getToscaPropertyName(TranslateTo translateTo, String heatPropertyName) {
932         return translateTo.getContext()
933                           .getElementMapping(translateTo.getResource().getType(), Constants.PROP, heatPropertyName);
934     }
935
936     /**
937      * Gets tosca attribute name.
938      *
939      * @param context          the context
940      * @param heatResourceType the heat resource type
941      * @param heatAttrName     the heat attr name
942      * @return the tosca attribute name
943      */
944     public static String getToscaAttributeName(TranslationContext context, String heatResourceType,
945                                                       String heatAttrName) {
946         return context.getElementMapping(heatResourceType, Constants.ATTR, heatAttrName);
947     }
948
949     /**
950      * Gets tosca attribute name.
951      *
952      * @param translateTo  the translate to
953      * @param heatAttrName the heat attr name
954      * @return the tosca attribute name
955      */
956     public static String getToscaAttributeName(TranslateTo translateTo, String heatAttrName) {
957         return translateTo.getContext()
958                           .getElementMapping(translateTo.getResource().getType(), Constants.ATTR, heatAttrName);
959     }
960
961     /**
962      * Create init substitution service template service template.
963      *
964      * @param templateName the template name
965      * @return the service template
966      */
967     public static ServiceTemplate createInitSubstitutionServiceTemplate(String templateName) {
968         ServiceTemplate nestedSubstitutionServiceTemplate = new ServiceTemplate();
969         Map<String, String> templateMetadata = new HashMap<>();
970         templateMetadata.put(ToscaConstants.ST_METADATA_TEMPLATE_NAME, templateName);
971         nestedSubstitutionServiceTemplate.setMetadata(templateMetadata);
972         nestedSubstitutionServiceTemplate.setTosca_definitions_version(ToscaConstants.TOSCA_DEFINITIONS_VERSION);
973         nestedSubstitutionServiceTemplate.setTopology_template(new TopologyTemplate());
974         List<Map<String, Import>> globalTypesImportList = GlobalTypesGenerator.getGlobalTypesImportList();
975         globalTypesImportList
976                 .addAll(HeatToToscaUtil.createImportList(Constants.GLOBAL_SUBSTITUTION_TYPES_TEMPLATE_NAME));
977         nestedSubstitutionServiceTemplate.setImports(globalTypesImportList);
978         return nestedSubstitutionServiceTemplate;
979     }
980
981     /**
982      * Create init global substitution service template service template.
983      *
984      * @return the service template
985      */
986     private static ServiceTemplate createInitGlobalSubstitutionServiceTemplate() {
987         ServiceTemplate globalSubstitutionServiceTemplate = new ServiceTemplate();
988         Map<String, String> templateMetadata = new HashMap<>();
989         templateMetadata
990                 .put(ToscaConstants.ST_METADATA_TEMPLATE_NAME, Constants.GLOBAL_SUBSTITUTION_TYPES_TEMPLATE_NAME);
991         globalSubstitutionServiceTemplate.setMetadata(templateMetadata);
992         globalSubstitutionServiceTemplate.setImports(GlobalTypesGenerator.getGlobalTypesImportList());
993         globalSubstitutionServiceTemplate.setTosca_definitions_version(ToscaConstants.TOSCA_DEFINITIONS_VERSION);
994         return globalSubstitutionServiceTemplate;
995     }
996
997     /**
998      * Create substitution node type node type.
999      *
1000      * @param substitutionServiceTemplate the substitution service template
1001      * @return the node type
1002      */
1003     public NodeType createSubstitutionNodeType(ServiceTemplate substitutionServiceTemplate) {
1004         NodeType substitutionNodeType = new NodeType();
1005         substitutionNodeType.setDerived_from(ToscaNodeType.ABSTRACT_SUBSTITUTE);
1006         substitutionNodeType.setDescription(substitutionServiceTemplate.getDescription());
1007         substitutionNodeType.setProperties(manageSubstitutionNodeTypeProperties(substitutionServiceTemplate));
1008         substitutionNodeType.setAttributes(manageSubstitutionNodeTypeAttributes(substitutionServiceTemplate));
1009         return substitutionNodeType;
1010     }
1011
1012     private Map<String, PropertyDefinition> manageSubstitutionNodeTypeProperties(ServiceTemplate substitutionServiceTemplate) {
1013         Map<String, PropertyDefinition> substitutionNodeTypeProperties = new HashMap<>();
1014         Map<String, ParameterDefinition> properties = substitutionServiceTemplate.getTopology_template().getInputs();
1015         if (properties == null) {
1016             return null;
1017         }
1018
1019         PropertyDefinition propertyDefinition;
1020         String toscaPropertyName;
1021         for (Map.Entry<String, ParameterDefinition> entry : properties.entrySet()) {
1022             toscaPropertyName = entry.getKey();
1023             propertyDefinition = new PropertyDefinition();
1024             ParameterDefinition parameterDefinition =
1025                     substitutionServiceTemplate.getTopology_template().getInputs().get(toscaPropertyName);
1026             propertyDefinition.setType(parameterDefinition.getType());
1027             propertyDefinition.setDescription(parameterDefinition.getDescription());
1028             propertyDefinition.setRequired(parameterDefinition.getRequired());
1029             propertyDefinition.set_default(parameterDefinition.get_default());
1030             propertyDefinition.setConstraints(parameterDefinition.getConstraints());
1031             propertyDefinition.setEntry_schema(parameterDefinition.getEntry_schema());
1032             propertyDefinition.setStatus(parameterDefinition.getStatus());
1033             substitutionNodeTypeProperties.put(toscaPropertyName, propertyDefinition);
1034         }
1035         return substitutionNodeTypeProperties;
1036     }
1037
1038     private Map<String, AttributeDefinition> manageSubstitutionNodeTypeAttributes(ServiceTemplate substitutionServiceTemplate) {
1039         Map<String, AttributeDefinition> substitutionNodeTypeAttributes = new HashMap<>();
1040         Map<String, ParameterDefinition> attributes = substitutionServiceTemplate.getTopology_template().getOutputs();
1041         if (attributes == null) {
1042             return null;
1043         }
1044         AttributeDefinition attributeDefinition;
1045         String toscaAttributeName;
1046
1047         for (Map.Entry<String, ParameterDefinition> entry : attributes.entrySet()) {
1048             attributeDefinition = new AttributeDefinition();
1049             toscaAttributeName = entry.getKey();
1050             ParameterDefinition parameterDefinition =
1051                     substitutionServiceTemplate.getTopology_template().getOutputs().get(toscaAttributeName);
1052             if (parameterDefinition.getType() != null && !parameterDefinition.getType().isEmpty()) {
1053                 attributeDefinition.setType(parameterDefinition.getType());
1054             } else {
1055                 attributeDefinition.setType(PropertyType.STRING.getDisplayName());
1056             }
1057             attributeDefinition.setDescription(parameterDefinition.getDescription());
1058             attributeDefinition.set_default(parameterDefinition.get_default());
1059             attributeDefinition.setEntry_schema(parameterDefinition.getEntry_schema());
1060             attributeDefinition.setStatus(parameterDefinition.getStatus());
1061             substitutionNodeTypeAttributes.put(toscaAttributeName, attributeDefinition);
1062         }
1063         return substitutionNodeTypeAttributes;
1064     }
1065
1066     /**
1067      * .
1068      * Create and add substitution mapping to the nested substitution service template, and update
1069      * the subtitution node type accordingly with the exposed requerments and capabilities
1070      *
1071      * @param context                           the translation context
1072      * @param substitutionNodeTypeKey           the substitution node type key
1073      * @param nestedSubstitutionServiceTemplate the nested substitution service template
1074      * @param substitutionNodeType              the substitution node type
1075      */
1076     public static void handleSubstitutionMapping(TranslationContext context, String substitutionNodeTypeKey,
1077                                                         ServiceTemplate nestedSubstitutionServiceTemplate,
1078                                                         NodeType substitutionNodeType) {
1079         Map<String, Map<String, List<String>>> substitutionMapping =
1080                 getSubstitutionNodeTypeExposedConnectionPoints(substitutionNodeType, nestedSubstitutionServiceTemplate,
1081                         context);
1082         //add substitution mapping after capability and requirement expose calculation
1083         nestedSubstitutionServiceTemplate.getTopology_template().setSubstitution_mappings(DataModelUtil
1084                                                                                                   .createSubstitutionTemplateSubMapping(
1085                                                                                                           substitutionNodeTypeKey,
1086                                                                                                           substitutionNodeType,
1087                                                                                                           substitutionMapping));
1088     }
1089
1090     /**
1091      * Gets node type with flat hierarchy.
1092      *
1093      * @param nodeTypeId      the node type id
1094      * @param serviceTemplate the service template
1095      * @param context         the context
1096      * @return the node type with flat hierarchy
1097      */
1098     public static NodeType getNodeTypeWithFlatHierarchy(String nodeTypeId, ServiceTemplate serviceTemplate,
1099                                                                TranslationContext context) {
1100         ToscaAnalyzerService toscaAnalyzerService = new ToscaAnalyzerServiceImpl();
1101         ToscaServiceModel toscaServiceModel =
1102                 HeatToToscaUtil.getToscaServiceModel(context, serviceTemplate.getMetadata());
1103         return (NodeType) toscaAnalyzerService.getFlatEntity(ToscaElementTypes.NODE_TYPE, nodeTypeId, serviceTemplate,
1104                 toscaServiceModel).getFlatEntity();
1105     }
1106
1107
1108     /**
1109      * Create abstract substitution node template.
1110      *
1111      * @param translateTo             the translate to
1112      * @param templateName            the template name
1113      * @param substitutionNodeTypeKey the substitution node type key
1114      * @return the abstract substitute node template
1115      */
1116     public static NodeTemplate createAbstractSubstitutionNodeTemplate(TranslateTo translateTo, String templateName,
1117                                                                              String substitutionNodeTypeKey) {
1118         NodeTemplate substitutionNodeTemplate = new NodeTemplate();
1119         List<String> directiveList = new ArrayList<>();
1120         directiveList.add(ToscaConstants.NODE_TEMPLATE_DIRECTIVE_SUBSTITUTABLE);
1121         substitutionNodeTemplate.setDirectives(directiveList);
1122         substitutionNodeTemplate.setType(substitutionNodeTypeKey);
1123         substitutionNodeTemplate.setProperties(
1124                 managerSubstitutionNodeTemplateProperties(translateTo, substitutionNodeTemplate, templateName));
1125         return substitutionNodeTemplate;
1126     }
1127
1128
1129     /**
1130      * Checks if the source and target resource is a valid candidate for adding tosca dependency
1131      * relationship.
1132      *
1133      * @param sourceResource   the source resource
1134      * @param targetResource   the target resource
1135      * @param dependencyEntity the dependency entity
1136      * @return true if the candidate resources are a valid combination for the dependency relationship
1137      * and false otherwise
1138      */
1139     public static boolean isValidDependsOnCandidate(Resource sourceResource, Resource targetResource,
1140                                                            ConsolidationEntityType dependencyEntity,
1141                                                            TranslationContext context) {
1142         dependencyEntity.setEntityType(sourceResource, targetResource, context);
1143         ConsolidationEntityType sourceEntityType = dependencyEntity.getSourceEntityType();
1144         ConsolidationEntityType targetEntityType = dependencyEntity.getTargetEntityType();
1145
1146         return ConsolidationTypesConnectivity.isDependsOnRelationshipValid(sourceEntityType, targetEntityType);
1147     }
1148
1149     private static Map<String, Object> managerSubstitutionNodeTemplateProperties(TranslateTo translateTo,
1150                                                                                         Template template,
1151                                                                                         String templateName) {
1152         Map<String, Object> substitutionProperties = new HashMap<>();
1153         Map<String, Object> heatProperties = translateTo.getResource().getProperties();
1154         if (Objects.nonNull(heatProperties)) {
1155             for (Map.Entry<String, Object> entry : heatProperties.entrySet()) {
1156                 Object property = TranslatorHeatToToscaPropertyConverter
1157                                           .getToscaPropertyValue(translateTo.getServiceTemplate(),
1158                                                   translateTo.getTranslatedId(), entry.getKey(), entry.getValue(), null,
1159                                                   translateTo.getHeatFileName(),
1160                                                   translateTo.getHeatOrchestrationTemplate(), template,
1161                                                   translateTo.getContext());
1162                 substitutionProperties.put(entry.getKey(), property);
1163             }
1164         }
1165         return addAbstractSubstitutionProperty(templateName, substitutionProperties);
1166     }
1167
1168     private static Map<String, Object> addAbstractSubstitutionProperty(String templateName,
1169                                                                               Map<String, Object> substitutionProperties) {
1170         Map<String, Object> innerProps = new HashMap<>();
1171         innerProps.put(ToscaConstants.SUBSTITUTE_SERVICE_TEMPLATE_PROPERTY_NAME,
1172                 ToscaUtil.getServiceTemplateFileName(templateName));
1173         substitutionProperties.put(ToscaConstants.SERVICE_TEMPLATE_FILTER_PROPERTY_NAME, innerProps);
1174         return substitutionProperties;
1175     }
1176
1177     private static Map<String, Map<String, List<String>>> getSubstitutionNodeTypeExposedConnectionPoints(NodeType substitutionNodeType,
1178                                                                                                                 ServiceTemplate substitutionServiceTemplate,
1179                                                                                                                 TranslationContext context) {
1180         Map<String, NodeTemplate> nodeTemplates =
1181                 substitutionServiceTemplate.getTopology_template().getNode_templates();
1182         String nodeTemplateId;
1183         NodeTemplate nodeTemplate;
1184         String nodeType;
1185         Map<String, Map<String, List<String>>> substitutionMapping = new HashMap<>();
1186         if (nodeTemplates == null) {
1187             return substitutionMapping;
1188         }
1189
1190         Map<String, List<String>> capabilitySubstitutionMapping = new HashMap<>();
1191         Map<String, List<String>> requirementSubstitutionMapping = new HashMap<>();
1192         substitutionMapping.put("capability", capabilitySubstitutionMapping);
1193         substitutionMapping.put("requirement", requirementSubstitutionMapping);
1194         List<Map<String, RequirementDefinition>> nodeTypeRequirementsDefinition;
1195         Map<String, RequirementAssignment> nodeTemplateRequirementsAssignment;
1196         List<Map<String, RequirementDefinition>> exposedRequirementsDefinition;
1197         Map<String, Map<String, RequirementAssignment>> fullFilledRequirementsDefinition = new HashMap<>();
1198         Map<String, CapabilityDefinition> nodeTypeCapabilitiesDefinition = new HashMap<>();
1199         Map<String, CapabilityDefinition> exposedCapabilitiesDefinition;
1200         ToscaAnalyzerService toscaAnalyzerService = new ToscaAnalyzerServiceImpl();
1201
1202         for (Map.Entry<String, NodeTemplate> entry : nodeTemplates.entrySet()) {
1203             nodeTemplateId = entry.getKey();
1204             nodeTemplate = entry.getValue();
1205             nodeType = nodeTemplate.getType();
1206
1207             // get requirements
1208             nodeTypeRequirementsDefinition = getNodeTypeReqs(nodeType, nodeTemplateId, substitutionServiceTemplate,
1209                     requirementSubstitutionMapping, context);
1210             nodeTemplateRequirementsAssignment = DataModelUtil.getNodeTemplateRequirements(nodeTemplate);
1211             fullFilledRequirementsDefinition.put(nodeTemplateId, nodeTemplateRequirementsAssignment);
1212             //set substitution node type requirements
1213             exposedRequirementsDefinition = toscaAnalyzerService
1214                                                     .calculateExposedRequirements(nodeTypeRequirementsDefinition,
1215                                                             nodeTemplateRequirementsAssignment);
1216             DataModelUtil.addSubstitutionNodeTypeRequirements(substitutionNodeType, exposedRequirementsDefinition,
1217                     nodeTemplateId);
1218
1219             //get capabilities
1220             addNodeTypeCapabilitiesToSubMapping(nodeTypeCapabilitiesDefinition, capabilitySubstitutionMapping, nodeType,
1221                     nodeTemplateId, substitutionServiceTemplate, context);
1222         }
1223
1224         exposedCapabilitiesDefinition = toscaAnalyzerService
1225                                                 .calculateExposedCapabilities(nodeTypeCapabilitiesDefinition,
1226                                                         fullFilledRequirementsDefinition);
1227         DataModelUtil.addNodeTypeCapabilitiesDef(substitutionNodeType, exposedCapabilitiesDefinition);
1228         return substitutionMapping;
1229     }
1230
1231     private static void addNodeTypeCapabilitiesToSubMapping(Map<String, CapabilityDefinition> nodeTypeCapabilitiesDefinition,
1232                                                                    Map<String, List<String>> capabilitySubstitutionMapping,
1233                                                                    String type, String templateName,
1234                                                                    ServiceTemplate serviceTemplate,
1235                                                                    TranslationContext context) {
1236         NodeType flatNodeType = getNodeTypeWithFlatHierarchy(type, serviceTemplate, context);
1237
1238         if (flatNodeType.getCapabilities() != null) {
1239             flatNodeType.getCapabilities().entrySet().stream().forEach(
1240                     capabilityNodeEntry -> addCapabilityToSubMapping(templateName, capabilityNodeEntry,
1241                             nodeTypeCapabilitiesDefinition, capabilitySubstitutionMapping));
1242         }
1243     }
1244
1245     private static void addCapabilityToSubMapping(String templateName,
1246                                                          Map.Entry<String, CapabilityDefinition> capabilityNodeEntry,
1247                                                          Map<String, CapabilityDefinition> nodeTypeCapabilitiesDefinition,
1248                                                          Map<String, List<String>> capabilitySubstitutionMapping) {
1249         String capabilityKey;
1250         List<String> capabilityMapping;
1251         capabilityKey = capabilityNodeEntry.getKey() + UNDERSCORE + templateName;
1252         nodeTypeCapabilitiesDefinition.put(capabilityKey, capabilityNodeEntry.getValue().clone());
1253         capabilityMapping = new ArrayList<>();
1254         capabilityMapping.add(templateName);
1255         capabilityMapping.add(capabilityNodeEntry.getKey());
1256         capabilitySubstitutionMapping.put(capabilityKey, capabilityMapping);
1257     }
1258
1259     private static List<Map<String, RequirementDefinition>> getNodeTypeReqs(String type, String templateName,
1260                                                                                    ServiceTemplate serviceTemplate,
1261                                                                                    Map<String, List<String>> requirementSubstitutionMapping,
1262                                                                                    TranslationContext context) {
1263         List<Map<String, RequirementDefinition>> requirementList = new ArrayList<>();
1264         NodeType flatNodeType = getNodeTypeWithFlatHierarchy(type, serviceTemplate, context);
1265         List<String> requirementMapping;
1266
1267         if (flatNodeType.getRequirements() == null) {
1268             return requirementList;
1269         }
1270
1271         for (Map<String, RequirementDefinition> requirementMap : flatNodeType.getRequirements()) {
1272             for (Map.Entry<String, RequirementDefinition> requirementNodeEntry : requirementMap.entrySet()) {
1273                 ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil();
1274                 RequirementDefinition requirementNodeEntryValue = toscaExtensionYamlUtil.yamlToObject(
1275                         toscaExtensionYamlUtil.objectToYaml(requirementNodeEntry.getValue()),
1276                         RequirementDefinition.class);
1277                 if (Objects.isNull(requirementNodeEntryValue.getOccurrences())) {
1278                     requirementNodeEntryValue.setOccurrences(new Object[] {1, 1});
1279                 }
1280                 Map<String, RequirementDefinition> requirementDef = new HashMap<>();
1281                 requirementDef.put(requirementNodeEntry.getKey(), requirementNodeEntryValue);
1282                 DataModelUtil.addRequirementToList(requirementList, requirementDef);
1283                 requirementMapping = new ArrayList<>();
1284                 requirementMapping.add(templateName);
1285                 requirementMapping.add(requirementNodeEntry.getKey());
1286                 requirementSubstitutionMapping
1287                         .put(requirementNodeEntry.getKey() + UNDERSCORE + templateName, requirementMapping);
1288                 if (Objects.isNull(requirementNodeEntryValue.getNode())) {
1289                     requirementNodeEntryValue.setOccurrences(new Object[] {1, 1});
1290                 }
1291             }
1292         }
1293         return requirementList;
1294     }
1295
1296     /**
1297      * Fetch global substitution service template service template.
1298      *
1299      * @param serviceTemplate the service template
1300      * @param context         the context
1301      * @return the service template
1302      */
1303     public static ServiceTemplate fetchGlobalSubstitutionServiceTemplate(ServiceTemplate serviceTemplate,
1304                                                                                 TranslationContext context) {
1305         ServiceTemplate globalSubstitutionServiceTemplate =
1306                 context.getTranslatedServiceTemplates().get(Constants.GLOBAL_SUBSTITUTION_TYPES_TEMPLATE_NAME);
1307         if (globalSubstitutionServiceTemplate == null) {
1308             globalSubstitutionServiceTemplate = HeatToToscaUtil.createInitGlobalSubstitutionServiceTemplate();
1309             context.getTranslatedServiceTemplates()
1310                    .put(Constants.GLOBAL_SUBSTITUTION_TYPES_TEMPLATE_NAME, globalSubstitutionServiceTemplate);
1311         }
1312         boolean isImportAddedToServiceTemplate = DataModelUtil
1313                                                          .isImportAddedToServiceTemplate(serviceTemplate.getImports(),
1314                                                                  Constants.GLOBAL_SUBSTITUTION_TYPES_TEMPLATE_NAME);
1315         if (!isImportAddedToServiceTemplate) {
1316             serviceTemplate.getImports()
1317                            .addAll(HeatToToscaUtil.createImportList(Constants.GLOBAL_SUBSTITUTION_TYPES_TEMPLATE_NAME));
1318         }
1319         return globalSubstitutionServiceTemplate;
1320     }
1321
1322     public static List<Map<String, Import>> createImportList(String templateName) {
1323         List<Map<String, Import>> imports = new ArrayList<>();
1324         Map<String, Import> importsMap = new HashMap<>();
1325         importsMap.put(templateName, HeatToToscaUtil.createServiceTemplateImport(templateName));
1326         imports.add(importsMap);
1327         return imports;
1328     }
1329
1330     /**
1331      * Create service template import import.
1332      *
1333      * @param serviceTemplate the service template
1334      * @return the import
1335      */
1336     public static Import createServiceTemplateImport(ServiceTemplate serviceTemplate) {
1337         Import serviceTemplateImport = new Import();
1338         serviceTemplateImport.setFile(ToscaUtil.getServiceTemplateFileName(serviceTemplate));
1339         return serviceTemplateImport;
1340     }
1341
1342     /**
1343      * Create service template import import.
1344      *
1345      * @param metadataTemplateName the service template name
1346      * @return the import
1347      */
1348     private static Import createServiceTemplateImport(String metadataTemplateName) {
1349         Import serviceTemplateImport = new Import();
1350         serviceTemplateImport.setFile(ToscaUtil.getServiceTemplateFileName(metadataTemplateName));
1351         return serviceTemplateImport;
1352     }
1353
1354     public static ToscaServiceModel createToscaServiceModel(ServiceTemplate entryDefinitionServiceTemplate,
1355                                                                    TranslationContext translationContext) {
1356         return new ToscaServiceModel(getCsarArtifactFiles(translationContext), getServiceTemplates(translationContext),
1357                                             ToscaUtil.getServiceTemplateFileName(entryDefinitionServiceTemplate));
1358     }
1359
1360     private static FileContentHandler getCsarArtifactFiles(TranslationContext translationContext) {
1361         FileContentHandler artifactFiles = new FileContentHandler();
1362         artifactFiles.setFiles(translationContext.getFiles());
1363         artifactFiles.setFiles(translationContext.getExternalArtifacts());
1364
1365         HeatTreeManager heatTreeManager = HeatTreeManagerUtil.initHeatTreeManager(translationContext.getFiles());
1366         heatTreeManager.createTree();
1367         ValidationStructureList validationStructureList = new ValidationStructureList(heatTreeManager.getTree());
1368         byte[] validationStructureFile =
1369                 FileUtils.convertToBytes(validationStructureList, FileUtils.FileExtension.JSON);
1370         artifactFiles.addFile("HEAT.meta", validationStructureFile);
1371         return artifactFiles;
1372     }
1373
1374
1375     private static Map<String, ServiceTemplate> getServiceTemplates(TranslationContext translationContext) {
1376         List<ServiceTemplate> serviceTemplates = new ArrayList<>();
1377         serviceTemplates.addAll(GlobalTypesGenerator.getGlobalTypesServiceTemplate(OnboardingTypesEnum.ZIP).values());
1378         serviceTemplates.addAll(translationContext.getTranslatedServiceTemplates().values());
1379         Map<String, ServiceTemplate> serviceTemplatesMap = new HashMap<>();
1380
1381         for (ServiceTemplate template : serviceTemplates) {
1382             serviceTemplatesMap.put(ToscaUtil.getServiceTemplateFileName(template), template);
1383         }
1384         return serviceTemplatesMap;
1385     }
1386
1387     public static String getNestedResourceTypePrefix(TranslateTo translateTo) {
1388         if (isSubInterfaceResource(translateTo.getResource(), translateTo.getContext()) && isSubInterfaceBoundToPort(
1389                 translateTo)) {
1390             return ToscaNodeType.VLAN_SUB_INTERFACE_RESOURCE_TYPE_PREFIX;
1391         }
1392         return ToscaNodeType.NESTED_HEAT_RESOURCE_TYPE_PREFIX;
1393     }
1394
1395     private static boolean isSubInterfaceBoundToPort(TranslateTo translateTo) {
1396         return HeatToToscaUtil.getSubInterfaceParentPortNodeTemplateId(translateTo).isPresent();
1397     }
1398
1399     //Method evaluate the  network role from sub interface node template id, designed considering
1400     // only single sub interface present in nested file else it will return null
1401     public static Optional<String> getNetworkRoleFromSubInterfaceId(Resource resource,
1402                                                                            TranslationContext translationContext) {
1403         Optional<String> networkRole = Optional.empty();
1404         Optional<String> nestedHeatFileName = HeatToToscaUtil.getNestedHeatFileName(resource);
1405
1406         if (!nestedHeatFileName.isPresent()) {
1407             return networkRole;
1408         }
1409
1410         HeatOrchestrationTemplate nestedHeatOrchestrationTemplate = new YamlUtil().yamlToObject(
1411                 translationContext.getFileContent(nestedHeatFileName.get()), HeatOrchestrationTemplate.class);
1412
1413         if (MapUtils.isNotEmpty(nestedHeatOrchestrationTemplate.getResources())) {
1414             ContrailV2VirtualMachineInterfaceHelper contrailV2VirtualMachineInterfaceHelper =
1415                     new ContrailV2VirtualMachineInterfaceHelper();
1416             Optional<Map.Entry<String, Resource>> vlanSubInterfaceResource =
1417                     nestedHeatOrchestrationTemplate.getResources().entrySet().stream()
1418                                                    .filter(resourceEntry -> contrailV2VirtualMachineInterfaceHelper
1419                                                                                     .isVlanSubInterfaceResource(
1420                                                                                             resourceEntry.getValue()))
1421                                                    .findFirst();
1422             if (vlanSubInterfaceResource.isPresent()) {
1423                 Map.Entry<String, Resource> vlanSubInterfaceResourceEntry = vlanSubInterfaceResource.get();
1424                 networkRole = extractNetworkRoleFromSubInterfaceId(vlanSubInterfaceResourceEntry.getKey(),
1425                         vlanSubInterfaceResourceEntry.getValue().getType());
1426             }
1427         }
1428         return networkRole;
1429     }
1430
1431 }