Rename packages from openecomp to onap.
[sdc.git] / openecomp-be / lib / openecomp-sdc-translator-lib / openecomp-sdc-translator-core / src / main / java / org / openecomp / sdc / translator / services / heattotosca / impl / functiontranslation / FunctionTranslationGetParamImpl.java
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.services.heattotosca.impl.functiontranslation;
22
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.onap.sdc.tosca.datatypes.model.ServiceTemplate;
27 import org.onap.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;
31
32 import java.util.ArrayList;
33 import java.util.HashMap;
34 import java.util.List;
35 import java.util.Map;
36
37 /**
38  * @author SHIRIA
39  * @since December 15, 2016.
40  */
41 public class FunctionTranslationGetParamImpl implements FunctionTranslation {
42   @Override
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));
52     return returnValue;
53   }
54
55   private static Object translateGetParamFunctionExpression(ServiceTemplate serviceTemplate,
56                                                             String resourceId,
57                                                             String propertyName,Object functionValue,
58                                                             String heatFileName,
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)) {
66         context
67             .addUsedHeatPseudoParams(heatFileName, (String) functionValue, (String) functionValue);
68       }
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));
77         } else {
78           ((List) returnValue).add(paramValue);
79         }
80       }
81     }
82
83     return returnValue;
84   }
85
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) {
91
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);
98       } else {
99         translatedInnerMapValue.put(entry.getKey(),
100             translatedInnerValue(serviceTemplate, resourceId, propertyName,entry.getValue(),
101                 heatFileName, heatOrchestrationTemplate, context));
102
103       }
104     }
105     return translatedInnerMapValue;
106   }
107
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) {
113       return value;
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));
122       }
123       return returnedList;
124     }
125
126     return value;
127   }
128 }