appc-config-generator-provider sonar fixes part 2
[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         validateInput(unEscapeData, dataType);
125         if (ConfigGeneratorConstant.DATA_TYPE_JSON.equalsIgnoreCase(dataType)) {
126             return StringEscapeUtils.escapeJavaScript(unEscapeData);
127         } else if (ConfigGeneratorConstant.DATA_TYPE_XML.equalsIgnoreCase(dataType)) {
128             return StringEscapeUtils.escapeXml(unEscapeData);
129         } else if (ConfigGeneratorConstant.DATA_TYPE_SQL.equalsIgnoreCase(dataType)) {
130             return EscapeUtils.escapeSql(unEscapeData);
131         } else {
132             throw new InvalidParameterException(DATA_TYPE_STR + ConfigGeneratorConstant.INPUT_PARAM_DATA_TYPE
133                 + ") param  value (" + dataType
134                 + ")is not supported  for escapeData conversion.");
135         }
136     }
137
138     private void validateInput(String unEscapeData, String dataType) throws InvalidParameterException {
139         if (StringUtils.isBlank(unEscapeData)) {
140             throw new InvalidParameterException("Unescape (" + ConfigGeneratorConstant.INPUT_PARAM_UNESCAPE_DATA
141                 + ") param is missing for escapeData conversion." + unEscapeData);
142         }
143         if (StringUtils.isBlank(dataType)) {
144             throw new InvalidParameterException(DATA_TYPE_STR + ConfigGeneratorConstant.INPUT_PARAM_DATA_TYPE
145                 + ")param is missing for escapeData conversion.");
146         }
147     }
148
149     public void unEscapeData(Map<String, String> inParams, SvcLogicContext ctx)
150         throws SvcLogicException {
151         log.trace("Received unEscapeData call with params : " + inParams);
152         String responsePrefix = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_RESPONSE_PRIFIX);
153         try {
154             responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix + ".") : "";
155             String escapeData = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_ESCAPE_DATA);
156             String dataType = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_DATA_TYPE);
157             String unEscapedData = tryFetchUnescapedData(escapeData, dataType);
158
159             ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_UNESCAPE_DATA,
160                 unEscapedData);
161             ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_STATUS,
162                 ConfigGeneratorConstant.OUTPUT_STATUS_SUCCESS);
163             log.trace("Converted unEscapeData Successfully :" + ctx.getAttributeKeySet());
164         } catch (Exception e) {
165             ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_STATUS,
166                 ConfigGeneratorConstant.OUTPUT_STATUS_FAILURE);
167             ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_ERROR_MESSAGE,
168                 e.getMessage());
169             log.error("Failed in unEscapeData Conversion", e);
170             throw new SvcLogicException(e.getMessage());
171         }
172     }
173
174     private String tryFetchUnescapedData(String escapeData, String dataType) throws InvalidParameterException {
175         if (StringUtils.isBlank(escapeData)) {
176             throw new InvalidParameterException("Escape (" + ConfigGeneratorConstant.INPUT_PARAM_ESCAPE_DATA
177                 + ") param is missing for escapeData conversion.");
178         }
179
180         if (StringUtils.isBlank(dataType)) {
181             throw new InvalidParameterException(DATA_TYPE_STR + ConfigGeneratorConstant.INPUT_PARAM_DATA_TYPE
182                 + ")param is missing for escapeData conversion.");
183         }
184         if (ConfigGeneratorConstant.DATA_TYPE_JSON.equalsIgnoreCase(dataType)) {
185             return StringEscapeUtils.unescapeJavaScript(escapeData);
186         } else if (ConfigGeneratorConstant.DATA_TYPE_XML.equalsIgnoreCase(dataType)) {
187             return StringEscapeUtils.unescapeXml(escapeData);
188         } else {
189             throw new InvalidParameterException(DATA_TYPE_STR + ConfigGeneratorConstant.INPUT_PARAM_DATA_TYPE
190                 + ") param  value (" + dataType
191                 + ")is not supported  for unEscapeData conversion.");
192         }
193     }
194
195
196     public void convertContextToJson(Map<String, String> inParams, SvcLogicContext ctx)
197         throws SvcLogicException {
198         log.trace("Received convertContextToJson call with params : " + inParams);
199         String responsePrefix = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_RESPONSE_PRIFIX);
200         String contextKey = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_CONTEXT_KEY);
201         try {
202             responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix + ".") : "";
203
204             ObjectMapper mapper = new ObjectMapper();
205             ObjectNode objectNode = mapper.createObjectNode();
206
207             Set<String> keys = ctx.getAttributeKeySet();
208             for (String key : keys) {
209                 if (key.startsWith(contextKey + ".")) {
210                     String objkey = key.replaceFirst(contextKey + ".", "");
211                     objectNode.put(objkey, ctx.getAttribute(key));
212
213                 }
214             }
215             ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.INPUT_PARAM_JSON_CONTENT,
216                 objectNode.toString());
217             ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_STATUS,
218                 ConfigGeneratorConstant.OUTPUT_STATUS_SUCCESS);
219             log.trace("convertContextToJson Successful");
220         } catch (Exception e) {
221             ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_STATUS,
222                 ConfigGeneratorConstant.OUTPUT_STATUS_FAILURE);
223             ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_ERROR_MESSAGE,
224                 e.getMessage());
225             log.error("Failed in convertContextToJson", e);
226             throw new SvcLogicException(e.getMessage());
227         }
228     }
229
230 }