995934933736f9279310aff90861528265329da7
[appc.git] /
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.appc.data.services.db;
22
23 import java.io.File;
24 import java.io.IOException;
25
26 import org.apache.commons.io.FileUtils;
27 import org.apache.commons.lang.StringEscapeUtils;
28 import org.apache.commons.lang.StringUtils;
29 import org.junit.Test;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 import org.openecomp.sdnc.sli.SvcLogicContext;
34
35 public class TestSQLSaveQuery {
36
37
38         private static final Logger LOG = LoggerFactory.getLogger(TestSQLSaveQuery.class);
39
40         private static String CRYPT_KEY = "";
41
42         //@Test
43         public void testSQLSaveQuery() {
44
45                 try {
46                         String message = FileUtils.readFileToString(new File("src/test/resources/query/sampledata.txt"));
47                         System.out.println("TestSQLSaveQuery.testSQLSaveQuery()" + message);
48
49                         SvcLogicContext ctx = new SvcLogicContext();
50                         ctx.setAttribute("request-id", "1234");
51
52                         String escapedMessage = StringEscapeUtils.escapeSql(message);
53                         ctx.setAttribute("log_message", escapedMessage);
54
55                         //String key = "INSERT INTO CONFIG_TRANSACTION_LOG " + " SET request_id = $request-id , message_type        =  'request' ,  message        =  '" + escapedMessage + "' ;";
56                         String key = "INSERT INTO CONFIG_TRANSACTION_LOG " + " SET request_id = $request-id , message_type  =  'request' ,  message        =  $log_message ;";
57                         System.out.println("Query : " + key);
58                         String resolvedContext = resolveCtxVars(key, ctx);
59
60                         System.out.println("Resolved : " + resolvedContext);
61
62                         ctx.setAttribute("log_message", null);
63
64
65                 } catch (IOException e) {
66                         // TODO Auto-generated catch block
67                         e.printStackTrace();
68                 }
69
70
71         }
72
73
74         private String resolveCtxVars(String key,
75                         SvcLogicContext ctx) {
76                 if (key == null) {
77                         return (null);
78                 }
79
80                 if (key.startsWith("'") && key.endsWith("'")) {
81                         key = key.substring(1, key.length() - 1);
82
83                         LOG.debug("Stripped outer single quotes - key is now [" + key + "]");
84                 }
85
86                 String[] keyTerms = key.split("\\s+");
87
88                 StringBuffer sqlBuffer = new StringBuffer();
89
90
91                 for (int i = 0; i < keyTerms.length; i++) {
92                         sqlBuffer.append(resolveTerm(keyTerms[i], ctx));
93                         sqlBuffer.append(" ");
94                 }
95
96
97                 return (sqlBuffer.toString());
98         }
99
100
101         private String resolveTerm(String term, SvcLogicContext ctx) {
102                 if (term == null) {
103                         return (null);
104                 }
105
106                 LOG.debug("resolveTerm: term is " + term);
107
108                 if (term.startsWith("$") && (ctx != null)) {
109                         // Resolve any index variables.
110                         /*
111                         String value = term;
112                         String resolved = resolveCtxVariable(term.substring(1), ctx);
113                         if(resolved != null){
114                                 value = "'" +resolved+ "'";
115                         }
116                         System.out.println("Dollar Term : " + term + " Value :"+ value);
117                         return value;
118 */
119                         return ("'" + resolveCtxVariable(term.substring(1), ctx) + "'");
120
121                 } else  {
122                         return (term);
123                 }
124
125         }
126
127         private String resolveCtxVariable(String ctxVarName, SvcLogicContext ctx) {
128
129                 if (ctxVarName.indexOf('[') == -1) {
130                         // Ctx variable contains no arrays
131                         if ("CRYPT_KEY".equals(ctxVarName)) {
132
133                                 // Handle crypt key as special case. If it's set as a context variable, use it.  Otherwise, use
134                                 // configured crypt key.
135                                 String cryptKey = ctx.getAttribute(ctxVarName);
136                                 if ((cryptKey != null) && (cryptKey.length() > 0)) {
137                                         return(cryptKey);
138                                 } else {
139                                         return(CRYPT_KEY);
140                                 }
141
142                         }
143                         return (ctx.getAttribute(ctxVarName));
144                 }
145
146                 // Resolve any array references
147                 StringBuffer sbuff = new StringBuffer();
148                 String[] ctxVarParts = ctxVarName.split("\\[");
149                 sbuff.append(ctxVarParts[0]);
150                 for (int i = 1; i < ctxVarParts.length; i++) {
151                         if (ctxVarParts[i].startsWith("$")) {
152                                 int endBracketLoc = ctxVarParts[i].indexOf("]");
153                                 if (endBracketLoc == -1) {
154                                         // Missing end bracket ... give up parsing
155                                         LOG.warn("Variable reference " + ctxVarName
156                                                         + " seems to be missing a ']'");
157                                         return (ctx.getAttribute(ctxVarName));
158                                 }
159
160                                 String idxVarName = ctxVarParts[i].substring(1, endBracketLoc);
161                                 String remainder = ctxVarParts[i].substring(endBracketLoc);
162
163                                 sbuff.append("[");
164                                 sbuff.append(ctx.getAttribute(idxVarName));
165                                 sbuff.append(remainder);
166
167                         } else {
168                                 // Index is not a variable reference
169                                 sbuff.append("[");
170                                 sbuff.append(ctxVarParts[i]);
171                         }
172                 }
173
174                 return (ctx.getAttribute(sbuff.toString()));
175         }
176
177
178 }