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