Changed to unmaintained
[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                     //We need to re-escape any json data that might be contained in Strings
64                     try {
65                         jsonData = JSONTool.escapeInternalJson(jsonData);
66                     } catch (Exception e) {
67                         log.error("Exception during JSONTool.escapeInternalJson",e);
68                     }
69                 }
70
71                 List<String> blockKeys = new ArrayList<>();
72                 if (blockKey != null) {
73                     blockKeys = Arrays.asList(blockKey.split(","));
74                 }
75
76                 Map<String, String> dgContext = JSONTool.convertToProperties(jsonData, blockKeys);
77                 log.trace("DG Context Populated:" + dgContext);
78
79                 for (Map.Entry<String, String> entry : dgContext.entrySet()) {
80                     trySetContextAttribute(ctx, entry);
81                 }
82             }
83             ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_STATUS,
84                 ConfigGeneratorConstant.OUTPUT_STATUS_SUCCESS);
85
86         } catch (Exception e) {
87             ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_STATUS,
88                 ConfigGeneratorConstant.OUTPUT_STATUS_FAILURE);
89             ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_ERROR_MESSAGE,
90                 e.getMessage());
91             log.error("Failed in JSON to DGContext Conversion", e);
92             throw new SvcLogicException(e.getMessage());
93         }
94     }
95
96     private void trySetContextAttribute(SvcLogicContext ctx, Entry<String, String> entry) {
97         if (entry != null && entry.getKey() != null) {
98             ctx.setAttribute(entry.getKey(), entry.getValue());
99         }
100     }
101
102
103     public void escapeData(Map<String, String> inParams, SvcLogicContext ctx)
104         throws SvcLogicException {
105         log.trace("Received escapeData call with params : " + inParams);
106         String responsePrefix = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_RESPONSE_PRIFIX);
107         try {
108             responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix + ".") : "";
109             String unEscapeData = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_UNESCAPE_DATA);
110             String dataType = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_DATA_TYPE);
111
112             String escapedData = tryFetchEscapedData(unEscapeData, dataType);
113             ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_ESCAPE_DATA,
114                 escapedData);
115             ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_STATUS,
116                 ConfigGeneratorConstant.OUTPUT_STATUS_SUCCESS);
117             log.trace("Data escapeData Successfully :" + ctx.getAttributeKeySet());
118         } catch (Exception e) {
119             ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_STATUS,
120                 ConfigGeneratorConstant.OUTPUT_STATUS_FAILURE);
121             ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_ERROR_MESSAGE,
122                 e.getMessage());
123             log.error("Failed in escapeData Conversion", e);
124             throw new SvcLogicException(e.getMessage());
125         }
126     }
127
128     private String tryFetchEscapedData(String unEscapeData, String dataType) throws InvalidParameterException {
129         validateInput(unEscapeData, dataType);
130         if (ConfigGeneratorConstant.DATA_TYPE_JSON.equalsIgnoreCase(dataType)) {
131             return StringEscapeUtils.escapeJavaScript(unEscapeData);
132         } else if (ConfigGeneratorConstant.DATA_TYPE_XML.equalsIgnoreCase(dataType)) {
133             return StringEscapeUtils.escapeXml(unEscapeData);
134         } else if (ConfigGeneratorConstant.DATA_TYPE_SQL.equalsIgnoreCase(dataType)) {
135             return EscapeUtils.escapeSql(unEscapeData);
136         } else {
137             throw new InvalidParameterException(DATA_TYPE_STR + ConfigGeneratorConstant.INPUT_PARAM_DATA_TYPE
138                 + ") param  value (" + dataType
139                 + ")is not supported  for escapeData conversion.");
140         }
141     }
142
143     private void validateInput(String unEscapeData, String dataType) throws InvalidParameterException {
144         if (StringUtils.isBlank(unEscapeData)) {
145             throw new InvalidParameterException("Unescape (" + ConfigGeneratorConstant.INPUT_PARAM_UNESCAPE_DATA
146                 + ") param is missing for escapeData conversion." + unEscapeData);
147         }
148         if (StringUtils.isBlank(dataType)) {
149             throw new InvalidParameterException(DATA_TYPE_STR + ConfigGeneratorConstant.INPUT_PARAM_DATA_TYPE
150                 + ")param is missing for escapeData conversion.");
151         }
152     }
153
154     public void unEscapeData(Map<String, String> inParams, SvcLogicContext ctx)
155         throws SvcLogicException {
156         log.trace("Received unEscapeData call with params : " + inParams);
157         String responsePrefix = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_RESPONSE_PRIFIX);
158         try {
159             responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix + ".") : "";
160             String escapeData = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_ESCAPE_DATA);
161             String dataType = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_DATA_TYPE);
162             String unEscapedData = tryFetchUnescapedData(escapeData, dataType);
163
164             ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_UNESCAPE_DATA,
165                 unEscapedData);
166             ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_STATUS,
167                 ConfigGeneratorConstant.OUTPUT_STATUS_SUCCESS);
168             log.trace("Converted unEscapeData Successfully :" + ctx.getAttributeKeySet());
169         } catch (Exception e) {
170             ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_STATUS,
171                 ConfigGeneratorConstant.OUTPUT_STATUS_FAILURE);
172             ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_ERROR_MESSAGE,
173                 e.getMessage());
174             log.error("Failed in unEscapeData Conversion", e);
175             throw new SvcLogicException(e.getMessage());
176         }
177     }
178
179     private String tryFetchUnescapedData(String escapeData, String dataType) throws InvalidParameterException {
180         if (StringUtils.isBlank(escapeData)) {
181             throw new InvalidParameterException("Escape (" + ConfigGeneratorConstant.INPUT_PARAM_ESCAPE_DATA
182                 + ") param is missing for escapeData conversion.");
183         }
184
185         if (StringUtils.isBlank(dataType)) {
186             throw new InvalidParameterException(DATA_TYPE_STR + ConfigGeneratorConstant.INPUT_PARAM_DATA_TYPE
187                 + ")param is missing for escapeData conversion.");
188         }
189         if (ConfigGeneratorConstant.DATA_TYPE_JSON.equalsIgnoreCase(dataType)) {
190             return StringEscapeUtils.unescapeJavaScript(escapeData);
191         } else if (ConfigGeneratorConstant.DATA_TYPE_XML.equalsIgnoreCase(dataType)) {
192             return StringEscapeUtils.unescapeXml(escapeData);
193         } else {
194             throw new InvalidParameterException(DATA_TYPE_STR + ConfigGeneratorConstant.INPUT_PARAM_DATA_TYPE
195                 + ") param  value (" + dataType
196                 + ")is not supported  for unEscapeData conversion.");
197         }
198     }
199
200
201     public void convertContextToJson(Map<String, String> inParams, SvcLogicContext ctx)
202         throws SvcLogicException {
203         log.trace("Received convertContextToJson call with params : " + inParams);
204         String responsePrefix = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_RESPONSE_PRIFIX);
205         String contextKey = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_CONTEXT_KEY);
206         try {
207             responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix + ".") : "";
208
209             ObjectMapper mapper = new ObjectMapper();
210             ObjectNode objectNode = mapper.createObjectNode();
211
212             Set<String> keys = ctx.getAttributeKeySet();
213             for (String key : keys) {
214                 if (key.startsWith(contextKey + ".")) {
215                     String objkey = key.replaceFirst(contextKey + ".", "");
216                     objectNode.put(objkey, ctx.getAttribute(key));
217
218                 }
219             }
220             ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.INPUT_PARAM_JSON_CONTENT,
221                 objectNode.toString());
222             ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_STATUS,
223                 ConfigGeneratorConstant.OUTPUT_STATUS_SUCCESS);
224             log.trace("convertContextToJson Successful");
225         } catch (Exception e) {
226             ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_STATUS,
227                 ConfigGeneratorConstant.OUTPUT_STATUS_FAILURE);
228             ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_ERROR_MESSAGE,
229                 e.getMessage());
230             log.error("Failed in convertContextToJson", e);
231             throw new SvcLogicException(e.getMessage());
232         }
233     }
234
235 }