69c5dffc456569cdfdd2b1db879466a0ae33e032
[appc.git] / appc-config / appc-config-generator / provider / src / main / java / org / onap / sdnc / config / generator / convert / ConvertNode.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
8  * =============================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * 
21  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.sdnc.config.generator.convert;
25
26 import com.att.eelf.configuration.EELFLogger;
27 import com.att.eelf.configuration.EELFManager;
28 import com.fasterxml.jackson.databind.ObjectMapper;
29 import com.fasterxml.jackson.databind.node.ObjectNode;
30 import java.util.ArrayList;
31 import java.util.Arrays;
32 import java.util.List;
33 import java.util.Map;
34 import java.util.Map.Entry;
35 import java.util.Set;
36 import org.apache.commons.lang.StringEscapeUtils;
37 import org.apache.commons.lang3.StringUtils;
38 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
39 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
40 import org.onap.ccsdk.sli.core.sli.SvcLogicJavaPlugin;
41 import org.onap.sdnc.config.generator.ConfigGeneratorConstant;
42 import org.onap.sdnc.config.generator.tool.EscapeUtils;
43 import org.onap.sdnc.config.generator.tool.JSONTool;
44
45 public class ConvertNode implements SvcLogicJavaPlugin {
46
47     private static final EELFLogger log = EELFManager.getInstance().getLogger(ConvertNode.class);
48     private static final String DATA_TYPE_STR = " Datatype (";
49
50     public void convertJson2DGContext(Map<String, String> inParams, SvcLogicContext ctx)
51         throws SvcLogicException {
52         log.trace("Received convertJson2DGContext call with params : " + inParams);
53         String responsePrefix = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_RESPONSE_PRIFIX);
54         try {
55             String jsonData = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_JSON_DATA);
56             String isEscaped = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_IS_ESCAPED);
57             String blockKey = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_BLOCK_KEYS);
58             responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix + ".") : "";
59
60             if (StringUtils.isNotBlank(jsonData)) {
61                 if (StringUtils.isNotBlank(isEscaped) && "Y".equalsIgnoreCase(isEscaped)) {
62                     jsonData = StringEscapeUtils.unescapeJavaScript(jsonData);
63                 }
64
65                 List<String> blockKeys = new ArrayList<>();
66                 if (blockKey != null) {
67                     blockKeys = Arrays.asList(blockKey.split(","));
68                 }
69
70                 Map<String, String> dgContext = JSONTool.convertToProperties(jsonData, blockKeys);
71                 log.trace("DG Context Populated:" + dgContext);
72
73                 for (Map.Entry<String, String> entry : dgContext.entrySet()) {
74                     trySetContextAttribute(ctx, entry);
75                 }
76             }
77             ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_STATUS,
78                 ConfigGeneratorConstant.OUTPUT_STATUS_SUCCESS);
79
80         } catch (Exception e) {
81             ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_STATUS,
82                 ConfigGeneratorConstant.OUTPUT_STATUS_FAILURE);
83             ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_ERROR_MESSAGE,
84                 e.getMessage());
85             log.error("Failed in JSON to DGContext Conversion", e);
86             throw new SvcLogicException(e.getMessage());
87         }
88     }
89
90     private void trySetContextAttribute(SvcLogicContext ctx, Entry<String, String> entry) {
91         if (entry != null && entry.getKey() != null) {
92             ctx.setAttribute(entry.getKey(), entry.getValue());
93         }
94     }
95
96
97     public void escapeData(Map<String, String> inParams, SvcLogicContext ctx)
98         throws SvcLogicException {
99         log.trace("Received escapeData call with params : " + inParams);
100         String responsePrefix = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_RESPONSE_PRIFIX);
101         try {
102             responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix + ".") : "";
103             String unEscapeData = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_UNESCAPE_DATA);
104             String dataType = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_DATA_TYPE);
105
106             String escapedData = tryFetchEscapedData(unEscapeData, dataType);
107             ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_ESCAPE_DATA,
108                 escapedData);
109             ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_STATUS,
110                 ConfigGeneratorConstant.OUTPUT_STATUS_SUCCESS);
111             log.trace("Data escapeData Successfully :" + ctx.getAttributeKeySet());
112         } catch (Exception e) {
113             ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_STATUS,
114                 ConfigGeneratorConstant.OUTPUT_STATUS_FAILURE);
115             ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_ERROR_MESSAGE,
116                 e.getMessage());
117             log.error("Failed in escapeData Conversion", e);
118             throw new SvcLogicException(e.getMessage());
119         }
120     }
121
122     private String tryFetchEscapedData(String unEscapeData, String dataType) throws InvalidParameterException {
123         validateInput(unEscapeData, dataType);
124         if (ConfigGeneratorConstant.DATA_TYPE_JSON.equalsIgnoreCase(dataType)) {
125             return StringEscapeUtils.escapeJavaScript(unEscapeData);
126         } else if (ConfigGeneratorConstant.DATA_TYPE_XML.equalsIgnoreCase(dataType)) {
127             return StringEscapeUtils.escapeXml(unEscapeData);
128         } else if (ConfigGeneratorConstant.DATA_TYPE_SQL.equalsIgnoreCase(dataType)) {
129             return EscapeUtils.escapeSql(unEscapeData);
130         } else {
131             throw new InvalidParameterException(DATA_TYPE_STR + ConfigGeneratorConstant.INPUT_PARAM_DATA_TYPE
132                 + ") param  value (" + dataType
133                 + ")is not supported  for escapeData conversion.");
134         }
135     }
136
137     private void validateInput(String unEscapeData, String dataType) throws InvalidParameterException {
138         if (StringUtils.isBlank(unEscapeData)) {
139             throw new InvalidParameterException("Unescape (" + ConfigGeneratorConstant.INPUT_PARAM_UNESCAPE_DATA
140                 + ") param is missing for escapeData conversion." + unEscapeData);
141         }
142         if (StringUtils.isBlank(dataType)) {
143             throw new InvalidParameterException(DATA_TYPE_STR + ConfigGeneratorConstant.INPUT_PARAM_DATA_TYPE
144                 + ")param is missing for escapeData conversion.");
145         }
146     }
147
148     public void unEscapeData(Map<String, String> inParams, SvcLogicContext ctx)
149         throws SvcLogicException {
150         log.trace("Received unEscapeData call with params : " + inParams);
151         String responsePrefix = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_RESPONSE_PRIFIX);
152         try {
153             responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix + ".") : "";
154             String escapeData = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_ESCAPE_DATA);
155             String dataType = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_DATA_TYPE);
156             String unEscapedData = tryFetchUnescapedData(escapeData, dataType);
157
158             ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_UNESCAPE_DATA,
159                 unEscapedData);
160             ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_STATUS,
161                 ConfigGeneratorConstant.OUTPUT_STATUS_SUCCESS);
162             log.trace("Converted unEscapeData Successfully :" + ctx.getAttributeKeySet());
163         } catch (Exception e) {
164             ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_STATUS,
165                 ConfigGeneratorConstant.OUTPUT_STATUS_FAILURE);
166             ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_ERROR_MESSAGE,
167                 e.getMessage());
168             log.error("Failed in unEscapeData Conversion", e);
169             throw new SvcLogicException(e.getMessage());
170         }
171     }
172
173     private String tryFetchUnescapedData(String escapeData, String dataType) throws InvalidParameterException {
174         if (StringUtils.isBlank(escapeData)) {
175             throw new InvalidParameterException("Escape (" + ConfigGeneratorConstant.INPUT_PARAM_ESCAPE_DATA
176                 + ") param is missing for escapeData conversion.");
177         }
178
179         if (StringUtils.isBlank(dataType)) {
180             throw new InvalidParameterException(DATA_TYPE_STR + ConfigGeneratorConstant.INPUT_PARAM_DATA_TYPE
181                 + ")param is missing for escapeData conversion.");
182         }
183         if (ConfigGeneratorConstant.DATA_TYPE_JSON.equalsIgnoreCase(dataType)) {
184             return StringEscapeUtils.unescapeJavaScript(escapeData);
185         } else if (ConfigGeneratorConstant.DATA_TYPE_XML.equalsIgnoreCase(dataType)) {
186             return StringEscapeUtils.unescapeXml(escapeData);
187         } else {
188             throw new InvalidParameterException(DATA_TYPE_STR + ConfigGeneratorConstant.INPUT_PARAM_DATA_TYPE
189                 + ") param  value (" + dataType
190                 + ")is not supported  for unEscapeData conversion.");
191         }
192     }
193
194
195     public void convertContextToJson(Map<String, String> inParams, SvcLogicContext ctx)
196         throws SvcLogicException {
197         log.trace("Received convertContextToJson call with params : " + inParams);
198         String responsePrefix = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_RESPONSE_PRIFIX);
199         String contextKey = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_CONTEXT_KEY);
200         try {
201             responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix + ".") : "";
202
203             ObjectMapper mapper = new ObjectMapper();
204             ObjectNode objectNode = mapper.createObjectNode();
205
206             Set<String> keys = ctx.getAttributeKeySet();
207             for (String key : keys) {
208                 if (key.startsWith(contextKey + ".")) {
209                     String objkey = key.replaceFirst(contextKey + ".", "");
210                     objectNode.put(objkey, ctx.getAttribute(key));
211
212                 }
213             }
214             ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.INPUT_PARAM_JSON_CONTENT,
215                 objectNode.toString());
216             ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_STATUS,
217                 ConfigGeneratorConstant.OUTPUT_STATUS_SUCCESS);
218             log.trace("convertContextToJson Successful");
219         } catch (Exception e) {
220             ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_STATUS,
221                 ConfigGeneratorConstant.OUTPUT_STATUS_FAILURE);
222             ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_ERROR_MESSAGE,
223                 e.getMessage());
224             log.error("Failed in convertContextToJson", e);
225             throw new SvcLogicException(e.getMessage());
226         }
227     }
228
229 }