1 /*******************************************************************************
\r
2 * ============LICENSE_START==================================================
\r
4 * * ===========================================================================
\r
5 * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
\r
6 * * ===========================================================================
\r
7 * * Licensed under the Apache License, Version 2.0 (the "License");
\r
8 * * you may not use this file except in compliance with the License.
\r
9 * * You may obtain a copy of the License at
\r
11 * * http://www.apache.org/licenses/LICENSE-2.0
\r
13 * * Unless required by applicable law or agreed to in writing, software
\r
14 * * distributed under the License is distributed on an "AS IS" BASIS,
\r
15 * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
16 * * See the License for the specific language governing permissions and
\r
17 * * limitations under the License.
\r
18 * * ============LICENSE_END====================================================
\r
20 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
\r
22 ******************************************************************************/
\r
25 package org.onap.dmaap.datarouter.provisioning;
\r
27 import java.io.IOException;
\r
28 import java.sql.Connection;
\r
29 import java.sql.ResultSet;
\r
30 import java.sql.SQLException;
\r
31 import java.sql.Statement;
\r
32 import java.text.ParseException;
\r
33 import java.text.SimpleDateFormat;
\r
34 import java.util.Date;
\r
35 import java.util.HashMap;
\r
36 import java.util.Map;
\r
38 import javax.servlet.ServletOutputStream;
\r
39 import javax.servlet.http.HttpServletRequest;
\r
40 import javax.servlet.http.HttpServletResponse;
\r
42 import org.onap.dmaap.datarouter.provisioning.beans.DeliveryRecord;
\r
43 import org.onap.dmaap.datarouter.provisioning.beans.EventLogRecord;
\r
44 import org.onap.dmaap.datarouter.provisioning.beans.ExpiryRecord;
\r
45 import org.onap.dmaap.datarouter.provisioning.beans.LOGJSONable;
\r
46 import org.onap.dmaap.datarouter.provisioning.beans.PublishRecord;
\r
47 import org.onap.dmaap.datarouter.provisioning.beans.Subscription;
\r
48 import org.onap.dmaap.datarouter.provisioning.eelf.EelfMsgs;
\r
49 import org.onap.dmaap.datarouter.provisioning.utils.DB;
\r
50 import org.onap.dmaap.datarouter.provisioning.utils.LOGJSONObject;
\r
52 import com.att.eelf.configuration.EELFLogger;
\r
53 import com.att.eelf.configuration.EELFManager;
\r
55 import static org.onap.dmaap.datarouter.provisioning.utils.HttpServletUtils.sendResponseError;
\r
58 * This servlet handles requests to the <feedLogURL> and <subLogURL>,
\r
59 * which are generated by the provisioning server to handle the log query API.
\r
61 * @author Robert Eby
\r
62 * @version $Id: LogServlet.java,v 1.11 2014/03/28 17:27:02 eby Exp $
\r
64 @SuppressWarnings("serial")
\r
65 public class LogServlet extends BaseServlet {
\r
66 //Adding EELF Logger Rally:US664892
\r
67 private static EELFLogger eelflogger = EELFManager.getInstance().getLogger(LogServlet.class);
\r
68 private static final long TWENTYFOUR_HOURS = (24 * 60 * 60 * 1000L);
\r
69 private static final String FMT_1 = "yyyy-MM-dd'T'HH:mm:ss'Z'";
\r
70 private static final String FMT_2 = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
\r
72 private final boolean isfeedlog;
\r
74 public abstract class RowHandler {
\r
75 private final ServletOutputStream out;
\r
76 private final String[] fields;
\r
77 private boolean firstrow;
\r
79 public RowHandler(ServletOutputStream out, String fieldparam, boolean b) {
\r
82 this.fields = (fieldparam != null) ? fieldparam.split(":") : null;
\r
84 public void handleRow(ResultSet rs) {
\r
86 LOGJSONable js = buildJSONable(rs);
\r
87 LOGJSONObject jo = js.asJSONObject();
\r
88 if (fields != null) {
\r
89 // filter out unwanted fields
\r
90 LOGJSONObject j2 = new LOGJSONObject();
\r
91 for (String key : fields) {
\r
92 Object v = jo.opt(key);
\r
98 String t = firstrow ? "\n" : ",\n";
\r
102 } catch (Exception exception) {
\r
103 intlogger.info("Failed to handle row. Exception = " + exception.getMessage(),exception);
\r
106 public abstract LOGJSONable buildJSONable(ResultSet rs) throws SQLException;
\r
108 public class PublishRecordRowHandler extends RowHandler {
\r
109 public PublishRecordRowHandler(ServletOutputStream out, String fields, boolean b) {
\r
110 super(out, fields, b);
\r
113 public LOGJSONable buildJSONable(ResultSet rs) throws SQLException {
\r
114 return new PublishRecord(rs);
\r
117 public class DeliveryRecordRowHandler extends RowHandler {
\r
118 public DeliveryRecordRowHandler(ServletOutputStream out, String fields, boolean b) {
\r
119 super(out, fields, b);
\r
122 public LOGJSONable buildJSONable(ResultSet rs) throws SQLException {
\r
123 return new DeliveryRecord(rs);
\r
126 public class ExpiryRecordRowHandler extends RowHandler {
\r
127 public ExpiryRecordRowHandler(ServletOutputStream out, String fields, boolean b) {
\r
128 super(out, fields, b);
\r
131 public LOGJSONable buildJSONable(ResultSet rs) throws SQLException {
\r
132 return new ExpiryRecord(rs);
\r
137 * This class must be created from either a {@link FeedLogServlet} or a {@link SubLogServlet}.
\r
138 * @param isFeedLog boolean to handle those places where a feedlog request is different from
\r
141 protected LogServlet(boolean isFeedLog) {
\r
142 this.isfeedlog = isFeedLog;
\r
146 * DELETE a logging URL -- not supported.
\r
149 public void doDelete(HttpServletRequest req, HttpServletResponse resp) {
\r
150 setIpFqdnRequestIDandInvocationIDForEelf("doDelete", req);
\r
151 eelflogger.info(EelfMsgs.ENTRY);
\r
153 eelflogger.info(EelfMsgs.MESSAGE_WITH_BEHALF_AND_FEEDID, req.getHeader(BEHALF_HEADER), getIdFromPath(req) + "");
\r
154 String message = "DELETE not allowed for the logURL.";
\r
155 EventLogRecord elr = new EventLogRecord(req);
\r
156 elr.setMessage(message);
\r
157 elr.setResult(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
\r
158 eventlogger.info(elr);
\r
159 sendResponseError(resp, HttpServletResponse.SC_METHOD_NOT_ALLOWED, message, eventlogger);
\r
161 eelflogger.info(EelfMsgs.EXIT);
\r
165 * GET a logging URL -- retrieve logging data for a feed or subscription.
\r
166 * See the <b>Logging API</b> document for details on how this method should be invoked.
\r
169 public void doGet(HttpServletRequest req, HttpServletResponse resp) {
\r
170 setIpFqdnRequestIDandInvocationIDForEelf("doGet", req);
\r
171 eelflogger.info(EelfMsgs.ENTRY);
\r
173 eelflogger.info(EelfMsgs.MESSAGE_WITH_BEHALF_AND_FEEDID, req.getHeader(BEHALF_HEADER), getIdFromPath(req) + "");
\r
174 int id = getIdFromPath(req);
\r
176 sendResponseError(resp, HttpServletResponse.SC_BAD_REQUEST, "Missing or bad feed/subscription number.", eventlogger);
\r
179 Map<String, String> map = buildMapFromRequest(req);
\r
180 if (map.get("err") != null) {
\r
181 sendResponseError(resp, HttpServletResponse.SC_BAD_REQUEST, "Invalid arguments: " + map.get("err"), eventlogger);
\r
184 // check Accept: header??
\r
186 resp.setStatus(HttpServletResponse.SC_OK);
\r
187 resp.setContentType(LOGLIST_CONTENT_TYPE);
\r
189 try (ServletOutputStream out = resp.getOutputStream()) {
\r
190 final String fields = req.getParameter("fields");
\r
194 // Handle /feedlog/feedid request
\r
195 boolean firstrow = true;
\r
197 // 1. Collect publish records for this feed
\r
198 RowHandler rh = new PublishRecordRowHandler(out, fields, firstrow);
\r
199 getPublishRecordsForFeed(id, rh, map);
\r
200 firstrow = rh.firstrow;
\r
202 // 2. Collect delivery records for subscriptions to this feed
\r
203 rh = new DeliveryRecordRowHandler(out, fields, firstrow);
\r
204 getDeliveryRecordsForFeed(id, rh, map);
\r
205 firstrow = rh.firstrow;
\r
207 // 3. Collect expiry records for subscriptions to this feed
\r
208 rh = new ExpiryRecordRowHandler(out, fields, firstrow);
\r
209 getExpiryRecordsForFeed(id, rh, map);
\r
211 // Handle /sublog/subid request
\r
212 Subscription sub = Subscription.getSubscriptionById(id);
\r
214 // 1. Collect publish records for the feed this subscription feeds
\r
215 RowHandler rh = new PublishRecordRowHandler(out, fields, true);
\r
216 getPublishRecordsForFeed(sub.getFeedid(), rh, map);
\r
218 // 2. Collect delivery records for this subscription
\r
219 rh = new DeliveryRecordRowHandler(out, fields, rh.firstrow);
\r
220 getDeliveryRecordsForSubscription(id, rh, map);
\r
222 // 3. Collect expiry records for this subscription
\r
223 rh = new ExpiryRecordRowHandler(out, fields, rh.firstrow);
\r
224 getExpiryRecordsForSubscription(id, rh, map);
\r
228 } catch (IOException ioe) {
\r
229 eventlogger.error("IOException: " + ioe.getMessage());
\r
232 eelflogger.info(EelfMsgs.EXIT);
\r
236 * PUT a logging URL -- not supported.
\r
239 public void doPut(HttpServletRequest req, HttpServletResponse resp) {
\r
240 setIpFqdnRequestIDandInvocationIDForEelf("doPut", req);
\r
241 eelflogger.info(EelfMsgs.ENTRY);
\r
243 eelflogger.info(EelfMsgs.MESSAGE_WITH_BEHALF_AND_FEEDID, req.getHeader(BEHALF_HEADER),getIdFromPath(req)+"");
\r
244 String message = "PUT not allowed for the logURL.";
\r
245 EventLogRecord elr = new EventLogRecord(req);
\r
246 elr.setMessage(message);
\r
247 elr.setResult(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
\r
248 eventlogger.info(elr);
\r
249 sendResponseError(resp, HttpServletResponse.SC_METHOD_NOT_ALLOWED, message, eventlogger);
\r
251 eelflogger.info(EelfMsgs.EXIT);
\r
255 * POST a logging URL -- not supported.
\r
258 public void doPost(HttpServletRequest req, HttpServletResponse resp) {
\r
259 setIpFqdnRequestIDandInvocationIDForEelf("doPost", req);
\r
260 eelflogger.info(EelfMsgs.ENTRY);
\r
262 eelflogger.info(EelfMsgs.MESSAGE_WITH_BEHALF, req.getHeader(BEHALF_HEADER));
\r
263 String message = "POST not allowed for the logURL.";
\r
264 EventLogRecord elr = new EventLogRecord(req);
\r
265 elr.setMessage(message);
\r
266 elr.setResult(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
\r
267 eventlogger.info(elr);
\r
268 sendResponseError(resp, HttpServletResponse.SC_METHOD_NOT_ALLOWED, message, eventlogger);
\r
270 eelflogger.info(EelfMsgs.EXIT);
\r
274 private Map<String, String> buildMapFromRequest(HttpServletRequest req) {
\r
275 Map<String, String> map = new HashMap<>();
\r
276 String s = req.getParameter("type");
\r
278 if (s.equals("pub") || s.equals("del") || s.equals("exp")) {
\r
279 map.put("type", s);
\r
281 map.put("err", "bad type");
\r
285 map.put("type", "all");
\r
287 map.put("publishSQL", "");
\r
288 map.put("statusSQL", "");
\r
289 map.put("resultSQL", "");
\r
290 map.put("reasonSQL", "");
\r
291 map.put("filenameSQL", "");
\r
293 s = req.getParameter("publishId");
\r
295 if (s.indexOf("'") >= 0) {
\r
296 map.put("err", "bad publishId");
\r
299 map.put("publishSQL", " AND PUBLISH_ID = '"+s+"'");
\r
302 s = req.getParameter("filename");
\r
304 map.put("filenameSQL", " AND FILENAME = '"+s+"'");
\r
307 s = req.getParameter("statusCode");
\r
310 if (s.equals("success")) {
\r
311 sql = " AND STATUS >= 200 AND STATUS < 300";
\r
312 } else if (s.equals("redirect")) {
\r
313 sql = " AND STATUS >= 300 AND STATUS < 400";
\r
314 } else if (s.equals("failure")) {
\r
315 sql = " AND STATUS >= 400";
\r
318 Integer n = Integer.parseInt(s);
\r
319 if ((n >= 100 && n < 600) || (n == -1))
\r
320 sql = " AND STATUS = " + n;
\r
321 } catch (NumberFormatException e) {
\r
325 map.put("err", "bad statusCode");
\r
328 map.put("statusSQL", sql);
\r
329 map.put("resultSQL", sql.replaceAll("STATUS", "RESULT"));
\r
332 s = req.getParameter("expiryReason");
\r
334 map.put("type", "exp");
\r
335 if (s.equals("notRetryable")) {
\r
336 map.put("reasonSQL", " AND REASON = 'notRetryable'");
\r
337 } else if (s.equals("retriesExhausted")) {
\r
338 map.put("reasonSQL", " AND REASON = 'retriesExhausted'");
\r
339 } else if (s.equals("diskFull")) {
\r
340 map.put("reasonSQL", " AND REASON = 'diskFull'");
\r
341 } else if (s.equals("other")) {
\r
342 map.put("reasonSQL", " AND REASON = 'other'");
\r
344 map.put("err", "bad expiryReason");
\r
349 long stime = getTimeFromParam(req.getParameter("start"));
\r
351 map.put("err", "bad start");
\r
354 long etime = getTimeFromParam(req.getParameter("end"));
\r
356 map.put("err", "bad end");
\r
359 if (stime == 0 && etime == 0) {
\r
360 etime = System.currentTimeMillis();
\r
361 stime = etime - TWENTYFOUR_HOURS;
\r
362 } else if (stime == 0) {
\r
363 stime = etime - TWENTYFOUR_HOURS;
\r
364 } else if (etime == 0) {
\r
365 etime = stime + TWENTYFOUR_HOURS;
\r
367 map.put("timeSQL", String.format(" AND EVENT_TIME >= %d AND EVENT_TIME <= %d", stime, etime));
\r
370 private long getTimeFromParam(final String s) {
\r
374 // First, look for an RFC 3339 date
\r
375 String fmt = (s.indexOf('.') > 0) ? FMT_2 : FMT_1;
\r
376 SimpleDateFormat sdf = new SimpleDateFormat(fmt);
\r
377 Date d = sdf.parse(s);
\r
378 return d.getTime();
\r
379 } catch (ParseException parseException) {
\r
380 intlogger.error("Exception in getting Time :- "+parseException.getMessage(),parseException);
\r
383 // Also allow a long (in ms); useful for testing
\r
384 long n = Long.parseLong(s);
\r
386 } catch (NumberFormatException numberFormatException) {
\r
387 intlogger.error("Exception in getting Time :- "+numberFormatException.getMessage(),numberFormatException);
\r
389 intlogger.info("Error parsing time="+s);
\r
393 private void getPublishRecordsForFeed(int feedid, RowHandler rh, Map<String, String> map) {
\r
394 String type = map.get("type");
\r
395 if (type.equals("all") || type.equals("pub")) {
\r
396 String sql = "select * from LOG_RECORDS where FEEDID = "+feedid
\r
397 + " AND TYPE = 'pub'"
\r
398 + map.get("timeSQL") + map.get("publishSQL") + map.get("statusSQL") + map.get("filenameSQL");
\r
399 getRecordsForSQL(sql, rh);
\r
402 private void getDeliveryRecordsForFeed(int feedid, RowHandler rh, Map<String, String> map) {
\r
403 String type = map.get("type");
\r
404 if (type.equals("all") || type.equals("del")) {
\r
405 String sql = "select * from LOG_RECORDS where FEEDID = "+feedid
\r
406 + " AND TYPE = 'del'"
\r
407 + map.get("timeSQL") + map.get("publishSQL") + map.get("resultSQL");
\r
408 getRecordsForSQL(sql, rh);
\r
411 private void getDeliveryRecordsForSubscription(int subid, RowHandler rh, Map<String, String> map) {
\r
412 String type = map.get("type");
\r
413 if (type.equals("all") || type.equals("del")) {
\r
414 String sql = "select * from LOG_RECORDS where DELIVERY_SUBID = "+subid
\r
415 + " AND TYPE = 'del'"
\r
416 + map.get("timeSQL") + map.get("publishSQL") + map.get("resultSQL");
\r
417 getRecordsForSQL(sql, rh);
\r
420 private void getExpiryRecordsForFeed(int feedid, RowHandler rh, Map<String, String> map) {
\r
421 String type = map.get("type");
\r
422 if (type.equals("all") || type.equals("exp")) {
\r
423 String st = map.get("statusSQL");
\r
424 if (st == null || st.length() == 0) {
\r
425 String sql = "select * from LOG_RECORDS where FEEDID = "+feedid
\r
426 + " AND TYPE = 'exp'"
\r
427 + map.get("timeSQL") + map.get("publishSQL") + map.get("reasonSQL");
\r
428 getRecordsForSQL(sql, rh);
\r
432 private void getExpiryRecordsForSubscription(int subid, RowHandler rh, Map<String, String> map) {
\r
433 String type = map.get("type");
\r
434 if (type.equals("all") || type.equals("exp")) {
\r
435 String st = map.get("statusSQL");
\r
436 if (st == null || st.length() == 0) {
\r
437 String sql = "select * from LOG_RECORDS where DELIVERY_SUBID = "+subid
\r
438 + " AND TYPE = 'exp'"
\r
439 + map.get("timeSQL") + map.get("publishSQL") + map.get("reasonSQL");
\r
440 getRecordsForSQL(sql, rh);
\r
444 private void getRecordsForSQL(String sql, RowHandler rh) {
\r
445 intlogger.debug(sql);
\r
446 long start = System.currentTimeMillis();
\r
448 Connection conn = null;
\r
450 conn = db.getConnection();
\r
451 try( Statement stmt = conn.createStatement()){
\r
452 try(ResultSet rs = stmt.executeQuery(sql)){
\r
453 while (rs.next()) {
\r
458 } catch (SQLException sqlException) {
\r
459 intlogger.info("Failed to get Records. Exception = " +sqlException.getMessage(),sqlException);
\r
464 intlogger.debug("Time: " + (System.currentTimeMillis()-start) + " ms");
\r