c8a7bd0caaec21c64ac7d8c5d9ba750b85b78b5c
[dmaap/datarouter.git] / datarouter-node / src / main / java / org / onap / dmaap / datarouter / node / StatusLog.java
1 /*******************************************************************************
2  * ============LICENSE_START==================================================
3  * * org.onap.dmaap
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
10  * *
11  *  *      http://www.apache.org/licenses/LICENSE-2.0
12  * *
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====================================================
19  * *
20  * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  * *
22  ******************************************************************************/
23
24 package org.onap.dmaap.datarouter.node;
25
26 import java.util.regex.*;
27 import java.util.*;
28 import java.io.*;
29 import java.nio.file.*;
30 import java.text.*;
31
32 /**
33  * Logging for data router delivery events (PUB/DEL/EXP)
34  */
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;
45     private long intvl;
46     private NodeConfigManager config = NodeConfigManager.getInstance();
47
48     {
49         try {
50             filedate = new SimpleDateFormat("-yyyyMMddHHmm");
51         } catch (Exception e) {
52         }
53     }
54
55     /**
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.
57      */
58     public static long parseInterval(String interval, int def) {
59         try {
60             Matcher m = Pattern.compile("(?:(\\d+)[Hh])?(?:(\\d+)[Mm])?(?:(\\d+)[Ss]?)?").matcher(interval);
61             if (m.matches()) {
62                 int dur = 0;
63                 String x = m.group(1);
64                 if (x != null) {
65                     dur += 3600 * Integer.parseInt(x);
66                 }
67                 x = m.group(2);
68                 if (x != null) {
69                     dur += 60 * Integer.parseInt(x);
70                 }
71                 x = m.group(3);
72                 if (x != null) {
73                     dur += Integer.parseInt(x);
74                 }
75                 if (dur < 60) {
76                     dur = 60;
77                 }
78                 int best = 86400;
79                 int dist = best - dur;
80                 if (dur > best) {
81                     dist = dur - best;
82                 }
83                 int base = 1;
84                 for (int i = 0; i < 8; i++) {
85                     int base2 = base;
86                     base *= 2;
87                     for (int j = 0; j < 4; j++) {
88                         int base3 = base2;
89                         base2 *= 3;
90                         for (int k = 0; k < 3; k++) {
91                             int cur = base3;
92                             base3 *= 5;
93                             int ndist = cur - dur;
94                             if (dur > cur) {
95                                 ndist = dur - cur;
96                             }
97                             if (ndist < dist) {
98                                 best = cur;
99                                 dist = ndist;
100                             }
101                         }
102                     }
103                 }
104                 def = best * 1000;
105             }
106         } catch (Exception e) {
107         }
108         return (def);
109     }
110
111     private synchronized void checkRoll(long now) throws IOException {
112         if (now >= nexttime) {
113             if (os != null) {
114                 os.close();
115                 os = null;
116             }
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;
123             notify();
124         }
125     }
126
127     /**
128      * Get the name of the current log file
129      *
130      * @return The full path name of the current event log file
131      */
132     public static synchronized String getCurLogFile() {
133         try {
134             instance.checkRoll(System.currentTimeMillis());
135         } catch (Exception e) {
136         }
137         return (instance.curfile);
138     }
139
140     private synchronized void log(String s) {
141         try {
142             long now = System.currentTimeMillis();
143             checkRoll(now);
144             if (os == null) {
145                 os = new FileOutputStream(curfile, true);
146                 (new File(plainfile)).delete();
147                 Files.createLink(Paths.get(plainfile), Paths.get(curfile));
148             }
149             os.write((NodeUtils.logts(new Date(now)) + '|' + s + '\n').getBytes());
150             os.flush();
151         } catch (IOException ioe) {
152         }
153     }
154
155     /**
156      * Log a received publication attempt.
157      *
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
167      */
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);
170     }
171
172     /**
173      * Log a data transfer error receiving a publication attempt
174      *
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
185      */
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);
188     }
189
190     /**
191      * Log a delivery attempt.
192      *
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
203      */
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) {
206             return;
207         }
208         instance.log("DEL|" + pubid + "|" + feedid + "|" + subid + "|" + requrl + "|" + method + "|" + ctype + "|" + clen + "|" + user + "|" + status + "|" + xpubid);
209     }
210
211     /**
212      * Log delivery attempts expired
213      *
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
223      */
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) {
226             return;
227         }
228         instance.log("EXP|" + pubid + "|" + feedid + "|" + subid + "|" + requrl + "|" + method + "|" + ctype + "|" + clen + "|" + reason + "|" + attempts);
229     }
230
231     /**
232      * Log extra statistics about unsuccessful delivery attempts.
233      *
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.
239      */
240     public static void logDelExtra(String pubid, String feedid, String subid, long clen, long sent) {
241         if (feedid == null) {
242             return;
243         }
244         instance.log("DLX|" + pubid + "|" + feedid + "|" + subid + "|" + clen + "|" + sent);
245     }
246
247     private StatusLog() {
248     }
249 }