2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 * ============LICENSE_END=========================================================
24 package org.onap.sdnc.config.generator.convert;
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;
34 import java.util.Map.Entry;
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;
45 public class ConvertNode implements SvcLogicJavaPlugin {
47 private static final EELFLogger log = EELFManager.getInstance().getLogger(ConvertNode.class);
48 private static final String DATA_TYPE_STR = " Datatype (";
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);
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 + ".") : "";
60 if (StringUtils.isNotBlank(jsonData)) {
61 if (StringUtils.isNotBlank(isEscaped) && "Y".equalsIgnoreCase(isEscaped)) {
62 jsonData = StringEscapeUtils.unescapeJavaScript(jsonData);
65 List<String> blockKeys = new ArrayList<>();
66 if (blockKey != null) {
67 blockKeys = Arrays.asList(blockKey.split(","));
70 Map<String, String> dgContext = JSONTool.convertToProperties(jsonData, blockKeys);
71 log.trace("DG Context Populated:" + dgContext);
73 for (Map.Entry<String, String> entry : dgContext.entrySet()) {
74 trySetContextAttribute(ctx, entry);
77 ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_STATUS,
78 ConfigGeneratorConstant.OUTPUT_STATUS_SUCCESS);
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,
85 log.error("Failed in JSON to DGContext Conversion", e);
86 throw new SvcLogicException(e.getMessage());
90 private void trySetContextAttribute(SvcLogicContext ctx, Entry<String, String> entry) {
91 if (entry != null && entry.getKey() != null) {
92 ctx.setAttribute(entry.getKey(), entry.getValue());
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);
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);
106 String escapedData = tryFetchEscapedData(unEscapeData, dataType);
107 ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_ESCAPE_DATA,
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,
117 log.error("Failed in escapeData Conversion", e);
118 throw new SvcLogicException(e.getMessage());
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);
131 throw new InvalidParameterException(DATA_TYPE_STR + ConfigGeneratorConstant.INPUT_PARAM_DATA_TYPE
132 + ") param value (" + dataType
133 + ")is not supported for escapeData conversion.");
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);
142 if (StringUtils.isBlank(dataType)) {
143 throw new InvalidParameterException(DATA_TYPE_STR + ConfigGeneratorConstant.INPUT_PARAM_DATA_TYPE
144 + ")param is missing for escapeData conversion.");
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);
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);
158 ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_UNESCAPE_DATA,
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,
168 log.error("Failed in unEscapeData Conversion", e);
169 throw new SvcLogicException(e.getMessage());
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.");
179 if (StringUtils.isBlank(dataType)) {
180 throw new InvalidParameterException(DATA_TYPE_STR + ConfigGeneratorConstant.INPUT_PARAM_DATA_TYPE
181 + ")param is missing for escapeData conversion.");
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);
188 throw new InvalidParameterException(DATA_TYPE_STR + ConfigGeneratorConstant.INPUT_PARAM_DATA_TYPE
189 + ") param value (" + dataType
190 + ")is not supported for unEscapeData conversion.");
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);
201 responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix + ".") : "";
203 ObjectMapper mapper = new ObjectMapper();
204 ObjectNode objectNode = mapper.createObjectNode();
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));
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,
224 log.error("Failed in convertContextToJson", e);
225 throw new SvcLogicException(e.getMessage());