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.ArrayList;
46 import java.util.Collection;
47 import java.util.HashMap;
48 import java.util.HashSet;
49 import java.util.List;
51 import java.util.Objects;
52 import java.util.Optional;
56 public class TranslationContext {
59 private static Map<String, Map<String, Map<String, String>>> translationMapping;
60 private static Map<String, ServiceTemplate> globalServiceTemplates;
61 private static Map<String, ImplementationConfiguration> nameExtractorImplMap;
62 private static Map<String, ImplementationConfiguration> supportedConsolidationComputeResources;
63 private static Map<String, ImplementationConfiguration> supportedConsolidationPortResources;
64 private static List enrichPortResourceProperties;
67 Configuration config = ConfigurationManager.lookup();
68 String propertyFileName = SdcCommon.HEAT_TO_TOSCA_MAPPING_CONF;
70 config.generateMap(ConfigConstants.MAPPING_NAMESPACE, ConfigConstants.RESOURCE_MAPPING_KEY);
72 globalServiceTemplates = GlobalTypesGenerator.getGlobalTypesServiceTemplate();
73 } catch (Exception exc) {
74 throw new RuntimeException("Failed to load GlobalTypes", exc);
76 nameExtractorImplMap = config.populateMap(ConfigConstants.TRANSLATOR_NAMESPACE,
77 ConfigConstants.NAMING_CONVENTION_EXTRACTOR_IMPL_KEY, ImplementationConfiguration.class);
78 supportedConsolidationComputeResources = config.populateMap(ConfigConstants
79 .MANDATORY_UNIFIED_MODEL_NAMESPACE, ConfigConstants
80 .SUPPORTED_CONSOLIDATION_COMPUTE_RESOURCES_KEY, ImplementationConfiguration.class);
81 supportedConsolidationPortResources = config.populateMap(ConfigConstants
82 .MANDATORY_UNIFIED_MODEL_NAMESPACE, ConfigConstants
83 .SUPPORTED_CONSOLIDATION_PORT_RESOURCES_KEY, ImplementationConfiguration.class);
84 enrichPortResourceProperties = config.getAsStringValues(ConfigConstants
85 .MANDATORY_UNIFIED_MODEL_NAMESPACE, ConfigConstants
86 .ENRICH_PORT_RESOURCE_PROP);
90 private ManifestFile manifest;
92 public static List getEnrichPortResourceProperties() {
93 return enrichPortResourceProperties;
96 private FileContentHandler files = new FileContentHandler();
97 private Map<String, FileData.Type> manifestFiles = new HashMap<>();
98 //Key - file name, value - file type
99 private Set<String> nestedHeatsFiles = new HashSet<>();
100 private FileContentHandler externalArtifacts = new FileContentHandler();
101 // Key - heat file name,value - set of heat resource ids which were translated
102 private Map<String, Set<String>> translatedResources = new HashMap<>();
103 // Key - heat file name, value - translated Node template id
104 private Map<String, Set<String>> heatStackGroupMembers = new HashMap<>();
105 // Key - heat file name, value - Map with Key - heat resource Id, Value - tosca entity template id
106 private Map<String, Map<String, String>> translatedIds = new HashMap<>();
107 // key - service template type, value - translated service templates
108 private Map<String, ServiceTemplate> translatedServiceTemplates = new HashMap<>();
109 //key - heat param name, value - shared resource data
110 private Map<String, TranslatedHeatResource> heatSharedResourcesByParam = new HashMap<>();
111 //key - translated substitute service template file name, value - source nested heat file name
112 private Map<String, String> nestedHeatFileName = new HashMap<>();
113 //Key - heat file name,value - Map eith key - heat pseudo param name,
114 // value - translated tosca parameter name
115 private Map<String, Map<String, String>> usedHeatPseudoParams = new HashMap<>();
116 //Consolidation data gathered for Unified TOSCA model
117 private ConsolidationData consolidationData = new ConsolidationData();
118 private Map<String, UnifiedSubstitutionData> unifiedSubstitutionData = new HashMap<>();
119 private Set<String> unifiedHandledServiceTemplates = new HashSet<>();
121 public static Map<String, ImplementationConfiguration>
122 getSupportedConsolidationComputeResources() {
123 return supportedConsolidationComputeResources;
126 public static void setSupportedConsolidationComputeResources(
127 Map<String, ImplementationConfiguration> supportedConsolidationComputeResources) {
128 TranslationContext.supportedConsolidationComputeResources =
129 supportedConsolidationComputeResources;
132 public static Map<String, ImplementationConfiguration> getSupportedConsolidationPortResources() {
133 return supportedConsolidationPortResources;
136 public static void setSupportedConsolidationPortResources(
137 Map<String, ImplementationConfiguration> supportedConsolidationPortResources) {
138 TranslationContext.supportedConsolidationPortResources = supportedConsolidationPortResources;
142 * Get nameExtractor implemetation class instance.
144 * @param extractorImplKey configuration key for the implementation class
145 * @return implemetation class instance
147 public static NameExtractor getNameExtractorImpl(String extractorImplKey) {
148 String nameExtractorImplClassName =
149 nameExtractorImplMap.get(extractorImplKey).getImplementationClass();
151 return CommonMethods.newInstance(nameExtractorImplClassName, NameExtractor.class);
154 public Map<String, UnifiedSubstitutionData> getUnifiedSubstitutionData() {
155 return unifiedSubstitutionData;
158 public void setUnifiedSubstitutionData(
159 Map<String, UnifiedSubstitutionData> unifiedSubstitutionData) {
160 this.unifiedSubstitutionData = unifiedSubstitutionData;
163 public void addCleanedNodeTemplate(String serviceTemplateName,
164 String nodeTemplateId,
165 UnifiedCompositionEntity unifiedCompositionEntity,
166 NodeTemplate nodeTemplate) {
167 this.unifiedSubstitutionData.putIfAbsent(serviceTemplateName, new UnifiedSubstitutionData());
168 this.unifiedSubstitutionData
169 .get(serviceTemplateName)
170 .addCleanedNodeTemplate(nodeTemplateId, unifiedCompositionEntity, nodeTemplate);
173 public NodeTemplate getCleanedNodeTemplate(String serviceTemplateName,
174 String nodeTemplateId) {
175 return this.unifiedSubstitutionData.get(serviceTemplateName)
176 .getCleanedNodeTemplate(nodeTemplateId);
179 public void addUnifiedNestedNodeTemplateId(String serviceTemplateName,
180 String nestedNodeTemplateId,
181 String unifiedNestedNodeTemplateId) {
182 this.unifiedSubstitutionData.putIfAbsent(serviceTemplateName, new UnifiedSubstitutionData());
183 this.unifiedSubstitutionData.get(serviceTemplateName)
184 .addUnifiedNestedNodeTemplateId(nestedNodeTemplateId, unifiedNestedNodeTemplateId);
187 public Optional<String> getUnifiedNestedNodeTemplateId(String serviceTemplateName,
188 String nestedNodeTemplateId) {
189 return this.unifiedSubstitutionData.get(serviceTemplateName) == null ? Optional.empty()
190 : this.unifiedSubstitutionData.get(serviceTemplateName)
191 .getUnifiedNestedNodeTemplateId(nestedNodeTemplateId);
194 public void addUnifiedNestedNodeTypeId(String serviceTemplateName,
195 String nestedNodeTypeId,
196 String unifiedNestedNodeTypeId) {
197 this.unifiedSubstitutionData.putIfAbsent(serviceTemplateName, new UnifiedSubstitutionData());
198 this.unifiedSubstitutionData.get(serviceTemplateName)
199 .addUnifiedNestedNodeTypeId(nestedNodeTypeId, unifiedNestedNodeTypeId);
202 public Optional<String> getUnifiedNestedNodeTypeId(String serviceTemplateName,
203 String nestedNodeTemplateId) {
204 return this.unifiedSubstitutionData.get(serviceTemplateName) == null ? Optional.empty()
205 : this.unifiedSubstitutionData.get(serviceTemplateName)
206 .getUnifiedNestedNodeTypeId(nestedNodeTemplateId);
209 public ConsolidationData getConsolidationData() {
210 return consolidationData;
213 public void setConsolidationData(ConsolidationData consolidationData) {
214 this.consolidationData = consolidationData;
217 public void addManifestFile(String fileName, FileData.Type fileType) {
218 this.manifestFiles.put(fileName, fileType);
221 public Set<String> getNestedHeatsFiles() {
222 return nestedHeatsFiles;
225 public Map<String, Set<String>> getHeatStackGroupMembers() {
226 return heatStackGroupMembers;
229 public FileContentHandler getFiles() {
233 public void setFiles(Map<String, byte[]> files) {
234 this.files.putAll(files);
237 public InputStream getFileContent(String fileName) {
238 return files.getFileContent(fileName);
241 public void addFile(String name, byte[] content) {
242 files.addFile(name, content);
245 public ManifestFile getManifest() {
249 public void setManifest(ManifestFile manifest) {
250 this.manifest = manifest;
253 public Map<String, Set<String>> getTranslatedResources() {
254 return translatedResources;
257 public Map<String, Map<String, String>> getTranslatedIds() {
258 return translatedIds;
261 public Set<String> getAllTranslatedResourceIdsFromDiffNestedFiles(String
262 nestedHeatFileNameToSkip){
263 Set<String> allTranslatedResourceIds = new HashSet<>();
265 this.translatedIds.entrySet().stream().filter(
266 heatFileNameToTranslatedIdsEntry -> !heatFileNameToTranslatedIdsEntry.getKey()
267 .equals(nestedHeatFileNameToSkip)).forEach(heatFileNameToTranslatedIdsEntry -> {
268 allTranslatedResourceIds.addAll(heatFileNameToTranslatedIdsEntry.getValue().keySet());
271 return allTranslatedResourceIds;
274 // get tosca name from mapping configuration file
275 //element type - parameter/attribute
276 // element name - heat parameter/attribute name
277 //return value - tosca parameter/attribute name
278 public String getElementMapping(String resourceType, String elementType, String elementName) {
279 if (Objects.isNull(translationMapping.get(resourceType))) {
282 if (Objects.isNull(translationMapping.get(resourceType).get(elementType))) {
285 return translationMapping.get(resourceType).get(elementType).get(elementName);
288 public Map<String, String> getElementMapping(String resourceType, String elementType) {
289 if (Objects.isNull(translationMapping.get(resourceType))) {
292 return translationMapping.get(resourceType).get(elementType);
295 public Set<String> getElementSet(String resourceType, String elementType) {
296 if (Objects.isNull(translationMapping.get(resourceType))) {
297 return new HashSet<>();
299 if (Objects.isNull(translationMapping.get(resourceType).get(elementType))) {
300 return new HashSet<>();
302 return translationMapping.get(resourceType).get(elementType).keySet();
305 public Map<String, ServiceTemplate> getTranslatedServiceTemplates() {
306 return translatedServiceTemplates;
309 public ServiceTemplate getGlobalSubstitutionServiceTemplate() {
310 return getTranslatedServiceTemplates().get(Constants.GLOBAL_SUBSTITUTION_TYPES_TEMPLATE_NAME);
313 public FileContentHandler getExternalArtifacts() {
314 return externalArtifacts;
317 public void addExternalArtifacts(String name, byte[] content) {
318 this.externalArtifacts.addFile(name, content);
321 public Map<String, TranslatedHeatResource> getHeatSharedResourcesByParam() {
322 return heatSharedResourcesByParam;
325 public void addHeatSharedResourcesByParam(String parameterName, String resourceId,
327 this.addHeatSharedResourcesByParam(parameterName,
328 new TranslatedHeatResource(resourceId, resource));
331 private void addHeatSharedResourcesByParam(String parameterName,
332 TranslatedHeatResource translatedHeatResource) {
333 this.heatSharedResourcesByParam.put(parameterName, translatedHeatResource);
336 public Map<String, ServiceTemplate> getGlobalServiceTemplates() {
337 return globalServiceTemplates;
340 public Map<String, String> getNestedHeatFileName() {
341 return nestedHeatFileName;
344 public void addNestedHeatFileName(String substituteServiceTempalteName,
345 String nestedHeatFileName) {
346 this.nestedHeatFileName.put(substituteServiceTempalteName, nestedHeatFileName);
349 public Map<String, Map<String, String>> getUsedHeatPseudoParams() {
350 return usedHeatPseudoParams;
353 public void addUsedHeatPseudoParams(String heatFileName, String heatPseudoParam, String
354 translatedToscaParam) {
355 if (Objects.isNull(this.usedHeatPseudoParams.get(heatFileName))) {
356 this.usedHeatPseudoParams.put(heatFileName, new HashMap<>());
358 this.usedHeatPseudoParams.get(heatFileName).put(heatPseudoParam, translatedToscaParam);
362 * Add the unified substitution data info in context. Contains a mapping of original node
363 * template id and the new node template id in the abstract substitute
365 * @param serviceTemplateFileName the service template file name
366 * @param originalNodeTemplateId the original node template id
367 * @param abstractNodeTemplateId the node template id in the abstract substitute
369 public void addUnifiedSubstitutionData(String serviceTemplateFileName,
370 String originalNodeTemplateId,
371 String abstractNodeTemplateId) {
373 Map<String, String> nodeAbstractNodeTemplateIdMap = this.getUnifiedSubstitutionData()
374 .computeIfAbsent(serviceTemplateFileName, k -> new UnifiedSubstitutionData())
375 .getNodesRelatedAbstractNode();
377 if (nodeAbstractNodeTemplateIdMap == null) {
378 nodeAbstractNodeTemplateIdMap = new HashMap<>();
380 nodeAbstractNodeTemplateIdMap.put(originalNodeTemplateId, abstractNodeTemplateId);
381 this.getUnifiedSubstitutionData().get(serviceTemplateFileName).setNodesRelatedAbstractNode(
382 nodeAbstractNodeTemplateIdMap);
386 * Add the unified substitution data info in context. Contains a mapping of original node
387 * template id and the new node template id in the abstract substitute
389 * @param serviceTemplateFileName the service template file name
390 * @param originalNodeTemplateId the original node template id
391 * @param substitutionServiceTemplateNodeTemplateId the node template id in the substitution
394 public void addSubstitutionServiceTemplateUnifiedSubstitutionData(
395 String serviceTemplateFileName,
396 String originalNodeTemplateId,
397 String substitutionServiceTemplateNodeTemplateId) {
399 Map<String, String> nodesRelatedSubstitutionServiceTemplateNodeTemplateIdMap = this
400 .getUnifiedSubstitutionData()
401 .computeIfAbsent(serviceTemplateFileName, k -> new UnifiedSubstitutionData())
402 .getNodesRelatedSubstitutionServiceTemplateNode();
404 if (nodesRelatedSubstitutionServiceTemplateNodeTemplateIdMap == null) {
405 nodesRelatedSubstitutionServiceTemplateNodeTemplateIdMap = new HashMap<>();
407 nodesRelatedSubstitutionServiceTemplateNodeTemplateIdMap.put(originalNodeTemplateId,
408 substitutionServiceTemplateNodeTemplateId);
409 this.getUnifiedSubstitutionData().get(serviceTemplateFileName)
410 .setNodesRelatedSubstitutionServiceTemplateNode(
411 nodesRelatedSubstitutionServiceTemplateNodeTemplateIdMap);
415 * Get unified abstract node template which is mapped to the input node template id.
417 * @param serviceTemplate the service template
418 * @param nodeTemplateId the node template id
420 public String getUnifiedAbstractNodeTemplateId(ServiceTemplate serviceTemplate,
421 String nodeTemplateId) {
422 UnifiedSubstitutionData unifiedSubstitutionData =
423 this.unifiedSubstitutionData.get(ToscaUtil.getServiceTemplateFileName(serviceTemplate));
424 return unifiedSubstitutionData.getNodesRelatedAbstractNode().get(nodeTemplateId);
428 * Get unified node template in the substitution service template which is mapped to the
429 * original input node template id.
431 * @param serviceTemplate the service template
432 * @param nodeTemplateId the node template id
434 public String getUnifiedSubstitutionNodeTemplateId(ServiceTemplate serviceTemplate,
435 String nodeTemplateId) {
436 UnifiedSubstitutionData unifiedSubstitutionData =
437 this.unifiedSubstitutionData.get(ToscaUtil.getServiceTemplateFileName(serviceTemplate));
438 return unifiedSubstitutionData.getNodesRelatedSubstitutionServiceTemplateNode()
439 .get(nodeTemplateId);
442 public int getHandledNestedComputeNodeTemplateIndex(String serviceTemplateName,
443 String computeType) {
444 return this.unifiedSubstitutionData.get(serviceTemplateName)
445 .getHandledNestedComputeNodeTemplateIndex(computeType);
448 public void updateHandledComputeType(String serviceTemplateName,
449 String handledComputeType,
450 String nestedServiceTemplateFileName) {
451 String globalSTName =
452 ToscaUtil.getServiceTemplateFileName(Constants.GLOBAL_SUBSTITUTION_TYPES_TEMPLATE_NAME);
453 this.unifiedSubstitutionData.putIfAbsent(
454 globalSTName, new UnifiedSubstitutionData());
455 this.unifiedSubstitutionData.get(globalSTName)
456 .addHandledComputeType(handledComputeType);
457 this.unifiedSubstitutionData.get(globalSTName).addHandlesNestedServiceTemplate(nestedServiceTemplateFileName);
459 this.unifiedSubstitutionData.putIfAbsent(serviceTemplateName, new UnifiedSubstitutionData());
460 this.unifiedSubstitutionData.get(serviceTemplateName).addHandlesNestedServiceTemplate(nestedServiceTemplateFileName);
463 public void addHandledComputeTypeInServiceTemplate(String serviceTemplateName,
464 String handledComputeType){
465 this.unifiedSubstitutionData.putIfAbsent(serviceTemplateName, new UnifiedSubstitutionData());
466 this.unifiedSubstitutionData.get(serviceTemplateName).addHandledComputeType(handledComputeType);
469 public boolean isComputeTypeHandledInServiceTemplate(String serviceTemplateName,
470 String computeType) {
471 return !Objects.isNull(this.unifiedSubstitutionData.get(serviceTemplateName))
472 && this.unifiedSubstitutionData.get(serviceTemplateName)
473 .isComputeTypeHandledInServiceTemplate(computeType);
476 public int getHandledNestedComputeNodeTemplateIndex(String serviceTemplateName,
477 String nestedServiceTemplateName,
479 return this.unifiedSubstitutionData.get(serviceTemplateName)
480 .getHandledNestedComputeNodeTemplateIndex(computeType);
483 public boolean isNestedServiceTemplateWasHandled(String serviceTemplateName,
484 String nestedServiceTemplateFileName) {
485 if (Objects.isNull(this.unifiedSubstitutionData.get(serviceTemplateName))) {
488 return this.unifiedSubstitutionData.get(serviceTemplateName)
489 .isNestedServiceTemplateWasHandled(nestedServiceTemplateFileName);
492 public Set<String> getAllRelatedNestedNodeTypeIds(){
493 String globalName = "GlobalSubstitutionTypes";
494 if(Objects.isNull(this.unifiedSubstitutionData) ||
495 Objects.isNull(this.unifiedSubstitutionData.get(globalName))){
496 return new HashSet<>();
499 return this.unifiedSubstitutionData.get(globalName).getAllRelatedNestedNodeTypeIds();
502 public boolean isUnifiedHandledServiceTemplate(ServiceTemplate serviceTemplate) {
503 String serviceTemplateFileName = ToscaUtil.getServiceTemplateFileName(serviceTemplate);
504 if (unifiedHandledServiceTemplates.contains(serviceTemplateFileName)) {
512 public void addUnifiedHandledServiceTeamplte(ServiceTemplate serviceTemplate) {
513 String serviceTemplateFileName = ToscaUtil.getServiceTemplateFileName(serviceTemplate);
514 this.unifiedHandledServiceTemplates.add(serviceTemplateFileName);
517 public boolean isNestedNodeWasHandled(String serviceTemplateName,
518 String nestedNodeTemplateId) {
519 if (Objects.isNull(this.unifiedSubstitutionData.get(serviceTemplateName))) {
522 return this.unifiedSubstitutionData.get(serviceTemplateName)
523 .isNestedNodeWasHandled(nestedNodeTemplateId);
526 public void addNestedNodeAsHandled(String serviceTemplateName,
527 String nestedNodeTemplateId) {
528 this.unifiedSubstitutionData.putIfAbsent(serviceTemplateName, new UnifiedSubstitutionData());
529 this.unifiedSubstitutionData.get(serviceTemplateName)
530 .addHandledNestedNodes(nestedNodeTemplateId);
533 public void updateUsedTimesForNestedComputeNodeType(String serviceTemplateName,
534 String computeType) {
535 this.unifiedSubstitutionData.putIfAbsent(serviceTemplateName, new UnifiedSubstitutionData());
537 this.unifiedSubstitutionData.get(serviceTemplateName)
538 .updateUsedTimesForNestedComputeNodeType(computeType);
541 public int getGlobalNodeTypeIndex(String serviceTemplateName,
542 String computeType) {
543 if (Objects.isNull(this.unifiedSubstitutionData.get(serviceTemplateName))) {
546 return this.unifiedSubstitutionData.get(serviceTemplateName).getGlobalNodeTypeIndex
550 public void addNewPropertyIdToNodeTemplate(String serviceTemplateName,
551 String newPropertyId,
552 Object origPropertyValue){
553 this.unifiedSubstitutionData.putIfAbsent(serviceTemplateName, new UnifiedSubstitutionData());
554 this.unifiedSubstitutionData.get(serviceTemplateName).addNewPropertyIdToNodeTemplate(
555 newPropertyId, origPropertyValue);
558 public Optional<Object> getNewPropertyInputParamId(String serviceTemplateName,
559 String newPropertyId){
560 if(Objects.isNull(this.unifiedSubstitutionData.get(serviceTemplateName))){
561 return Optional.empty();
564 return this.unifiedSubstitutionData.get(serviceTemplateName).getNewPropertyInputParam
568 public Map<String, Object> getAllNewPropertyInputParamIdsPerNodeTenplateId(String serviceTemplateName){
569 if(Objects.isNull(this.unifiedSubstitutionData.get(serviceTemplateName))){
570 return new HashMap<>();
573 return this.unifiedSubstitutionData.get(serviceTemplateName).getAllNewPropertyInputParamIds();