2 * Copyright © 2016-2017 European Support Limited
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.openecomp.sdc.translator.services.heattotosca;
19 import org.apache.commons.collections4.CollectionUtils;
20 import org.apache.commons.collections4.MapUtils;
21 import org.apache.commons.io.FilenameUtils;
22 import org.openecomp.core.translator.api.HeatToToscaTranslator;
23 import org.openecomp.core.translator.datatypes.TranslatorOutput;
24 import org.openecomp.core.translator.factory.HeatToToscaTranslatorFactory;
25 import org.openecomp.core.utilities.file.FileContentHandler;
26 import org.openecomp.core.utilities.file.FileUtils;
27 import org.openecomp.core.utilities.orchestration.OnboardingTypesEnum;
28 import org.openecomp.core.validation.util.MessageContainerUtil;
29 import org.openecomp.sdc.common.errors.CoreException;
30 import org.openecomp.sdc.common.togglz.ToggleableFeature;
31 import org.openecomp.sdc.common.utils.SdcCommon;
32 import org.openecomp.sdc.datatypes.error.ErrorLevel;
33 import org.openecomp.sdc.datatypes.error.ErrorMessage;
34 import org.openecomp.sdc.heat.datatypes.HeatBoolean;
35 import org.openecomp.sdc.heat.datatypes.manifest.FileData;
36 import org.openecomp.sdc.heat.datatypes.model.HeatOrchestrationTemplate;
37 import org.openecomp.sdc.heat.datatypes.model.HeatResourcesTypes;
38 import org.openecomp.sdc.heat.datatypes.model.Resource;
39 import org.openecomp.sdc.heat.datatypes.structure.HeatStructureTree;
40 import org.openecomp.sdc.heat.datatypes.structure.ValidationStructureList;
41 import org.openecomp.sdc.heat.services.HeatConstants;
42 import org.openecomp.sdc.heat.services.tree.HeatTreeManager;
43 import org.openecomp.sdc.heat.services.tree.HeatTreeManagerUtil;
44 import org.openecomp.sdc.logging.api.Logger;
45 import org.openecomp.sdc.logging.api.LoggerFactory;
46 import org.openecomp.sdc.tosca.datatypes.ToscaCapabilityType;
47 import org.openecomp.sdc.tosca.datatypes.ToscaElementTypes;
48 import org.openecomp.sdc.tosca.datatypes.ToscaNodeType;
49 import org.openecomp.sdc.tosca.datatypes.ToscaRelationshipType;
50 import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel;
51 import org.openecomp.sdc.tosca.datatypes.model.AttributeDefinition;
52 import org.openecomp.sdc.tosca.datatypes.model.CapabilityDefinition;
53 import org.openecomp.sdc.tosca.datatypes.model.Import;
54 import org.openecomp.sdc.tosca.datatypes.model.NodeTemplate;
55 import org.openecomp.sdc.tosca.datatypes.model.NodeType;
56 import org.openecomp.sdc.tosca.datatypes.model.ParameterDefinition;
57 import org.openecomp.sdc.tosca.datatypes.model.PropertyDefinition;
58 import org.openecomp.sdc.tosca.datatypes.model.PropertyType;
59 import org.openecomp.sdc.tosca.datatypes.model.RequirementAssignment;
60 import org.openecomp.sdc.tosca.datatypes.model.RequirementDefinition;
61 import org.openecomp.sdc.tosca.datatypes.model.ServiceTemplate;
62 import org.openecomp.sdc.tosca.datatypes.model.Template;
63 import org.openecomp.sdc.tosca.datatypes.model.TopologyTemplate;
64 import org.openecomp.sdc.tosca.services.DataModelUtil;
65 import org.openecomp.sdc.tosca.services.ToscaAnalyzerService;
66 import org.openecomp.sdc.tosca.services.ToscaConstants;
67 import org.openecomp.sdc.tosca.services.ToscaExtensionYamlUtil;
68 import org.openecomp.sdc.tosca.services.ToscaUtil;
69 import org.openecomp.sdc.tosca.services.YamlUtil;
70 import org.openecomp.sdc.tosca.services.impl.ToscaAnalyzerServiceImpl;
71 import org.openecomp.sdc.translator.datatypes.heattotosca.AttachedPropertyVal;
72 import org.openecomp.sdc.translator.datatypes.heattotosca.AttachedResourceId;
73 import org.openecomp.sdc.translator.datatypes.heattotosca.ReferenceType;
74 import org.openecomp.sdc.translator.datatypes.heattotosca.TranslationContext;
75 import org.openecomp.sdc.translator.datatypes.heattotosca.to.FileDataCollection;
76 import org.openecomp.sdc.translator.datatypes.heattotosca.to.TranslateTo;
77 import org.openecomp.sdc.translator.services.heattotosca.errors.ResourceNotFoundInHeatFileErrorBuilder;
78 import org.openecomp.sdc.translator.services.heattotosca.globaltypes.GlobalTypesGenerator;
79 import org.openecomp.sdc.translator.services.heattotosca.helper.FunctionTranslationHelper;
80 import org.openecomp.sdc.translator.services.heattotosca.mapping.TranslatorHeatToToscaPropertyConverter;
82 import java.io.IOException;
83 import java.io.InputStream;
84 import java.util.ArrayList;
85 import java.util.Collection;
86 import java.util.HashMap;
87 import java.util.HashSet;
88 import java.util.List;
90 import java.util.Objects;
91 import java.util.Optional;
93 import java.util.stream.Collectors;
96 * The type Heat to tosca util.
98 public class HeatToToscaUtil {
100 private static final Logger LOGGER = LoggerFactory.getLogger(HeatToToscaUtil.class);
101 public static final String FQ_NAME = "fq_name";
102 public static final String GET_PARAM = "get_param";
103 private static final String FORWARDER = "forwarder";
104 private static final String GET_ATTR = "get_attr";
105 private static final String GET_RESOURCE = "get_resource";
108 * Load and translate template data translator output.
110 * @param fileNameContentMap the file name content map
111 * @return the translator output
113 public static TranslatorOutput loadAndTranslateTemplateData(
114 FileContentHandler fileNameContentMap) {
115 HeatToToscaTranslator heatToToscaTranslator =
116 HeatToToscaTranslatorFactory.getInstance().createInterface();
118 try (InputStream fileContent = fileNameContentMap.getFileContent(SdcCommon.MANIFEST_NAME)) {
119 heatToToscaTranslator.addManifest(SdcCommon.MANIFEST_NAME, FileUtils.toByteArray(fileContent));
120 } catch (IOException e) {
121 throw new RuntimeException("Failed to read manifest", e);
124 fileNameContentMap.getFileList().stream()
125 .filter(fileName -> !(fileName.equals(SdcCommon.MANIFEST_NAME))).forEach(
126 fileName -> heatToToscaTranslator
127 .addFile(fileName, FileUtils.toByteArray
128 (fileNameContentMap.getFileContent(fileName))));
130 Map<String, List<ErrorMessage>> errors = heatToToscaTranslator.validate();
131 if (MapUtils.isNotEmpty(MessageContainerUtil.getMessageByLevel(ErrorLevel.ERROR, errors))) {
132 TranslatorOutput translatorOutput = new TranslatorOutput();
133 translatorOutput.setErrorMessages(errors);
134 return translatorOutput;
137 try (InputStream structureFile = getHeatStructureTreeFile(fileNameContentMap)) {
138 heatToToscaTranslator.addExternalArtifacts(SdcCommon.HEAT_META, structureFile);
139 return heatToToscaTranslator.translate();
140 } catch (IOException e) {
141 // rethrow as a RuntimeException to keep the signature backward compatible
142 throw new RuntimeException("Failed to read Heat template tree", e);
147 private static InputStream getHeatStructureTreeFile(FileContentHandler fileNameContentMap) {
148 HeatTreeManager heatTreeManager = HeatTreeManagerUtil.initHeatTreeManager(fileNameContentMap);
149 heatTreeManager.createTree();
150 HeatStructureTree tree = heatTreeManager.getTree();
151 ValidationStructureList validationStructureList = new ValidationStructureList(tree);
152 return FileUtils.convertToInputStream(validationStructureList, FileUtils.FileExtension.JSON);
156 * Build list of files to search optional.
158 * @param heatFileName the heat file name
159 * @param filesDataList the files data list
160 * @param types the types
161 * @return the optional
163 public static Optional<List<FileData>> buildListOfFilesToSearch(String heatFileName,
164 List<FileData> filesDataList,
165 FileData.Type... types) {
166 List<FileData> list = new ArrayList<>(filesDataList);
167 Optional<FileData> resourceFileData = HeatToToscaUtil.getFileData(heatFileName, filesDataList);
168 if (resourceFileData.isPresent() && Objects.nonNull(resourceFileData.get().getData())) {
169 list.addAll(resourceFileData.get().getData());
171 return Optional.ofNullable(HeatToToscaUtil.getFilteredListOfFileDataByTypes(list, types));
175 * Gets filtered list of file data by types.
177 * @param filesToSearch the files to search
178 * @param types the types
179 * @return the filtered list of file data by types
181 public static List<FileData> getFilteredListOfFileDataByTypes(List<FileData> filesToSearch,
182 FileData.Type... types) {
183 return filesToSearch.stream().filter(FileData.buildFileDataPredicateByType(types))
184 .collect(Collectors.toList());
188 * Gets file data from the list according to the input heat file name.
190 * @param heatFileName the heat file name
191 * @param fileDataList the file data list
192 * @return the file data
194 public static Optional<FileData> getFileData(String heatFileName,
195 Collection<FileData> fileDataList) {
196 for (FileData file : fileDataList) {
197 if (file.getFile().equals(heatFileName)) {
198 return Optional.of(file);
201 return Optional.empty();
205 * Gets file data which is supported by the translator, from the context according the input heat
208 * @param heatFileName the heat file name
209 * @param context the translation context
210 * @return the file data
212 public static FileData getFileData(String heatFileName, TranslationContext context) {
214 List<FileData> fileDataList = context.getManifest().getContent().getData();
215 for (FileData fileData : fileDataList) {
216 if (TranslationService.getTypesToProcessByTranslator().contains(fileData.getType())
217 && fileData.getFile().equals(heatFileName)) {
224 static FileDataCollection getFileCollectionsByFilter(List<FileData> fileDataList,
225 Set<FileData.Type> typeFilter,
226 TranslationContext translationContext) {
227 FileDataCollection fileDataCollection = new FileDataCollection();
228 Map<String, FileData> filteredFiles = filterFileDataListByType(fileDataList, typeFilter);
229 Set<String> referenced = new HashSet<>();
231 for (FileData fileData : filteredFiles.values()) {
232 String fileName = fileData.getFile();
234 if (FileData.isHeatFile(fileData.getType())) {
235 if (fileData.getBase() != null && fileData.getBase()) {
236 fileDataCollection.addBaseFiles(fileData);
238 HeatOrchestrationTemplate heatOrchestrationTemplate = new YamlUtil()
239 .yamlToObject(translationContext.getFileContent(fileName),
240 HeatOrchestrationTemplate.class);
241 if (MapUtils.isNotEmpty(heatOrchestrationTemplate.getResources())) {
242 applyFilterOnFileCollection(heatOrchestrationTemplate, translationContext,
243 fileDataCollection, filteredFiles, referenced);
247 fileDataCollection.addArtifactFiles(fileData);
248 filteredFiles.remove(fileData.getFile());
252 referenced.forEach(filteredFiles::remove);
253 if (!CollectionUtils.isEmpty(fileDataCollection.getBaseFile())) {
254 for (FileData fileData : fileDataCollection.getBaseFile()) {
255 filteredFiles.remove(fileData.getFile());
258 fileDataCollection.setAddOnFiles(filteredFiles.values());
259 return fileDataCollection;
262 private static void applyFilterOnFileCollection(
263 HeatOrchestrationTemplate heatOrchestrationTemplate,
264 TranslationContext translationContext,
265 FileDataCollection fileDataCollection, Map<String, FileData> filteredFiles,
266 Set<String> referenced) {
267 List<String> filenames = extractFilenamesFromFileDataList(filteredFiles.values());
269 for (Resource resource : heatOrchestrationTemplate.getResources().values()) {
270 if (filenames.contains(resource.getType())) {
271 handleNestedFile(translationContext, fileDataCollection, filteredFiles, referenced,
273 } else if (resource.getType()
274 .equals(HeatResourcesTypes.RESOURCE_GROUP_RESOURCE_TYPE.getHeatResource())) {
275 handleResourceGrpNestedFile(resource, translationContext, fileDataCollection,
276 filteredFiles, filenames, referenced);
281 private static void handleResourceGrpNestedFile(Resource resource,
282 TranslationContext translationContext,
283 FileDataCollection fileDataCollection,
284 Map<String, FileData> filteredFiles,
285 List<String> filenames,
286 Set<String> referenced) {
287 Object resourceDef = resource.getProperties().get(HeatConstants.RESOURCE_DEF_PROPERTY_NAME);
288 Object innerTypeDef = ((Map) resourceDef).get("type");
289 if (innerTypeDef instanceof String) {
290 String internalResourceType = (String) innerTypeDef;
291 if (filenames.contains(internalResourceType)) {
292 handleNestedFile(translationContext, fileDataCollection, filteredFiles,
293 referenced, internalResourceType);
298 private static void handleNestedFile(TranslationContext translationContext,
299 FileDataCollection fileDataCollection,
300 Map<String, FileData> filteredFiles, Set<String> referenced,
301 String nestedFileName) {
302 referenced.add(nestedFileName);
303 fileDataCollection.addNestedFiles(filteredFiles.get(nestedFileName));
304 translationContext.getNestedHeatsFiles().add(nestedFileName);
307 private static Map<String, FileData> filterFileDataListByType(List<FileData> fileDataList,
308 Set<FileData.Type> typesToGet) {
309 Map<String, FileData> filtered = new HashMap<>();
310 fileDataList.stream().filter(file -> typesToGet.contains(file.getType()))
311 .forEach(file -> filtered.put(file.getFile(), file));
315 private static List<String> extractFilenamesFromFileDataList(Collection<FileData> fileDataList) {
316 return fileDataList.stream().map(FileData::getFile).collect(Collectors.toList());
320 * Extract attached resource id optional.
322 * @param translateTo the translate to
323 * @param propertyName the property name
324 * @return the optional
326 public static Optional<AttachedResourceId> extractAttachedResourceId(TranslateTo translateTo,
327 String propertyName) {
328 Object propertyValue = translateTo.getResource().getProperties().get(propertyName);
329 if (propertyValue == null) {
330 return Optional.empty();
332 return extractAttachedResourceId(translateTo.getHeatFileName(),
333 translateTo.getHeatOrchestrationTemplate(), translateTo.getContext(), propertyValue);
337 * Extract attached resource id optional.
339 * @param heatFileName the heat file name
340 * @param heatOrchestrationTemplate the heat orchestration template
341 * @param context the context
342 * @param propertyValue the property value
343 * @return the optional
345 public static Optional<AttachedResourceId> extractAttachedResourceId(
347 HeatOrchestrationTemplate heatOrchestrationTemplate,
348 TranslationContext context,
349 Object propertyValue) {
354 if (Objects.isNull(propertyValue)) {
355 return Optional.empty();
358 ReferenceType referenceType = ReferenceType.OTHER;
359 if (propertyValue instanceof Map && !((Map) propertyValue).isEmpty()) {
360 Map<String, Object> propMap = (Map) propertyValue;
361 Map.Entry<String, Object> entry = propMap.entrySet().iterator().next();
362 entity = entry.getValue();
363 String key = entry.getKey();
364 referenceType = getReferenceTypeFromAttachedResouce(key);
366 if (!FunctionTranslationFactory.getInstance(entry.getKey()).isPresent()) {
369 translatedId = FunctionTranslationFactory.getInstance(entry.getKey()).get()
370 .translateFunction(null, null, null, entry.getKey(), entry.getValue(), heatFileName,
371 heatOrchestrationTemplate, null, context);
373 if (translatedId instanceof String
374 && !FunctionTranslationHelper.isResourceSupported((String) translatedId)) {
379 translatedId = propertyValue;
380 entity = propertyValue;
383 return Optional.of(new AttachedResourceId(translatedId, entity, referenceType));
386 private static ReferenceType getReferenceTypeFromAttachedResouce(String key) {
387 ReferenceType referenceType;
390 referenceType = ReferenceType.GET_RESOURCE;
393 referenceType = ReferenceType.GET_PARAM;
396 referenceType = ReferenceType.GET_ATTR;
399 referenceType = ReferenceType.OTHER;
403 return referenceType;
407 * Gets contrail attached heat resource id.
409 * @param attachedResource the attached resource
410 * @return the contrail attached heat resource id
412 public static Optional<String> getContrailAttachedHeatResourceId(
413 AttachedResourceId attachedResource) {
414 if (attachedResource == null) {
415 return Optional.empty();
418 if (attachedResource.isGetResource()) {
419 return Optional.of((String) attachedResource.getEntityId());
422 if (attachedResource.isGetAttr()) {
423 return getResourceId(attachedResource.getEntityId());
425 return Optional.empty();
429 * Extract property optional.
431 * @param propertyValue the property value
432 * @return the optional
434 public static Optional<AttachedPropertyVal> extractProperty(Object propertyValue) {
435 Object attachedPropertyVal;
436 if (Objects.isNull(propertyValue)) {
437 return Optional.empty();
440 ReferenceType referenceType = ReferenceType.OTHER;
441 if (propertyValue instanceof Map && !((Map) propertyValue).isEmpty()) {
442 Map<String, Object> propMap = (Map) propertyValue;
443 Map.Entry<String, Object> entry = propMap.entrySet().iterator().next();
444 attachedPropertyVal = entry.getValue();
445 String key = entry.getKey();
448 referenceType = ReferenceType.GET_RESOURCE;
451 referenceType = ReferenceType.GET_PARAM;
454 referenceType = ReferenceType.GET_ATTR;
461 attachedPropertyVal = propertyValue;
463 return Optional.of(new AttachedPropertyVal(attachedPropertyVal, referenceType));
469 * @param nodeTemplate the node template
470 * @param propertyKey the property key
472 public static void mapBoolean(NodeTemplate nodeTemplate, String propertyKey) {
473 Object value = nodeTemplate.getProperties().get(propertyKey);
474 if (value != null && !(value instanceof Map)) {
475 nodeTemplate.getProperties().put(propertyKey, HeatBoolean.eval(value));
482 * @param nodeTemplate the node template
483 * @param propertyListKey the property list key
485 public static void mapBooleanList(NodeTemplate nodeTemplate, String propertyListKey) {
486 Object listValue = nodeTemplate.getProperties().get(propertyListKey);
487 if (listValue instanceof List) {
488 List booleanList = (List) listValue;
489 for (int i = 0; i < booleanList.size(); i++) {
490 Object value = booleanList.get(i);
491 if (value != null && !(value instanceof Map)) {
492 booleanList.set(i, HeatBoolean.eval(value));
500 * Is yml file type boolean.
502 * @param filename the filename
503 * @return the boolean
505 public static boolean isYmlFileType(String filename) {
506 String extension = FilenameUtils.getExtension(filename);
507 return "yaml".equalsIgnoreCase(extension)
508 || "yml".equalsIgnoreCase(extension);
512 * Is nested resource boolean.
514 * @param resource the resource
515 * @return the boolean
517 public static boolean isNestedResource(Resource resource) {
518 String resourceType = resource.getType();
520 if (resourceType.equals(HeatResourcesTypes.RESOURCE_GROUP_RESOURCE_TYPE.getHeatResource())) {
521 Object resourceDef = resource.getProperties().get(HeatConstants.RESOURCE_DEF_PROPERTY_NAME);
522 if (!(((Map) resourceDef).get("type") instanceof String)) {
523 //currently only resource group which is poinitng to nested heat file is supported
524 //dynamic type is currently not supported
527 String internalResourceType = (String) ((Map) resourceDef).get("type");
528 if (isYamlFile(internalResourceType)) {
531 } else if (isYamlFile(resourceType)) {
538 * Checks if the nested resource represents a VFC or a complex VFC (Heat file should contain at
539 * least one or more compute nodes).
541 * @param resource the resource
542 * @param context the context
543 * @return true if the resource represents a VFC and false otherwise.
545 public static boolean isNestedVfcResource(Resource resource, TranslationContext context) {
546 Optional<String> nestedHeatFileName = HeatToToscaUtil.getNestedHeatFileName(resource);
547 HeatOrchestrationTemplate nestedHeatOrchestrationTemplate = new YamlUtil()
548 .yamlToObject(context.getFileContent(nestedHeatFileName.get()),
549 HeatOrchestrationTemplate.class);
550 if (Objects.nonNull(nestedHeatOrchestrationTemplate.getResources())) {
551 for (String innerResourceId : nestedHeatOrchestrationTemplate.getResources().keySet()) {
552 if (ConsolidationDataUtil
553 .isComputeResource(nestedHeatOrchestrationTemplate, innerResourceId)) {
562 * Get nested heat file name in case of nested resource.
564 * @param resource the resource
565 * @return the nested heat file name
567 public static Optional<String> getNestedHeatFileName(Resource resource) {
568 if (!isNestedResource(resource)) {
569 return Optional.empty();
572 String resourceType = resource.getType();
574 if (resourceType.equals(HeatResourcesTypes.RESOURCE_GROUP_RESOURCE_TYPE.getHeatResource())) {
575 Object resourceDef = resource.getProperties().get(HeatConstants.RESOURCE_DEF_PROPERTY_NAME);
576 String internalResourceType = (String) ((Map) resourceDef).get("type");
577 return Optional.of(internalResourceType);
579 return Optional.of(resourceType);
585 * @param resource the resource
586 * @return the nested file
588 public static Optional<String> getNestedFile(Resource resource) {
589 if (!isNestedResource(resource)) {
590 return Optional.empty();
592 String resourceType = resource.getType();
593 if (resourceType.equals(HeatResourcesTypes.RESOURCE_GROUP_RESOURCE_TYPE.getHeatResource())) {
594 Object resourceDef = resource.getProperties().get(HeatConstants.RESOURCE_DEF_PROPERTY_NAME);
595 String internalResourceType = (String) ((Map) resourceDef).get("type");
596 return Optional.of(internalResourceType);
598 return Optional.of(resourceType);
602 private static boolean isYamlFile(String fileName) {
603 return fileName.endsWith(".yaml") || fileName.endsWith(".yml");
609 * @param heatOrchestrationTemplate the heat orchestration template
610 * @param resourceId the resource id
611 * @param heatFileName the heat file name
612 * @return the resource
614 public static Resource getResource(HeatOrchestrationTemplate heatOrchestrationTemplate,
615 String resourceId, String heatFileName) {
616 Resource resource = heatOrchestrationTemplate.getResources().get(resourceId);
617 if (resource == null) {
618 throw new CoreException(
619 new ResourceNotFoundInHeatFileErrorBuilder(resourceId, heatFileName).build());
628 * @param resourceId the resource id
629 * @param heatOrchestrationTemplate heat orchestration template
630 * @param heatFileName heat file name
631 * @return resource type
633 public static String getResourceType(String resourceId,
634 HeatOrchestrationTemplate heatOrchestrationTemplate,
635 String heatFileName) {
636 return HeatToToscaUtil.getResource(heatOrchestrationTemplate, resourceId, heatFileName)
641 * Is heat file nested boolean.
643 * @param translateTo the translate to
644 * @param heatFileName the heat file name
645 * @return the boolean
647 public static boolean isHeatFileNested(TranslateTo translateTo, String heatFileName) {
648 return translateTo.getContext().getNestedHeatsFiles().contains(heatFileName);
652 * Extract contrail get resource attached heat resource id optional.
654 * @param propertyValue the property value
655 * @return the optional
657 public static Optional<String> extractContrailGetResourceAttachedHeatResourceId(
658 Object propertyValue) {
659 if (propertyValue instanceof Map) {
660 if (((Map) propertyValue).containsKey(GET_ATTR)) {
661 return getResourceId(((Map) propertyValue).get(GET_ATTR));
662 } else if (((Map) propertyValue).containsKey(GET_RESOURCE)) {
663 return getHeatResourceIdFromResource((Map) propertyValue);
665 Collection valCollection = ((Map) propertyValue).values();
666 return evaluateHeatResourceId(valCollection);
668 } else if (propertyValue instanceof List) {
669 return evaluateHeatResourceId((List) propertyValue);
671 return Optional.empty();
674 private static Optional<String> getResourceId(Object data) {
675 if (data instanceof List && CollectionUtils.size(data) > 1
676 && FQ_NAME.equals(((List) data).get(1))
677 && ((List) data).get(0) instanceof String) {
678 return Optional.of((String) ((List) data).get(0));
680 LOGGER.warn("invalid format of 'get_attr' function - " + data.toString());
681 return Optional.empty();
685 private static Optional<String> getHeatResourceIdFromResource(Map propertyValue) {
686 Object value = propertyValue.get(GET_RESOURCE);
687 if (value instanceof String) {
688 return Optional.of((String) value);
690 LOGGER.warn("invalid format of 'get_resource' function - " + propertyValue.toString());
691 return Optional.empty();
695 private static Optional<String> evaluateHeatResourceId(Collection propertyValue) {
696 for (Object prop : propertyValue) {
697 Optional<String> ret = extractContrailGetResourceAttachedHeatResourceId(prop);
698 if (ret.isPresent()) {
702 return Optional.empty();
705 * Gets tosca service model.
707 * @param context translation context
708 * @return the tosca service model
710 public static ToscaServiceModel getToscaServiceModel(TranslationContext context) {
711 Map<String, String> metadata = new HashMap<>();
712 metadata.put(ToscaConstants.ST_METADATA_TEMPLATE_NAME, Constants.MAIN_TEMPLATE_NAME);
713 return getToscaServiceModel(context, metadata);
717 * Gets tosca service model.
719 * @param context translation context
720 * @param entryDefinitionMetadata template name of the entry definition servie template
721 * @return the tosca service model
723 public static ToscaServiceModel getToscaServiceModel(
724 TranslationContext context,
725 Map<String, String> entryDefinitionMetadata) {
726 Map<String, ServiceTemplate> serviceTemplates =
727 new HashMap<>(context.getGlobalServiceTemplates());
728 Collection<ServiceTemplate> tmpServiceTemplates =
729 context.getTranslatedServiceTemplates().values();
730 for (ServiceTemplate serviceTemplate : tmpServiceTemplates) {
731 ToscaUtil.addServiceTemplateToMapWithKeyFileName(serviceTemplates, serviceTemplate);
733 return new ToscaServiceModel(null, serviceTemplates,
734 ToscaUtil.getServiceTemplateFileName(entryDefinitionMetadata));
738 * Gets service template from context.
740 * @param serviceTemplateFileName the service template file name
741 * @param context the context
742 * @return the service template from context
744 public static Optional<ServiceTemplate> getServiceTemplateFromContext(
745 String serviceTemplateFileName, TranslationContext context) {
746 for (ServiceTemplate serviceTemplate : context.getTranslatedServiceTemplates().values()) {
747 if (ToscaUtil.getServiceTemplateFileName(serviceTemplate).equals(serviceTemplateFileName)) {
748 return Optional.of(serviceTemplate);
751 return Optional.empty();
755 * Adding link requerment from port node template to network node template.
757 * @param portNodeTemplate port node template
758 * @param networkTranslatedId network node template id
760 public static RequirementAssignment addLinkReqFromPortToNetwork(NodeTemplate portNodeTemplate,
761 String networkTranslatedId) {
762 RequirementAssignment requirement = new RequirementAssignment();
763 requirement.setCapability(ToscaCapabilityType.NATIVE_NETWORK_LINKABLE);
764 requirement.setRelationship(ToscaRelationshipType.NATIVE_NETWORK_LINK_TO);
765 requirement.setNode(networkTranslatedId);
766 DataModelUtil.addRequirementAssignment(portNodeTemplate,
767 ToscaConstants.LINK_REQUIREMENT_ID, requirement);
772 * Adding binding requerment from sub interface node template to interface (port) node template.
774 * @param subInterfaceNodeTemplate sub interface template
775 * @param interfaceTranslatedId interface node template id
777 public static void addBindingReqFromSubInterfaceToInterface(
778 NodeTemplate subInterfaceNodeTemplate, String interfaceTranslatedId) {
779 RequirementAssignment requirement = new RequirementAssignment();
780 requirement.setCapability(ToscaCapabilityType.NATIVE_NETWORK_BINDABLE);
781 requirement.setRelationship(ToscaRelationshipType.NATIVE_NETWORK_BINDS_TO);
782 requirement.setNode(interfaceTranslatedId);
784 .addRequirementAssignment(subInterfaceNodeTemplate,
785 ToscaConstants.BINDING_REQUIREMENT_ID, requirement);
789 * Get property Parameter Name Value.
791 * @param property property
792 * @return Parameter name in case the property include "get_param" function
794 public static Optional<String> getPropertyParameterNameValue(Object property) {
795 if (Objects.isNull(property)) {
796 return Optional.empty();
798 Optional<AttachedPropertyVal> extractedProperty = extractProperty(property);
799 return extractedProperty.flatMap(HeatToToscaUtil::getParameterName);
802 private static Optional<String> getParameterName(AttachedPropertyVal extractedProperty) {
803 if (!extractedProperty.isGetParam()) {
804 return Optional.empty();
806 Object getParamFuncValue = extractedProperty.getPropertyValue();
807 if (getParamFuncValue instanceof String) {
808 return Optional.of((String) getParamFuncValue);
810 return Optional.of((String) ((List) getParamFuncValue).get(0));
814 public static String getToscaPropertyName(TranslationContext context, String heatResourceType,
815 String heatPropertyName) {
816 return context.getElementMapping(heatResourceType, Constants.PROP, heatPropertyName);
820 * Gets tosca property name.
822 * @param translateTo the translate to
823 * @param heatPropertyName the heat property name
824 * @return the tosca property name
826 public static String getToscaPropertyName(TranslateTo translateTo, String heatPropertyName) {
827 return translateTo.getContext()
828 .getElementMapping(translateTo.getResource().getType(), Constants.PROP, heatPropertyName);
832 * Gets tosca attribute name.
834 * @param context the context
835 * @param heatResourceType the heat resource type
836 * @param heatAttrName the heat attr name
837 * @return the tosca attribute name
839 public static String getToscaAttributeName(TranslationContext context, String heatResourceType,
840 String heatAttrName) {
841 return context.getElementMapping(heatResourceType, Constants.ATTR, heatAttrName);
845 * Gets tosca attribute name.
847 * @param translateTo the translate to
848 * @param heatAttrName the heat attr name
849 * @return the tosca attribute name
851 public static String getToscaAttributeName(TranslateTo translateTo, String heatAttrName) {
852 return translateTo.getContext()
853 .getElementMapping(translateTo.getResource().getType(), Constants.ATTR, heatAttrName);
857 * Create init substitution service template service template.
859 * @param templateName the template name
860 * @return the service template
862 public static ServiceTemplate createInitSubstitutionServiceTemplate(String templateName) {
863 ServiceTemplate nestedSubstitutionServiceTemplate = new ServiceTemplate();
864 Map<String, String> templateMetadata = new HashMap<>();
865 templateMetadata.put(ToscaConstants.ST_METADATA_TEMPLATE_NAME, templateName);
866 nestedSubstitutionServiceTemplate.setMetadata(templateMetadata);
867 nestedSubstitutionServiceTemplate
868 .setTosca_definitions_version(ToscaConstants.TOSCA_DEFINITIONS_VERSION);
869 nestedSubstitutionServiceTemplate.setTopology_template(new TopologyTemplate());
870 List<Map<String, Import>> globalTypesImportList =
871 GlobalTypesGenerator.getGlobalTypesImportList();
872 globalTypesImportList.addAll(
873 HeatToToscaUtil.createImportList(Constants.GLOBAL_SUBSTITUTION_TYPES_TEMPLATE_NAME));
874 nestedSubstitutionServiceTemplate.setImports(globalTypesImportList);
875 return nestedSubstitutionServiceTemplate;
879 * Create init global substitution service template service template.
881 * @return the service template
883 public static ServiceTemplate createInitGlobalSubstitutionServiceTemplate() {
884 ServiceTemplate globalSubstitutionServiceTemplate = new ServiceTemplate();
885 Map<String, String> templateMetadata = new HashMap<>();
886 templateMetadata.put(ToscaConstants.ST_METADATA_TEMPLATE_NAME,
887 Constants.GLOBAL_SUBSTITUTION_TYPES_TEMPLATE_NAME);
888 globalSubstitutionServiceTemplate.setMetadata(templateMetadata);
889 globalSubstitutionServiceTemplate
890 .setImports(GlobalTypesGenerator.getGlobalTypesImportList());
891 globalSubstitutionServiceTemplate
892 .setTosca_definitions_version(ToscaConstants.TOSCA_DEFINITIONS_VERSION);
893 return globalSubstitutionServiceTemplate;
897 * Create substitution node type node type.
899 * @param substitutionServiceTemplate the substitution service template
900 * @return the node type
902 public NodeType createSubstitutionNodeType(ServiceTemplate substitutionServiceTemplate) {
903 NodeType substitutionNodeType = new NodeType();
904 substitutionNodeType.setDerived_from(ToscaNodeType.ABSTRACT_SUBSTITUTE);
905 substitutionNodeType.setDescription(substitutionServiceTemplate.getDescription());
907 .setProperties(manageSubstitutionNodeTypeProperties(substitutionServiceTemplate));
909 .setAttributes(manageSubstitutionNodeTypeAttributes(substitutionServiceTemplate));
910 return substitutionNodeType;
913 private Map<String, PropertyDefinition> manageSubstitutionNodeTypeProperties(
914 ServiceTemplate substitutionServiceTemplate) {
915 Map<String, PropertyDefinition> substitutionNodeTypeProperties = new HashMap<>();
916 Map<String, ParameterDefinition> properties =
917 substitutionServiceTemplate.getTopology_template().getInputs();
918 if (properties == null) {
922 PropertyDefinition propertyDefinition;
923 String toscaPropertyName;
924 for (Map.Entry<String, ParameterDefinition> entry : properties.entrySet()) {
925 toscaPropertyName = entry.getKey();
926 propertyDefinition = new PropertyDefinition();
927 ParameterDefinition parameterDefinition =
928 substitutionServiceTemplate.getTopology_template().getInputs().get(toscaPropertyName);
929 propertyDefinition.setType(parameterDefinition.getType());
930 propertyDefinition.setDescription(parameterDefinition.getDescription());
931 propertyDefinition.setRequired(parameterDefinition.getRequired());
932 propertyDefinition.set_default(parameterDefinition.get_default());
933 propertyDefinition.setConstraints(parameterDefinition.getConstraints());
934 propertyDefinition.setEntry_schema(parameterDefinition.getEntry_schema());
935 propertyDefinition.setStatus(parameterDefinition.getStatus());
936 substitutionNodeTypeProperties.put(toscaPropertyName, propertyDefinition);
938 return substitutionNodeTypeProperties;
941 private Map<String, AttributeDefinition> manageSubstitutionNodeTypeAttributes(
942 ServiceTemplate substitutionServiceTemplate) {
943 Map<String, AttributeDefinition> substitutionNodeTypeAttributes = new HashMap<>();
944 Map<String, ParameterDefinition> attributes =
945 substitutionServiceTemplate.getTopology_template().getOutputs();
946 if (attributes == null) {
949 AttributeDefinition attributeDefinition;
950 String toscaAttributeName;
952 for (Map.Entry<String, ParameterDefinition> entry : attributes.entrySet()) {
953 attributeDefinition = new AttributeDefinition();
954 toscaAttributeName = entry.getKey();
955 ParameterDefinition parameterDefinition =
956 substitutionServiceTemplate.getTopology_template().getOutputs().get(toscaAttributeName);
957 if (parameterDefinition.getType() != null && !parameterDefinition.getType().isEmpty()) {
958 attributeDefinition.setType(parameterDefinition.getType());
960 attributeDefinition.setType(PropertyType.STRING.getDisplayName());
962 attributeDefinition.setDescription(parameterDefinition.getDescription());
963 attributeDefinition.set_default(parameterDefinition.get_default());
964 attributeDefinition.setEntry_schema(parameterDefinition.getEntry_schema());
965 attributeDefinition.setStatus(parameterDefinition.getStatus());
966 substitutionNodeTypeAttributes.put(toscaAttributeName, attributeDefinition);
968 return substitutionNodeTypeAttributes;
973 * Create and add substitution mapping to the nested substitution service template, and update
974 * the subtitution node type accordingly with the exposed requerments and capabilities
976 * @param context the translation context
977 * @param substitutionNodeTypeKey the substitution node type key
978 * @param nestedSubstitutionServiceTemplate the nested substitution service template
979 * @param substitutionNodeType the substitution node type
981 public static void handleSubstitutionMapping(
982 TranslationContext context,
983 String substitutionNodeTypeKey,
984 ServiceTemplate nestedSubstitutionServiceTemplate,
985 NodeType substitutionNodeType) {
986 Map<String, Map<String, List<String>>> substitutionMapping =
987 getSubstitutionNodeTypeExposedConnectionPoints(substitutionNodeType,
988 nestedSubstitutionServiceTemplate, context);
989 //add substitution mapping after capability and requirement expose calculation
990 nestedSubstitutionServiceTemplate.getTopology_template().setSubstitution_mappings(
991 DataModelUtil.createSubstitutionTemplateSubMapping(substitutionNodeTypeKey,
992 substitutionNodeType, substitutionMapping));
996 * Gets node type with flat hierarchy.
998 * @param nodeTypeId the node type id
999 * @param serviceTemplate the service template
1000 * @param context the context
1001 * @return the node type with flat hierarchy
1003 public static NodeType getNodeTypeWithFlatHierarchy(String nodeTypeId,
1004 ServiceTemplate serviceTemplate,
1005 TranslationContext context) {
1006 ToscaAnalyzerService toscaAnalyzerService = new ToscaAnalyzerServiceImpl();
1007 ToscaServiceModel toscaServiceModel = HeatToToscaUtil
1008 .getToscaServiceModel(context, serviceTemplate.getMetadata());
1009 return (NodeType) toscaAnalyzerService
1010 .getFlatEntity(ToscaElementTypes.NODE_TYPE, nodeTypeId, serviceTemplate, toscaServiceModel);
1014 * Create substitution node template node template.
1016 * @param translateTo the translate to
1017 * @param templateName the template name
1018 * @param substitutionNodeTypeKey the substitution node type key
1019 * @return the node template
1021 public NodeTemplate createSubstitutionNodeTemplate(TranslateTo translateTo, String templateName,
1022 String substitutionNodeTypeKey) {
1023 NodeTemplate substitutionNodeTemplate = new NodeTemplate();
1024 List<String> directiveList = new ArrayList<>();
1025 directiveList.add(ToscaConstants.NODE_TEMPLATE_DIRECTIVE_SUBSTITUTABLE);
1026 substitutionNodeTemplate.setDirectives(directiveList);
1027 substitutionNodeTemplate.setType(substitutionNodeTypeKey);
1028 substitutionNodeTemplate.setProperties(
1029 managerSubstitutionNodeTemplateProperties(translateTo, substitutionNodeTemplate,
1031 return substitutionNodeTemplate;
1035 * Create abstract substitution node template.
1037 * @param translateTo the translate to
1038 * @param templateName the template name
1039 * @param substitutionNodeTypeKey the substitution node type key
1040 * @return the abstract substitute node template
1042 public static NodeTemplate createAbstractSubstitutionNodeTemplate(
1043 TranslateTo translateTo,
1044 String templateName,
1045 String substitutionNodeTypeKey) {
1046 NodeTemplate substitutionNodeTemplate = new NodeTemplate();
1047 List<String> directiveList = new ArrayList<>();
1048 directiveList.add(ToscaConstants.NODE_TEMPLATE_DIRECTIVE_SUBSTITUTABLE);
1049 substitutionNodeTemplate.setDirectives(directiveList);
1050 substitutionNodeTemplate.setType(substitutionNodeTypeKey);
1051 substitutionNodeTemplate.setProperties(
1052 managerSubstitutionNodeTemplateProperties(translateTo, substitutionNodeTemplate,
1054 return substitutionNodeTemplate;
1059 * Checks if the source and target resource is a valid candidate for adding tosca dependency
1062 * @param heatOrchestrationTemplate the heat orchestration template
1063 * @param sourceResource the source resource
1064 * @param targetResource the target resource
1065 * @param dependencyEntity the dependency entity
1066 * @return true if the candidate resources are a valid combination for the dependency relationship
1067 * and false otherwise
1069 public static boolean isValidDependsOnCandidate(HeatOrchestrationTemplate
1070 heatOrchestrationTemplate,
1071 Resource sourceResource,
1072 Resource targetResource,
1073 ConsolidationEntityType dependencyEntity,
1074 TranslationContext context) {
1076 .setEntityType(heatOrchestrationTemplate, sourceResource, targetResource, context);
1077 ConsolidationEntityType sourceEntityType = dependencyEntity.getSourceEntityType();
1078 ConsolidationEntityType targetEntityType = dependencyEntity.getTargetEntityType();
1080 return ConsolidationTypesConnectivity.isDependsOnRelationshipValid(sourceEntityType, targetEntityType);
1083 private static Map<String, Object> managerSubstitutionNodeTemplateProperties(
1084 TranslateTo translateTo,
1086 String templateName) {
1087 Map<String, Object> substitutionProperties = new HashMap<>();
1088 Map<String, Object> heatProperties = translateTo.getResource().getProperties();
1089 if (Objects.nonNull(heatProperties)) {
1090 for (Map.Entry<String, Object> entry : heatProperties.entrySet()) {
1091 Object property = TranslatorHeatToToscaPropertyConverter
1092 .getToscaPropertyValue(translateTo.getServiceTemplate(),
1093 translateTo.getTranslatedId(), entry.getKey(),
1094 entry.getValue(), null, translateTo.getHeatFileName(),
1095 translateTo.getHeatOrchestrationTemplate(), template, translateTo.getContext());
1096 substitutionProperties.put(entry.getKey(), property);
1099 return addAbstractSubstitutionProperty(templateName, substitutionProperties);
1102 private static Map<String, Object> addAbstractSubstitutionProperty(String templateName,
1104 substitutionProperties) {
1105 Map<String, Object> innerProps = new HashMap<>();
1106 innerProps.put(ToscaConstants.SUBSTITUTE_SERVICE_TEMPLATE_PROPERTY_NAME,
1107 ToscaUtil.getServiceTemplateFileName(templateName));
1108 substitutionProperties.put(ToscaConstants.SERVICE_TEMPLATE_FILTER_PROPERTY_NAME, innerProps);
1109 return substitutionProperties;
1112 private static Map<String, Map<String, List<String>>>
1113 getSubstitutionNodeTypeExposedConnectionPoints(NodeType substitutionNodeType,
1114 ServiceTemplate substitutionServiceTemplate,
1115 TranslationContext context) {
1116 Map<String, NodeTemplate> nodeTemplates =
1117 substitutionServiceTemplate.getTopology_template().getNode_templates();
1118 String nodeTemplateId;
1119 NodeTemplate nodeTemplate;
1121 Map<String, Map<String, List<String>>> substitutionMapping = new HashMap<>();
1122 if (nodeTemplates == null) {
1123 return substitutionMapping;
1126 Map<String, List<String>> capabilitySubstitutionMapping = new HashMap<>();
1127 Map<String, List<String>> requirementSubstitutionMapping = new HashMap<>();
1128 substitutionMapping.put("capability", capabilitySubstitutionMapping);
1129 substitutionMapping.put("requirement", requirementSubstitutionMapping);
1130 List<Map<String, RequirementDefinition>> nodeTypeRequirementsDefinition;
1131 Map<String, RequirementAssignment> nodeTemplateRequirementsAssignment;
1132 List<Map<String, RequirementDefinition>> exposedRequirementsDefinition;
1133 Map<String, Map<String, RequirementAssignment>> fullFilledRequirementsDefinition =
1135 Map<String, CapabilityDefinition> nodeTypeCapabilitiesDefinition = new HashMap<>();
1136 Map<String, CapabilityDefinition> exposedCapabilitiesDefinition;
1137 ToscaAnalyzerService toscaAnalyzerService = new ToscaAnalyzerServiceImpl();
1139 for (Map.Entry<String, NodeTemplate> entry : nodeTemplates.entrySet()) {
1140 nodeTemplateId = entry.getKey();
1141 nodeTemplate = entry.getValue();
1142 nodeType = nodeTemplate.getType();
1145 nodeTypeRequirementsDefinition =
1146 getNodeTypeReqs(nodeType, nodeTemplateId, substitutionServiceTemplate,
1147 requirementSubstitutionMapping, context);
1148 nodeTemplateRequirementsAssignment = DataModelUtil.getNodeTemplateRequirements(nodeTemplate);
1149 fullFilledRequirementsDefinition.put(nodeTemplateId, nodeTemplateRequirementsAssignment);
1150 //set substitution node type requirements
1151 exposedRequirementsDefinition =
1152 toscaAnalyzerService.calculateExposedRequirements(nodeTypeRequirementsDefinition,
1153 nodeTemplateRequirementsAssignment);
1155 .addSubstitutionNodeTypeRequirements(substitutionNodeType, exposedRequirementsDefinition,
1159 addNodeTypeCapabilitiesToSubMapping(nodeTypeCapabilitiesDefinition,
1160 capabilitySubstitutionMapping, nodeType,
1161 nodeTemplateId, substitutionServiceTemplate, context);
1164 exposedCapabilitiesDefinition =
1165 toscaAnalyzerService.calculateExposedCapabilities(nodeTypeCapabilitiesDefinition,
1166 fullFilledRequirementsDefinition);
1167 DataModelUtil.addNodeTypeCapabilitiesDef(substitutionNodeType, exposedCapabilitiesDefinition);
1168 return substitutionMapping;
1171 private static void addNodeTypeCapabilitiesToSubMapping(
1172 Map<String, CapabilityDefinition> nodeTypeCapabilitiesDefinition,
1173 Map<String, List<String>> capabilitySubstitutionMapping, String type, String templateName,
1174 ServiceTemplate serviceTemplate, TranslationContext context) {
1175 NodeType flatNodeType =
1176 getNodeTypeWithFlatHierarchy(type, serviceTemplate, context);
1178 if (flatNodeType.getCapabilities() != null) {
1179 flatNodeType.getCapabilities()
1182 .filter(capabilityNodeEntry -> shouldCapabilityNeedsToBeAdded(capabilityNodeEntry.getKey()))
1183 .forEach(capabilityNodeEntry ->
1184 addCapabilityToSubMapping(
1185 templateName, capabilityNodeEntry, nodeTypeCapabilitiesDefinition, capabilitySubstitutionMapping));
1189 private static boolean shouldCapabilityNeedsToBeAdded(String capabilityKey) {
1190 return !capabilityKey.contains(FORWARDER) || ToggleableFeature.FORWARDER_CAPABILITY.isActive();
1193 private static void addCapabilityToSubMapping(String templateName,
1194 Map.Entry<String, CapabilityDefinition> capabilityNodeEntry,
1195 Map<String, CapabilityDefinition> nodeTypeCapabilitiesDefinition,
1196 Map<String, List<String>> capabilitySubstitutionMapping) {
1197 String capabilityKey;
1198 List<String> capabilityMapping;
1199 capabilityKey = capabilityNodeEntry.getKey() + "_" + templateName;
1200 nodeTypeCapabilitiesDefinition.put(capabilityKey, capabilityNodeEntry.getValue().clone());
1201 capabilityMapping = new ArrayList<>();
1202 capabilityMapping.add(templateName);
1203 capabilityMapping.add(capabilityNodeEntry.getKey());
1204 capabilitySubstitutionMapping.put(capabilityKey, capabilityMapping);
1207 private static List<Map<String, RequirementDefinition>> getNodeTypeReqs(
1209 String templateName,
1210 ServiceTemplate serviceTemplate,
1211 Map<String, List<String>> requirementSubstitutionMapping,
1212 TranslationContext context) {
1213 List<Map<String, RequirementDefinition>> requirementList = new ArrayList<>();
1214 NodeType flatNodeType = getNodeTypeWithFlatHierarchy(type, serviceTemplate, context);
1215 List<String> requirementMapping;
1217 if (flatNodeType.getRequirements() == null) {
1218 return requirementList;
1221 for (Map<String, RequirementDefinition> requirementMap : flatNodeType.getRequirements()) {
1222 for (Map.Entry<String, RequirementDefinition> requirementNodeEntry : requirementMap
1224 ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil();
1225 RequirementDefinition requirementNodeEntryValue = toscaExtensionYamlUtil
1226 .yamlToObject(toscaExtensionYamlUtil.objectToYaml(requirementNodeEntry.getValue()),
1227 RequirementDefinition.class);
1228 if (Objects.isNull(requirementNodeEntryValue.getOccurrences())) {
1229 requirementNodeEntryValue.setOccurrences(new Object[]{1, 1});
1231 Map<String, RequirementDefinition> requirementDef = new HashMap<>();
1232 requirementDef.put(requirementNodeEntry.getKey(), requirementNodeEntryValue);
1233 DataModelUtil.addRequirementToList(requirementList, requirementDef);
1234 requirementMapping = new ArrayList<>();
1235 requirementMapping.add(templateName);
1236 requirementMapping.add(requirementNodeEntry.getKey());
1237 requirementSubstitutionMapping
1238 .put(requirementNodeEntry.getKey() + "_" + templateName, requirementMapping);
1239 if (Objects.isNull(requirementNodeEntryValue.getNode())) {
1240 requirementNodeEntryValue.setOccurrences(new Object[]{1, 1});
1244 return requirementList;
1248 * Fetch global substitution service template service template.
1250 * @param serviceTemplate the service template
1251 * @param context the context
1252 * @return the service template
1254 public static ServiceTemplate fetchGlobalSubstitutionServiceTemplate(
1255 ServiceTemplate serviceTemplate,
1256 TranslationContext context) {
1257 ServiceTemplate globalSubstitutionServiceTemplate =
1258 context.getTranslatedServiceTemplates()
1259 .get(Constants.GLOBAL_SUBSTITUTION_TYPES_TEMPLATE_NAME);
1260 if (globalSubstitutionServiceTemplate == null) {
1261 globalSubstitutionServiceTemplate =
1262 HeatToToscaUtil.createInitGlobalSubstitutionServiceTemplate();
1263 context.getTranslatedServiceTemplates()
1264 .put(Constants.GLOBAL_SUBSTITUTION_TYPES_TEMPLATE_NAME,
1265 globalSubstitutionServiceTemplate);
1267 boolean isImportAddedToServiceTemplate =
1268 DataModelUtil.isImportAddedToServiceTemplate(serviceTemplate.getImports(), Constants
1269 .GLOBAL_SUBSTITUTION_TYPES_TEMPLATE_NAME);
1270 if (!isImportAddedToServiceTemplate) {
1271 serviceTemplate.getImports()
1273 HeatToToscaUtil.createImportList(Constants.GLOBAL_SUBSTITUTION_TYPES_TEMPLATE_NAME));
1275 return globalSubstitutionServiceTemplate;
1278 public static List<Map<String, Import>> createImportList(String templateName) {
1279 List<Map<String, Import>> imports = new ArrayList<>();
1280 Map<String, Import> importsMap = new HashMap<>();
1281 importsMap.put(templateName, HeatToToscaUtil.createServiceTemplateImport(templateName));
1282 imports.add(importsMap);
1287 * Create service template import import.
1289 * @param serviceTemplate the service template
1290 * @return the import
1292 public static Import createServiceTemplateImport(ServiceTemplate serviceTemplate) {
1293 Import serviceTemplateImport = new Import();
1294 serviceTemplateImport.setFile(ToscaUtil.getServiceTemplateFileName(serviceTemplate));
1295 return serviceTemplateImport;
1299 * Create service template import import.
1301 * @param metadataTemplateName the service template name
1302 * @return the import
1304 public static Import createServiceTemplateImport(String metadataTemplateName) {
1305 Import serviceTemplateImport = new Import();
1306 serviceTemplateImport.setFile(ToscaUtil.getServiceTemplateFileName(metadataTemplateName));
1307 return serviceTemplateImport;
1310 public static ToscaServiceModel createToscaServiceModel(ServiceTemplate
1311 entryDefinitionServiceTemplate,
1312 TranslationContext translationContext) {
1313 return new ToscaServiceModel(getCsarArtifactFiles(translationContext),
1314 getServiceTemplates(translationContext),
1315 ToscaUtil.getServiceTemplateFileName(entryDefinitionServiceTemplate));
1318 private static FileContentHandler getCsarArtifactFiles(TranslationContext translationContext) {
1319 FileContentHandler artifactFiles = new FileContentHandler();
1320 artifactFiles.setFiles(translationContext.getFiles());
1321 artifactFiles.setFiles(translationContext.getExternalArtifacts());
1323 HeatTreeManager heatTreeManager =
1324 HeatTreeManagerUtil.initHeatTreeManager(translationContext.getFiles());
1325 heatTreeManager.createTree();
1326 ValidationStructureList validationStructureList =
1327 new ValidationStructureList(heatTreeManager.getTree());
1328 byte[] validationStructureFile =
1329 FileUtils.convertToBytes(validationStructureList, FileUtils.FileExtension.JSON);
1330 artifactFiles.addFile("HEAT.meta", validationStructureFile);
1331 return artifactFiles;
1335 private static Map<String, ServiceTemplate> getServiceTemplates(TranslationContext
1336 translationContext) {
1337 List<ServiceTemplate> serviceTemplates = new ArrayList<>();
1338 serviceTemplates.addAll(GlobalTypesGenerator
1339 .getGlobalTypesServiceTemplate(OnboardingTypesEnum.ZIP).values());
1340 serviceTemplates.addAll(translationContext.getTranslatedServiceTemplates().values());
1341 Map<String, ServiceTemplate> serviceTemplatesMap = new HashMap<>();
1343 for (ServiceTemplate template : serviceTemplates) {
1344 serviceTemplatesMap.put(ToscaUtil.getServiceTemplateFileName(template), template);
1346 return serviceTemplatesMap;