summaryrefslogtreecommitdiffstats
path: root/openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/src/main/java/org/openecomp/sdc/translator/services/heattotosca/impl/functiontranslation/FunctionTranslationGetAttrImpl.java
blob: cc188d71a6ca3a3aabfe4b6efe5df57bd6c344dd (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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
/*-
 * ============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.services.heattotosca.impl.functiontranslation;

import org.openecomp.core.utilities.yaml.YamlUtil;
import org.openecomp.sdc.heat.datatypes.model.HeatOrchestrationTemplate;
import org.openecomp.sdc.heat.datatypes.model.Resource;
import org.openecomp.sdc.heat.services.HeatConstants;
import org.openecomp.sdc.tosca.datatypes.ToscaFunctions;
import org.openecomp.sdc.tosca.datatypes.model.NodeTemplate;
import org.openecomp.sdc.tosca.datatypes.model.ServiceTemplate;
import org.openecomp.sdc.tosca.datatypes.model.Template;
import org.openecomp.sdc.translator.datatypes.heattotosca.TranslationContext;
import org.openecomp.sdc.translator.datatypes.heattotosca.unifiedmodel.consolidation.EntityConsolidationData;
import org.openecomp.sdc.translator.services.heattotosca.ConfigConstants;
import org.openecomp.sdc.translator.services.heattotosca.ConsolidationDataUtil;
import org.openecomp.sdc.translator.services.heattotosca.Constants;
import org.openecomp.sdc.translator.services.heattotosca.FunctionTranslation;
import org.openecomp.sdc.translator.services.heattotosca.FunctionTranslationFactory;
import org.openecomp.sdc.translator.services.heattotosca.HeatToToscaUtil;
import org.openecomp.sdc.translator.services.heattotosca.NameExtractor;
import org.openecomp.sdc.translator.services.heattotosca.helper.FunctionTranslationHelper;
import org.openecomp.sdc.translator.services.heattotosca.impl.resourcetranslation.ResourceTranslationBase;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

public class FunctionTranslationGetAttrImpl implements FunctionTranslation {


  @Override
  public Object translateFunction(ServiceTemplate serviceTemplate,
                                  String resourceId, String propertyName, String functionKey,
                                  Object functionValue, String heatFileName,
                                  HeatOrchestrationTemplate heatOrchestrationTemplate,
                                  Template toscaTemplate, TranslationContext context) {
    Object returnValue = new HashMap<>();
    List<Object> attributeFunctionExpression =
        translateGetAttributeFunctionExpression(serviceTemplate, resourceId, functionValue,
            propertyName, heatFileName, heatOrchestrationTemplate, (NodeTemplate) toscaTemplate,
            context);
    if (FunctionTranslationHelper.isResourceSupported(attributeFunctionExpression.get(0).toString())
        && FunctionTranslationHelper.isAttributeSupported(attributeFunctionExpression.get(0)
        .toString())) {
      ((Map) returnValue)
          .put(ToscaFunctions.GET_ATTRIBUTE.getDisplayName(), attributeFunctionExpression);
    } else {
      returnValue = attributeFunctionExpression;
    }

    return returnValue;
  }

  private static List<Object> translateGetAttributeFunctionExpression(
      ServiceTemplate serviceTemplate,
      String resourceId,
      Object functionValue,
      String propertyName,
      String heatFileName,
      HeatOrchestrationTemplate heatOrchestrationTemplate,
      NodeTemplate nodeTemplate,
      TranslationContext context) {

    List<Object> attributeParamList = (List) functionValue;
    List<Object> toscaAttributeParamList = new ArrayList<>();

    Optional<String> targetResourceTranslatedId = Optional.empty();
    String targetResourceId = null;
    if( attributeParamList.get(0) instanceof String) {
      targetResourceId = (String) attributeParamList.get(0);
      targetResourceTranslatedId =
          handleResourceName(targetResourceId, heatFileName, heatOrchestrationTemplate,
              context);
    }
    if (!targetResourceTranslatedId.isPresent()) {
      //unsupported resource
      toscaAttributeParamList
          .add(
              FunctionTranslationHelper.getUnsupportedResourcePrefix() + attributeParamList.get(0));
      return toscaAttributeParamList;
    } else {
      toscaAttributeParamList.add(targetResourceTranslatedId.get());
    }

    Optional<List<Object>> toscaAttList =
        handleAttributeName(attributeParamList, heatOrchestrationTemplate, propertyName,
            heatFileName, serviceTemplate,
            context);
    if (!toscaAttList.isPresent()) {
      //Unsupported attribute
      toscaAttributeParamList.clear();
      toscaAttributeParamList
          .add(FunctionTranslationHelper.getUnsupportedAttributePrefix() + attributeParamList.get(0)
              + "." + attributeParamList.get(1));
      return toscaAttributeParamList;
    } else {
      toscaAttributeParamList.addAll(toscaAttList.get());

      handleGetAttrConsolidationData(serviceTemplate, resourceId, propertyName, heatFileName,
          heatOrchestrationTemplate, context, targetResourceId,
          targetResourceTranslatedId,
          toscaAttList.get());
    }

    Optional<List<Object>> toscaIndexOrKey = handleAttributeIndexOrKey(serviceTemplate,
        resourceId, propertyName, HeatToToscaUtil
            .getResourceType((String) attributeParamList.get(0), heatOrchestrationTemplate,
                heatFileName), attributeParamList, context, heatFileName,
        heatOrchestrationTemplate);
    toscaIndexOrKey.ifPresent(toscaAttributeParamList::addAll);

    return toscaAttributeParamList;
  }

  private static void handleGetAttrConsolidationData(
      ServiceTemplate serviceTemplate,
      String resourceId, String propertyName,
      String heatFileName,
      HeatOrchestrationTemplate heatOrchestrationTemplate,
      TranslationContext context,
      String targetResourceId,
      Optional<String> targetResourceTranslatedId,
      List<Object> toscaAttList) {

    Optional<String> resourceTranslatedId = Optional.empty();
    if (resourceId != null) {
      resourceTranslatedId =
          handleResourceName(resourceId, heatFileName, heatOrchestrationTemplate,
              context);
      resourceTranslatedId
          .ifPresent(resourceTranslatedIdValue -> handleGetAttrOutConsolidationData(serviceTemplate,
              propertyName,
              heatOrchestrationTemplate, context, resourceId, targetResourceTranslatedId.get(),
              resourceTranslatedIdValue, toscaAttList, heatFileName));
    }

    if (targetResourceTranslatedId.isPresent()) {
      handleGetAttrInConsolidationData(serviceTemplate, resourceId, resourceTranslatedId,
          propertyName, heatOrchestrationTemplate, context, targetResourceId,
          targetResourceTranslatedId.get(), toscaAttList, heatFileName);
    }
  }

  private static void handleGetAttrOutConsolidationData(
      ServiceTemplate serviceTemplate,
      String propertyName,
      HeatOrchestrationTemplate heatOrchestrationTemplate,
      TranslationContext context,
      String resourceId,
      String targetTranslatedResourceId,
      String resourceTranslatedId,
      List<Object> toscaAttList,
      String heatFileName) {
    if (serviceTemplate != null) {
      Optional<EntityConsolidationData> entityConsolidationData =
          getEntityConsolidationData(serviceTemplate, heatOrchestrationTemplate, context,
              resourceId, resourceTranslatedId, heatFileName);
      if (entityConsolidationData.isPresent()) {
        String attName = (String) toscaAttList.get(0);
        handleNodeGetAttrOut(targetTranslatedResourceId, propertyName, heatOrchestrationTemplate,
            context, resourceId, entityConsolidationData.get(), attName);
      }
    }
  }

  private static void handleGetAttrInConsolidationData(
      ServiceTemplate serviceTemplate,
      String resourceId,
      Optional<String> resourceTranslatedId,
      String propertyName,
      HeatOrchestrationTemplate heatOrchestrationTemplate,
      TranslationContext context,
      String targetResourceId,
      String targetResourceTranslatedId,
      List<Object> toscaAttList,
      String  heatFileName) {

    if (serviceTemplate != null) {
      Optional<EntityConsolidationData> entityConsolidationData =
          getEntityConsolidationData(serviceTemplate, heatOrchestrationTemplate, context,
              targetResourceId, targetResourceTranslatedId, heatFileName);
      if (entityConsolidationData.isPresent()) {
        String attName = (String) toscaAttList.get(0);
        if (resourceTranslatedId.isPresent()) {
          handleNodeGetAttrIn(resourceTranslatedId.get(), propertyName, heatOrchestrationTemplate,
              context,
              resourceId, entityConsolidationData.get(), attName);
        } else {
          ConsolidationDataUtil
              .updateOutputGetAttributeInConsolidationData(entityConsolidationData.get(),
                  propertyName, attName);
        }
      }
    }
  }

  private static void handleNodeGetAttrOut(String nodeTemplateId, String propertyName,
                                           HeatOrchestrationTemplate heatOrchestrationTemplate,
                                           TranslationContext context, String resourceId,
                                           EntityConsolidationData entityConsolidationData,
                                           String attName) {
    Resource resource = heatOrchestrationTemplate.getResources().get(resourceId);
    boolean isNestedResource = HeatToToscaUtil.isNestedResource(resource);
    String toscaPropertyName = propertyName;
    if (!isNestedResource) {
      toscaPropertyName = HeatToToscaUtil.getToscaPropertyName(context, resource
          .getType(), propertyName);
    }
    ConsolidationDataUtil
        .updateNodeGetAttributeOut(entityConsolidationData,
            nodeTemplateId, toscaPropertyName, attName);
  }

  private static void handleNodeGetAttrIn(String nodeTemplateId, String propertyName,
                                          HeatOrchestrationTemplate heatOrchestrationTemplate,
                                          TranslationContext context, String resourceId,
                                          EntityConsolidationData entityConsolidationData,
                                          String attName) {
    Resource resource = heatOrchestrationTemplate.getResources().get(resourceId);
    boolean isNestedResource = HeatToToscaUtil.isNestedResource(resource);
    String toscaPropertyName = propertyName;
    if (!isNestedResource) {
      toscaPropertyName = HeatToToscaUtil.getToscaPropertyName(context, resource
          .getType(), propertyName);
    }
    ConsolidationDataUtil
        .updateNodeGetAttributeIn(entityConsolidationData,
            nodeTemplateId, toscaPropertyName, attName);
  }

  private static Optional<EntityConsolidationData> getEntityConsolidationData(
      ServiceTemplate serviceTemplate,
      HeatOrchestrationTemplate heatOrchestrationTemplate,
      TranslationContext context,
      String resourceId,
      String resourceTranslatedId,
      String heatFileName) {
    Resource resource = heatOrchestrationTemplate.getResources().get(resourceId);
    if (ConsolidationDataUtil.isComputeResource(heatOrchestrationTemplate, resourceId)) {
      String resourceType = heatOrchestrationTemplate.getResources().get(resourceId).getType();
      NameExtractor nodeTypeNameExtractor =
          context.getNameExtractorImpl(resourceType);
      String computeType =
          nodeTypeNameExtractor.extractNodeTypeName(
              resource, resourceId, context.getTranslatedIds().get(heatFileName).get(resourceId));

      return Optional.of(
          ConsolidationDataUtil.getComputeTemplateConsolidationData(context, serviceTemplate,
              computeType, resourceId));
    } else if (ConsolidationDataUtil.isPortResource(heatOrchestrationTemplate, resourceId)) {
      return Optional.of(ConsolidationDataUtil
          .getPortTemplateConsolidationData(context, serviceTemplate, resourceId));
    } else if (HeatToToscaUtil.isNestedResource(resource)) {
      return Optional.of(ConsolidationDataUtil
          .getNestedTemplateConsolidationData(context, serviceTemplate, resourceId));
    }
    return Optional.empty();
  }

  private static Optional<List<Object>> handleAttributeIndexOrKey(
      ServiceTemplate serviceTemplate,
      String resourceId, String propertyName,
      String resourceType,
      List<Object> attributeParamList,
      TranslationContext context,
      String heatFileName,
      HeatOrchestrationTemplate heatOrchestrationTemplate) {

    List<Object> attributeIndexOrKey = new ArrayList<>();
    if (attributeParamList.size() < 3) {
      return Optional.empty();
    }

    Object attributeName = attributeParamList.get(1);
    for (int i = 2; i < attributeParamList.size(); i++) {

      if (isInteger(attributeParamList.get(i))) {
        attributeIndexOrKey.add(attributeParamList.get(i));
      } else if (attributeParamList.get(i) instanceof Map) {
        attributeIndexOrKey.add(getToscaAttributeValue(serviceTemplate, resourceId,
            propertyName, attributeParamList.get(i), resourceType, heatFileName,
            heatOrchestrationTemplate, null, context));

      } else {
        Object toscaAttributeName = resourceType == null ? null : context
            .getElementMapping(resourceType, Constants.ATTR,
                getAttributeFullPath(attributeParamList, i));
        if (toscaAttributeName == null) {
          toscaAttributeName = attributeParamList.get(i);
        }
        attributeIndexOrKey.add(toscaAttributeName);
      }
    }

    return Optional.of(attributeIndexOrKey);
  }

  private static String getAttributeFullPath(List<Object> attributeParamList, int attributeIndex) {
    if (attributeParamList.size() < 3) {
      return null;
    }
    StringBuffer attributeFullPath = new StringBuffer();
    attributeFullPath.append(attributeParamList.get(1));
    for (int j = 2; j <= attributeIndex; j++) {
      if (isInteger(attributeParamList.get(j))) {
        continue;
      }
      attributeFullPath.append(ConfigConstants.TRANS_MAPPING_DELIMITER_CHAR);
      attributeFullPath.append(attributeParamList.get(j));
    }
    return attributeFullPath.toString();
  }

  private static boolean isInteger(Object inputNumber) {
    if (inputNumber == null) {
      return false;
    }

    try {
      Integer.parseInt(String.valueOf(inputNumber));
      return true;
    } catch (NumberFormatException exception) {
      return false;
    }
  }

  private static Optional<String> handleResourceName(String resourceId, String heatFileName,
                                                     HeatOrchestrationTemplate
                                                         heatOrchestrationTemplate,
                                                     TranslationContext context) {
    return ResourceTranslationBase
        .getResourceTranslatedId(heatFileName, heatOrchestrationTemplate, resourceId, context);
  }

  private static Optional<List<Object>> handleAttributeName(List<Object> attributeParamList,
                                                            HeatOrchestrationTemplate
                                                                heatOrchestrationTemplate,
                                                            String propertyName,
                                                            String heatFileName,
                                                            ServiceTemplate serviceTemplate,
                                                            TranslationContext context) {
    String resourceId = (String) attributeParamList.get(0);
    Resource resource =
        HeatToToscaUtil.getResource(heatOrchestrationTemplate, resourceId, heatFileName);

    if (attributeParamList.size() == 1) {
      return getResourceTranslatedAttributesList(resource, context);
    }

    if(!(attributeParamList.get(1) instanceof String)){
      //todo - once dynamic attr name will be supported the commented line will be support it in
      // the first translation phase.
//      Object toscaAttributeValue = getToscaAttributeValue(serviceTemplate, resourceId, propertyName,
//          attributeParamList.get(1), resource
//              .getType(), heatFileName, heatOrchestrationTemplate, null, context);
//      List<Object> dynamicAttrValue = new ArrayList<>();
//      dynamicAttrValue.add(toscaAttributeValue);
//      return Optional.of(dynamicAttrValue);
      return Optional.empty();
    }

    if (HeatToToscaUtil.isNestedResource(resource)) {
      return getNestedResourceTranslatedAttribute((String) attributeParamList.get(1));
    } else {
      return getResourceTranslatedAttribute(resource, (String) attributeParamList.get(1), context);
    }
  }

  private static Optional<List<Object>> getNestedResourceTranslatedAttribute(String attributeName) {
    List<Object> translatedAttributesList = new ArrayList<>();

    if (attributeName.startsWith(HeatConstants.GET_ATTR_FROM_RESOURCE_GROUP_PREFIX)) {
      String[] attributeSplit = attributeName.split("\\.");
      if (attributeSplit.length == 2) {
        translatedAttributesList.add(attributeSplit[1]);
      } else if (attributeSplit.length == 3) {
        translatedAttributesList.add(attributeSplit[2]);
        translatedAttributesList.add(Integer.valueOf(attributeSplit[1]));
      } else {
        return Optional.empty();
      }
    } else {
      translatedAttributesList.add(attributeName);
    }
    return Optional.of(translatedAttributesList);
  }

  private static Optional<List<Object>> getResourceTranslatedAttributesList(Resource resource,
                                                                            TranslationContext
                                                                                context) {
    List<Object> translatedAttributes = new ArrayList<>();
    if (HeatToToscaUtil.isNestedResource(resource)) {
      Optional<String> nestedFile = HeatToToscaUtil.getNestedFile(resource);
      if (!nestedFile.isPresent()) {
        return Optional.empty();
      }
      HeatOrchestrationTemplate nestedHeatOrchestrationTemplate = new YamlUtil()
          .yamlToObject(context.getFiles().getFileContent(nestedFile.get()),
              HeatOrchestrationTemplate.class);
      translatedAttributes.addAll(nestedHeatOrchestrationTemplate.getOutputs().keySet());
      return Optional.of(translatedAttributes);

    } else {
      Map<String, String> resourceMappingAttributes =
          context.getElementMapping(resource.getType(), Constants.ATTR);
      if (resourceMappingAttributes == null) {
        return Optional.empty();
      }
      Set<String> mappingAttributes = new HashSet<>();
      mappingAttributes
          .addAll(resourceMappingAttributes.values().stream().collect(Collectors.toList()));
      translatedAttributes.addAll(mappingAttributes);
      return Optional.of(translatedAttributes);
    }
  }

  private static Optional<List<Object>> getResourceTranslatedAttribute(Resource resource,
                                                                       String attributeName,
                                                                       TranslationContext context) {
    List<Object> translatedAttributesList = new ArrayList<>();
    String translatedAttribute =
        context.getElementMapping(resource.getType(), Constants.ATTR, attributeName);
    if (translatedAttribute != null) {
      translatedAttributesList.add(translatedAttribute);
      return Optional.of(translatedAttributesList);
    } else {   //unsupported attribute
      return Optional.empty();
    }
  }

  private static Object getToscaAttributeValue(ServiceTemplate serviceTemplate,
                                               String resourceId, String propertyName,
                                               Object attributeVal, String resourceType,
                                               String heatFileName,
                                               HeatOrchestrationTemplate heatOrchestrationTemplate,
                                               Template template, TranslationContext context) {
    if (attributeVal instanceof Map && !((Map) attributeVal).isEmpty()) {
      Map.Entry<String, Object> functionMapEntry =
          (Map.Entry<String, Object>) ((Map) attributeVal).entrySet().iterator().next();
      if (FunctionTranslationFactory.getInstance(functionMapEntry.getKey()).isPresent()) {
        return FunctionTranslationFactory.getInstance(functionMapEntry.getKey()).get()
            .translateFunction(serviceTemplate, resourceId, propertyName,
                functionMapEntry.getKey(), functionMapEntry
                    .getValue(), heatFileName, heatOrchestrationTemplate, template, context);
      }
      Map<String, Object> attrValueMap = new HashMap<>();
      for (Map.Entry<String, Object> entry : ((Map<String, Object>) attributeVal).entrySet()) {
        attrValueMap.put(entry.getKey(),
            getToscaAttributeValue(serviceTemplate, resourceId, propertyName, entry.getValue(),
                resourceType, heatFileName, heatOrchestrationTemplate, template, context));
      }
      return attrValueMap;
    } else if (attributeVal instanceof List && !((List) attributeVal).isEmpty()) {
      List propertyValueArray = new ArrayList<>();
      for (int i = 0; i < ((List) attributeVal).size(); i++) {
        propertyValueArray.add(
            getToscaAttributeValue(serviceTemplate, resourceId, propertyName,
                ((List) attributeVal).get(i), resourceType, heatFileName,
                heatOrchestrationTemplate, template, context));
      }
      return propertyValueArray;
    }
    return attributeVal;
  }


}