1 /*******************************************************************************
2 * ============LICENSE_START==================================================
4 * * ===========================================================================
5 * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * * ===========================================================================
7 * * Licensed under the Apache License, Version 2.0 (the "License");
8 * * you may not use this file except in compliance with the License.
9 * * You may obtain a copy of the License at
11 * * http://www.apache.org/licenses/LICENSE-2.0
13 * * Unless required by applicable law or agreed to in writing, software
14 * * distributed under the License is distributed on an "AS IS" BASIS,
15 * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * * See the License for the specific language governing permissions and
17 * * limitations under the License.
18 * * ============LICENSE_END====================================================
20 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 ******************************************************************************/
24 package org.onap.dmaap.datarouter.node;
26 import java.util.regex.*;
29 import java.nio.file.*;
33 * Logging for data router delivery events (PUB/DEL/EXP)
35 public class StatusLog {
36 private static StatusLog instance = new StatusLog();
37 private HashSet<String> toship = new HashSet<String>();
38 private SimpleDateFormat filedate;
39 private String prefix = "logs/events";
40 private String suffix = ".log";
41 private String plainfile;
42 private String curfile;
43 private long nexttime;
44 private OutputStream os;
46 private NodeConfigManager config = NodeConfigManager.getInstance();
50 filedate = new SimpleDateFormat("-yyyyMMddHHmm");
51 } catch (Exception e) {
56 * Parse an interval of the form xxhyymzzs and round it to the nearest whole fraction of 24 hours.If no units are specified, assume seconds.
58 public static long parseInterval(String interval, int def) {
60 Matcher m = Pattern.compile("(?:(\\d+)[Hh])?(?:(\\d+)[Mm])?(?:(\\d+)[Ss]?)?").matcher(interval);
63 String x = m.group(1);
65 dur += 3600 * Integer.parseInt(x);
69 dur += 60 * Integer.parseInt(x);
73 dur += Integer.parseInt(x);
79 int dist = best - dur;
84 for (int i = 0; i < 8; i++) {
87 for (int j = 0; j < 4; j++) {
90 for (int k = 0; k < 3; k++) {
93 int ndist = cur - dur;
106 } catch (Exception e) {
111 private synchronized void checkRoll(long now) throws IOException {
112 if (now >= nexttime) {
117 intvl = parseInterval(config.getEventLogInterval(), 300000);
118 prefix = config.getEventLogPrefix();
119 suffix = config.getEventLogSuffix();
120 nexttime = now - now % intvl + intvl;
121 curfile = prefix + filedate.format(new Date(nexttime - intvl)) + suffix;
122 plainfile = prefix + suffix;
128 * Get the name of the current log file
130 * @return The full path name of the current event log file
132 public static synchronized String getCurLogFile() {
134 instance.checkRoll(System.currentTimeMillis());
135 } catch (Exception e) {
137 return (instance.curfile);
140 private synchronized void log(String s) {
142 long now = System.currentTimeMillis();
145 os = new FileOutputStream(curfile, true);
146 (new File(plainfile)).delete();
147 Files.createLink(Paths.get(plainfile), Paths.get(curfile));
149 os.write((NodeUtils.logts(new Date(now)) + '|' + s + '\n').getBytes());
151 } catch (IOException ioe) {
156 * Log a received publication attempt.
158 * @param pubid The publish ID assigned by the node
159 * @param feedid The feed id given by the publisher
160 * @param requrl The URL of the received request
161 * @param method The method (DELETE or PUT) in the received request
162 * @param ctype The content type (if method is PUT and clen > 0)
163 * @param clen The content length (if method is PUT)
164 * @param srcip The IP address of the publisher
165 * @param user The identity of the publisher
166 * @param status The status returned to the publisher
168 public static void logPub(String pubid, String feedid, String requrl, String method, String ctype, long clen, String srcip, String user, int status) {
169 instance.log("PUB|" + pubid + "|" + feedid + "|" + requrl + "|" + method + "|" + ctype + "|" + clen + "|" + srcip + "|" + user + "|" + status);
173 * Log a data transfer error receiving a publication attempt
175 * @param pubid The publish ID assigned by the node
176 * @param feedid The feed id given by the publisher
177 * @param requrl The URL of the received request
178 * @param method The method (DELETE or PUT) in the received request
179 * @param ctype The content type (if method is PUT and clen > 0)
180 * @param clen The expected content length (if method is PUT)
181 * @param rcvd The content length received
182 * @param srcip The IP address of the publisher
183 * @param user The identity of the publisher
184 * @param error The error message from the IO exception
186 public static void logPubFail(String pubid, String feedid, String requrl, String method, String ctype, long clen, long rcvd, String srcip, String user, String error) {
187 instance.log("PBF|" + pubid + "|" + feedid + "|" + requrl + "|" + method + "|" + ctype + "|" + clen + "|" + rcvd + "|" + srcip + "|" + user + "|" + error);
191 * Log a delivery attempt.
193 * @param pubid The publish ID assigned by the node
194 * @param feedid The feed ID
195 * @param subid The (space delimited list of) subscription ID
196 * @param requrl The URL used in the attempt
197 * @param method The method (DELETE or PUT) in the attempt
198 * @param ctype The content type (if method is PUT, not metaonly, and clen > 0)
199 * @param clen The content length (if PUT and not metaonly)
200 * @param user The identity given to the subscriber
201 * @param status The status returned by the subscriber or -1 if an exeception occured trying to connect
202 * @param xpubid The publish ID returned by the subscriber
204 public static void logDel(String pubid, String feedid, String subid, String requrl, String method, String ctype, long clen, String user, int status, String xpubid) {
205 if (feedid == null) {
208 instance.log("DEL|" + pubid + "|" + feedid + "|" + subid + "|" + requrl + "|" + method + "|" + ctype + "|" + clen + "|" + user + "|" + status + "|" + xpubid);
212 * Log delivery attempts expired
214 * @param pubid The publish ID assigned by the node
215 * @param feedid The feed ID
216 * @param subid The (space delimited list of) subscription ID
217 * @param requrl The URL that would be delivered to
218 * @param method The method (DELETE or PUT) in the request
219 * @param ctype The content type (if method is PUT, not metaonly, and clen > 0)
220 * @param clen The content length (if PUT and not metaonly)
221 * @param reason The reason the attempts were discontinued
222 * @param attempts The number of attempts made
224 public static void logExp(String pubid, String feedid, String subid, String requrl, String method, String ctype, long clen, String reason, int attempts) {
225 if (feedid == null) {
228 instance.log("EXP|" + pubid + "|" + feedid + "|" + subid + "|" + requrl + "|" + method + "|" + ctype + "|" + clen + "|" + reason + "|" + attempts);
232 * Log extra statistics about unsuccessful delivery attempts.
234 * @param pubid The publish ID assigned by the node
235 * @param feedid The feed ID
236 * @param subid The (space delimited list of) subscription ID
237 * @param clen The content length
238 * @param sent The # of bytes sent or -1 if subscriber returned an error instead of 100 Continue, otherwise, the number of bytes sent before an error occurred.
240 public static void logDelExtra(String pubid, String feedid, String subid, long clen, long sent) {
241 if (feedid == null) {
244 instance.log("DLX|" + pubid + "|" + feedid + "|" + subid + "|" + clen + "|" + sent);
247 private StatusLog() {