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.apache.log4j.Logger;
\r
43 import org.onap.dmaap.datarouter.provisioning.beans.DeliveryRecord;
\r
44 import org.onap.dmaap.datarouter.provisioning.beans.EventLogRecord;
\r
45 import org.onap.dmaap.datarouter.provisioning.beans.ExpiryRecord;
\r
46 import org.onap.dmaap.datarouter.provisioning.beans.LOGJSONable;
\r
47 import org.onap.dmaap.datarouter.provisioning.beans.PublishRecord;
\r
48 import org.onap.dmaap.datarouter.provisioning.beans.Subscription;
\r
49 import org.onap.dmaap.datarouter.provisioning.eelf.EelfMsgs;
\r
50 import org.onap.dmaap.datarouter.provisioning.utils.DB;
\r
51 import org.onap.dmaap.datarouter.provisioning.utils.LOGJSONObject;
\r
53 import com.att.eelf.configuration.EELFLogger;
\r
54 import com.att.eelf.configuration.EELFManager;
\r
57 * This servlet handles requests to the <feedLogURL> and <subLogURL>,
\r
58 * which are generated by the provisioning server to handle the log query API.
\r
60 * @author Robert Eby
\r
61 * @version $Id: LogServlet.java,v 1.11 2014/03/28 17:27:02 eby Exp $
\r
63 @SuppressWarnings("serial")
\r
64 public class LogServlet extends BaseServlet {
\r
65 //Adding EELF Logger Rally:US664892
\r
66 private static EELFLogger eelflogger = EELFManager.getInstance().getLogger("org.onap.dmaap.datarouter.provisioning.LogServlet");
\r
67 private static final long TWENTYFOUR_HOURS = (24 * 60 * 60 * 1000L);
\r
68 private static final String fmt1 = "yyyy-MM-dd'T'HH:mm:ss'Z'";
\r
69 private static final String fmt2 = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
\r
71 private static boolean isfeedlog;
\r
73 public abstract class RowHandler {
\r
74 private final ServletOutputStream out;
\r
75 private final String[] fields;
\r
76 private boolean firstrow;
\r
78 public RowHandler(ServletOutputStream out, String fieldparam, boolean b) {
\r
81 this.fields = (fieldparam != null) ? fieldparam.split(":") : null;
\r
83 public void handleRow(ResultSet rs) {
\r
85 LOGJSONable js = buildJSONable(rs);
\r
86 LOGJSONObject jo = js.asJSONObject();
\r
87 if (fields != null) {
\r
88 // filter out unwanted fields
\r
89 LOGJSONObject j2 = new LOGJSONObject();
\r
90 for (String key : fields) {
\r
91 Object v = jo.opt(key);
\r
97 String t = firstrow ? "\n" : ",\n";
\r
101 } catch (Exception exception) {
\r
102 intlogger.info("Failed to handle row. Exception = " + exception.getMessage(),exception);
\r
105 public abstract LOGJSONable buildJSONable(ResultSet rs) throws SQLException;
\r
107 public class PublishRecordRowHandler extends RowHandler {
\r
108 public PublishRecordRowHandler(ServletOutputStream out, String fields, boolean b) {
\r
109 super(out, fields, b);
\r
112 public LOGJSONable buildJSONable(ResultSet rs) throws SQLException {
\r
113 return new PublishRecord(rs);
\r
116 public class DeliveryRecordRowHandler extends RowHandler {
\r
117 public DeliveryRecordRowHandler(ServletOutputStream out, String fields, boolean b) {
\r
118 super(out, fields, b);
\r
121 public LOGJSONable buildJSONable(ResultSet rs) throws SQLException {
\r
122 return new DeliveryRecord(rs);
\r
125 public class ExpiryRecordRowHandler extends RowHandler {
\r
126 public ExpiryRecordRowHandler(ServletOutputStream out, String fields, boolean b) {
\r
127 super(out, fields, b);
\r
130 public LOGJSONable buildJSONable(ResultSet rs) throws SQLException {
\r
131 return new ExpiryRecord(rs);
\r
136 * This class must be created from either a {@link FeedLogServlet} or a {@link SubLogServlet}.
\r
137 * @param isFeedLog boolean to handle those places where a feedlog request is different from
\r
140 protected LogServlet(boolean isFeedLog) {
\r
141 this.isfeedlog = isFeedLog;
\r
145 * DELETE a logging URL -- not supported.
\r
148 public void doDelete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
\r
149 setIpAndFqdnForEelf("doDelete");
\r
150 eelflogger.info(EelfMsgs.MESSAGE_WITH_BEHALF_AND_FEEDID, req.getHeader(BEHALF_HEADER),getIdFromPath(req)+"");
\r
151 String message = "DELETE not allowed for the logURL.";
\r
152 EventLogRecord elr = new EventLogRecord(req);
\r
153 elr.setMessage(message);
\r
154 elr.setResult(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
\r
155 eventlogger.info(elr);
\r
156 resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, message);
\r
159 * GET a logging URL -- retrieve logging data for a feed or subscription.
\r
160 * See the <b>Logging API</b> document for details on how this method should be invoked.
\r
163 public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
\r
164 setIpAndFqdnForEelf("doGet");
\r
165 eelflogger.info(EelfMsgs.MESSAGE_WITH_BEHALF_AND_FEEDID, req.getHeader(BEHALF_HEADER),getIdFromPath(req)+"");
\r
166 int id = getIdFromPath(req);
\r
168 resp.sendError(HttpServletResponse.SC_BAD_REQUEST, "Missing or bad feed/subscription number.");
\r
171 Map<String, String> map = buildMapFromRequest(req);
\r
172 if (map.get("err") != null) {
\r
173 resp.sendError(HttpServletResponse.SC_BAD_REQUEST, "Invalid arguments: "+map.get("err"));
\r
176 // check Accept: header??
\r
178 resp.setStatus(HttpServletResponse.SC_OK);
\r
179 resp.setContentType(LOGLIST_CONTENT_TYPE);
\r
180 @SuppressWarnings("resource")
\r
181 ServletOutputStream out = resp.getOutputStream();
\r
182 final String fields = req.getParameter("fields");
\r
186 // Handle /feedlog/feedid request
\r
187 boolean firstrow = true;
\r
189 // 1. Collect publish records for this feed
\r
190 RowHandler rh = new PublishRecordRowHandler(out, fields, firstrow);
\r
191 getPublishRecordsForFeed(id, rh, map);
\r
192 firstrow = rh.firstrow;
\r
194 // 2. Collect delivery records for subscriptions to this feed
\r
195 rh = new DeliveryRecordRowHandler(out, fields, firstrow);
\r
196 getDeliveryRecordsForFeed(id, rh, map);
\r
197 firstrow = rh.firstrow;
\r
199 // 3. Collect expiry records for subscriptions to this feed
\r
200 rh = new ExpiryRecordRowHandler(out, fields, firstrow);
\r
201 getExpiryRecordsForFeed(id, rh, map);
\r
203 // Handle /sublog/subid request
\r
204 Subscription sub = Subscription.getSubscriptionById(id);
\r
206 // 1. Collect publish records for the feed this subscription feeds
\r
207 RowHandler rh = new PublishRecordRowHandler(out, fields, true);
\r
208 getPublishRecordsForFeed(sub.getFeedid(), rh, map);
\r
210 // 2. Collect delivery records for this subscription
\r
211 rh = new DeliveryRecordRowHandler(out, fields, rh.firstrow);
\r
212 getDeliveryRecordsForSubscription(id, rh, map);
\r
214 // 3. Collect expiry records for this subscription
\r
215 rh = new ExpiryRecordRowHandler(out, fields, rh.firstrow);
\r
216 getExpiryRecordsForSubscription(id, rh, map);
\r
222 * PUT a logging URL -- not supported.
\r
225 public void doPut(HttpServletRequest req, HttpServletResponse resp) throws IOException {
\r
226 setIpAndFqdnForEelf("doPut");
\r
227 eelflogger.info(EelfMsgs.MESSAGE_WITH_BEHALF_AND_FEEDID, req.getHeader(BEHALF_HEADER),getIdFromPath(req)+"");
\r
228 String message = "PUT not allowed for the logURL.";
\r
229 EventLogRecord elr = new EventLogRecord(req);
\r
230 elr.setMessage(message);
\r
231 elr.setResult(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
\r
232 eventlogger.info(elr);
\r
233 resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, message);
\r
236 * POST a logging URL -- not supported.
\r
239 public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
\r
240 setIpAndFqdnForEelf("doPost");
\r
241 eelflogger.info(EelfMsgs.MESSAGE_WITH_BEHALF, req.getHeader(BEHALF_HEADER));
\r
242 String message = "POST not allowed for the logURL.";
\r
243 EventLogRecord elr = new EventLogRecord(req);
\r
244 elr.setMessage(message);
\r
245 elr.setResult(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
\r
246 eventlogger.info(elr);
\r
247 resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, message);
\r
250 private Map<String, String> buildMapFromRequest(HttpServletRequest req) {
\r
251 Map<String, String> map = new HashMap<String, String>();
\r
252 String s = req.getParameter("type");
\r
254 if (s.equals("pub") || s.equals("del") || s.equals("exp")) {
\r
255 map.put("type", s);
\r
257 map.put("err", "bad type");
\r
261 map.put("type", "all");
\r
262 map.put("publishSQL", "");
\r
263 map.put("statusSQL", "");
\r
264 map.put("resultSQL", "");
\r
265 map.put("reasonSQL", "");
\r
267 s = req.getParameter("publishId");
\r
269 if (s.indexOf("'") >= 0) {
\r
270 map.put("err", "bad publishId");
\r
273 map.put("publishSQL", " AND PUBLISH_ID = '"+s+"'");
\r
276 s = req.getParameter("statusCode");
\r
279 if (s.equals("success")) {
\r
280 sql = " AND STATUS >= 200 AND STATUS < 300";
\r
281 } else if (s.equals("redirect")) {
\r
282 sql = " AND STATUS >= 300 AND STATUS < 400";
\r
283 } else if (s.equals("failure")) {
\r
284 sql = " AND STATUS >= 400";
\r
287 Integer n = Integer.parseInt(s);
\r
288 if ((n >= 100 && n < 600) || (n == -1))
\r
289 sql = " AND STATUS = " + n;
\r
290 } catch (NumberFormatException e) {
\r
294 map.put("err", "bad statusCode");
\r
297 map.put("statusSQL", sql);
\r
298 map.put("resultSQL", sql.replaceAll("STATUS", "RESULT"));
\r
301 s = req.getParameter("expiryReason");
\r
303 map.put("type", "exp");
\r
304 if (s.equals("notRetryable")) {
\r
305 map.put("reasonSQL", " AND REASON = 'notRetryable'");
\r
306 } else if (s.equals("retriesExhausted")) {
\r
307 map.put("reasonSQL", " AND REASON = 'retriesExhausted'");
\r
308 } else if (s.equals("diskFull")) {
\r
309 map.put("reasonSQL", " AND REASON = 'diskFull'");
\r
310 } else if (s.equals("other")) {
\r
311 map.put("reasonSQL", " AND REASON = 'other'");
\r
313 map.put("err", "bad expiryReason");
\r
318 long stime = getTimeFromParam(req.getParameter("start"));
\r
320 map.put("err", "bad start");
\r
323 long etime = getTimeFromParam(req.getParameter("end"));
\r
325 map.put("err", "bad end");
\r
328 if (stime == 0 && etime == 0) {
\r
329 etime = System.currentTimeMillis();
\r
330 stime = etime - TWENTYFOUR_HOURS;
\r
331 } else if (stime == 0) {
\r
332 stime = etime - TWENTYFOUR_HOURS;
\r
333 } else if (etime == 0) {
\r
334 etime = stime + TWENTYFOUR_HOURS;
\r
336 map.put("timeSQL", String.format(" AND EVENT_TIME >= %d AND EVENT_TIME <= %d", stime, etime));
\r
339 private long getTimeFromParam(final String s) {
\r
343 // First, look for an RFC 3339 date
\r
344 String fmt = (s.indexOf('.') > 0) ? fmt2 : fmt1;
\r
345 SimpleDateFormat sdf = new SimpleDateFormat(fmt);
\r
346 Date d = sdf.parse(s);
\r
347 return d.getTime();
\r
348 } catch (ParseException parseException) {
\r
349 intlogger.error("Exception in getting Time :- "+parseException.getMessage(),parseException);
\r
352 // Also allow a long (in ms); useful for testing
\r
353 long n = Long.parseLong(s);
\r
355 } catch (NumberFormatException numberFormatException) {
\r
356 intlogger.error("Exception in getting Time :- "+numberFormatException.getMessage(),numberFormatException);
\r
358 intlogger.info("Error parsing time="+s);
\r
362 private void getPublishRecordsForFeed(int feedid, RowHandler rh, Map<String, String> map) {
\r
363 String type = map.get("type");
\r
364 if (type.equals("all") || type.equals("pub")) {
\r
365 String sql = "select * from LOG_RECORDS where FEEDID = "+feedid
\r
366 + " AND TYPE = 'pub'"
\r
367 + map.get("timeSQL") + map.get("publishSQL") + map.get("statusSQL");
\r
368 getRecordsForSQL(sql, rh);
\r
371 private void getDeliveryRecordsForFeed(int feedid, RowHandler rh, Map<String, String> map) {
\r
372 String type = map.get("type");
\r
373 if (type.equals("all") || type.equals("del")) {
\r
374 String sql = "select * from LOG_RECORDS where FEEDID = "+feedid
\r
375 + " AND TYPE = 'del'"
\r
376 + map.get("timeSQL") + map.get("publishSQL") + map.get("resultSQL");
\r
377 getRecordsForSQL(sql, rh);
\r
380 private void getDeliveryRecordsForSubscription(int subid, RowHandler rh, Map<String, String> map) {
\r
381 String type = map.get("type");
\r
382 if (type.equals("all") || type.equals("del")) {
\r
383 String sql = "select * from LOG_RECORDS where DELIVERY_SUBID = "+subid
\r
384 + " AND TYPE = 'del'"
\r
385 + map.get("timeSQL") + map.get("publishSQL") + map.get("resultSQL");
\r
386 getRecordsForSQL(sql, rh);
\r
389 private void getExpiryRecordsForFeed(int feedid, RowHandler rh, Map<String, String> map) {
\r
390 String type = map.get("type");
\r
391 if (type.equals("all") || type.equals("exp")) {
\r
392 String st = map.get("statusSQL");
\r
393 if (st == null || st.length() == 0) {
\r
394 String sql = "select * from LOG_RECORDS where FEEDID = "+feedid
\r
395 + " AND TYPE = 'exp'"
\r
396 + map.get("timeSQL") + map.get("publishSQL") + map.get("reasonSQL");
\r
397 getRecordsForSQL(sql, rh);
\r
401 private void getExpiryRecordsForSubscription(int subid, RowHandler rh, Map<String, String> map) {
\r
402 String type = map.get("type");
\r
403 if (type.equals("all") || type.equals("exp")) {
\r
404 String st = map.get("statusSQL");
\r
405 if (st == null || st.length() == 0) {
\r
406 String sql = "select * from LOG_RECORDS where DELIVERY_SUBID = "+subid
\r
407 + " AND TYPE = 'exp'"
\r
408 + map.get("timeSQL") + map.get("publishSQL") + map.get("reasonSQL");
\r
409 getRecordsForSQL(sql, rh);
\r
413 private void getRecordsForSQL(String sql, RowHandler rh) {
\r
414 intlogger.debug(sql);
\r
415 long start = System.currentTimeMillis();
\r
417 Connection conn = null;
\r
419 conn = db.getConnection();
\r
420 try( Statement stmt = conn.createStatement()){
\r
421 try(ResultSet rs = stmt.executeQuery(sql)){
\r
422 while (rs.next()) {
\r
427 } catch (SQLException sqlException) {
\r
428 intlogger.info("Failed to get Records. Exception = " +sqlException.getMessage(),sqlException);
\r
433 intlogger.debug("Time: " + (System.currentTimeMillis()-start) + " ms");
\r