ccc0d900d1dcd384b49f12a3d15c97abc4620ac1
[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 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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.onap.sdnc.config.generator.convert;
26
27 import java.util.ArrayList;
28 import java.util.Arrays;
29 import java.util.List;
30 import java.util.Map;
31 import java.util.Set;
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;
44
45 public class ConvertNode implements SvcLogicJavaPlugin {
46
47     private static final EELFLogger log = EELFManager.getInstance().getLogger(ConvertNode.class);
48
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);
53         try {
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 + ".") : "";
58
59             if (StringUtils.isNotBlank(jsonData)) {
60                 if (StringUtils.isNotBlank(isEscaped) && isEscaped.equalsIgnoreCase("Y")) {
61                     jsonData = StringEscapeUtils.unescapeJavaScript(jsonData);
62                 }
63
64                 List<String> blockKeys = new ArrayList<String>();
65                 if (blockKey != null) {
66                     blockKeys = Arrays.asList(blockKey.split(","));
67                 }
68
69                 Map<String, String> dgContext = JSONTool.convertToProperties(jsonData, blockKeys);
70                 log.trace("DG Context Populated:" + dgContext);
71
72                 for (Map.Entry<String, String> entry : dgContext.entrySet()) {
73                     if (entry != null && entry.getKey() != null) {
74                         ctx.setAttribute(entry.getKey(), entry.getValue());
75                     }
76                 }
77             }
78             ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_STATUS,
79                     ConfigGeneratorConstant.OUTPUT_STATUS_SUCCESS);
80
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,
85                     e.getMessage());
86             log.error("Failed in JSON to DGContext Conversion" + e.getMessage());
87             throw new SvcLogicException(e.getMessage());
88         }
89     }
90
91
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);
96         try {
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);
100
101             if (StringUtils.isBlank(unEscapeData)) {
102                 throw new Exception("Unescape (" + ConfigGeneratorConstant.INPUT_PARAM_UNESCAPE_DATA
103                         + ") param is missing for escapeData conversion." + unEscapeData);
104             }
105
106             if (StringUtils.isBlank(dataType)) {
107                 throw new Exception(" Datatype (" + ConfigGeneratorConstant.INPUT_PARAM_DATA_TYPE
108                         + ")param is missing for escapeData conversion.");
109             }
110
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);
118             } else {
119                 throw new Exception(" Datatype (" + ConfigGeneratorConstant.INPUT_PARAM_DATA_TYPE
120                         + ") param  value (" + dataType
121                         + ")is not supported  for escapeData conversion.");
122             }
123             ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_ESCAPE_DATA,
124                     escapedData);
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,
132                     e.getMessage());
133             log.error("Failed in escapeData Conversion" + e.getMessage());
134             throw new SvcLogicException(e.getMessage());
135         }
136     }
137
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);
142         try {
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);
146
147             if (StringUtils.isBlank(escapeData)) {
148                 throw new Exception("Escape (" + ConfigGeneratorConstant.INPUT_PARAM_ESCAPE_DATA
149                         + ") param is missing for escapeData conversion.");
150             }
151
152             if (StringUtils.isBlank(dataType)) {
153                 throw new Exception(" Datatype (" + ConfigGeneratorConstant.INPUT_PARAM_DATA_TYPE
154                         + ")param is missing for escapeData conversion.");
155             }
156
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);
162             } else {
163                 throw new Exception(" Datatype (" + ConfigGeneratorConstant.INPUT_PARAM_DATA_TYPE
164                         + ") param  value (" + dataType
165                         + ")is not supported  for unEscapeData conversion.");
166             }
167             ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_UNESCAPE_DATA,
168                     unEscapedData);
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,
176                     e.getMessage());
177             log.error("Failed in unEscapeData Conversion" + e.getMessage());
178             throw new SvcLogicException(e.getMessage());
179         }
180     }
181
182
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);
188         try {
189             responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix + ".") : "";
190
191             ObjectMapper mapper = new ObjectMapper();
192             ObjectNode objectNode = mapper.createObjectNode();
193
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));
199
200                 }
201             }
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,
211                     e.getMessage());
212             log.error("Failed in convertContextToJson" + e.getMessage());
213             throw new SvcLogicException(e.getMessage());
214         }
215     }
216
217 }