736318bcab188a26e7cb225ff036561244c2efca
[sdc.git] /
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.datatypes.heattotosca;
22
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;
43
44 import java.io.InputStream;
45 import java.util.HashMap;
46 import java.util.HashSet;
47 import java.util.Map;
48 import java.util.Objects;
49 import java.util.Optional;
50 import java.util.Set;
51
52
53 public class TranslationContext {
54
55
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;
61
62   static {
63     Configuration config = ConfigurationManager.lookup();
64     String propertyFileName = SdcCommon.HEAT_TO_TOSCA_MAPPING_CONF;
65     translationMapping =
66         config.generateMap(ConfigConstants.MAPPING_NAMESPACE, ConfigConstants.RESOURCE_MAPPING_KEY);
67     try {
68       globalServiceTemplates = GlobalTypesGenerator.getGlobalTypesServiceTemplate();
69     } catch (Exception e) {
70       throw new RuntimeException("Failed to load GlobalTypes", e);
71     }
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);
80
81   }
82
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();
107
108   public static Map<String, ImplementationConfiguration>
109   getSupportedConsolidationComputeResources() {
110     return supportedConsolidationComputeResources;
111   }
112
113   public static void setSupportedConsolidationComputeResources(
114       Map<String, ImplementationConfiguration> supportedConsolidationComputeResources) {
115     TranslationContext.supportedConsolidationComputeResources =
116         supportedConsolidationComputeResources;
117   }
118
119   public static Map<String, ImplementationConfiguration> getSupportedConsolidationPortResources() {
120     return supportedConsolidationPortResources;
121   }
122
123   public static void setSupportedConsolidationPortResources(
124       Map<String, ImplementationConfiguration> supportedConsolidationPortResources) {
125     TranslationContext.supportedConsolidationPortResources = supportedConsolidationPortResources;
126   }
127
128   /**
129    * Get nameExtractor implemetation class instance.
130    *
131    * @param extractorImplKey configuration key for the implementation class
132    * @return implemetation class instance
133    */
134   public static NameExtractor getNameExtractorImpl(String extractorImplKey) {
135     String nameExtractorImplClassName =
136         nameExtractorImplMap.get(extractorImplKey).getImplementationClass();
137
138     return CommonMethods.newInstance(nameExtractorImplClassName, NameExtractor.class);
139   }
140
141   public Map<String, UnifiedSubstitutionData> getUnifiedSubstitutionData() {
142     return unifiedSubstitutionData;
143   }
144
145   public void setUnifiedSubstitutionData(
146       Map<String, UnifiedSubstitutionData> unifiedSubstitutionData) {
147     this.unifiedSubstitutionData = unifiedSubstitutionData;
148   }
149
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);
158   }
159
160   public NodeTemplate getCleanedNodeTemplate(String serviceTemplateName,
161                                              String nodeTemplateId) {
162     return this.unifiedSubstitutionData.get(serviceTemplateName)
163         .getCleanedNodeTemplate(nodeTemplateId);
164   }
165
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);
172   }
173
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);
178   }
179
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);
185   }
186
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);
191   }
192
193   public ConsolidationData getConsolidationData() {
194     return consolidationData;
195   }
196
197   public void setConsolidationData(ConsolidationData consolidationData) {
198     this.consolidationData = consolidationData;
199   }
200
201   public void addManifestFile(String fileName, FileData.Type fileType) {
202     this.manifestFiles.put(fileName, fileType);
203   }
204
205   public Set<String> getNestedHeatsFiles() {
206     return nestedHeatsFiles;
207   }
208
209   public Map<String, Set<String>> getHeatStackGroupMembers() {
210     return heatStackGroupMembers;
211   }
212
213   public FileContentHandler getFiles() {
214     return files;
215   }
216
217   public void setFiles(Map<String, byte[]> files) {
218     this.files.putAll(files);
219   }
220
221   public InputStream getFileContent(String fileName) {
222     return files.getFileContent(fileName);
223   }
224
225   public void addFile(String name, byte[] content) {
226     files.addFile(name, content);
227   }
228
229   public ManifestFile getManifest() {
230     return manifest;
231   }
232
233   public void setManifest(ManifestFile manifest) {
234     this.manifest = manifest;
235   }
236
237   public Map<String, Set<String>> getTranslatedResources() {
238     return translatedResources;
239   }
240
241   public Map<String, Map<String, String>> getTranslatedIds() {
242     return translatedIds;
243   }
244
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))) {
251       return null;
252     }
253     if (Objects.isNull(translationMapping.get(resourceType).get(elementType))) {
254       return null;
255     }
256     return translationMapping.get(resourceType).get(elementType).get(elementName);
257   }
258
259   public Map<String, String> getElementMapping(String resourceType, String elementType) {
260     if (Objects.isNull(translationMapping.get(resourceType))) {
261       return null;
262     }
263     return translationMapping.get(resourceType).get(elementType);
264   }
265
266   public Set<String> getElementSet(String resourceType, String elementType) {
267     if (Objects.isNull(translationMapping.get(resourceType))) {
268       return new HashSet<>();
269     }
270     if (Objects.isNull(translationMapping.get(resourceType).get(elementType))) {
271       return new HashSet<>();
272     }
273     return translationMapping.get(resourceType).get(elementType).keySet();
274   }
275
276   public Map<String, ServiceTemplate> getTranslatedServiceTemplates() {
277     return translatedServiceTemplates;
278   }
279
280   public ServiceTemplate getGlobalSubstitutionServiceTemplate() {
281     return getTranslatedServiceTemplates().get(Constants.GLOBAL_SUBSTITUTION_TYPES_TEMPLATE_NAME);
282   }
283
284   public FileContentHandler getExternalArtifacts() {
285     return externalArtifacts;
286   }
287
288   public void addExternalArtifacts(String name, byte[] content) {
289     this.externalArtifacts.addFile(name, content);
290   }
291
292   public Map<String, TranslatedHeatResource> getHeatSharedResourcesByParam() {
293     return heatSharedResourcesByParam;
294   }
295
296   public void addHeatSharedResourcesByParam(String parameterName, String resourceId,
297                                             Resource resource) {
298     this.addHeatSharedResourcesByParam(parameterName,
299         new TranslatedHeatResource(resourceId, resource));
300   }
301
302   private void addHeatSharedResourcesByParam(String parameterName,
303                                             TranslatedHeatResource translatedHeatResource) {
304     this.heatSharedResourcesByParam.put(parameterName, translatedHeatResource);
305   }
306
307   public Map<String, ServiceTemplate> getGlobalServiceTemplates() {
308     return globalServiceTemplates;
309   }
310
311   public Map<String, String> getNestedHeatFileName() {
312     return nestedHeatFileName;
313   }
314
315   public void addNestedHeatFileName(String substituteServiceTempalteName,
316                                     String nestedHeatFileName) {
317     this.nestedHeatFileName.put(substituteServiceTempalteName, nestedHeatFileName);
318   }
319
320   public Map<String, Map<String, String>> getUsedHeatPseudoParams() {
321     return usedHeatPseudoParams;
322   }
323
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<>());
328     }
329     this.usedHeatPseudoParams.get(heatFileName).put(heatPseudoParam, translatedToscaParam);
330   }
331
332   /**
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
335    *
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
339    */
340   public void addUnifiedSubstitutionData(String serviceTemplateFileName,
341                                          String originalNodeTemplateId,
342                                          String abstractNodeTemplateId) {
343
344     Map<String, String> nodeAbstractNodeTemplateIdMap = this.getUnifiedSubstitutionData()
345         .computeIfAbsent(serviceTemplateFileName, k -> new UnifiedSubstitutionData())
346         .getNodesRelatedAbstractNode();
347
348     if (nodeAbstractNodeTemplateIdMap == null) {
349       nodeAbstractNodeTemplateIdMap = new HashMap<>();
350     }
351     nodeAbstractNodeTemplateIdMap.put(originalNodeTemplateId, abstractNodeTemplateId);
352     this.getUnifiedSubstitutionData().get(serviceTemplateFileName).setNodesRelatedAbstractNode(
353         nodeAbstractNodeTemplateIdMap);
354   }
355
356   /**
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
359    *
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
363    *                                                  service template
364    */
365   public void addSubstitutionServiceTemplateUnifiedSubstitutionData(String serviceTemplateFileName,
366                                                 String originalNodeTemplateId,
367                                                 String substitutionServiceTemplateNodeTemplateId) {
368
369     Map<String, String> nodesRelatedSubstitutionServiceTemplateNodeTemplateIdMap = this
370         .getUnifiedSubstitutionData()
371         .computeIfAbsent(serviceTemplateFileName, k -> new UnifiedSubstitutionData())
372         .getNodesRelatedSubstitutionServiceTemplateNode();
373
374     if (nodesRelatedSubstitutionServiceTemplateNodeTemplateIdMap == null) {
375       nodesRelatedSubstitutionServiceTemplateNodeTemplateIdMap = new HashMap<>();
376     }
377     nodesRelatedSubstitutionServiceTemplateNodeTemplateIdMap.put(originalNodeTemplateId,
378         substitutionServiceTemplateNodeTemplateId);
379     this.getUnifiedSubstitutionData().get(serviceTemplateFileName)
380         .setNodesRelatedSubstitutionServiceTemplateNode(
381             nodesRelatedSubstitutionServiceTemplateNodeTemplateIdMap);
382   }
383
384   /**
385    * Get unified abstract node template which is mapped to the input node template id.
386    *
387    * @param serviceTemplate the service template
388    * @param nodeTemplateId  the node template id
389    */
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);
395   }
396
397   /**
398    * Get unified node template in the substitution service template which is mapped to the
399    * original input node template id.
400    *
401    * @param serviceTemplate the service template
402    * @param nodeTemplateId  the node template id
403    */
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);
410   }
411
412   public int getHandledNestedComputeNodeTemplateIndex(String serviceTemplateName,
413                                                       String computeType){
414     return this.unifiedSubstitutionData.get(serviceTemplateName)
415         .getHandledNestedComputeNodeTemplateIndex(computeType);
416   }
417
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);
424   }
425
426   public boolean isNestedServiceTemplateWasHandled(String serviceTemplateName,
427                                                    String nestedServiceTemplateFileName){
428     if(Objects.isNull(this.unifiedSubstitutionData.get(serviceTemplateName))){
429       return false;
430     }
431     return this.unifiedSubstitutionData.get(serviceTemplateName)
432         .isNestedServiceTemplateWasHandled(nestedServiceTemplateFileName);
433   }
434
435   public void addNestedFileToUsedNestedComputeType(String serviceTemplateName,
436                                                    String nestedServiceTemplateFileName,
437                                                    String computeType){
438     this.unifiedSubstitutionData.putIfAbsent(serviceTemplateName, new UnifiedSubstitutionData());
439
440     this.unifiedSubstitutionData.get(serviceTemplateName).addNestedFileToUsedNestedComputeType
441         (computeType, nestedServiceTemplateFileName);
442   }
443
444   public int getGlobalNodeTypeIndex(String serviceTemplateName,
445                                     String computeType){
446     if(Objects.isNull(this.unifiedSubstitutionData.get(serviceTemplateName))){
447       return 0;
448     }
449     return this.unifiedSubstitutionData.get(serviceTemplateName).getGlobalNodeTypeIndex
450         (computeType);
451   }
452 }