2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.onap.sdnc.config.generator.convert;
27 import com.att.eelf.configuration.EELFLogger;
28 import com.att.eelf.configuration.EELFManager;
29 import com.fasterxml.jackson.databind.ObjectMapper;
30 import com.fasterxml.jackson.databind.node.ObjectNode;
31 import java.util.ArrayList;
32 import java.util.Arrays;
33 import java.util.List;
35 import java.util.Map.Entry;
37 import org.apache.commons.lang.StringEscapeUtils;
38 import org.apache.commons.lang3.StringUtils;
39 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
40 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
41 import org.onap.ccsdk.sli.core.sli.SvcLogicJavaPlugin;
42 import org.onap.sdnc.config.generator.ConfigGeneratorConstant;
43 import org.onap.sdnc.config.generator.tool.EscapeUtils;
44 import org.onap.sdnc.config.generator.tool.JSONTool;
46 public class ConvertNode implements SvcLogicJavaPlugin {
48 private static final EELFLogger log = EELFManager.getInstance().getLogger(ConvertNode.class);
49 private static final String DATA_TYPE_STR = " Datatype (";
51 public void convertJson2DGContext(Map<String, String> inParams, SvcLogicContext ctx)
52 throws SvcLogicException {
53 log.trace("Received convertJson2DGContext call with params : " + inParams);
54 String responsePrefix = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_RESPONSE_PRIFIX);
56 String jsonData = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_JSON_DATA);
57 String isEscaped = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_IS_ESCAPED);
58 String blockKey = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_BLOCK_KEYS);
59 responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix + ".") : "";
61 if (StringUtils.isNotBlank(jsonData)) {
62 if (StringUtils.isNotBlank(isEscaped) && "Y".equalsIgnoreCase(isEscaped)) {
63 jsonData = StringEscapeUtils.unescapeJavaScript(jsonData);
66 List<String> blockKeys = new ArrayList<>();
67 if (blockKey != null) {
68 blockKeys = Arrays.asList(blockKey.split(","));
71 Map<String, String> dgContext = JSONTool.convertToProperties(jsonData, blockKeys);
72 log.trace("DG Context Populated:" + dgContext);
74 for (Map.Entry<String, String> entry : dgContext.entrySet()) {
75 trySetContextAttribute(ctx, entry);
78 ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_STATUS,
79 ConfigGeneratorConstant.OUTPUT_STATUS_SUCCESS);
81 } catch (Exception e) {
82 ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_STATUS,
83 ConfigGeneratorConstant.OUTPUT_STATUS_FAILURE);
84 ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_ERROR_MESSAGE,
86 log.error("Failed in JSON to DGContext Conversion", e);
87 throw new SvcLogicException(e.getMessage());
91 private void trySetContextAttribute(SvcLogicContext ctx, Entry<String, String> entry) {
92 if (entry != null && entry.getKey() != null) {
93 ctx.setAttribute(entry.getKey(), entry.getValue());
98 public void escapeData(Map<String, String> inParams, SvcLogicContext ctx)
99 throws SvcLogicException {
100 log.trace("Received escapeData call with params : " + inParams);
101 String responsePrefix = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_RESPONSE_PRIFIX);
103 responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix + ".") : "";
104 String unEscapeData = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_UNESCAPE_DATA);
105 String dataType = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_DATA_TYPE);
107 String escapedData = tryFetchEscapedData(unEscapeData, dataType);
108 ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_ESCAPE_DATA,
110 ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_STATUS,
111 ConfigGeneratorConstant.OUTPUT_STATUS_SUCCESS);
112 log.trace("Data escapeData Successfully :" + ctx.getAttributeKeySet());
113 } catch (Exception e) {
114 ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_STATUS,
115 ConfigGeneratorConstant.OUTPUT_STATUS_FAILURE);
116 ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_ERROR_MESSAGE,
118 log.error("Failed in escapeData Conversion", e);
119 throw new SvcLogicException(e.getMessage());
123 private String tryFetchEscapedData(String unEscapeData, String dataType) throws InvalidParameterException {
124 if (StringUtils.isBlank(unEscapeData)) {
125 throw new InvalidParameterException("Unescape (" + ConfigGeneratorConstant.INPUT_PARAM_UNESCAPE_DATA
126 + ") param is missing for escapeData conversion." + unEscapeData);
128 if (StringUtils.isBlank(dataType)) {
129 throw new InvalidParameterException(DATA_TYPE_STR + ConfigGeneratorConstant.INPUT_PARAM_DATA_TYPE
130 + ")param is missing for escapeData conversion.");
132 if (ConfigGeneratorConstant.DATA_TYPE_JSON.equalsIgnoreCase(dataType)) {
133 return StringEscapeUtils.escapeJavaScript(unEscapeData);
134 } else if (ConfigGeneratorConstant.DATA_TYPE_XML.equalsIgnoreCase(dataType)) {
135 return StringEscapeUtils.escapeXml(unEscapeData);
136 } else if (ConfigGeneratorConstant.DATA_TYPE_SQL.equalsIgnoreCase(dataType)) {
137 return EscapeUtils.escapeSql(unEscapeData);
139 throw new InvalidParameterException(DATA_TYPE_STR + ConfigGeneratorConstant.INPUT_PARAM_DATA_TYPE
140 + ") param value (" + dataType
141 + ")is not supported for escapeData conversion.");
145 public void unEscapeData(Map<String, String> inParams, SvcLogicContext ctx)
146 throws SvcLogicException {
147 log.trace("Received unEscapeData call with params : " + inParams);
148 String responsePrefix = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_RESPONSE_PRIFIX);
150 responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix + ".") : "";
151 String escapeData = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_ESCAPE_DATA);
152 String dataType = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_DATA_TYPE);
153 String unEscapedData = tryFetchUnescapedData(escapeData, dataType);
155 ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_UNESCAPE_DATA,
157 ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_STATUS,
158 ConfigGeneratorConstant.OUTPUT_STATUS_SUCCESS);
159 log.trace("Converted unEscapeData Successfully :" + ctx.getAttributeKeySet());
160 } catch (Exception e) {
161 ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_STATUS,
162 ConfigGeneratorConstant.OUTPUT_STATUS_FAILURE);
163 ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_ERROR_MESSAGE,
165 log.error("Failed in unEscapeData Conversion", e);
166 throw new SvcLogicException(e.getMessage());
170 private String tryFetchUnescapedData(String escapeData, String dataType) throws InvalidParameterException {
171 if (StringUtils.isBlank(escapeData)) {
172 throw new InvalidParameterException("Escape (" + ConfigGeneratorConstant.INPUT_PARAM_ESCAPE_DATA
173 + ") param is missing for escapeData conversion.");
176 if (StringUtils.isBlank(dataType)) {
177 throw new InvalidParameterException(DATA_TYPE_STR + ConfigGeneratorConstant.INPUT_PARAM_DATA_TYPE
178 + ")param is missing for escapeData conversion.");
180 if (ConfigGeneratorConstant.DATA_TYPE_JSON.equalsIgnoreCase(dataType)) {
181 return StringEscapeUtils.unescapeJavaScript(escapeData);
182 } else if (ConfigGeneratorConstant.DATA_TYPE_XML.equalsIgnoreCase(dataType)) {
183 return StringEscapeUtils.unescapeXml(escapeData);
185 throw new InvalidParameterException(DATA_TYPE_STR + ConfigGeneratorConstant.INPUT_PARAM_DATA_TYPE
186 + ") param value (" + dataType
187 + ")is not supported for unEscapeData conversion.");
192 public void convertContextToJson(Map<String, String> inParams, SvcLogicContext ctx)
193 throws SvcLogicException {
194 log.trace("Received convertContextToJson call with params : " + inParams);
195 String responsePrefix = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_RESPONSE_PRIFIX);
196 String contextKey = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_CONTEXT_KEY);
198 responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix + ".") : "";
200 ObjectMapper mapper = new ObjectMapper();
201 ObjectNode objectNode = mapper.createObjectNode();
203 Set<String> keys = ctx.getAttributeKeySet();
204 for (String key : keys) {
205 if (key.startsWith(contextKey + ".")) {
206 String objkey = key.replaceFirst(contextKey + ".", "");
207 objectNode.put(objkey, ctx.getAttribute(key));
211 ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.INPUT_PARAM_JSON_CONTENT,
212 objectNode.toString());
213 ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_STATUS,
214 ConfigGeneratorConstant.OUTPUT_STATUS_SUCCESS);
215 log.trace("convertContextToJson Successful");
216 } catch (Exception e) {
217 ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_STATUS,
218 ConfigGeneratorConstant.OUTPUT_STATUS_FAILURE);
219 ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_ERROR_MESSAGE,
221 log.error("Failed in convertContextToJson", e);
222 throw new SvcLogicException(e.getMessage());