d3ea34f9a79e3cf0a56a89a0151d1af27cecdd18
[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 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;
34 import java.util.Map;
35 import java.util.Map.Entry;
36 import java.util.Set;
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;
45
46 public class ConvertNode implements SvcLogicJavaPlugin {
47
48     private static final EELFLogger log = EELFManager.getInstance().getLogger(ConvertNode.class);
49     private static final String DATA_TYPE_STR = " Datatype (";
50
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);
55         try {
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 + ".") : "";
60
61             if (StringUtils.isNotBlank(jsonData)) {
62                 if (StringUtils.isNotBlank(isEscaped) && "Y".equalsIgnoreCase(isEscaped)) {
63                     jsonData = StringEscapeUtils.unescapeJavaScript(jsonData);
64                 }
65
66                 List<String> blockKeys = new ArrayList<>();
67                 if (blockKey != null) {
68                     blockKeys = Arrays.asList(blockKey.split(","));
69                 }
70
71                 Map<String, String> dgContext = JSONTool.convertToProperties(jsonData, blockKeys);
72                 log.trace("DG Context Populated:" + dgContext);
73
74                 for (Map.Entry<String, String> entry : dgContext.entrySet()) {
75                     trySetContextAttribute(ctx, entry);
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);
87             throw new SvcLogicException(e.getMessage());
88         }
89     }
90
91     private void trySetContextAttribute(SvcLogicContext ctx, Entry<String, String> entry) {
92         if (entry != null && entry.getKey() != null) {
93             ctx.setAttribute(entry.getKey(), entry.getValue());
94         }
95     }
96
97
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);
102         try {
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);
106
107             String escapedData = tryFetchEscapedData(unEscapeData, dataType);
108             ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_ESCAPE_DATA,
109                 escapedData);
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,
117                 e.getMessage());
118             log.error("Failed in escapeData Conversion", e);
119             throw new SvcLogicException(e.getMessage());
120         }
121     }
122
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);
127         }
128         if (StringUtils.isBlank(dataType)) {
129             throw new InvalidParameterException(DATA_TYPE_STR + ConfigGeneratorConstant.INPUT_PARAM_DATA_TYPE
130                 + ")param is missing for escapeData conversion.");
131         }
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);
138         } else {
139             throw new InvalidParameterException(DATA_TYPE_STR + ConfigGeneratorConstant.INPUT_PARAM_DATA_TYPE
140                 + ") param  value (" + dataType
141                 + ")is not supported  for escapeData conversion.");
142         }
143     }
144
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);
149         try {
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);
154
155             ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_UNESCAPE_DATA,
156                 unEscapedData);
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,
164                 e.getMessage());
165             log.error("Failed in unEscapeData Conversion", e);
166             throw new SvcLogicException(e.getMessage());
167         }
168     }
169
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.");
174         }
175
176         if (StringUtils.isBlank(dataType)) {
177             throw new InvalidParameterException(DATA_TYPE_STR + ConfigGeneratorConstant.INPUT_PARAM_DATA_TYPE
178                 + ")param is missing for escapeData conversion.");
179         }
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);
184         } else {
185             throw new InvalidParameterException(DATA_TYPE_STR + ConfigGeneratorConstant.INPUT_PARAM_DATA_TYPE
186                 + ") param  value (" + dataType
187                 + ")is not supported  for unEscapeData conversion.");
188         }
189     }
190
191
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);
197         try {
198             responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix + ".") : "";
199
200             ObjectMapper mapper = new ObjectMapper();
201             ObjectNode objectNode = mapper.createObjectNode();
202
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));
208
209                 }
210             }
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,
220                 e.getMessage());
221             log.error("Failed in convertContextToJson", e);
222             throw new SvcLogicException(e.getMessage());
223         }
224     }
225
226 }