aboutsummaryrefslogtreecommitdiffstats
path: root/openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/src/main/java/org/openecomp/sdc/translator/datatypes/heattotosca/TranslationContext.java
blob: 736318bcab188a26e7cb225ff036561244c2efca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
/*-
 * ============LICENSE_START=======================================================
 * SDC
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.openecomp.sdc.translator.datatypes.heattotosca;

import org.openecomp.config.api.Configuration;
import org.openecomp.config.api.ConfigurationManager;
import org.openecomp.core.utilities.CommonMethods;
import org.openecomp.core.utilities.file.FileContentHandler;
import org.openecomp.sdc.common.utils.SdcCommon;
import org.openecomp.sdc.datatypes.configuration.ImplementationConfiguration;
import org.openecomp.sdc.heat.datatypes.manifest.FileData;
import org.openecomp.sdc.heat.datatypes.manifest.ManifestFile;
import org.openecomp.sdc.heat.datatypes.model.Resource;
import org.openecomp.sdc.tosca.datatypes.model.NodeTemplate;
import org.openecomp.sdc.tosca.datatypes.model.ServiceTemplate;
import org.openecomp.sdc.tosca.services.ToscaUtil;
import org.openecomp.sdc.translator.datatypes.heattotosca.to.TranslatedHeatResource;
import org.openecomp.sdc.translator.datatypes.heattotosca.unifiedmodel.composition.UnifiedCompositionEntity;
import org.openecomp.sdc.translator.datatypes.heattotosca.unifiedmodel.composition.UnifiedSubstitutionData;
import org.openecomp.sdc.translator.datatypes.heattotosca.unifiedmodel.consolidation.ConsolidationData;
import org.openecomp.sdc.translator.services.heattotosca.ConfigConstants;
import org.openecomp.sdc.translator.services.heattotosca.Constants;
import org.openecomp.sdc.translator.services.heattotosca.NameExtractor;
import org.openecomp.sdc.translator.services.heattotosca.globaltypes.GlobalTypesGenerator;

import java.io.InputStream;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;


public class TranslationContext {


  private static Map<String, Map<String, Map<String, String>>> translationMapping;
  private static Map<String, ServiceTemplate> globalServiceTemplates;
  private static Map<String, ImplementationConfiguration> nameExtractorImplMap;
  private static Map<String, ImplementationConfiguration> supportedConsolidationComputeResources;
  private static Map<String, ImplementationConfiguration> supportedConsolidationPortResources;

  static {
    Configuration config = ConfigurationManager.lookup();
    String propertyFileName = SdcCommon.HEAT_TO_TOSCA_MAPPING_CONF;
    translationMapping =
        config.generateMap(ConfigConstants.MAPPING_NAMESPACE, ConfigConstants.RESOURCE_MAPPING_KEY);
    try {
      globalServiceTemplates = GlobalTypesGenerator.getGlobalTypesServiceTemplate();
    } catch (Exception e) {
      throw new RuntimeException("Failed to load GlobalTypes", e);
    }
    nameExtractorImplMap = config.populateMap(ConfigConstants.TRANSLATOR_NAMESPACE,
        ConfigConstants.NAMING_CONVENTION_EXTRACTOR_IMPL_KEY, ImplementationConfiguration.class);
    supportedConsolidationComputeResources = config.populateMap(ConfigConstants
        .MANDATORY_UNIFIED_MODEL_NAMESPACE, ConfigConstants
        .SUPPORTED_CONSOLIDATION_COMPUTE_RESOURCES_KEY, ImplementationConfiguration.class);
    supportedConsolidationPortResources = config.populateMap(ConfigConstants
        .MANDATORY_UNIFIED_MODEL_NAMESPACE, ConfigConstants
        .SUPPORTED_CONSOLIDATION_PORT_RESOURCES_KEY, ImplementationConfiguration.class);

  }

  private Map<String, UnifiedSubstitutionData> unifiedSubstitutionData = new HashMap<>();
  private ManifestFile manifest;
  private FileContentHandler files = new FileContentHandler();
  private Map<String, FileData.Type> manifestFiles = new HashMap<>();
  //Key - file name, value - file type
  private Set<String> nestedHeatsFiles = new HashSet<>();
  private FileContentHandler externalArtifacts = new FileContentHandler();
  // Key - heat file name,value - set of heat resource ids which were translated
  private Map<String, Set<String>> translatedResources = new HashMap<>();
  // Key - heat file name, value - translated Node template id
  private Map<String, Set<String>> heatStackGroupMembers = new HashMap<>();
  // Key - heat file name, value - Map with Key - heat resource Id, Value - tosca entity template id
  private Map<String, Map<String, String>> translatedIds = new HashMap<>();
  // key - service template type, value - translated service templates
  private Map<String, ServiceTemplate> translatedServiceTemplates = new HashMap<>();
  //key - heat param name, value - shared resource data
  private Map<String, TranslatedHeatResource> heatSharedResourcesByParam = new HashMap<>();
  //key - translated substitute service template file name, value - source nested heat file name
  private Map<String, String> nestedHeatFileName = new HashMap<>();
  //Key - heat file name,value - Map eith key - heat pseudo param name,
  // value - translated tosca parameter name
  private Map<String, Map<String, String>> usedHeatPseudoParams = new HashMap<>();
  //Consolidation data gathered for Unified TOSCA model
  private ConsolidationData consolidationData = new ConsolidationData();

  public static Map<String, ImplementationConfiguration>
  getSupportedConsolidationComputeResources() {
    return supportedConsolidationComputeResources;
  }

  public static void setSupportedConsolidationComputeResources(
      Map<String, ImplementationConfiguration> supportedConsolidationComputeResources) {
    TranslationContext.supportedConsolidationComputeResources =
        supportedConsolidationComputeResources;
  }

  public static Map<String, ImplementationConfiguration> getSupportedConsolidationPortResources() {
    return supportedConsolidationPortResources;
  }

  public static void setSupportedConsolidationPortResources(
      Map<String, ImplementationConfiguration> supportedConsolidationPortResources) {
    TranslationContext.supportedConsolidationPortResources = supportedConsolidationPortResources;
  }

  /**
   * Get nameExtractor implemetation class instance.
   *
   * @param extractorImplKey configuration key for the implementation class
   * @return implemetation class instance
   */
  public static NameExtractor getNameExtractorImpl(String extractorImplKey) {
    String nameExtractorImplClassName =
        nameExtractorImplMap.get(extractorImplKey).getImplementationClass();

    return CommonMethods.newInstance(nameExtractorImplClassName, NameExtractor.class);
  }

  public Map<String, UnifiedSubstitutionData> getUnifiedSubstitutionData() {
    return unifiedSubstitutionData;
  }

  public void setUnifiedSubstitutionData(
      Map<String, UnifiedSubstitutionData> unifiedSubstitutionData) {
    this.unifiedSubstitutionData = unifiedSubstitutionData;
  }

  public void addCleanedNodeTemplate(String serviceTemplateName,
                                     String nodeTemplateId,
                                     UnifiedCompositionEntity unifiedCompositionEntity,
                                     NodeTemplate nodeTemplate) {
    this.unifiedSubstitutionData.putIfAbsent(serviceTemplateName, new UnifiedSubstitutionData());
    this.unifiedSubstitutionData
        .get(serviceTemplateName)
        .addCleanedNodeTemplate(nodeTemplateId, unifiedCompositionEntity, nodeTemplate);
  }

  public NodeTemplate getCleanedNodeTemplate(String serviceTemplateName,
                                             String nodeTemplateId) {
    return this.unifiedSubstitutionData.get(serviceTemplateName)
        .getCleanedNodeTemplate(nodeTemplateId);
  }

  public void addUnifiedNestedNodeTemplateId(String serviceTemplateName,
                                             String nestedNodeTemplateId,
                                             String unifiedNestedNodeTemplateId) {
    this.unifiedSubstitutionData.putIfAbsent(serviceTemplateName, new UnifiedSubstitutionData());
    this.unifiedSubstitutionData.get(serviceTemplateName)
        .addUnifiedNestedNodeTemplateId(nestedNodeTemplateId, unifiedNestedNodeTemplateId);
  }

  public Optional<String> getUnifiedNestedNodeTemplateId(String serviceTemplateName,
                                                         String nestedNodeTemplateId) {
    return this.unifiedSubstitutionData.get(serviceTemplateName) == null ? Optional.empty()
            :this.unifiedSubstitutionData.get(serviceTemplateName).getUnifiedNestedNodeTemplateId(nestedNodeTemplateId);
  }

  public void addUnifiedNestedNodeTypeId(String serviceTemplateName,
                                             String nestedNodeTypeId,
                                             String unifiedNestedNodeTypeId){
    this.unifiedSubstitutionData.putIfAbsent(serviceTemplateName, new UnifiedSubstitutionData());
    this.unifiedSubstitutionData.get(serviceTemplateName).addUnifiedNestedNodeTypeId(nestedNodeTypeId, unifiedNestedNodeTypeId);
  }

  public Optional<String> getUnifiedNestedNodeTypeId(String serviceTemplateName,
                                                     String nestedNodeTemplateId) {
    return this.unifiedSubstitutionData.get(serviceTemplateName) == null ? Optional.empty()
            : this.unifiedSubstitutionData.get(serviceTemplateName).getUnifiedNestedNodeTypeId(nestedNodeTemplateId);
  }

  public ConsolidationData getConsolidationData() {
    return consolidationData;
  }

  public void setConsolidationData(ConsolidationData consolidationData) {
    this.consolidationData = consolidationData;
  }

  public void addManifestFile(String fileName, FileData.Type fileType) {
    this.manifestFiles.put(fileName, fileType);
  }

  public Set<String> getNestedHeatsFiles() {
    return nestedHeatsFiles;
  }

  public Map<String, Set<String>> getHeatStackGroupMembers() {
    return heatStackGroupMembers;
  }

  public FileContentHandler getFiles() {
    return files;
  }

  public void setFiles(Map<String, byte[]> files) {
    this.files.putAll(files);
  }

  public InputStream getFileContent(String fileName) {
    return files.getFileContent(fileName);
  }

  public void addFile(String name, byte[] content) {
    files.addFile(name, content);
  }

  public ManifestFile getManifest() {
    return manifest;
  }

  public void setManifest(ManifestFile manifest) {
    this.manifest = manifest;
  }

  public Map<String, Set<String>> getTranslatedResources() {
    return translatedResources;
  }

  public Map<String, Map<String, String>> getTranslatedIds() {
    return translatedIds;
  }

  // get tosca name from mapping configuration file
  //element type - parameter/attribute
  // element name - heat parameter/attribute name
  //return value - tosca parameter/attribute name
  public String getElementMapping(String resourceType, String elementType, String elementName) {
    if (Objects.isNull(translationMapping.get(resourceType))) {
      return null;
    }
    if (Objects.isNull(translationMapping.get(resourceType).get(elementType))) {
      return null;
    }
    return translationMapping.get(resourceType).get(elementType).get(elementName);
  }

  public Map<String, String> getElementMapping(String resourceType, String elementType) {
    if (Objects.isNull(translationMapping.get(resourceType))) {
      return null;
    }
    return translationMapping.get(resourceType).get(elementType);
  }

  public Set<String> getElementSet(String resourceType, String elementType) {
    if (Objects.isNull(translationMapping.get(resourceType))) {
      return new HashSet<>();
    }
    if (Objects.isNull(translationMapping.get(resourceType).get(elementType))) {
      return new HashSet<>();
    }
    return translationMapping.get(resourceType).get(elementType).keySet();
  }

  public Map<String, ServiceTemplate> getTranslatedServiceTemplates() {
    return translatedServiceTemplates;
  }

  public ServiceTemplate getGlobalSubstitutionServiceTemplate() {
    return getTranslatedServiceTemplates().get(Constants.GLOBAL_SUBSTITUTION_TYPES_TEMPLATE_NAME);
  }

  public FileContentHandler getExternalArtifacts() {
    return externalArtifacts;
  }

  public void addExternalArtifacts(String name, byte[] content) {
    this.externalArtifacts.addFile(name, content);
  }

  public Map<String, TranslatedHeatResource> getHeatSharedResourcesByParam() {
    return heatSharedResourcesByParam;
  }

  public void addHeatSharedResourcesByParam(String parameterName, String resourceId,
                                            Resource resource) {
    this.addHeatSharedResourcesByParam(parameterName,
        new TranslatedHeatResource(resourceId, resource));
  }

  private void addHeatSharedResourcesByParam(String parameterName,
                                            TranslatedHeatResource translatedHeatResource) {
    this.heatSharedResourcesByParam.put(parameterName, translatedHeatResource);
  }

  public Map<String, ServiceTemplate> getGlobalServiceTemplates() {
    return globalServiceTemplates;
  }

  public Map<String, String> getNestedHeatFileName() {
    return nestedHeatFileName;
  }

  public void addNestedHeatFileName(String substituteServiceTempalteName,
                                    String nestedHeatFileName) {
    this.nestedHeatFileName.put(substituteServiceTempalteName, nestedHeatFileName);
  }

  public Map<String, Map<String, String>> getUsedHeatPseudoParams() {
    return usedHeatPseudoParams;
  }

  public void addUsedHeatPseudoParams(String heatFileName, String heatPseudoParam, String
      translatedToscaParam) {
    if (Objects.isNull(this.usedHeatPseudoParams.get(heatFileName))) {
      this.usedHeatPseudoParams.put(heatFileName, new HashMap<>());
    }
    this.usedHeatPseudoParams.get(heatFileName).put(heatPseudoParam, translatedToscaParam);
  }

  /**
   * Add the unified substitution data info in context. Contains a mapping of original node
   * template id and the new node template id in the abstract substitute
   *
   * @param serviceTemplateFileName the service template file name
   * @param originalNodeTemplateId  the original node template id
   * @param abstractNodeTemplateId  the node template id in the abstract substitute
   */
  public void addUnifiedSubstitutionData(String serviceTemplateFileName,
                                         String originalNodeTemplateId,
                                         String abstractNodeTemplateId) {

    Map<String, String> nodeAbstractNodeTemplateIdMap = this.getUnifiedSubstitutionData()
        .computeIfAbsent(serviceTemplateFileName, k -> new UnifiedSubstitutionData())
        .getNodesRelatedAbstractNode();

    if (nodeAbstractNodeTemplateIdMap == null) {
      nodeAbstractNodeTemplateIdMap = new HashMap<>();
    }
    nodeAbstractNodeTemplateIdMap.put(originalNodeTemplateId, abstractNodeTemplateId);
    this.getUnifiedSubstitutionData().get(serviceTemplateFileName).setNodesRelatedAbstractNode(
        nodeAbstractNodeTemplateIdMap);
  }

  /**
   * Add the unified substitution data info in context. Contains a mapping of original node
   * template id and the new node template id in the abstract substitute
   *
   * @param serviceTemplateFileName                   the service template file name
   * @param originalNodeTemplateId                    the original node template id
   * @param substitutionServiceTemplateNodeTemplateId the node template id in the substitution
   *                                                  service template
   */
  public void addSubstitutionServiceTemplateUnifiedSubstitutionData(String serviceTemplateFileName,
                                                String originalNodeTemplateId,
                                                String substitutionServiceTemplateNodeTemplateId) {

    Map<String, String> nodesRelatedSubstitutionServiceTemplateNodeTemplateIdMap = this
        .getUnifiedSubstitutionData()
        .computeIfAbsent(serviceTemplateFileName, k -> new UnifiedSubstitutionData())
        .getNodesRelatedSubstitutionServiceTemplateNode();

    if (nodesRelatedSubstitutionServiceTemplateNodeTemplateIdMap == null) {
      nodesRelatedSubstitutionServiceTemplateNodeTemplateIdMap = new HashMap<>();
    }
    nodesRelatedSubstitutionServiceTemplateNodeTemplateIdMap.put(originalNodeTemplateId,
        substitutionServiceTemplateNodeTemplateId);
    this.getUnifiedSubstitutionData().get(serviceTemplateFileName)
        .setNodesRelatedSubstitutionServiceTemplateNode(
            nodesRelatedSubstitutionServiceTemplateNodeTemplateIdMap);
  }

  /**
   * Get unified abstract node template which is mapped to the input node template id.
   *
   * @param serviceTemplate the service template
   * @param nodeTemplateId  the node template id
   */
  public String getUnifiedAbstractNodeTemplateId(ServiceTemplate serviceTemplate,
                                                 String nodeTemplateId) {
    UnifiedSubstitutionData unifiedSubstitutionData =
        this.unifiedSubstitutionData.get(ToscaUtil.getServiceTemplateFileName(serviceTemplate));
    return unifiedSubstitutionData.getNodesRelatedAbstractNode().get(nodeTemplateId);
  }

  /**
   * Get unified node template in the substitution service template which is mapped to the
   * original input node template id.
   *
   * @param serviceTemplate the service template
   * @param nodeTemplateId  the node template id
   */
  public String getUnifiedSubstitutionNodeTemplateId(ServiceTemplate serviceTemplate,
                                                     String nodeTemplateId) {
    UnifiedSubstitutionData unifiedSubstitutionData =
        this.unifiedSubstitutionData.get(ToscaUtil.getServiceTemplateFileName(serviceTemplate));
    return unifiedSubstitutionData.getNodesRelatedSubstitutionServiceTemplateNode()
        .get(nodeTemplateId);
  }

  public int getHandledNestedComputeNodeTemplateIndex(String serviceTemplateName,
                                                      String computeType){
    return this.unifiedSubstitutionData.get(serviceTemplateName)
        .getHandledNestedComputeNodeTemplateIndex(computeType);
  }

  public void updateHandledComputeType(String serviceTemplateName,
                                       String nestedServiceTemplateFileName,
                                       String handledComputeType){
    this.unifiedSubstitutionData.putIfAbsent(serviceTemplateName, new UnifiedSubstitutionData());
    this.unifiedSubstitutionData.get(serviceTemplateName)
        .addHandledComputeType(nestedServiceTemplateFileName, handledComputeType);
  }

  public boolean isNestedServiceTemplateWasHandled(String serviceTemplateName,
                                                   String nestedServiceTemplateFileName){
    if(Objects.isNull(this.unifiedSubstitutionData.get(serviceTemplateName))){
      return false;
    }
    return this.unifiedSubstitutionData.get(serviceTemplateName)
        .isNestedServiceTemplateWasHandled(nestedServiceTemplateFileName);
  }

  public void addNestedFileToUsedNestedComputeType(String serviceTemplateName,
                                                   String nestedServiceTemplateFileName,
                                                   String computeType){
    this.unifiedSubstitutionData.putIfAbsent(serviceTemplateName, new UnifiedSubstitutionData());

    this.unifiedSubstitutionData.get(serviceTemplateName).addNestedFileToUsedNestedComputeType
        (computeType, nestedServiceTemplateFileName);
  }

  public int getGlobalNodeTypeIndex(String serviceTemplateName,
                                    String computeType){
    if(Objects.isNull(this.unifiedSubstitutionData.get(serviceTemplateName))){
      return 0;
    }
    return this.unifiedSubstitutionData.get(serviceTemplateName).getGlobalNodeTypeIndex
        (computeType);
  }
}