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.services.heattotosca;
23 import org.openecomp.core.utilities.file.FileContentHandler;
24 import org.openecomp.core.utilities.file.FileUtils;
25 import org.openecomp.core.utilities.json.JsonUtil;
26 import org.openecomp.sdc.common.utils.AsdcCommon;
27 import org.openecomp.sdc.heat.datatypes.manifest.FileData;
28 import org.openecomp.sdc.heat.datatypes.manifest.ManifestFile;
29 import org.openecomp.sdc.heat.datatypes.model.Resource;
30 import org.openecomp.sdc.tosca.datatypes.model.ServiceTemplate;
31 import org.openecomp.sdc.translator.datatypes.heattotosca.to.TranslatedHeatResource;
32 import org.openecomp.sdc.translator.services.heattotosca.globaltypes.GlobalTypesGenerator;
34 import java.io.InputStream;
35 import java.util.HashMap;
36 import java.util.HashSet;
41 public class TranslationContext {
44 private static Map<String, Map<String, Map<String, String>>> translationMapping;
45 private static Map<String, ServiceTemplate> globalServiceTemplates;
49 String propertyFileName = AsdcCommon.HEAT_TO_TOSCA_MAPPING_CONF;
50 InputStream is = FileUtils.getFileInputStream(propertyFileName);
51 translationMapping = JsonUtil.json2Object(is, Map.class);
52 globalServiceTemplates = GlobalTypesGenerator.getGlobalTypesServiceTemplate();
55 private ManifestFile manifest;
57 private FileContentHandler files = new FileContentHandler();
59 private Map<String, FileData.Type> manifestFiles = new HashMap<>();
60 //Key - file name, value - file type
61 private Set<String> nestedHeatsFiles = new HashSet<>();
62 private FileContentHandler externalArtifacts = new FileContentHandler();
64 private Map<String, Set<String>> translatedResources = new HashMap<>();
65 // Key - heat file name,value - set of heat resource ids which were translated
66 private Map<String, Set<String>> heatStackGroupMembers = new HashMap<>();
67 // Key - heat file name, value - translated Node template id
68 private Map<String, Map<String, String>> translatedIds = new HashMap<>();
69 // Key - heat file name, value - Map with Key - heat resource Id, Value - tosca entity template id
70 private Map<String, ServiceTemplate> translatedServiceTemplates = new HashMap<>();
71 // key - service template type, value - translated service templates
72 private Map<String, TranslatedHeatResource> heatSharedResourcesByParam = new HashMap<>();
73 //key - heat param name, value - shared resource data
75 public void addManifestFile(String fileName, FileData.Type fileType) {
76 this.manifestFiles.put(fileName, fileType);
79 public Set<String> getNestedHeatsFiles() {
80 return nestedHeatsFiles;
83 public Map<String, Set<String>> getHeatStackGroupMembers() {
84 return heatStackGroupMembers;
87 public FileContentHandler getFiles() {
91 public void setFiles(Map<String, byte[]> files) {
92 this.files.putAll(files);
95 public InputStream getFileContent(String fileName) {
96 return files.getFileContent(fileName);
99 public void addFile(String name, byte[] content) {
100 files.addFile(name, content);
103 public ManifestFile getManifest() {
107 public void setManifest(ManifestFile manifest) {
108 this.manifest = manifest;
111 public Map<String, Set<String>> getTranslatedResources() {
112 return translatedResources;
115 public Map<String, Map<String, String>> getTranslatedIds() {
116 return translatedIds;
119 // get tosca name from mapping configuration file
120 //element type - parameter/attribute
121 // element name - heat parameter/attribute name
122 //return value - tosca parameter/attribute name
123 public String getElementMapping(String resourceType, String elementType, String elementName) {
124 return translationMapping.get(resourceType).get(elementType).get(elementName);
127 public Map<String, String> getElementMapping(String resourceType, String elementType) {
128 return translationMapping.get(resourceType).get(elementType);
131 public Set<String> getElementSet(String resourceType, String elementType) {
132 return translationMapping.get(resourceType).get(elementType).keySet();
135 public Map<String, ServiceTemplate> getTranslatedServiceTemplates() {
136 return translatedServiceTemplates;
139 public ServiceTemplate getGlobalSubstitutionServiceTemplate() {
140 return getTranslatedServiceTemplates().get(Constants.GLOBAL_SUBSTITUTION_TYPES_TEMPLATE_NAME);
143 public FileContentHandler getExternalArtifacts() {
144 return externalArtifacts;
147 public void addExternalArtifacts(String name, byte[] content) {
148 this.externalArtifacts.addFile(name, content);
152 public Map<String, TranslatedHeatResource> getHeatSharedResourcesByParam() {
153 return heatSharedResourcesByParam;
156 public void addHeatSharedResourcesByParam(String parameterName, String resourceId,
158 this.addHeatSharedResourcesByParam(parameterName,
159 new TranslatedHeatResource(resourceId, resource));
162 public void addHeatSharedResourcesByParam(String parameterName,
163 TranslatedHeatResource translatedHeatResource) {
164 this.heatSharedResourcesByParam.put(parameterName, translatedHeatResource);
167 public Map<String, ServiceTemplate> getGlobalServiceTemplates() {
168 return globalServiceTemplates;