2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.sdc.translator.datatypes.heattotosca;
23 import org.openecomp.config.api.Configuration;
24 import org.openecomp.config.api.ConfigurationManager;
25 import org.openecomp.core.utilities.CommonMethods;
26 import org.openecomp.core.utilities.file.FileContentHandler;
27 import org.openecomp.sdc.common.utils.SdcCommon;
28 import org.openecomp.sdc.datatypes.configuration.ImplementationConfiguration;
29 import org.openecomp.sdc.heat.datatypes.manifest.FileData;
30 import org.openecomp.sdc.heat.datatypes.manifest.ManifestFile;
31 import org.openecomp.sdc.heat.datatypes.model.Resource;
32 import org.openecomp.sdc.tosca.datatypes.model.NodeTemplate;
33 import org.openecomp.sdc.tosca.datatypes.model.ServiceTemplate;
34 import org.openecomp.sdc.tosca.services.ToscaUtil;
35 import org.openecomp.sdc.translator.datatypes.heattotosca.to.TranslatedHeatResource;
36 import org.openecomp.sdc.translator.datatypes.heattotosca.unifiedmodel.composition.UnifiedCompositionEntity;
37 import org.openecomp.sdc.translator.datatypes.heattotosca.unifiedmodel.composition.UnifiedSubstitutionData;
38 import org.openecomp.sdc.translator.datatypes.heattotosca.unifiedmodel.consolidation.ConsolidationData;
39 import org.openecomp.sdc.translator.services.heattotosca.ConfigConstants;
40 import org.openecomp.sdc.translator.services.heattotosca.Constants;
41 import org.openecomp.sdc.translator.services.heattotosca.NameExtractor;
42 import org.openecomp.sdc.translator.services.heattotosca.globaltypes.GlobalTypesGenerator;
44 import java.io.InputStream;
45 import java.util.HashMap;
46 import java.util.HashSet;
48 import java.util.Objects;
49 import java.util.Optional;
53 public class TranslationContext {
56 private static Map<String, Map<String, Map<String, String>>> translationMapping;
57 private static Map<String, ServiceTemplate> globalServiceTemplates;
58 private static Map<String, ImplementationConfiguration> nameExtractorImplMap;
59 private static Map<String, ImplementationConfiguration> supportedConsolidationComputeResources;
60 private static Map<String, ImplementationConfiguration> supportedConsolidationPortResources;
63 Configuration config = ConfigurationManager.lookup();
64 String propertyFileName = SdcCommon.HEAT_TO_TOSCA_MAPPING_CONF;
66 config.generateMap(ConfigConstants.MAPPING_NAMESPACE, ConfigConstants.RESOURCE_MAPPING_KEY);
68 globalServiceTemplates = GlobalTypesGenerator.getGlobalTypesServiceTemplate();
69 } catch (Exception e) {
70 throw new RuntimeException("Failed to load GlobalTypes", e);
72 nameExtractorImplMap = config.populateMap(ConfigConstants.TRANSLATOR_NAMESPACE,
73 ConfigConstants.NAMING_CONVENTION_EXTRACTOR_IMPL_KEY, ImplementationConfiguration.class);
74 supportedConsolidationComputeResources = config.populateMap(ConfigConstants
75 .MANDATORY_UNIFIED_MODEL_NAMESPACE, ConfigConstants
76 .SUPPORTED_CONSOLIDATION_COMPUTE_RESOURCES_KEY, ImplementationConfiguration.class);
77 supportedConsolidationPortResources = config.populateMap(ConfigConstants
78 .MANDATORY_UNIFIED_MODEL_NAMESPACE, ConfigConstants
79 .SUPPORTED_CONSOLIDATION_PORT_RESOURCES_KEY, ImplementationConfiguration.class);
83 private Map<String, UnifiedSubstitutionData> unifiedSubstitutionData = new HashMap<>();
84 private ManifestFile manifest;
85 private FileContentHandler files = new FileContentHandler();
86 private Map<String, FileData.Type> manifestFiles = new HashMap<>();
87 //Key - file name, value - file type
88 private Set<String> nestedHeatsFiles = new HashSet<>();
89 private FileContentHandler externalArtifacts = new FileContentHandler();
90 // Key - heat file name,value - set of heat resource ids which were translated
91 private Map<String, Set<String>> translatedResources = new HashMap<>();
92 // Key - heat file name, value - translated Node template id
93 private Map<String, Set<String>> heatStackGroupMembers = new HashMap<>();
94 // Key - heat file name, value - Map with Key - heat resource Id, Value - tosca entity template id
95 private Map<String, Map<String, String>> translatedIds = new HashMap<>();
96 // key - service template type, value - translated service templates
97 private Map<String, ServiceTemplate> translatedServiceTemplates = new HashMap<>();
98 //key - heat param name, value - shared resource data
99 private Map<String, TranslatedHeatResource> heatSharedResourcesByParam = new HashMap<>();
100 //key - translated substitute service template file name, value - source nested heat file name
101 private Map<String, String> nestedHeatFileName = new HashMap<>();
102 //Key - heat file name,value - Map eith key - heat pseudo param name,
103 // value - translated tosca parameter name
104 private Map<String, Map<String, String>> usedHeatPseudoParams = new HashMap<>();
105 //Consolidation data gathered for Unified TOSCA model
106 private ConsolidationData consolidationData = new ConsolidationData();
108 public static Map<String, ImplementationConfiguration>
109 getSupportedConsolidationComputeResources() {
110 return supportedConsolidationComputeResources;
113 public static void setSupportedConsolidationComputeResources(
114 Map<String, ImplementationConfiguration> supportedConsolidationComputeResources) {
115 TranslationContext.supportedConsolidationComputeResources =
116 supportedConsolidationComputeResources;
119 public static Map<String, ImplementationConfiguration> getSupportedConsolidationPortResources() {
120 return supportedConsolidationPortResources;
123 public static void setSupportedConsolidationPortResources(
124 Map<String, ImplementationConfiguration> supportedConsolidationPortResources) {
125 TranslationContext.supportedConsolidationPortResources = supportedConsolidationPortResources;
129 * Get nameExtractor implemetation class instance.
131 * @param extractorImplKey configuration key for the implementation class
132 * @return implemetation class instance
134 public static NameExtractor getNameExtractorImpl(String extractorImplKey) {
135 String nameExtractorImplClassName =
136 nameExtractorImplMap.get(extractorImplKey).getImplementationClass();
138 return CommonMethods.newInstance(nameExtractorImplClassName, NameExtractor.class);
141 public Map<String, UnifiedSubstitutionData> getUnifiedSubstitutionData() {
142 return unifiedSubstitutionData;
145 public void setUnifiedSubstitutionData(
146 Map<String, UnifiedSubstitutionData> unifiedSubstitutionData) {
147 this.unifiedSubstitutionData = unifiedSubstitutionData;
150 public void addCleanedNodeTemplate(String serviceTemplateName,
151 String nodeTemplateId,
152 UnifiedCompositionEntity unifiedCompositionEntity,
153 NodeTemplate nodeTemplate) {
154 this.unifiedSubstitutionData.putIfAbsent(serviceTemplateName, new UnifiedSubstitutionData());
155 this.unifiedSubstitutionData
156 .get(serviceTemplateName)
157 .addCleanedNodeTemplate(nodeTemplateId, unifiedCompositionEntity, nodeTemplate);
160 public NodeTemplate getCleanedNodeTemplate(String serviceTemplateName,
161 String nodeTemplateId) {
162 return this.unifiedSubstitutionData.get(serviceTemplateName)
163 .getCleanedNodeTemplate(nodeTemplateId);
166 public void addUnifiedNestedNodeTemplateId(String serviceTemplateName,
167 String nestedNodeTemplateId,
168 String unifiedNestedNodeTemplateId) {
169 this.unifiedSubstitutionData.putIfAbsent(serviceTemplateName, new UnifiedSubstitutionData());
170 this.unifiedSubstitutionData.get(serviceTemplateName)
171 .addUnifiedNestedNodeTemplateId(nestedNodeTemplateId, unifiedNestedNodeTemplateId);
174 public Optional<String> getUnifiedNestedNodeTemplateId(String serviceTemplateName,
175 String nestedNodeTemplateId) {
176 return this.unifiedSubstitutionData.get(serviceTemplateName) == null ? Optional.empty()
177 :this.unifiedSubstitutionData.get(serviceTemplateName).getUnifiedNestedNodeTemplateId(nestedNodeTemplateId);
180 public void addUnifiedNestedNodeTypeId(String serviceTemplateName,
181 String nestedNodeTypeId,
182 String unifiedNestedNodeTypeId){
183 this.unifiedSubstitutionData.putIfAbsent(serviceTemplateName, new UnifiedSubstitutionData());
184 this.unifiedSubstitutionData.get(serviceTemplateName).addUnifiedNestedNodeTypeId(nestedNodeTypeId, unifiedNestedNodeTypeId);
187 public Optional<String> getUnifiedNestedNodeTypeId(String serviceTemplateName,
188 String nestedNodeTemplateId) {
189 return this.unifiedSubstitutionData.get(serviceTemplateName) == null ? Optional.empty()
190 : this.unifiedSubstitutionData.get(serviceTemplateName).getUnifiedNestedNodeTypeId(nestedNodeTemplateId);
193 public ConsolidationData getConsolidationData() {
194 return consolidationData;
197 public void setConsolidationData(ConsolidationData consolidationData) {
198 this.consolidationData = consolidationData;
201 public void addManifestFile(String fileName, FileData.Type fileType) {
202 this.manifestFiles.put(fileName, fileType);
205 public Set<String> getNestedHeatsFiles() {
206 return nestedHeatsFiles;
209 public Map<String, Set<String>> getHeatStackGroupMembers() {
210 return heatStackGroupMembers;
213 public FileContentHandler getFiles() {
217 public void setFiles(Map<String, byte[]> files) {
218 this.files.putAll(files);
221 public InputStream getFileContent(String fileName) {
222 return files.getFileContent(fileName);
225 public void addFile(String name, byte[] content) {
226 files.addFile(name, content);
229 public ManifestFile getManifest() {
233 public void setManifest(ManifestFile manifest) {
234 this.manifest = manifest;
237 public Map<String, Set<String>> getTranslatedResources() {
238 return translatedResources;
241 public Map<String, Map<String, String>> getTranslatedIds() {
242 return translatedIds;
245 // get tosca name from mapping configuration file
246 //element type - parameter/attribute
247 // element name - heat parameter/attribute name
248 //return value - tosca parameter/attribute name
249 public String getElementMapping(String resourceType, String elementType, String elementName) {
250 if (Objects.isNull(translationMapping.get(resourceType))) {
253 if (Objects.isNull(translationMapping.get(resourceType).get(elementType))) {
256 return translationMapping.get(resourceType).get(elementType).get(elementName);
259 public Map<String, String> getElementMapping(String resourceType, String elementType) {
260 if (Objects.isNull(translationMapping.get(resourceType))) {
263 return translationMapping.get(resourceType).get(elementType);
266 public Set<String> getElementSet(String resourceType, String elementType) {
267 if (Objects.isNull(translationMapping.get(resourceType))) {
268 return new HashSet<>();
270 if (Objects.isNull(translationMapping.get(resourceType).get(elementType))) {
271 return new HashSet<>();
273 return translationMapping.get(resourceType).get(elementType).keySet();
276 public Map<String, ServiceTemplate> getTranslatedServiceTemplates() {
277 return translatedServiceTemplates;
280 public ServiceTemplate getGlobalSubstitutionServiceTemplate() {
281 return getTranslatedServiceTemplates().get(Constants.GLOBAL_SUBSTITUTION_TYPES_TEMPLATE_NAME);
284 public FileContentHandler getExternalArtifacts() {
285 return externalArtifacts;
288 public void addExternalArtifacts(String name, byte[] content) {
289 this.externalArtifacts.addFile(name, content);
292 public Map<String, TranslatedHeatResource> getHeatSharedResourcesByParam() {
293 return heatSharedResourcesByParam;
296 public void addHeatSharedResourcesByParam(String parameterName, String resourceId,
298 this.addHeatSharedResourcesByParam(parameterName,
299 new TranslatedHeatResource(resourceId, resource));
302 private void addHeatSharedResourcesByParam(String parameterName,
303 TranslatedHeatResource translatedHeatResource) {
304 this.heatSharedResourcesByParam.put(parameterName, translatedHeatResource);
307 public Map<String, ServiceTemplate> getGlobalServiceTemplates() {
308 return globalServiceTemplates;
311 public Map<String, String> getNestedHeatFileName() {
312 return nestedHeatFileName;
315 public void addNestedHeatFileName(String substituteServiceTempalteName,
316 String nestedHeatFileName) {
317 this.nestedHeatFileName.put(substituteServiceTempalteName, nestedHeatFileName);
320 public Map<String, Map<String, String>> getUsedHeatPseudoParams() {
321 return usedHeatPseudoParams;
324 public void addUsedHeatPseudoParams(String heatFileName, String heatPseudoParam, String
325 translatedToscaParam) {
326 if (Objects.isNull(this.usedHeatPseudoParams.get(heatFileName))) {
327 this.usedHeatPseudoParams.put(heatFileName, new HashMap<>());
329 this.usedHeatPseudoParams.get(heatFileName).put(heatPseudoParam, translatedToscaParam);
333 * Add the unified substitution data info in context. Contains a mapping of original node
334 * template id and the new node template id in the abstract substitute
336 * @param serviceTemplateFileName the service template file name
337 * @param originalNodeTemplateId the original node template id
338 * @param abstractNodeTemplateId the node template id in the abstract substitute
340 public void addUnifiedSubstitutionData(String serviceTemplateFileName,
341 String originalNodeTemplateId,
342 String abstractNodeTemplateId) {
344 Map<String, String> nodeAbstractNodeTemplateIdMap = this.getUnifiedSubstitutionData()
345 .computeIfAbsent(serviceTemplateFileName, k -> new UnifiedSubstitutionData())
346 .getNodesRelatedAbstractNode();
348 if (nodeAbstractNodeTemplateIdMap == null) {
349 nodeAbstractNodeTemplateIdMap = new HashMap<>();
351 nodeAbstractNodeTemplateIdMap.put(originalNodeTemplateId, abstractNodeTemplateId);
352 this.getUnifiedSubstitutionData().get(serviceTemplateFileName).setNodesRelatedAbstractNode(
353 nodeAbstractNodeTemplateIdMap);
357 * Add the unified substitution data info in context. Contains a mapping of original node
358 * template id and the new node template id in the abstract substitute
360 * @param serviceTemplateFileName the service template file name
361 * @param originalNodeTemplateId the original node template id
362 * @param substitutionServiceTemplateNodeTemplateId the node template id in the substitution
365 public void addSubstitutionServiceTemplateUnifiedSubstitutionData(String serviceTemplateFileName,
366 String originalNodeTemplateId,
367 String substitutionServiceTemplateNodeTemplateId) {
369 Map<String, String> nodesRelatedSubstitutionServiceTemplateNodeTemplateIdMap = this
370 .getUnifiedSubstitutionData()
371 .computeIfAbsent(serviceTemplateFileName, k -> new UnifiedSubstitutionData())
372 .getNodesRelatedSubstitutionServiceTemplateNode();
374 if (nodesRelatedSubstitutionServiceTemplateNodeTemplateIdMap == null) {
375 nodesRelatedSubstitutionServiceTemplateNodeTemplateIdMap = new HashMap<>();
377 nodesRelatedSubstitutionServiceTemplateNodeTemplateIdMap.put(originalNodeTemplateId,
378 substitutionServiceTemplateNodeTemplateId);
379 this.getUnifiedSubstitutionData().get(serviceTemplateFileName)
380 .setNodesRelatedSubstitutionServiceTemplateNode(
381 nodesRelatedSubstitutionServiceTemplateNodeTemplateIdMap);
385 * Get unified abstract node template which is mapped to the input node template id.
387 * @param serviceTemplate the service template
388 * @param nodeTemplateId the node template id
390 public String getUnifiedAbstractNodeTemplateId(ServiceTemplate serviceTemplate,
391 String nodeTemplateId) {
392 UnifiedSubstitutionData unifiedSubstitutionData =
393 this.unifiedSubstitutionData.get(ToscaUtil.getServiceTemplateFileName(serviceTemplate));
394 return unifiedSubstitutionData.getNodesRelatedAbstractNode().get(nodeTemplateId);
398 * Get unified node template in the substitution service template which is mapped to the
399 * original input node template id.
401 * @param serviceTemplate the service template
402 * @param nodeTemplateId the node template id
404 public String getUnifiedSubstitutionNodeTemplateId(ServiceTemplate serviceTemplate,
405 String nodeTemplateId) {
406 UnifiedSubstitutionData unifiedSubstitutionData =
407 this.unifiedSubstitutionData.get(ToscaUtil.getServiceTemplateFileName(serviceTemplate));
408 return unifiedSubstitutionData.getNodesRelatedSubstitutionServiceTemplateNode()
409 .get(nodeTemplateId);
412 public int getHandledNestedComputeNodeTemplateIndex(String serviceTemplateName,
414 return this.unifiedSubstitutionData.get(serviceTemplateName)
415 .getHandledNestedComputeNodeTemplateIndex(computeType);
418 public void updateHandledComputeType(String serviceTemplateName,
419 String nestedServiceTemplateFileName,
420 String handledComputeType){
421 this.unifiedSubstitutionData.putIfAbsent(serviceTemplateName, new UnifiedSubstitutionData());
422 this.unifiedSubstitutionData.get(serviceTemplateName)
423 .addHandledComputeType(nestedServiceTemplateFileName, handledComputeType);
426 public boolean isNestedServiceTemplateWasHandled(String serviceTemplateName,
427 String nestedServiceTemplateFileName){
428 if(Objects.isNull(this.unifiedSubstitutionData.get(serviceTemplateName))){
431 return this.unifiedSubstitutionData.get(serviceTemplateName)
432 .isNestedServiceTemplateWasHandled(nestedServiceTemplateFileName);
435 public void addNestedFileToUsedNestedComputeType(String serviceTemplateName,
436 String nestedServiceTemplateFileName,
438 this.unifiedSubstitutionData.putIfAbsent(serviceTemplateName, new UnifiedSubstitutionData());
440 this.unifiedSubstitutionData.get(serviceTemplateName).addNestedFileToUsedNestedComputeType
441 (computeType, nestedServiceTemplateFileName);
444 public int getGlobalNodeTypeIndex(String serviceTemplateName,
446 if(Objects.isNull(this.unifiedSubstitutionData.get(serviceTemplateName))){
449 return this.unifiedSubstitutionData.get(serviceTemplateName).getGlobalNodeTypeIndex