DMAAP-1195 [DR] Remove DR code smells
[dmaap/datarouter.git] / datarouter-prov / src / main / java / org / onap / dmaap / datarouter / provisioning / beans / LogRecord.java
1 /*******************************************************************************\r
2  * ============LICENSE_START==================================================\r
3  * * org.onap.dmaap\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
10  * *\r
11  *  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  * *\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
19  * *\r
20  * * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
21  * *\r
22  ******************************************************************************/\r
23 \r
24 \r
25 package org.onap.dmaap.datarouter.provisioning.beans;\r
26 \r
27 import com.att.eelf.configuration.EELFLogger;\r
28 import com.att.eelf.configuration.EELFManager;\r
29 import java.io.IOException;\r
30 import java.io.OutputStream;\r
31 import java.sql.Connection;\r
32 import java.sql.PreparedStatement;\r
33 import java.sql.ResultSet;\r
34 import java.sql.SQLException;\r
35 import java.sql.Types;\r
36 import java.text.ParseException;\r
37 import java.util.Iterator;\r
38 \r
39 import org.onap.dmaap.datarouter.provisioning.utils.DB;\r
40 import org.onap.dmaap.datarouter.provisioning.utils.RLEBitSet;\r
41 \r
42 \r
43 /**\r
44  * The representation of a Log Record, as retrieved from the DB.  Since this record format is only used to replicate\r
45  * between provisioning servers, it is very bare-bones; e.g. there are no field setters and only 1 getter.\r
46  *\r
47  * @author Robert Eby\r
48  * @version $Id: LogRecord.java,v 1.7 2014/03/12 19:45:41 eby Exp $\r
49  */\r
50 public class LogRecord extends BaseLogRecord {\r
51 \r
52     /**\r
53      * Print all log records whose RECORD_IDs are in the bit set provided.\r
54      *\r
55      * @param os the {@link OutputStream} to print the records on\r
56      * @param bs the {@link RLEBitSet} listing the record IDs to print\r
57      * @throws IOException in case of I/O error\r
58      */\r
59     private static EELFLogger intlogger = EELFManager.getInstance().getLogger("InternalLog");\r
60     private final String type;\r
61     private final String feedFileID;\r
62     private final String remoteAddr;\r
63     private final String user;\r
64     private final int status;\r
65     private final int subID;\r
66     private final String fileID;\r
67     private final int result;\r
68     private final int attempts;\r
69     private final String reason;\r
70     private final long recordId;\r
71     private final long clength2;\r
72     private final String fileName;\r
73 \r
74     /**\r
75      * LogRecord constructor.\r
76      * @param rs ResultSet from SQL statement\r
77      * @throws SQLException in case of SQL error\r
78      */\r
79     public LogRecord(ResultSet rs) throws SQLException {\r
80         super(rs);\r
81         this.type = rs.getString("TYPE");\r
82         this.feedFileID = rs.getString("FEED_FILEID");\r
83         this.remoteAddr = rs.getString("REMOTE_ADDR");\r
84         this.user = rs.getString("USER");\r
85         this.status = rs.getInt("STATUS");\r
86 \r
87         this.subID = rs.getInt("DELIVERY_SUBID");\r
88         this.fileID = rs.getString("DELIVERY_FILEID");\r
89         this.result = rs.getInt("RESULT");\r
90 \r
91         this.attempts = rs.getInt("ATTEMPTS");\r
92         this.reason = rs.getString("REASON");\r
93 \r
94         this.recordId = rs.getLong("RECORD_ID");\r
95         this.clength2 = rs.getLong("CONTENT_LENGTH_2");\r
96         this.fileName = rs.getString("FILENAME");\r
97     }\r
98 \r
99     /**\r
100      * LogRecord Constructor from string array.\r
101      * @param pp string array of LogRecord attributes\r
102      * @throws ParseException in case of parse error\r
103      */\r
104     public LogRecord(String[] pp) throws ParseException {\r
105         super(pp);\r
106         this.type = pp[8];\r
107         this.feedFileID = pp[9];\r
108         this.remoteAddr = pp[10];\r
109         this.user = pp[11];\r
110         this.status = Integer.parseInt(pp[12]);\r
111 \r
112         this.subID = Integer.parseInt(pp[13]);\r
113         this.fileID = pp[14];\r
114         this.result = Integer.parseInt(pp[15]);\r
115 \r
116         this.attempts = Integer.parseInt(pp[16]);\r
117         this.reason = pp[17];\r
118 \r
119         this.recordId = Long.parseLong(pp[18]);\r
120         this.clength2 = (pp.length == 21) ? Long.parseLong(pp[19]) : 0;\r
121         this.fileName = pp[20];\r
122     }\r
123 \r
124     /**\r
125      * Get Log Records.\r
126      * @param os outputstream\r
127      * @param bs RLEBitSet object\r
128      * @throws IOException in case of I/O error\r
129      */\r
130     public static void printLogRecords(OutputStream os, RLEBitSet bs) throws IOException {\r
131         final String sql = "select * from LOG_RECORDS where RECORD_ID >= ? AND RECORD_ID <= ?";\r
132         DB db = new DB();\r
133         try (Connection conn = db.getConnection()) {\r
134             Iterator<Long[]> iter = bs.getRangeIterator();\r
135             try (PreparedStatement ps = conn.prepareStatement(sql)) {\r
136                 while (iter.hasNext()) {\r
137                     Long[] nxt = iter.next();\r
138                     ps.setLong(1, nxt[0]);\r
139                     ps.setLong(2, nxt[1]);\r
140                     try (ResultSet rs = ps.executeQuery()) {\r
141                         while (rs.next()) {\r
142                             LogRecord lr = new LogRecord(rs);\r
143                             os.write(lr.toString().getBytes());\r
144                         }\r
145                         ps.clearParameters();\r
146                     }\r
147                 }\r
148             }\r
149         } catch (SQLException e) {\r
150             intlogger.error("PROV0001 printLogRecords: " + e.getMessage(), e);\r
151         }\r
152     }\r
153 \r
154     public long getRecordId() {\r
155         return recordId;\r
156     }\r
157 \r
158     @Override\r
159     public String toString() {\r
160         return\r
161                 sdf.format(getEventTime()) + "|"\r
162                         + "LOG|"\r
163                         + getPublishId() + "|"\r
164                         + getFeedid() + "|"\r
165                         + getRequestUri() + "|"\r
166                         + getMethod() + "|"\r
167                         + getContentType() + "|"\r
168                         + getContentLength() + "|"\r
169                         + type + "|"\r
170                         + feedFileID + "|"\r
171                         + remoteAddr + "|"\r
172                         + user + "|"\r
173                         + status + "|"\r
174                         + subID + "|"\r
175                         + fileID + "|"\r
176                         + result + "|"\r
177                         + attempts + "|"\r
178                         + reason + "|"\r
179                         + recordId + "|"\r
180                         + clength2\r
181                         + "\n";\r
182     }\r
183 \r
184     @Override\r
185     public void load(PreparedStatement ps) throws SQLException {\r
186         ps.setString(1, type);\r
187         super.load(ps);                // loads fields 2-8\r
188         if (type.equals("pub")) {\r
189             ps.setString(9, feedFileID);\r
190             ps.setString(10, remoteAddr);\r
191             ps.setString(11, user);\r
192             ps.setInt(12, status);\r
193             ps.setNull(13, Types.INTEGER);\r
194             ps.setNull(14, Types.VARCHAR);\r
195             ps.setNull(15, Types.INTEGER);\r
196             ps.setNull(16, Types.INTEGER);\r
197             ps.setNull(17, Types.VARCHAR);\r
198             ps.setLong(18, recordId);\r
199             ps.setNull(19, Types.BIGINT);\r
200             ps.setString(20, fileName);\r
201         } else if (type.equals("del")) {\r
202             ps.setNull(9, Types.VARCHAR);\r
203             ps.setNull(10, Types.VARCHAR);\r
204             ps.setString(11, user);\r
205             ps.setNull(12, Types.INTEGER);\r
206             ps.setInt(13, subID);\r
207             ps.setString(14, fileID);\r
208             ps.setInt(15, result);\r
209             ps.setNull(16, Types.INTEGER);\r
210             ps.setNull(17, Types.VARCHAR);\r
211             ps.setLong(18, recordId);\r
212             ps.setNull(19, Types.BIGINT);\r
213             ps.setString(20, fileName);\r
214         } else if (type.equals("exp")) {\r
215             ps.setNull(9, Types.VARCHAR);\r
216             ps.setNull(10, Types.VARCHAR);\r
217             ps.setNull(11, Types.VARCHAR);\r
218             ps.setNull(12, Types.INTEGER);\r
219             ps.setInt(13, subID);\r
220             ps.setString(14, fileID);\r
221             ps.setNull(15, Types.INTEGER);\r
222             ps.setInt(16, attempts);\r
223             ps.setString(17, reason);\r
224             ps.setLong(18, recordId);\r
225             ps.setNull(19, Types.BIGINT);\r
226             ps.setString(20, fileName);\r
227         } else if (type.equals("pbf")) {\r
228             ps.setString(9, feedFileID);\r
229             ps.setString(10, remoteAddr);\r
230             ps.setString(11, user);\r
231             ps.setNull(12, Types.INTEGER);\r
232             ps.setNull(13, Types.INTEGER);\r
233             ps.setNull(14, Types.VARCHAR);\r
234             ps.setNull(15, Types.INTEGER);\r
235             ps.setNull(16, Types.INTEGER);\r
236             ps.setNull(17, Types.VARCHAR);\r
237             ps.setLong(18, recordId);\r
238             ps.setLong(19, clength2);\r
239             ps.setString(20, fileName);\r
240         } else if (type.equals("dlx")) {\r
241             ps.setNull(9, Types.VARCHAR);\r
242             ps.setNull(10, Types.VARCHAR);\r
243             ps.setNull(11, Types.VARCHAR);\r
244             ps.setNull(12, Types.INTEGER);\r
245             ps.setInt(13, subID);\r
246             ps.setNull(14, Types.VARCHAR);\r
247             ps.setNull(15, Types.INTEGER);\r
248             ps.setNull(16, Types.INTEGER);\r
249             ps.setNull(17, Types.VARCHAR);\r
250             ps.setLong(18, recordId);\r
251             ps.setLong(19, clength2);\r
252             ps.setString(20, fileName);\r
253         }\r
254     }\r
255 \r
256     public static void main(String[] args) throws IOException {\r
257         LogRecord.printLogRecords(System.out, new RLEBitSet(args[0]));\r
258     }\r
259 }\r