2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.translator.services.heattotosca.impl.functiontranslation;
23 import org.openecomp.sdc.heat.datatypes.model.HeatOrchestrationTemplate;
24 import org.openecomp.sdc.heat.datatypes.model.HeatPseudoParameters;
25 import org.openecomp.sdc.tosca.datatypes.ToscaFunctions;
26 import org.openecomp.sdc.tosca.datatypes.model.ServiceTemplate;
27 import org.openecomp.sdc.tosca.datatypes.model.Template;
28 import org.openecomp.sdc.translator.datatypes.heattotosca.TranslationContext;
29 import org.openecomp.sdc.translator.services.heattotosca.FunctionTranslation;
30 import org.openecomp.sdc.translator.services.heattotosca.FunctionTranslationFactory;
32 import java.util.ArrayList;
33 import java.util.HashMap;
34 import java.util.List;
39 * @since December 15, 2016.
41 public class FunctionTranslationGetParamImpl implements FunctionTranslation {
43 public Object translateFunction(ServiceTemplate serviceTemplate, String resourceId,
44 String propertyName, String functionKey,
45 Object functionValue, String heatFileName,
46 HeatOrchestrationTemplate heatOrchestrationTemplate,
47 Template toscaTemplate, TranslationContext context) {
48 Map returnValue = new HashMap<>();
49 returnValue.put(ToscaFunctions.GET_INPUT.getDisplayName(),
50 translateGetParamFunctionExpression(serviceTemplate, resourceId, propertyName,
51 functionValue, heatFileName, heatOrchestrationTemplate, context));
55 private static Object translateGetParamFunctionExpression(ServiceTemplate serviceTemplate,
57 String propertyName,Object functionValue,
59 HeatOrchestrationTemplate
60 heatOrchestrationTemplate,
61 TranslationContext context) {
62 Object returnValue = null;
63 if (functionValue instanceof String) {
64 returnValue = functionValue;
65 if (HeatPseudoParameters.getPseudoParameterNames().contains(functionValue)) {
67 .addUsedHeatPseudoParams(heatFileName, (String) functionValue, (String) functionValue);
69 } else if (functionValue instanceof List) {
70 returnValue = new ArrayList<>();
71 for (int i = 0; i < ((List) functionValue).size(); i++) {
72 Object paramValue = ((List) functionValue).get(i);
73 if ((paramValue instanceof Map && !((Map) paramValue).isEmpty())) {
74 Map<String, Object> paramMap = (Map) paramValue;
75 ((List) returnValue).add(translatedInnerMap(serviceTemplate, resourceId,
76 propertyName, paramMap, heatFileName, heatOrchestrationTemplate, context));
78 ((List) returnValue).add(paramValue);
86 private static Object translatedInnerMap(ServiceTemplate serviceTemplate, String resourceId,
87 String propertyName, Map<String, Object> paramMap,
88 String heatFileName,HeatOrchestrationTemplate
89 heatOrchestrationTemplate,
90 TranslationContext context) {
92 Map<String, Object> translatedInnerMapValue = new HashMap<>();
93 for (Map.Entry<String, Object> entry : paramMap.entrySet()) {
94 if (FunctionTranslationFactory.getInstance(entry.getKey()).isPresent()) {
95 return FunctionTranslationFactory.getInstance(entry.getKey()).get()
96 .translateFunction(serviceTemplate, resourceId, propertyName, entry.getKey(),
97 entry.getValue(), heatFileName, heatOrchestrationTemplate, null, context);
99 translatedInnerMapValue.put(entry.getKey(),
100 translatedInnerValue(serviceTemplate, resourceId, propertyName,entry.getValue(),
101 heatFileName, heatOrchestrationTemplate, context));
105 return translatedInnerMapValue;
108 private static Object translatedInnerValue(ServiceTemplate serviceTemplate, String resourceId,
109 String propertyName,Object value, String heatFileName,
110 HeatOrchestrationTemplate heatOrchestrationTemplate,
111 TranslationContext context) {
112 if (value instanceof String) {
114 } else if (value instanceof Map) {
115 return translatedInnerMap(serviceTemplate, resourceId, propertyName,(Map<String, Object>)
116 value, heatFileName, heatOrchestrationTemplate, context);
117 } else if (value instanceof List) {
118 List returnedList = new ArrayList();
119 for (int i = 0; i < ((List) value).size(); i++) {
120 returnedList.add(translatedInnerValue(serviceTemplate, resourceId, propertyName,
121 ((List) value).get(i), heatFileName, heatOrchestrationTemplate, context));