2 * Copyright © 2016-2018 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.
16 package org.openecomp.sdc.translator.services.heattotosca;
18 import static org.openecomp.sdc.heat.services.HeatResourceUtil.extractNetworkRoleFromSubInterfaceId;
19 import static org.openecomp.sdc.translator.services.heattotosca.impl.functiontranslation.FunctionTranslator.getFunctionTranslateTo;
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;
29 import java.util.Objects;
30 import java.util.Optional;
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;
101 * The type Heat to tosca util.
103 public class HeatToToscaUtil {
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 = "_";
113 * Load and translate template data translator output.
115 * @param fileNameContentMap the file name content map
116 * @return the translator output
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);
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;
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);
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);
151 * Build list of files to search optional.
153 * @param heatFileName the heat file name
154 * @param filesDataList the files data list
155 * @param types the types
156 * @return the optional
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());
164 return Optional.ofNullable(HeatToToscaUtil.getFilteredListOfFileDataByTypes(list, types));
168 * Gets filtered list of file data by types.
170 * @param filesToSearch the files to search
171 * @param types the types
172 * @return the filtered list of file data by types
174 public static List<FileData> getFilteredListOfFileDataByTypes(List<FileData> filesToSearch, FileData.Type... types) {
175 return filesToSearch.stream().filter(FileData.buildFileDataPredicateByType(types)).collect(Collectors.toList());
179 * Gets file data from the list according to the input heat file name.
181 * @param heatFileName the heat file name
182 * @param fileDataList the file data list
183 * @return the file data
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);
191 return Optional.empty();
195 * Gets file data which is supported by the translator, from the context according the input heat file name.
197 * @param heatFileName the heat file name
198 * @param context the translation context
199 * @return the file data
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)) {
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);
220 return fileDataCollection;
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);
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));
240 fileDataCollection.addArtifactFiles(fileData);
241 filteredFiles.remove(fileData.getFile());
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());
251 fileDataCollection.setAddOnFiles(filteredFiles.values());
252 return fileDataCollection;
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;
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);
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);
291 return Optional.empty();
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);
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);
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);
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()));
317 private static List<String> extractFilenamesFromFileDataList(Collection<FileData> fileDataList) {
318 return fileDataList.stream().map(FileData::getFile).collect(Collectors.toList());
322 * Extract attached resource id optional.
324 * @param translateTo the translate to
325 * @param propertyName the property name
326 * @return the optional
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();
333 return extractAttachedResourceId(translateTo.getHeatFileName(), translateTo.getHeatOrchestrationTemplate(), translateTo.getContext(),
338 * Extract attached resource id optional.
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
346 public static Optional<AttachedResourceId> extractAttachedResourceId(String heatFileName, HeatOrchestrationTemplate heatOrchestrationTemplate,
347 TranslationContext context, Object propertyValue) {
349 Object translatedId = null;
350 if (Objects.isNull(propertyValue)) {
351 return Optional.empty();
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)) {
369 translatedId = propertyValue;
370 entity = propertyValue;
372 return Optional.of(new AttachedResourceId(translatedId, entity, referenceType));
375 private static ReferenceType getReferenceTypeFromAttachedResouce(String key) {
376 ReferenceType referenceType;
379 referenceType = ReferenceType.GET_RESOURCE;
382 referenceType = ReferenceType.GET_PARAM;
385 referenceType = ReferenceType.GET_ATTR;
388 referenceType = ReferenceType.OTHER;
391 return referenceType;
395 * Gets contrail attached heat resource id.
397 * @param attachedResource the attached resource
398 * @return the contrail attached heat resource id
400 public static Optional<String> getContrailAttachedHeatResourceId(AttachedResourceId attachedResource) {
401 if (attachedResource == null) {
402 return Optional.empty();
404 if (attachedResource.isGetResource()) {
405 return Optional.of((String) attachedResource.getEntityId());
407 if (attachedResource.isGetAttr()) {
408 return getResourceId(attachedResource.getEntityId());
410 return Optional.empty();
414 * Extract property optional.
416 * @param propertyValue the property value
417 * @return the optional
419 private static Optional<AttachedPropertyVal> extractProperty(Object propertyValue) {
420 Object attachedPropertyVal;
421 if (Objects.isNull(propertyValue)) {
422 return Optional.empty();
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();
432 referenceType = ReferenceType.GET_RESOURCE;
435 referenceType = ReferenceType.GET_PARAM;
438 referenceType = ReferenceType.GET_ATTR;
444 attachedPropertyVal = propertyValue;
446 return Optional.of(new AttachedPropertyVal(attachedPropertyVal, referenceType));
452 * @param nodeTemplate the node template
453 * @param propertyKey the property key
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));
465 * @param nodeTemplate the node template
466 * @param propertyListKey the property list key
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));
482 * Is yml file type boolean.
484 * @param filename the filename
485 * @return the boolean
487 public static boolean isYmlFileType(String filename) {
488 String extension = FilenameUtils.getExtension(filename);
489 return "yaml".equalsIgnoreCase(extension) || "yml".equalsIgnoreCase(extension);
493 * Is nested resource boolean.
495 * @param resource the resource
496 * @return the boolean
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
505 //dynamic type is currently not supported
508 String internalResourceType = (String) ((Map) resourceDef).get(HeatConstants.RESOURCE_DEF_TYPE_PROPERTY_NAME);
509 if (isYamlFile(internalResourceType)) {
512 } else if (isYamlFile(resourceType)) {
519 * Checks if the current HEAT resource if of type sub interface.
521 * @param resource the resource
522 * @return true if the resource is of sub interface type and false otherwise
524 public static boolean isSubInterfaceResource(Resource resource, TranslationContext context) {
525 //Check if resource group is a nested resource
526 if (!isNestedResource(resource)) {
529 Optional<String> nestedHeatFileName = HeatToToscaUtil.getNestedHeatFileName(resource);
530 return nestedHeatFileName.filter(fileName -> isNestedVlanResource(fileName, context)).isPresent();
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);
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();
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);
557 return Optional.empty();
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);
565 public static boolean isValueSpecsPropertyExists(Resource resource) {
566 return MapUtils.isNotEmpty(resource.getProperties()) && resource.getProperties().containsKey(HeatConstants.VALUE_SPECS_PROPERTY_NAME);
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));
574 return Optional.empty();
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)
582 return resource.getType();
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);
595 resourceDefPropertiesMap = subInterfaceResource.getProperties();
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());
607 return Optional.empty();
611 * Checks if the nested resource represents a VFC or a complex VFC (Heat file should contain at least one or more compute nodes).
613 * @param resource the resource
614 * @param context the context
615 * @return true if the resource represents a VFC and false otherwise.
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);
626 * Get nested heat file name in case of nested resource.
628 * @param resource the resource
629 * @return the nested heat file name
631 private static Optional<String> getNestedHeatFileName(Resource resource) {
632 if (!isNestedResource(resource)) {
633 return Optional.empty();
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);
641 return Optional.of(resourceType);
647 * @param resource the resource
648 * @return the nested file
650 public static Optional<String> getNestedFile(Resource resource) {
651 if (!isNestedResource(resource)) {
652 return Optional.empty();
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);
660 return Optional.of(resourceType);
664 public static boolean isYamlFile(String fileName) {
665 return fileName.endsWith(".yaml") || fileName.endsWith(".yml");
671 * @param heatOrchestrationTemplate the heat orchestration template
672 * @param resourceId the resource id
673 * @param heatFileName the heat file name
674 * @return the resource
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());
687 * @param resourceId the resource id
688 * @param heatOrchestrationTemplate heat orchestration template
689 * @param heatFileName heat file name
690 * @return resource type
692 public static String getResourceType(String resourceId, HeatOrchestrationTemplate heatOrchestrationTemplate, String heatFileName) {
693 return HeatToToscaUtil.getResource(heatOrchestrationTemplate, resourceId, heatFileName).getType();
697 * Is heat file nested boolean.
699 * @param translateTo the translate to
700 * @param heatFileName the heat file name
701 * @return the boolean
703 public static boolean isHeatFileNested(TranslateTo translateTo, String heatFileName) {
704 return isHeatFileNested(translateTo.getContext(), heatFileName);
707 public static boolean isHeatFileNested(TranslationContext context, String heatFileName) {
708 return context.getNestedHeatsFiles().contains(heatFileName);
712 * Extract contrail get resource attached heat resource id optional.
714 * @param propertyValue the property value
715 * @return the optional
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);
724 Collection valCollection = ((Map) propertyValue).values();
725 return evaluateHeatResourceId(valCollection);
727 } else if (propertyValue instanceof List) {
728 return evaluateHeatResourceId((List) propertyValue);
730 return Optional.empty();
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));
738 LOGGER.warn("invalid format of 'get_attr' function - " + data.toString());
739 return Optional.empty();
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);
748 LOGGER.warn("invalid format of 'get_resource' function - " + propertyValue.toString());
749 return Optional.empty();
753 private static Optional<String> evaluateHeatResourceId(Collection propertyValue) {
754 for (Object prop : propertyValue) {
755 Optional<String> ret = extractContrailGetResourceAttachedHeatResourceId(prop);
756 if (ret.isPresent()) {
760 return Optional.empty();
764 * Gets tosca service model.
766 * @param context translation context
767 * @return the tosca service model
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);
776 * Gets tosca service model.
778 * @param context translation context
779 * @param entryDefinitionMetadata template name of the entry definition servie template
780 * @return the tosca service model
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);
788 return new ToscaServiceModel(null, serviceTemplates, ToscaUtil.getServiceTemplateFileName(entryDefinitionMetadata));
792 * Gets service template from context.
794 * @param serviceTemplateFileName the service template file name
795 * @param context the context
796 * @return the service template from context
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);
804 return Optional.empty();
808 * Adding link requerment from port node template to network node template.
810 * @param portNodeTemplate port node template
811 * @param networkTranslatedId network node template id
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);
823 * Adding binding requerment from sub interface node template to interface (port) node template.
825 * @param subInterfaceNodeTemplate sub interface template
826 * @param interfaceTranslatedId interface node template id
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);
837 * Get property Parameter Name Value.
839 * @param property property
840 * @return Parameter name in case the property include "get_param" function
842 public static Optional<String> getPropertyParameterNameValue(Object property) {
843 if (Objects.isNull(property)) {
844 return Optional.empty();
846 Optional<AttachedPropertyVal> extractedProperty = extractProperty(property);
847 if (extractedProperty.isPresent()) {
848 return getParameterName(extractedProperty.get());
850 return Optional.empty();
853 private static Optional<String> getParameterName(AttachedPropertyVal extractedProperty) {
854 if (!extractedProperty.isGetParam()) {
855 return Optional.empty();
857 Object getParamFuncValue = extractedProperty.getPropertyValue();
858 if (getParamFuncValue instanceof String) {
859 return Optional.of((String) getParamFuncValue);
861 return Optional.of((String) ((List) getParamFuncValue).get(0));
865 public static String getToscaPropertyName(TranslationContext context, String heatResourceType, String heatPropertyName) {
866 return context.getElementMapping(heatResourceType, Constants.PROP, heatPropertyName);
870 * Gets tosca property name.
872 * @param translateTo the translate to
873 * @param heatPropertyName the heat property name
874 * @return the tosca property name
876 public static String getToscaPropertyName(TranslateTo translateTo, String heatPropertyName) {
877 return translateTo.getContext().getElementMapping(translateTo.getResource().getType(), Constants.PROP, heatPropertyName);
881 * Gets tosca attribute name.
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
888 public static String getToscaAttributeName(TranslationContext context, String heatResourceType, String heatAttrName) {
889 return context.getElementMapping(heatResourceType, Constants.ATTR, heatAttrName);
893 * Gets tosca attribute name.
895 * @param translateTo the translate to
896 * @param heatAttrName the heat attr name
897 * @return the tosca attribute name
899 public static String getToscaAttributeName(TranslateTo translateTo, String heatAttrName) {
900 return translateTo.getContext().getElementMapping(translateTo.getResource().getType(), Constants.ATTR, heatAttrName);
904 * Create init substitution service template service template.
906 * @param templateName the template name
907 * @return the service template
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;
923 * Create init global substitution service template service template.
925 * @return the service template
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;
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
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
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));
956 * Gets node type with flat hierarchy.
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
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)
971 * Create abstract substitution node template.
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
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;
989 * Checks if the source and target resource is a valid candidate for adding tosca dependency relationship.
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
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);
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);
1015 return addAbstractSubstitutionProperty(templateName, substitutionProperties);
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;
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;
1032 Map<String, Map<String, List<String>>> substitutionMapping = new HashMap<>();
1033 if (nodeTemplates == null) {
1034 return substitutionMapping;
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();
1052 nodeTypeRequirementsDefinition = getNodeTypeReqs(nodeType, nodeTemplateId, substitutionServiceTemplate, requirementSubstitutionMapping,
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);
1061 addNodeTypeCapabilitiesToSubMapping(nodeTypeCapabilitiesDefinition, capabilitySubstitutionMapping, nodeType, nodeTemplateId,
1062 substitutionServiceTemplate, context);
1064 exposedCapabilitiesDefinition = toscaAnalyzerService
1065 .calculateExposedCapabilities(nodeTypeCapabilitiesDefinition, fullFilledRequirementsDefinition);
1066 DataModelUtil.setNodeTypeCapabilitiesDef(substitutionNodeType, exposedCapabilitiesDefinition);
1067 return substitutionMapping;
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));
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);
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;
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});
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});
1123 return requirementList;
1127 * Fetch global substitution service template service template.
1129 * @param serviceTemplate the service template
1130 * @param context the context
1131 * @return the service template
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);
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));
1145 return globalSubstitutionServiceTemplate;
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);
1157 * Create service template import import.
1159 * @param serviceTemplate the service template
1160 * @return the import
1162 public static Import createServiceTemplateImport(ServiceTemplate serviceTemplate) {
1163 Import serviceTemplateImport = new Import();
1164 serviceTemplateImport.setFile(ToscaUtil.getServiceTemplateFileName(serviceTemplate));
1165 return serviceTemplateImport;
1169 * Create service template import import.
1171 * @param metadataTemplateName the service template name
1172 * @return the import
1174 private static Import createServiceTemplateImport(String metadataTemplateName) {
1175 Import serviceTemplateImport = new Import();
1176 serviceTemplateImport.setFile(ToscaUtil.getServiceTemplateFileName(metadataTemplateName));
1177 return serviceTemplateImport;
1180 public static ToscaServiceModel createToscaServiceModel(ServiceTemplate entryDefinitionServiceTemplate, TranslationContext translationContext) {
1181 return new ToscaServiceModel(getCsarArtifactFiles(translationContext), getServiceTemplates(translationContext),
1182 ToscaUtil.getServiceTemplateFileName(entryDefinitionServiceTemplate));
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;
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);
1205 return serviceTemplatesMap;
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;
1212 return ToscaNodeType.NESTED_HEAT_RESOURCE_TYPE_PREFIX;
1215 private static boolean isSubInterfaceBoundToPort(TranslateTo translateTo) {
1216 return HeatToToscaUtil.getSubInterfaceParentPortNodeTemplateId(translateTo).isPresent();
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()) {
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());
1242 * Create substitution node type node type.
1244 * @param substitutionServiceTemplate the substitution service template
1245 * @return the node type
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;
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) {
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);
1277 return substitutionNodeTypeProperties;
1279 //Method evaluate the network role from sub interface node template id, designed considering
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) {
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());
1296 attributeDefinition.setType(PropertyType.STRING.getDisplayName());
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);
1304 return substitutionNodeTypeAttributes;