Second part of onap rename
[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 : APP-C
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property.  All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdnc.config.generator.convert;
22
23 import java.util.ArrayList;
24 import java.util.Arrays;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.Set;
28
29 import org.apache.commons.lang.StringEscapeUtils;
30 import org.apache.commons.lang3.StringUtils;
31 import org.openecomp.sdnc.config.generator.ConfigGeneratorConstant;
32 import org.openecomp.sdnc.config.generator.tool.EscapeUtils;
33 import org.openecomp.sdnc.config.generator.tool.JSONTool;
34 import org.onap.ccsdk.sli.core.sli.SvcLogicJavaPlugin;
35
36 import com.att.eelf.configuration.EELFLogger;
37 import com.att.eelf.configuration.EELFManager;
38 import com.fasterxml.jackson.databind.ObjectMapper;
39 import com.fasterxml.jackson.databind.node.ObjectNode;
40
41 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
42 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
43
44 public class ConvertNode implements SvcLogicJavaPlugin {
45
46     private static final  EELFLogger log = EELFManager.getInstance().getLogger(ConvertNode.class);
47
48     public void convertJson2DGContext( Map<String, String> inParams, SvcLogicContext ctx) throws SvcLogicException {
49         log.trace("Received convertJson2DGContext call with params : " + inParams);
50         String responsePrefix = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_RESPONSE_PRIFIX);
51         try {
52             String jsonData =  inParams.get(ConfigGeneratorConstant.INPUT_PARAM_JSON_DATA);
53             String isEscaped =  inParams.get(ConfigGeneratorConstant.INPUT_PARAM_IS_ESCAPED);
54             String blockKey =  inParams.get(ConfigGeneratorConstant.INPUT_PARAM_BLOCK_KEYS);
55             responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix+".") : "";
56
57             if(StringUtils.isNotBlank(jsonData)){
58                 if(StringUtils.isNotBlank(isEscaped) && isEscaped.equalsIgnoreCase("Y")){
59                     jsonData = StringEscapeUtils.unescapeJavaScript(jsonData);
60                 }
61
62                 List<String> blockKeys = new ArrayList<String>();
63                 if(blockKey != null){
64                     blockKeys = Arrays.asList(blockKey.split(","));
65                 }
66
67                 Map<String, String> dgContext = JSONTool.convertToProperties(jsonData, blockKeys);
68                 log.trace("DG Context Populated:"+dgContext);
69
70                 for (Map.Entry<String, String> entry : dgContext.entrySet()) {
71                     if(entry != null && entry.getKey() != null){
72                         ctx.setAttribute(entry.getKey(), entry.getValue());
73                     }
74                 }
75             }
76             ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_STATUS, ConfigGeneratorConstant.OUTPUT_STATUS_SUCCESS);
77
78         } catch (Exception e) {
79             ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_STATUS, ConfigGeneratorConstant.OUTPUT_STATUS_FAILURE);
80             ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_ERROR_MESSAGE,e.getMessage());
81             log.error("Failed in JSON to DGContext Conversion" + e.getMessage());
82             throw new SvcLogicException(e.getMessage());
83         }
84     }
85
86
87     public void escapeData( Map<String, String> inParams, SvcLogicContext ctx) throws SvcLogicException {
88         log.trace("Received escapeData call with params : " + inParams);
89         String responsePrefix = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_RESPONSE_PRIFIX);
90         try {
91             responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix+".") : "";
92             String unEscapeData =  inParams.get(ConfigGeneratorConstant.INPUT_PARAM_UNESCAPE_DATA);
93             String dataType =  inParams.get(ConfigGeneratorConstant.INPUT_PARAM_DATA_TYPE);
94
95             if(StringUtils.isBlank(unEscapeData)){
96                 throw new Exception("Unescape ("+ConfigGeneratorConstant.INPUT_PARAM_UNESCAPE_DATA +") param is missing for escapeData conversion." + unEscapeData);
97             }
98
99             if(StringUtils.isBlank(dataType)){
100                 throw new Exception(" Datatype ("+ConfigGeneratorConstant.INPUT_PARAM_DATA_TYPE+")param is missing for escapeData conversion.");
101             }
102
103             String escapedData = null;
104             if(ConfigGeneratorConstant.DATA_TYPE_JSON.equalsIgnoreCase(dataType)){
105                 escapedData = StringEscapeUtils.escapeJavaScript(unEscapeData);
106             }else if(ConfigGeneratorConstant.DATA_TYPE_XML.equalsIgnoreCase(dataType)){
107                 escapedData = StringEscapeUtils.escapeXml(unEscapeData);
108             }else if(ConfigGeneratorConstant.DATA_TYPE_SQL.equalsIgnoreCase(dataType)){
109                 escapedData = EscapeUtils.escapeSql(unEscapeData);
110             }else{
111                 throw new Exception(" Datatype ("+ConfigGeneratorConstant.INPUT_PARAM_DATA_TYPE+") param  value ("+dataType+")is not supported  for escapeData conversion.");
112             }
113             ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_ESCAPE_DATA, escapedData);
114             ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_STATUS, ConfigGeneratorConstant.OUTPUT_STATUS_SUCCESS);
115             log.trace("Data escapeData Successfully :" + ctx.getAttributeKeySet());
116         } catch (Exception e) {
117             ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_STATUS, ConfigGeneratorConstant.OUTPUT_STATUS_FAILURE);
118             ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_ERROR_MESSAGE,e.getMessage());
119             log.error("Failed in escapeData Conversion" + e.getMessage());
120             throw new SvcLogicException(e.getMessage());
121         }
122     }
123
124     public void unEscapeData( Map<String, String> inParams, SvcLogicContext ctx) throws SvcLogicException {
125         log.trace("Received unEscapeData call with params : " + inParams);
126         String responsePrefix = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_RESPONSE_PRIFIX);
127         try {
128             responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix+".") : "";
129             String escapeData =  inParams.get(ConfigGeneratorConstant.INPUT_PARAM_ESCAPE_DATA);
130             String dataType =  inParams.get(ConfigGeneratorConstant.INPUT_PARAM_DATA_TYPE);
131
132             if(StringUtils.isBlank(escapeData)){
133                 throw new Exception("Escape ("+ConfigGeneratorConstant.INPUT_PARAM_ESCAPE_DATA +") param is missing for escapeData conversion.");
134             }
135
136             if(StringUtils.isBlank(dataType)){
137                 throw new Exception(" Datatype ("+ConfigGeneratorConstant.INPUT_PARAM_DATA_TYPE+")param is missing for escapeData conversion.");
138             }
139
140             String unEscapedData = null;
141             if(ConfigGeneratorConstant.DATA_TYPE_JSON.equalsIgnoreCase(dataType)){
142                 unEscapedData = StringEscapeUtils.unescapeJavaScript(escapeData);
143             }else if(ConfigGeneratorConstant.DATA_TYPE_XML.equalsIgnoreCase(dataType)){
144                 unEscapedData = StringEscapeUtils.unescapeXml(escapeData);
145             }else{
146                 throw new Exception(" Datatype ("+ConfigGeneratorConstant.INPUT_PARAM_DATA_TYPE+") param  value ("+dataType+")is not supported  for unEscapeData conversion.");
147             }
148             ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_UNESCAPE_DATA, unEscapedData);
149             ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_STATUS, ConfigGeneratorConstant.OUTPUT_STATUS_SUCCESS);
150             log.trace("Converted unEscapeData Successfully :" + ctx.getAttributeKeySet());
151         } catch (Exception e) {
152             ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_STATUS, ConfigGeneratorConstant.OUTPUT_STATUS_FAILURE);
153             ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_ERROR_MESSAGE,e.getMessage());
154             log.error("Failed in unEscapeData Conversion" + e.getMessage());
155             throw new SvcLogicException(e.getMessage());
156         }
157     }
158     
159     
160      public void convertContextToJson(Map<String, String> inParams, SvcLogicContext ctx) throws SvcLogicException{
161         log.trace("Received convertContextToJson call with params : " + inParams);
162         String responsePrefix = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_RESPONSE_PRIFIX);
163         String contextKey = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_CONTEXT_KEY);
164         try {
165             responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix+".") : "";
166             
167             ObjectMapper mapper = new ObjectMapper();
168             ObjectNode objectNode = mapper.createObjectNode();
169             
170             Set<String> keys = ctx.getAttributeKeySet();    
171             for (String key : keys) {
172                 if(key.startsWith(contextKey +".")){
173                      String objkey=  key.replaceFirst(contextKey + ".", "");
174                      objectNode.put(objkey, ctx.getAttribute(key));
175                      
176                 }                
177             }
178             ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.INPUT_PARAM_JSON_CONTENT, objectNode.toString());
179             ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_STATUS, ConfigGeneratorConstant.OUTPUT_STATUS_SUCCESS);
180             log.trace("convertContextToJson Successful" );
181         } catch (Exception e) {
182             ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_STATUS, ConfigGeneratorConstant.OUTPUT_STATUS_FAILURE);
183             ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_ERROR_MESSAGE,e.getMessage());
184             log.error("Failed in convertContextToJson" + e.getMessage());
185             throw new SvcLogicException(e.getMessage());
186         }
187      }
188
189 }