Update project structure to org.onap
[dmaap/datarouter.git] / datarouter-prov / src / main / java / org / onap / dmaap / datarouter / provisioning / LogServlet.java
diff --git a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/LogServlet.java b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/LogServlet.java
new file mode 100644 (file)
index 0000000..7437787
--- /dev/null
@@ -0,0 +1,433 @@
+/*******************************************************************************\r
+ * ============LICENSE_START==================================================\r
+ * * org.onap.dmaap\r
+ * * ===========================================================================\r
+ * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
+ * * ===========================================================================\r
+ * * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * * you may not use this file except in compliance with the License.\r
+ * * You may obtain a copy of the License at\r
+ * * \r
+ *  *      http://www.apache.org/licenses/LICENSE-2.0\r
+ * * \r
+ *  * Unless required by applicable law or agreed to in writing, software\r
+ * * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * * See the License for the specific language governing permissions and\r
+ * * limitations under the License.\r
+ * * ============LICENSE_END====================================================\r
+ * *\r
+ * * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
+ * *\r
+ ******************************************************************************/\r
+\r
+\r
+package org.onap.dmaap.datarouter.provisioning;\r
+\r
+import java.io.IOException;\r
+import java.sql.Connection;\r
+import java.sql.ResultSet;\r
+import java.sql.SQLException;\r
+import java.sql.Statement;\r
+import java.text.ParseException;\r
+import java.text.SimpleDateFormat;\r
+import java.util.Date;\r
+import java.util.HashMap;\r
+import java.util.Map;\r
+\r
+import javax.servlet.ServletOutputStream;\r
+import javax.servlet.http.HttpServletRequest;\r
+import javax.servlet.http.HttpServletResponse;\r
+\r
+import org.json.LOGJSONObject;\r
+import org.onap.dmaap.datarouter.provisioning.beans.DeliveryRecord;\r
+import org.onap.dmaap.datarouter.provisioning.beans.EventLogRecord;\r
+import org.onap.dmaap.datarouter.provisioning.beans.ExpiryRecord;\r
+import org.onap.dmaap.datarouter.provisioning.beans.LOGJSONable;\r
+import org.onap.dmaap.datarouter.provisioning.beans.PublishRecord;\r
+import org.onap.dmaap.datarouter.provisioning.beans.Subscription;\r
+import org.onap.dmaap.datarouter.provisioning.eelf.EelfMsgs;\r
+import org.onap.dmaap.datarouter.provisioning.utils.DB;\r
+\r
+import com.att.eelf.configuration.EELFLogger;\r
+import com.att.eelf.configuration.EELFManager;\r
+\r
+/**\r
+ * This servlet handles requests to the <feedLogURL> and  <subLogURL>,\r
+ * which are generated by the provisioning server to handle the log query API.\r
+ *\r
+ * @author Robert Eby\r
+ * @version $Id: LogServlet.java,v 1.11 2014/03/28 17:27:02 eby Exp $\r
+ */\r
+@SuppressWarnings("serial")\r
+public class LogServlet extends BaseServlet {\r
+       //Adding EELF Logger Rally:US664892  \r
+    private static EELFLogger eelflogger = EELFManager.getInstance().getLogger("org.onap.dmaap.datarouter.provisioning.LogServlet");\r
+\r
+       private static final long TWENTYFOUR_HOURS = (24 * 60 * 60 * 1000L);\r
+       private static final String fmt1 = "yyyy-MM-dd'T'HH:mm:ss'Z'";\r
+       private static final String fmt2 = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";\r
+\r
+       private boolean isfeedlog;\r
+\r
+       public abstract class RowHandler {\r
+               private final ServletOutputStream out;\r
+               private final String[] fields;\r
+               public boolean firstrow;\r
+\r
+               public RowHandler(ServletOutputStream out, String fieldparam, boolean b) {\r
+                       this.out = out;\r
+                       this.firstrow = b;\r
+                       this.fields = (fieldparam != null) ? fieldparam.split(":") : null;\r
+               }\r
+               public void handleRow(ResultSet rs) {\r
+                       try {\r
+                               LOGJSONable js = buildJSONable(rs);\r
+                               LOGJSONObject jo = js.asJSONObject();\r
+                               if (fields != null) {\r
+                                       // filter out unwanted fields\r
+                                       LOGJSONObject j2 = new LOGJSONObject();\r
+                                       for (String key : fields) {\r
+                                               Object v = jo.opt(key);\r
+                                               if (v != null)\r
+                                                       j2.put(key, v);\r
+                                       }\r
+                                       jo = j2;\r
+                               }\r
+                               String t = firstrow ? "\n" : ",\n";\r
+                               t += jo.toString();\r
+                               out.print(t);\r
+                               firstrow = false;\r
+                       } catch (Exception e) {\r
+                               // ignore\r
+                       }\r
+               }\r
+               public abstract LOGJSONable buildJSONable(ResultSet rs) throws SQLException;\r
+       }\r
+       public class PublishRecordRowHandler extends RowHandler {\r
+               public PublishRecordRowHandler(ServletOutputStream out, String fields, boolean b) {\r
+                       super(out, fields, b);\r
+               }\r
+               @Override\r
+               public LOGJSONable buildJSONable(ResultSet rs) throws SQLException {\r
+                       return new PublishRecord(rs);\r
+               }\r
+       }\r
+       public class DeliveryRecordRowHandler extends RowHandler {\r
+               public DeliveryRecordRowHandler(ServletOutputStream out, String fields, boolean b) {\r
+                       super(out, fields, b);\r
+               }\r
+               @Override\r
+               public LOGJSONable buildJSONable(ResultSet rs) throws SQLException {\r
+                       return new DeliveryRecord(rs);\r
+               }\r
+       }\r
+       public class ExpiryRecordRowHandler extends RowHandler {\r
+               public ExpiryRecordRowHandler(ServletOutputStream out, String fields, boolean b) {\r
+                       super(out, fields, b);\r
+               }\r
+               @Override\r
+               public LOGJSONable buildJSONable(ResultSet rs) throws SQLException {\r
+                       return new ExpiryRecord(rs);\r
+               }\r
+       }\r
+\r
+       /**\r
+        * This class must be created from either a {@link FeedLogServlet} or a {@link SubLogServlet}.\r
+        * @param isFeedLog boolean to handle those places where a feedlog request is different from\r
+        * a sublog request\r
+        */\r
+       protected LogServlet(boolean isFeedLog) {\r
+               this.isfeedlog = isFeedLog;\r
+       }\r
+\r
+       /**\r
+        * DELETE a logging URL -- not supported.\r
+        */\r
+       @Override\r
+       public void doDelete(HttpServletRequest req, HttpServletResponse resp) throws IOException {\r
+               setIpAndFqdnForEelf("doDelete");\r
+               eelflogger.info(EelfMsgs.MESSAGE_WITH_BEHALF_AND_FEEDID, req.getHeader(BEHALF_HEADER),getIdFromPath(req)+"");\r
+               String message = "DELETE not allowed for the logURL.";\r
+               EventLogRecord elr = new EventLogRecord(req);\r
+               elr.setMessage(message);\r
+               elr.setResult(HttpServletResponse.SC_METHOD_NOT_ALLOWED);\r
+               eventlogger.info(elr);\r
+               resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, message);\r
+       }\r
+       /**\r
+        * GET a logging URL -- retrieve logging data for a feed or subscription.\r
+        * See the <b>Logging API</b> document for details on how this method should be invoked.\r
+        */\r
+       @Override\r
+       public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {\r
+               setIpAndFqdnForEelf("doGet");\r
+               eelflogger.info(EelfMsgs.MESSAGE_WITH_BEHALF_AND_FEEDID, req.getHeader(BEHALF_HEADER),getIdFromPath(req)+"");\r
+               int id = getIdFromPath(req);\r
+               if (id < 0) {\r
+                       resp.sendError(HttpServletResponse.SC_BAD_REQUEST, "Missing or bad feed/subscription number.");\r
+                       return;\r
+               }\r
+               Map<String, String> map = buildMapFromRequest(req);\r
+               if (map.get("err") != null) {\r
+                       resp.sendError(HttpServletResponse.SC_BAD_REQUEST, "Invalid arguments: "+map.get("err"));\r
+                       return;\r
+               }\r
+               // check Accept: header??\r
+\r
+               resp.setStatus(HttpServletResponse.SC_OK);\r
+               resp.setContentType(LOGLIST_CONTENT_TYPE);\r
+               @SuppressWarnings("resource")\r
+               ServletOutputStream out = resp.getOutputStream();\r
+               final String fields = req.getParameter("fields");\r
+\r
+               out.print("[");\r
+               if (isfeedlog) {\r
+                       // Handle /feedlog/feedid request\r
+                       boolean firstrow = true;\r
+\r
+                       // 1. Collect publish records for this feed\r
+                       RowHandler rh = new PublishRecordRowHandler(out, fields, firstrow);\r
+                       getPublishRecordsForFeed(id, rh, map);\r
+                       firstrow = rh.firstrow;\r
+\r
+                       // 2. Collect delivery records for subscriptions to this feed\r
+                       rh = new DeliveryRecordRowHandler(out, fields, firstrow);\r
+                       getDeliveryRecordsForFeed(id, rh, map);\r
+                       firstrow = rh.firstrow;\r
+\r
+                       // 3. Collect expiry records for subscriptions to this feed\r
+                       rh = new ExpiryRecordRowHandler(out, fields, firstrow);\r
+                       getExpiryRecordsForFeed(id, rh, map);\r
+               } else {\r
+                       // Handle /sublog/subid request\r
+                       Subscription sub = Subscription.getSubscriptionById(id);\r
+                       if (sub != null) {\r
+                               // 1. Collect publish records for the feed this subscription feeds\r
+                               RowHandler rh = new PublishRecordRowHandler(out, fields, true);\r
+                               getPublishRecordsForFeed(sub.getFeedid(), rh, map);\r
+\r
+                               // 2. Collect delivery records for this subscription\r
+                               rh = new DeliveryRecordRowHandler(out, fields, rh.firstrow);\r
+                               getDeliveryRecordsForSubscription(id, rh, map);\r
+\r
+                               // 3. Collect expiry records for this subscription\r
+                               rh = new ExpiryRecordRowHandler(out, fields, rh.firstrow);\r
+                               getExpiryRecordsForSubscription(id, rh, map);\r
+                       }\r
+               }\r
+               out.print("\n]");\r
+       }\r
+       /**\r
+        * PUT a logging URL -- not supported.\r
+        */\r
+       @Override\r
+       public void doPut(HttpServletRequest req, HttpServletResponse resp) throws IOException {\r
+               setIpAndFqdnForEelf("doPut");\r
+               eelflogger.info(EelfMsgs.MESSAGE_WITH_BEHALF_AND_FEEDID, req.getHeader(BEHALF_HEADER),getIdFromPath(req)+"");\r
+               String message = "PUT not allowed for the logURL.";\r
+               EventLogRecord elr = new EventLogRecord(req);\r
+               elr.setMessage(message);\r
+               elr.setResult(HttpServletResponse.SC_METHOD_NOT_ALLOWED);\r
+               eventlogger.info(elr);\r
+               resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, message);\r
+       }\r
+       /**\r
+        * POST a logging URL -- not supported.\r
+        */\r
+       @Override\r
+       public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {\r
+               setIpAndFqdnForEelf("doPost");\r
+               eelflogger.info(EelfMsgs.MESSAGE_WITH_BEHALF, req.getHeader(BEHALF_HEADER));\r
+               String message = "POST not allowed for the logURL.";\r
+               EventLogRecord elr = new EventLogRecord(req);\r
+               elr.setMessage(message);\r
+               elr.setResult(HttpServletResponse.SC_METHOD_NOT_ALLOWED);\r
+               eventlogger.info(elr);\r
+               resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, message);\r
+       }\r
+\r
+       private Map<String, String> buildMapFromRequest(HttpServletRequest req) {\r
+               Map<String, String> map = new HashMap<String, String>();\r
+               String s = req.getParameter("type");\r
+               if (s != null) {\r
+                       if (s.equals("pub") || s.equals("del") || s.equals("exp")) {\r
+                               map.put("type", s);\r
+                       } else {\r
+                               map.put("err", "bad type");\r
+                               return map;\r
+                       }\r
+               } else\r
+                       map.put("type", "all");\r
+               map.put("publishSQL", "");\r
+               map.put("statusSQL", "");\r
+               map.put("resultSQL", "");\r
+               map.put("reasonSQL", "");\r
+\r
+               s = req.getParameter("publishId");\r
+               if (s != null) {\r
+                       if (s.indexOf("'") >= 0) {\r
+                               map.put("err", "bad publishId");\r
+                               return map;\r
+                       }\r
+                       map.put("publishSQL", " AND PUBLISH_ID = '"+s+"'");\r
+               }\r
+\r
+               s = req.getParameter("statusCode");\r
+               if (s != null) {\r
+                       String sql = null;\r
+                       if (s.equals("success")) {\r
+                               sql = " AND STATUS >= 200 AND STATUS < 300";\r
+                       } else if (s.equals("redirect")) {\r
+                               sql = " AND STATUS >= 300 AND STATUS < 400";\r
+                       } else if (s.equals("failure")) {\r
+                               sql = " AND STATUS >= 400";\r
+                       } else {\r
+                               try {\r
+                                       Integer n = Integer.parseInt(s);\r
+                                       if ((n >= 100 && n < 600) || (n == -1))\r
+                                               sql = " AND STATUS = " + n;\r
+                               } catch (NumberFormatException e) {\r
+                               }\r
+                       }\r
+                       if (sql == null) {\r
+                               map.put("err", "bad statusCode");\r
+                               return map;\r
+                       }\r
+                       map.put("statusSQL", sql);\r
+                       map.put("resultSQL", sql.replaceAll("STATUS", "RESULT"));\r
+               }\r
+\r
+               s = req.getParameter("expiryReason");\r
+               if (s != null) {\r
+                       map.put("type", "exp");\r
+                       if (s.equals("notRetryable")) {\r
+                               map.put("reasonSQL", " AND REASON = 'notRetryable'");\r
+                       } else if (s.equals("retriesExhausted")) {\r
+                               map.put("reasonSQL", " AND REASON = 'retriesExhausted'");\r
+                       } else if (s.equals("diskFull")) {\r
+                               map.put("reasonSQL", " AND REASON = 'diskFull'");\r
+                       } else if (s.equals("other")) {\r
+                               map.put("reasonSQL", " AND REASON = 'other'");\r
+                       } else {\r
+                               map.put("err", "bad expiryReason");\r
+                               return map;\r
+                       }\r
+               }\r
+\r
+               long stime = getTimeFromParam(req.getParameter("start"));\r
+               if (stime < 0) {\r
+                       map.put("err", "bad start");\r
+                       return map;\r
+               }\r
+               long etime = getTimeFromParam(req.getParameter("end"));\r
+               if (etime < 0) {\r
+                       map.put("err", "bad end");\r
+                       return map;\r
+               }\r
+               if (stime == 0 && etime == 0) {\r
+                       etime = System.currentTimeMillis();\r
+                       stime = etime - TWENTYFOUR_HOURS;\r
+               } else if (stime == 0) {\r
+                       stime = etime - TWENTYFOUR_HOURS;\r
+               } else if (etime == 0) {\r
+                       etime = stime + TWENTYFOUR_HOURS;\r
+               }\r
+               map.put("timeSQL", String.format(" AND EVENT_TIME >= %d AND EVENT_TIME <= %d", stime, etime));\r
+               return map;\r
+       }\r
+       private long getTimeFromParam(final String s) {\r
+               if (s == null)\r
+                       return 0;\r
+               try {\r
+                       // First, look for an RFC 3339 date\r
+                       String fmt = (s.indexOf('.') > 0) ? fmt2 : fmt1;\r
+                       SimpleDateFormat sdf = new SimpleDateFormat(fmt);\r
+                       Date d = sdf.parse(s);\r
+                       return d.getTime();\r
+               } catch (ParseException e) {\r
+               }\r
+               try {\r
+                       // Also allow a long (in ms); useful for testing\r
+                       long n = Long.parseLong(s);\r
+                       return n;\r
+               } catch (NumberFormatException e) {\r
+               }\r
+               intlogger.info("Error parsing time="+s);\r
+               return -1;\r
+       }\r
+\r
+       private void getPublishRecordsForFeed(int feedid, RowHandler rh, Map<String, String> map) {\r
+               String type = map.get("type");\r
+               if (type.equals("all") || type.equals("pub")) {\r
+                       String sql = "select * from LOG_RECORDS where FEEDID = "+feedid\r
+                               + " AND TYPE = 'pub'"\r
+                               + map.get("timeSQL") + map.get("publishSQL") + map.get("statusSQL");\r
+                       getRecordsForSQL(sql, rh);\r
+               }\r
+       }\r
+       private void getDeliveryRecordsForFeed(int feedid, RowHandler rh, Map<String, String> map) {\r
+               String type = map.get("type");\r
+               if (type.equals("all") || type.equals("del")) {\r
+                       String sql = "select * from LOG_RECORDS where FEEDID = "+feedid\r
+                               + " AND TYPE = 'del'"\r
+                               + map.get("timeSQL") + map.get("publishSQL") + map.get("resultSQL");\r
+                       getRecordsForSQL(sql, rh);\r
+               }\r
+       }\r
+       private void getDeliveryRecordsForSubscription(int subid, RowHandler rh, Map<String, String> map) {\r
+               String type = map.get("type");\r
+               if (type.equals("all") || type.equals("del")) {\r
+                       String sql = "select * from LOG_RECORDS where DELIVERY_SUBID = "+subid\r
+                               + " AND TYPE = 'del'"\r
+                               + map.get("timeSQL") + map.get("publishSQL") + map.get("resultSQL");\r
+                       getRecordsForSQL(sql, rh);\r
+               }\r
+       }\r
+       private void getExpiryRecordsForFeed(int feedid, RowHandler rh, Map<String, String> map) {\r
+               String type = map.get("type");\r
+               if (type.equals("all") || type.equals("exp")) {\r
+                       String st = map.get("statusSQL");\r
+                       if (st == null || st.length() == 0) {\r
+                               String sql = "select * from LOG_RECORDS where FEEDID = "+feedid\r
+                                       + " AND TYPE = 'exp'"\r
+                                       + map.get("timeSQL") + map.get("publishSQL") + map.get("reasonSQL");\r
+                               getRecordsForSQL(sql, rh);\r
+                       }\r
+               }\r
+       }\r
+       private void getExpiryRecordsForSubscription(int subid, RowHandler rh, Map<String, String> map) {\r
+               String type = map.get("type");\r
+               if (type.equals("all") || type.equals("exp")) {\r
+                       String st = map.get("statusSQL");\r
+                       if (st == null || st.length() == 0) {\r
+                               String sql = "select * from LOG_RECORDS where DELIVERY_SUBID = "+subid\r
+                                       + " AND TYPE = 'exp'"\r
+                                       + map.get("timeSQL") + map.get("publishSQL") + map.get("reasonSQL");\r
+                               getRecordsForSQL(sql, rh);\r
+                       }\r
+               }\r
+       }\r
+       private void getRecordsForSQL(String sql, RowHandler rh) {\r
+               intlogger.debug(sql);\r
+               long start = System.currentTimeMillis();\r
+               DB db = new DB();\r
+               Connection conn = null;\r
+               try {\r
+                       conn = db.getConnection();\r
+                       Statement  stmt = conn.createStatement();\r
+                       ResultSet rs = stmt.executeQuery(sql);\r
+                       while (rs.next()) {\r
+                               rh.handleRow(rs);\r
+                       }\r
+                       rs.close();\r
+                       stmt.close();\r
+               } catch (SQLException e) {\r
+                       e.printStackTrace();\r
+               } finally {\r
+                       if (conn != null)\r
+                               db.release(conn);\r
+               }\r
+               intlogger.debug("Time: " + (System.currentTimeMillis()-start) + " ms");\r
+       }\r
+}\r