push addional code
[sdc.git] / openecomp-be / lib / openecomp-sdc-translator-lib / openecomp-sdc-translator-core / src / main / java / org / openecomp / sdc / translator / services / heattotosca / TranslationContext.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.sdc.translator.services.heattotosca;
22
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;
33
34 import java.io.InputStream;
35 import java.util.HashMap;
36 import java.util.HashSet;
37 import java.util.Map;
38 import java.util.Set;
39
40
41 public class TranslationContext {
42
43
44   private static Map<String, Map<String, Map<String, String>>> translationMapping;
45   private static Map<String, ServiceTemplate> globalServiceTemplates;
46
47   static {
48
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();
53   }
54
55   private ManifestFile manifest;
56
57   private FileContentHandler files = new FileContentHandler();
58
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();
63
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
74
75   public void addManifestFile(String fileName, FileData.Type fileType) {
76     this.manifestFiles.put(fileName, fileType);
77   }
78
79   public Set<String> getNestedHeatsFiles() {
80     return nestedHeatsFiles;
81   }
82
83   public Map<String, Set<String>> getHeatStackGroupMembers() {
84     return heatStackGroupMembers;
85   }
86
87   public FileContentHandler getFiles() {
88     return files;
89   }
90
91   public void setFiles(Map<String, byte[]> files) {
92     this.files.putAll(files);
93   }
94
95   public InputStream getFileContent(String fileName) {
96     return files.getFileContent(fileName);
97   }
98
99   public void addFile(String name, byte[] content) {
100     files.addFile(name, content);
101   }
102
103   public ManifestFile getManifest() {
104     return manifest;
105   }
106
107   public void setManifest(ManifestFile manifest) {
108     this.manifest = manifest;
109   }
110
111   public Map<String, Set<String>> getTranslatedResources() {
112     return translatedResources;
113   }
114
115   public Map<String, Map<String, String>> getTranslatedIds() {
116     return translatedIds;
117   }
118
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);
125   }
126
127   public Map<String, String> getElementMapping(String resourceType, String elementType) {
128     return translationMapping.get(resourceType).get(elementType);
129   }
130
131   public Set<String> getElementSet(String resourceType, String elementType) {
132     return translationMapping.get(resourceType).get(elementType).keySet();
133   }
134
135   public Map<String, ServiceTemplate> getTranslatedServiceTemplates() {
136     return translatedServiceTemplates;
137   }
138
139   public ServiceTemplate getGlobalSubstitutionServiceTemplate() {
140     return getTranslatedServiceTemplates().get(Constants.GLOBAL_SUBSTITUTION_TYPES_TEMPLATE_NAME);
141   }
142
143   public FileContentHandler getExternalArtifacts() {
144     return externalArtifacts;
145   }
146
147   public void addExternalArtifacts(String name, byte[] content) {
148     this.externalArtifacts.addFile(name, content);
149   }
150
151
152   public Map<String, TranslatedHeatResource> getHeatSharedResourcesByParam() {
153     return heatSharedResourcesByParam;
154   }
155
156   public void addHeatSharedResourcesByParam(String parameterName, String resourceId,
157                                             Resource resource) {
158     this.addHeatSharedResourcesByParam(parameterName,
159         new TranslatedHeatResource(resourceId, resource));
160   }
161
162   public void addHeatSharedResourcesByParam(String parameterName,
163                                             TranslatedHeatResource translatedHeatResource) {
164     this.heatSharedResourcesByParam.put(parameterName, translatedHeatResource);
165   }
166
167   public Map<String, ServiceTemplate> getGlobalServiceTemplates() {
168     return globalServiceTemplates;
169   }
170
171
172 }