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 java.util.ArrayList;
28 import java.util.Arrays;
29 import java.util.List;
32 import org.apache.commons.lang.StringEscapeUtils;
33 import org.apache.commons.lang3.StringUtils;
34 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
35 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
36 import org.onap.ccsdk.sli.core.sli.SvcLogicJavaPlugin;
37 import org.onap.sdnc.config.generator.ConfigGeneratorConstant;
38 import org.onap.sdnc.config.generator.tool.EscapeUtils;
39 import org.onap.sdnc.config.generator.tool.JSONTool;
40 import com.att.eelf.configuration.EELFLogger;
41 import com.att.eelf.configuration.EELFManager;
42 import com.fasterxml.jackson.databind.ObjectMapper;
43 import com.fasterxml.jackson.databind.node.ObjectNode;
45 public class ConvertNode implements SvcLogicJavaPlugin {
47 private static final EELFLogger log = EELFManager.getInstance().getLogger(ConvertNode.class);
49 public void convertJson2DGContext(Map<String, String> inParams, SvcLogicContext ctx)
50 throws SvcLogicException {
51 log.trace("Received convertJson2DGContext call with params : " + inParams);
52 String responsePrefix = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_RESPONSE_PRIFIX);
54 String jsonData = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_JSON_DATA);
55 String isEscaped = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_IS_ESCAPED);
56 String blockKey = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_BLOCK_KEYS);
57 responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix + ".") : "";
59 if (StringUtils.isNotBlank(jsonData)) {
60 if (StringUtils.isNotBlank(isEscaped) && isEscaped.equalsIgnoreCase("Y")) {
61 jsonData = StringEscapeUtils.unescapeJavaScript(jsonData);
64 List<String> blockKeys = new ArrayList<String>();
65 if (blockKey != null) {
66 blockKeys = Arrays.asList(blockKey.split(","));
69 Map<String, String> dgContext = JSONTool.convertToProperties(jsonData, blockKeys);
70 log.trace("DG Context Populated:" + dgContext);
72 for (Map.Entry<String, String> entry : dgContext.entrySet()) {
73 if (entry != null && entry.getKey() != null) {
74 ctx.setAttribute(entry.getKey(), entry.getValue());
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.getMessage());
87 throw new SvcLogicException(e.getMessage());
92 public void escapeData(Map<String, String> inParams, SvcLogicContext ctx)
93 throws SvcLogicException {
94 log.trace("Received escapeData call with params : " + inParams);
95 String responsePrefix = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_RESPONSE_PRIFIX);
97 responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix + ".") : "";
98 String unEscapeData = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_UNESCAPE_DATA);
99 String dataType = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_DATA_TYPE);
101 if (StringUtils.isBlank(unEscapeData)) {
102 throw new Exception("Unescape (" + ConfigGeneratorConstant.INPUT_PARAM_UNESCAPE_DATA
103 + ") param is missing for escapeData conversion." + unEscapeData);
106 if (StringUtils.isBlank(dataType)) {
107 throw new Exception(" Datatype (" + ConfigGeneratorConstant.INPUT_PARAM_DATA_TYPE
108 + ")param is missing for escapeData conversion.");
111 String escapedData = null;
112 if (ConfigGeneratorConstant.DATA_TYPE_JSON.equalsIgnoreCase(dataType)) {
113 escapedData = StringEscapeUtils.escapeJavaScript(unEscapeData);
114 } else if (ConfigGeneratorConstant.DATA_TYPE_XML.equalsIgnoreCase(dataType)) {
115 escapedData = StringEscapeUtils.escapeXml(unEscapeData);
116 } else if (ConfigGeneratorConstant.DATA_TYPE_SQL.equalsIgnoreCase(dataType)) {
117 escapedData = EscapeUtils.escapeSql(unEscapeData);
119 throw new Exception(" Datatype (" + ConfigGeneratorConstant.INPUT_PARAM_DATA_TYPE
120 + ") param value (" + dataType
121 + ")is not supported for escapeData conversion.");
123 ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_ESCAPE_DATA,
125 ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_STATUS,
126 ConfigGeneratorConstant.OUTPUT_STATUS_SUCCESS);
127 log.trace("Data escapeData Successfully :" + ctx.getAttributeKeySet());
128 } catch (Exception e) {
129 ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_STATUS,
130 ConfigGeneratorConstant.OUTPUT_STATUS_FAILURE);
131 ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_ERROR_MESSAGE,
133 log.error("Failed in escapeData Conversion" + e.getMessage());
134 throw new SvcLogicException(e.getMessage());
138 public void unEscapeData(Map<String, String> inParams, SvcLogicContext ctx)
139 throws SvcLogicException {
140 log.trace("Received unEscapeData call with params : " + inParams);
141 String responsePrefix = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_RESPONSE_PRIFIX);
143 responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix + ".") : "";
144 String escapeData = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_ESCAPE_DATA);
145 String dataType = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_DATA_TYPE);
147 if (StringUtils.isBlank(escapeData)) {
148 throw new Exception("Escape (" + ConfigGeneratorConstant.INPUT_PARAM_ESCAPE_DATA
149 + ") param is missing for escapeData conversion.");
152 if (StringUtils.isBlank(dataType)) {
153 throw new Exception(" Datatype (" + ConfigGeneratorConstant.INPUT_PARAM_DATA_TYPE
154 + ")param is missing for escapeData conversion.");
157 String unEscapedData = null;
158 if (ConfigGeneratorConstant.DATA_TYPE_JSON.equalsIgnoreCase(dataType)) {
159 unEscapedData = StringEscapeUtils.unescapeJavaScript(escapeData);
160 } else if (ConfigGeneratorConstant.DATA_TYPE_XML.equalsIgnoreCase(dataType)) {
161 unEscapedData = StringEscapeUtils.unescapeXml(escapeData);
163 throw new Exception(" Datatype (" + ConfigGeneratorConstant.INPUT_PARAM_DATA_TYPE
164 + ") param value (" + dataType
165 + ")is not supported for unEscapeData conversion.");
167 ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_UNESCAPE_DATA,
169 ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_STATUS,
170 ConfigGeneratorConstant.OUTPUT_STATUS_SUCCESS);
171 log.trace("Converted unEscapeData Successfully :" + ctx.getAttributeKeySet());
172 } catch (Exception e) {
173 ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_STATUS,
174 ConfigGeneratorConstant.OUTPUT_STATUS_FAILURE);
175 ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_ERROR_MESSAGE,
177 log.error("Failed in unEscapeData Conversion" + e.getMessage());
178 throw new SvcLogicException(e.getMessage());
183 public void convertContextToJson(Map<String, String> inParams, SvcLogicContext ctx)
184 throws SvcLogicException {
185 log.trace("Received convertContextToJson call with params : " + inParams);
186 String responsePrefix = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_RESPONSE_PRIFIX);
187 String contextKey = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_CONTEXT_KEY);
189 responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix + ".") : "";
191 ObjectMapper mapper = new ObjectMapper();
192 ObjectNode objectNode = mapper.createObjectNode();
194 Set<String> keys = ctx.getAttributeKeySet();
195 for (String key : keys) {
196 if (key.startsWith(contextKey + ".")) {
197 String objkey = key.replaceFirst(contextKey + ".", "");
198 objectNode.put(objkey, ctx.getAttribute(key));
202 ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.INPUT_PARAM_JSON_CONTENT,
203 objectNode.toString());
204 ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_STATUS,
205 ConfigGeneratorConstant.OUTPUT_STATUS_SUCCESS);
206 log.trace("convertContextToJson Successful");
207 } catch (Exception e) {
208 ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_STATUS,
209 ConfigGeneratorConstant.OUTPUT_STATUS_FAILURE);
210 ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_ERROR_MESSAGE,
212 log.error("Failed in convertContextToJson" + e.getMessage());
213 throw new SvcLogicException(e.getMessage());