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.beans;
\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
39 import org.onap.dmaap.datarouter.provisioning.utils.DB;
\r
40 import org.onap.dmaap.datarouter.provisioning.utils.RLEBitSet;
\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
47 * @author Robert Eby
\r
48 * @version $Id: LogRecord.java,v 1.7 2014/03/12 19:45:41 eby Exp $
\r
50 public class LogRecord extends BaseLogRecord {
\r
53 * Print all log records whose RECORD_IDs are in the bit set provided.
\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
59 private static EELFLogger intlogger = EELFManager.getInstance().getLogger("InternalLog");
\r
63 * @param os outputstream
\r
64 * @param bs RLEBitSet object
\r
65 * @throws IOException in case of I/O error
\r
67 public static void printLogRecords(OutputStream os, RLEBitSet bs) throws IOException {
\r
68 final String sql = "select * from LOG_RECORDS where RECORD_ID >= ? AND RECORD_ID <= ?";
\r
70 try (Connection conn = db.getConnection()) {
\r
71 Iterator<Long[]> iter = bs.getRangeIterator();
\r
72 try (PreparedStatement ps = conn.prepareStatement(sql)) {
\r
73 while (iter.hasNext()) {
\r
74 Long[] nxt = iter.next();
\r
75 ps.setLong(1, nxt[0]);
\r
76 ps.setLong(2, nxt[1]);
\r
77 try (ResultSet rs = ps.executeQuery()) {
\r
79 LogRecord lr = new LogRecord(rs);
\r
80 os.write(lr.toString().getBytes());
\r
82 ps.clearParameters();
\r
86 } catch (SQLException e) {
\r
87 intlogger.error("PROV0001 printLogRecords: " + e.getMessage(), e);
\r
91 private final String type;
\r
92 private final String feedFileid;
\r
93 private final String remoteAddr;
\r
94 private final String user;
\r
95 private final int status;
\r
96 private final int subid;
\r
97 private final String fileid;
\r
98 private final int result;
\r
99 private final int attempts;
\r
100 private final String reason;
\r
101 private final long recordId;
\r
102 private final long clength2;
\r
103 private final String fileName;
\r
106 * LogRecord constructor.
\r
107 * @param rs ResultSet from SQL statement
\r
108 * @throws SQLException in case of SQL error
\r
110 public LogRecord(ResultSet rs) throws SQLException {
\r
112 this.type = rs.getString("TYPE");
\r
113 this.feedFileid = rs.getString("FEED_FILEID");
\r
114 this.remoteAddr = rs.getString("REMOTE_ADDR");
\r
115 this.user = rs.getString("USER");
\r
116 this.status = rs.getInt("STATUS");
\r
118 this.subid = rs.getInt("DELIVERY_SUBID");
\r
119 this.fileid = rs.getString("DELIVERY_FILEID");
\r
120 this.result = rs.getInt("RESULT");
\r
122 this.attempts = rs.getInt("ATTEMPTS");
\r
123 this.reason = rs.getString("REASON");
\r
125 this.recordId = rs.getLong("RECORD_ID");
\r
126 this.clength2 = rs.getLong("CONTENT_LENGTH_2");
\r
127 this.fileName = rs.getString("FILENAME");
\r
131 * LogRecord Constructor from string array.
\r
132 * @param pp string array of LogRecord attributes
\r
133 * @throws ParseException in case of parse error
\r
135 public LogRecord(String[] pp) throws ParseException {
\r
138 this.feedFileid = pp[9];
\r
139 this.remoteAddr = pp[10];
\r
140 this.user = pp[11];
\r
141 this.status = Integer.parseInt(pp[12]);
\r
143 this.subid = Integer.parseInt(pp[13]);
\r
144 this.fileid = pp[14];
\r
145 this.result = Integer.parseInt(pp[15]);
\r
147 this.attempts = Integer.parseInt(pp[16]);
\r
148 this.reason = pp[17];
\r
150 this.recordId = Long.parseLong(pp[18]);
\r
151 this.clength2 = (pp.length == 21) ? Long.parseLong(pp[19]) : 0;
\r
152 this.fileName = pp[20];
\r
155 public long getRecordId() {
\r
160 public String toString() {
\r
162 sdf.format(getEventTime()) + "|"
\r
164 + getPublishId() + "|"
\r
165 + getFeedid() + "|"
\r
166 + getRequestUri() + "|"
\r
167 + getMethod() + "|"
\r
168 + getContentType() + "|"
\r
169 + getContentLength() + "|"
\r
186 public void load(PreparedStatement ps) throws SQLException {
\r
187 ps.setString(1, type);
\r
188 super.load(ps); // loads fields 2-8
\r
189 if (type.equals("pub")) {
\r
190 ps.setString(9, feedFileid);
\r
191 ps.setString(10, remoteAddr);
\r
192 ps.setString(11, user);
\r
193 ps.setInt(12, status);
\r
194 ps.setNull(13, Types.INTEGER);
\r
195 ps.setNull(14, Types.VARCHAR);
\r
196 ps.setNull(15, Types.INTEGER);
\r
197 ps.setNull(16, Types.INTEGER);
\r
198 ps.setNull(17, Types.VARCHAR);
\r
199 ps.setLong(18, recordId);
\r
200 ps.setNull(19, Types.BIGINT);
\r
201 ps.setString(20, fileName);
\r
202 } else if (type.equals("del")) {
\r
203 ps.setNull(9, Types.VARCHAR);
\r
204 ps.setNull(10, Types.VARCHAR);
\r
205 ps.setString(11, user);
\r
206 ps.setNull(12, Types.INTEGER);
\r
207 ps.setInt(13, subid);
\r
208 ps.setString(14, fileid);
\r
209 ps.setInt(15, result);
\r
210 ps.setNull(16, Types.INTEGER);
\r
211 ps.setNull(17, Types.VARCHAR);
\r
212 ps.setLong(18, recordId);
\r
213 ps.setNull(19, Types.BIGINT);
\r
214 ps.setString(20, fileName);
\r
215 } else if (type.equals("exp")) {
\r
216 ps.setNull(9, Types.VARCHAR);
\r
217 ps.setNull(10, Types.VARCHAR);
\r
218 ps.setNull(11, Types.VARCHAR);
\r
219 ps.setNull(12, Types.INTEGER);
\r
220 ps.setInt(13, subid);
\r
221 ps.setString(14, fileid);
\r
222 ps.setNull(15, Types.INTEGER);
\r
223 ps.setInt(16, attempts);
\r
224 ps.setString(17, reason);
\r
225 ps.setLong(18, recordId);
\r
226 ps.setNull(19, Types.BIGINT);
\r
227 ps.setString(20, fileName);
\r
228 } else if (type.equals("pbf")) {
\r
229 ps.setString(9, feedFileid);
\r
230 ps.setString(10, remoteAddr);
\r
231 ps.setString(11, user);
\r
232 ps.setNull(12, Types.INTEGER);
\r
233 ps.setNull(13, Types.INTEGER);
\r
234 ps.setNull(14, Types.VARCHAR);
\r
235 ps.setNull(15, Types.INTEGER);
\r
236 ps.setNull(16, Types.INTEGER);
\r
237 ps.setNull(17, Types.VARCHAR);
\r
238 ps.setLong(18, recordId);
\r
239 ps.setLong(19, clength2);
\r
240 ps.setString(20, fileName);
\r
241 } else if (type.equals("dlx")) {
\r
242 ps.setNull(9, Types.VARCHAR);
\r
243 ps.setNull(10, Types.VARCHAR);
\r
244 ps.setNull(11, Types.VARCHAR);
\r
245 ps.setNull(12, Types.INTEGER);
\r
246 ps.setInt(13, subid);
\r
247 ps.setNull(14, Types.VARCHAR);
\r
248 ps.setNull(15, Types.INTEGER);
\r
249 ps.setNull(16, Types.INTEGER);
\r
250 ps.setNull(17, Types.VARCHAR);
\r
251 ps.setLong(18, recordId);
\r
252 ps.setLong(19, clength2);
\r
253 ps.setString(20, fileName);
\r
257 public static void main(String[] args) throws IOException {
\r
258 LogRecord.printLogRecords(System.out, new RLEBitSet(args[0]));
\r