remove ODL/karaf from common
[ccsdk/sli/core.git] / sli / common / src / main / java / org / onap / ccsdk / sli / core / sli / MessageWriter.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : CCSDK
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                                              reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.ccsdk.sli.core.sli;
23
24 import java.io.File;
25 import java.io.FileInputStream;
26 import java.sql.SQLException;
27 import java.text.SimpleDateFormat;
28 import java.util.ArrayList;
29 import java.util.Date;
30 import java.util.Properties;
31 import javax.sql.rowset.CachedRowSet;
32 import org.onap.ccsdk.sli.core.dblib.DbLibService;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 @Deprecated
37 public class MessageWriter {
38
39         private static final Logger LOG = LoggerFactory.getLogger(MessageWriter.class);
40
41         private static final String DBLIB_SERVICE = "org.onap.ccsdk.sli.core.dblib.DBResourceManager";
42         private static final String SVCLOGIC_PROP_VAR = "SDNC_SLI_PROPERTIES";
43         private static final String SDNC_CONFIG_DIR = "SDNC_CONFIG_DIR";
44
45         private static final String INCOMING_PROPERTY_NAME = "org.onap.ccsdk.sli.MessageWriter.writeIncomingRequests";
46         private static final String OUTGOING_PROPERTY_NAME = "org.onap.ccsdk.sli.MessageWriter.writeOutgoingRequests";
47
48         private static final SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
49
50         private static DbLibService dbLibService = null;
51
52         private static boolean incomingEnabled = false;
53         private static boolean outgoingEnabled = false;
54
55         private static boolean initialized = false;
56
57         @SuppressWarnings({ "rawtypes", "unchecked" })
58         private static void init() {
59                 if (initialized)
60                         return;
61
62                 initialized = true;
63
64                 // Read properties
65                 Properties props = new Properties();
66                 String propPath = System.getenv(SVCLOGIC_PROP_VAR);
67
68                 if (propPath == null) {
69                         String propDir = System.getenv(SDNC_CONFIG_DIR);
70                         if (propDir == null) {
71                                 propDir = "/opt/sdnc/data/properties";
72                         }
73                         propPath = propDir + "/svclogic.properties";
74                         LOG.warn("Environment variable " + SVCLOGIC_PROP_VAR + " unset - defaulting to " + propPath);
75                 }
76
77                 File propFile = new File(propPath);
78
79                 if (!propFile.exists()) {
80                         LOG.warn("Property file does not exist: " + propPath);
81                 }
82
83                 try {
84                         props.load(new FileInputStream(propFile));
85                 } catch (Exception e) {
86                         LOG.warn("Error loading property file: " + propPath, e);
87                 }
88
89                 incomingEnabled = Boolean.valueOf(props.getProperty(INCOMING_PROPERTY_NAME, "false"));
90                 outgoingEnabled = Boolean.valueOf(props.getProperty(OUTGOING_PROPERTY_NAME, "false"));
91
92                 LOG.info(INCOMING_PROPERTY_NAME + ": " + incomingEnabled);
93                 LOG.info(OUTGOING_PROPERTY_NAME + ": " + outgoingEnabled);
94         }
95
96         public static void saveOutgoingRequest(
97                 String requestId,
98                 String serviceInstanceId,
99                 String targetUrl,
100                 String request) {
101                 try {
102                         init();
103
104                         if (!outgoingEnabled)
105                                 return;
106
107                         if (serviceInstanceId == null || serviceInstanceId.trim().length() == 0)
108                                 serviceInstanceId = "NA";
109
110                         int seqnum = getLastSequenceNumber("OUTGOING_MESSAGE", requestId) + 1;
111                         String now = df.format(new Date());
112
113                         String sql = "INSERT INTO OUTGOING_MESSAGE (\n" +
114                                 "       request_id, sequence_number, service_instance_id, target_url, request, start_time)\n" +
115                                 "VALUES (?, ?, ?, ?, ?, ?)";
116
117                         ArrayList<String> data = new ArrayList<>();
118                         data.add(requestId);
119                         data.add(String.valueOf(seqnum));
120                         data.add(serviceInstanceId);
121                         data.add(targetUrl);
122                         data.add(request);
123                         data.add(now);
124
125                         dbLibService.writeData(sql, data, null);
126
127                 } catch (Exception e) {
128                         LOG.warn("Failed to save outgoing request for request-id: " + requestId, e);
129                 }
130         }
131
132         public static void saveOutgoingResponse(String requestId, int httpResponseCode, String response) {
133                 try {
134                         init();
135
136                         if (!outgoingEnabled)
137                                 return;
138
139                         int seqnum = getLastSequenceNumber("OUTGOING_MESSAGE", requestId);
140                         if (seqnum == 0) {
141                                 LOG.warn("Failed to save outgoing response for request-id: " + requestId +
142                                         ": Request record not found in OUTGOING_MESSAGE");
143                                 return;
144                         }
145
146                         String now = df.format(new Date());
147
148                         String sql = "UPDATE OUTGOING_MESSAGE SET http_response_code = ?, response = ?,\n" +
149                                 "       duration = timestampdiff(MICROSECOND, start_time, ?) / 1000\n" +
150                                 "WHERE request_id = ? AND sequence_number = ?";
151
152                         ArrayList<String> data = new ArrayList<>();
153                         data.add(String.valueOf(httpResponseCode));
154                         data.add(response);
155                         data.add(now);
156                         data.add(requestId);
157                         data.add(String.valueOf(seqnum));
158
159                         dbLibService.writeData(sql, data, null);
160
161                 } catch (Exception e) {
162                         LOG.warn("Failed to save outgoing response for request-id: " + requestId, e);
163                 }
164         }
165
166         public static void saveIncomingRequest(
167                 String requestId,
168                 String serviceInstanceId,
169                 String requestHost,
170                 String request) {
171                 try {
172                         init();
173
174                         if (!incomingEnabled)
175                                 return;
176
177                         if (serviceInstanceId == null || serviceInstanceId.trim().length() == 0)
178                                 serviceInstanceId = "NA";
179
180                         int seqnum = getLastSequenceNumber("INCOMING_MESSAGE", requestId) + 1;
181                         String now = df.format(new Date());
182
183                         String sql = "INSERT INTO INCOMING_MESSAGE (\n" +
184                                 "       request_id, sequence_number, service_instance_id, request_host, request, start_time)\n" +
185                                 "VALUES (?, ?, ?, ?, ?, ?)";
186
187                         ArrayList<String> data = new ArrayList<>();
188                         data.add(requestId);
189                         data.add(String.valueOf(seqnum));
190                         data.add(serviceInstanceId);
191                         data.add(requestHost);
192                         data.add(request);
193                         data.add(now);
194
195                         dbLibService.writeData(sql, data, null);
196
197                 } catch (Exception e) {
198                         LOG.warn("Failed to save incoming request for request-id: " + requestId, e);
199                 }
200         }
201
202         public static void saveIncomingResponse(String requestId, int httpResponseCode, String response) {
203                 try {
204                         init();
205
206                         if (!incomingEnabled)
207                                 return;
208
209                         int seqnum = getLastSequenceNumber("INCOMING_MESSAGE", requestId);
210                         if (seqnum == 0) {
211                                 LOG.warn("Failed to save response for request-id: " + requestId +
212                                         ": Request record not found in INCOMING_MESSAGE");
213                                 return;
214                         }
215
216                         String now = df.format(new Date());
217
218                         String sql = "UPDATE INCOMING_MESSAGE SET http_response_code = ?, response = ?,\n" +
219                                 "       duration = timestampdiff(MICROSECOND, start_time, ?) / 1000\n" +
220                                 "WHERE request_id = ? AND sequence_number = ?";
221
222                         ArrayList<String> data = new ArrayList<>();
223                         data.add(String.valueOf(httpResponseCode));
224                         data.add(response);
225                         data.add(now);
226                         data.add(requestId);
227                         data.add(String.valueOf(seqnum));
228
229                         dbLibService.writeData(sql, data, null);
230
231                 } catch (Exception e) {
232                         LOG.warn("Failed to save response for request-id: " + requestId, e);
233                 }
234         }
235
236         public static String getServiceInstanceId(String requestId) throws SQLException {
237                 init();
238
239                 String sql = "SELECT service_instance_id FROM OUTGOING_MESSAGE WHERE request_id = '" + requestId +
240                         "' ORDER BY sequence_number DESC";
241
242                 CachedRowSet rs = null;
243                 try {
244                         rs = dbLibService.getData(sql, null, null);
245                         if (rs.next()) {
246                                 return rs.getString("service_instance_id");
247                         }
248                 } finally {
249                         if (rs != null) {
250                                 try {
251                                         rs.close();
252                                 } catch (Exception e) {
253                                         LOG.warn("Failed to close CachedRowSet", e);
254                                 }
255                         }
256                 }
257                 return null;
258         }
259
260         private static int getLastSequenceNumber(String tableName, String requestId) throws SQLException {
261                 String sql = "SELECT sequence_number FROM " + tableName + " WHERE request_id = '" + requestId +
262                         "' ORDER BY sequence_number DESC";
263
264                 CachedRowSet rs = null;
265                 try {
266                         rs = dbLibService.getData(sql, null, null);
267                         if (rs.next()) {
268                                 return rs.getInt("sequence_number");
269                         }
270                 } finally {
271                         if (rs != null) {
272                                 try {
273                                         rs.close();
274                                 } catch (Exception e) {
275                                         LOG.warn("Failed to close CachedRowSet", e);
276                                 }
277                         }
278                 }
279                 return 0;
280         }
281 }