2 * Copyright © 2016-2018 European Support Limited
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.openecomp.sdc.translator.services.heattotosca.impl.functiontranslation;
19 import java.util.ArrayList;
20 import java.util.HashMap;
21 import java.util.List;
23 import java.util.Optional;
25 import org.openecomp.sdc.heat.datatypes.model.HeatPseudoParameters;
26 import org.openecomp.sdc.tosca.datatypes.ToscaFunctions;
27 import org.openecomp.sdc.translator.services.heattotosca.FunctionTranslation;
28 import org.openecomp.sdc.translator.services.heattotosca.FunctionTranslationFactory;
30 public class FunctionTranslationGetParamImpl implements FunctionTranslation {
31 private static Object translateGetParamFunctionExpression(FunctionTranslator functionTranslator) {
32 Object functionValue = functionTranslator.getFunctionValue();
33 Object returnValue = null;
34 if (functionValue instanceof String) {
35 returnValue = functionValue;
36 if (HeatPseudoParameters.getPseudoParameterNames().contains(functionValue)) {
37 functionTranslator.getContext().addUsedHeatPseudoParams(functionTranslator.getHeatFileName(),
38 (String) functionValue, (String) functionValue);
40 } else if (functionValue instanceof List) {
41 returnValue = new ArrayList<>();
42 for (int i = 0; i < ((List) functionValue).size(); i++) {
43 Object paramValue = ((List) functionValue).get(i);
44 if ((paramValue instanceof Map && !((Map) paramValue).isEmpty())) {
45 Map<String, Object> paramMap = (Map) paramValue;
46 ((List) returnValue).add(translatedInnerMap(functionTranslator, paramMap));
48 ((List) returnValue).add(paramValue);
56 private static Object translatedInnerMap(FunctionTranslator functionTranslator, Map<String, Object> paramMap) {
57 Map<String, Object> translatedInnerMapValue = new HashMap<>();
58 for (Map.Entry<String, Object> entry : paramMap.entrySet()) {
59 Optional<FunctionTranslation> functionTranslationInstance =
60 FunctionTranslationFactory.getInstance(entry.getKey());
61 if (functionTranslationInstance.isPresent()) {
62 functionTranslator.setFunctionValue(entry.getValue());
63 return functionTranslationInstance.get().translateFunction(functionTranslator);
65 translatedInnerMapValue.put(entry.getKey(), translatedInnerValue(functionTranslator, entry.getValue()));
68 return translatedInnerMapValue;
71 private static Object translatedInnerValue(FunctionTranslator functionTranslator,
73 if (value instanceof String) {
75 } else if (value instanceof Map) {
76 return translatedInnerMap(functionTranslator, (Map<String, Object>) value);
77 } else if (value instanceof List) {
78 List<Object> returnedList = new ArrayList<>();
79 for (int i = 0; i < ((List) value).size(); i++) {
80 returnedList.add(translatedInnerValue(functionTranslator, ((List) value).get(i)));
89 public Object translateFunction(FunctionTranslator functionTranslator) {
90 Map<String, Object> returnValue = new HashMap<>();
91 returnValue.put(ToscaFunctions.GET_INPUT.getFunctionName(),
92 translateGetParamFunctionExpression(functionTranslator));