a9a48ade2b7e830d2f36c81299fa8d47582b3615
[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 com.att.eelf.configuration.EELFLogger;
27 import com.att.eelf.configuration.EELFManager;
28 import java.io.File;
29 import java.io.FileOutputStream;
30 import java.io.IOException;
31 import java.io.OutputStream;
32 import java.nio.file.Files;
33 import java.nio.file.Paths;
34 import java.text.SimpleDateFormat;
35 import java.util.Date;
36 import java.util.regex.Matcher;
37 import java.util.regex.Pattern;
38
39 /**
40  * Logging for data router delivery events (PUB/DEL/EXP).
41  */
42 public class StatusLog {
43
44     private static final String EXCEPTION = "Exception";
45     private static EELFLogger eelfLogger = EELFManager.getInstance().getLogger(StatusLog.class);
46     private static StatusLog instance = new StatusLog();
47     private SimpleDateFormat filedate = new SimpleDateFormat("-yyyyMMddHHmm");
48
49     private String prefix = "logs/events";
50     private String suffix = ".log";
51     private String plainfile;
52     private String curfile;
53     private long nexttime;
54     private OutputStream os;
55     private long intvl;
56     private static NodeConfigManager config = NodeConfigManager.getInstance();
57
58     private StatusLog() {
59     }
60
61     /**
62      * Parse an interval of the form xxhyymzzs and round it to the nearest whole fraction of 24 hours.If no units are
63      * specified, assume seconds.
64      */
65     public static long parseInterval(String interval, int def) {
66         try {
67             Matcher matcher = Pattern.compile("(?:(\\d+)[Hh])?(?:(\\d+)[Mm])?(?:(\\d+)[Ss]?)?").matcher(interval);
68             if (matcher.matches()) {
69                 int dur = getDur(matcher);
70                 int best = 86400;
71                 int dist = best - dur;
72                 if (dur > best) {
73                     dist = dur - best;
74                 }
75                 best = getBest(dur, best, dist);
76                 def = best * 1000;
77             }
78         } catch (Exception e) {
79             eelfLogger.error(EXCEPTION, e);
80         }
81         return (def);
82     }
83
84     private static int getBest(int dur, int best, int dist) {
85         int base = 1;
86         for (int i = 0; i < 8; i++) {
87             int base2 = base;
88             base *= 2;
89             for (int j = 0; j < 4; j++) {
90                 int base3 = base2;
91                 base2 *= 3;
92                 for (int k = 0; k < 3; k++) {
93                     int cur = base3;
94                     base3 *= 5;
95                     int ndist = cur - dur;
96                     if (dur > cur) {
97                         ndist = dur - cur;
98                     }
99                     if (ndist < dist) {
100                         best = cur;
101                         dist = ndist;
102                     }
103                 }
104             }
105         }
106         return best;
107     }
108
109     private static int getDur(Matcher matcher) {
110         int dur = 0;
111         String match = matcher.group(1);
112         if (match != null) {
113             dur += 3600 * Integer.parseInt(match);
114         }
115         match = matcher.group(2);
116         if (match != null) {
117             dur += 60 * Integer.parseInt(match);
118         }
119         match = matcher.group(3);
120         if (match != null) {
121             dur += Integer.parseInt(match);
122         }
123         if (dur < 60) {
124             dur = 60;
125         }
126         return dur;
127     }
128
129     /**
130      * Get the name of the current log file.
131      *
132      * @return The full path name of the current event log file
133      */
134     public static synchronized String getCurLogFile() {
135         try {
136             instance.checkRoll(System.currentTimeMillis());
137         } catch (Exception e) {
138             eelfLogger.error(EXCEPTION, e);
139         }
140         return (instance.curfile);
141     }
142
143     /**
144      * Log a received publication attempt.
145      *
146      * @param pubid The publish ID assigned by the node
147      * @param feedid The feed id given by the publisher
148      * @param requrl The URL of the received request
149      * @param method The method (DELETE or PUT) in the received request
150      * @param ctype The content type (if method is PUT and clen > 0)
151      * @param clen The content length (if method is PUT)
152      * @param srcip The IP address of the publisher
153      * @param user The identity of the publisher
154      * @param status The status returned to the publisher
155      */
156     public static void logPub(String pubid, String feedid, String requrl, String method, String ctype, long clen,
157             String srcip, String user, int status) {
158         instance.log(
159                 "PUB|" + pubid + "|" + feedid + "|" + requrl + "|" + method + "|" + ctype + "|" + clen + "|" + srcip
160                         + "|" + user + "|" + status);
161         eelfLogger.info("PUB|" + pubid + "|" + feedid + "|" + requrl + "|" + method + "|" + ctype + "|" + clen + "|" + srcip
162                                 + "|" + user + "|" + status);
163     }
164
165     /**
166      * Log a data transfer error receiving a publication attempt.
167      *
168      * @param pubid The publish ID assigned by the node
169      * @param feedid The feed id given by the publisher
170      * @param requrl The URL of the received request
171      * @param method The method (DELETE or PUT) in the received request
172      * @param ctype The content type (if method is PUT and clen > 0)
173      * @param clen The expected content length (if method is PUT)
174      * @param rcvd The content length received
175      * @param srcip The IP address of the publisher
176      * @param user The identity of the publisher
177      * @param error The error message from the IO exception
178      */
179     public static void logPubFail(String pubid, String feedid, String requrl, String method, String ctype, long clen,
180             long rcvd, String srcip, String user, String error) {
181         instance.log("PBF|" + pubid + "|" + feedid + "|" + requrl + "|" + method + "|" + ctype + "|" + clen + "|" + rcvd
182                 + "|" + srcip + "|" + user + "|" + error);
183         eelfLogger.info("PBF|" + pubid + "|" + feedid + "|" + requrl + "|" + method + "|" + ctype + "|" + clen + "|" + rcvd
184                                 + "|" + srcip + "|" + user + "|" + error);
185     }
186
187     /**
188      * Log a delivery attempt.
189      *
190      * @param pubid The publish ID assigned by the node
191      * @param feedid The feed ID
192      * @param subid The (space delimited list of) subscription ID
193      * @param requrl The URL used in the attempt
194      * @param method The method (DELETE or PUT) in the attempt
195      * @param ctype The content type (if method is PUT, not metaonly, and clen > 0)
196      * @param clen The content length (if PUT and not metaonly)
197      * @param user The identity given to the subscriber
198      * @param status The status returned by the subscriber or -1 if an exeception occured trying to connect
199      * @param xpubid The publish ID returned by the subscriber
200      */
201     public static void logDel(String pubid, String feedid, String subid, String requrl, String method, String ctype,
202             long clen, String user, int status, String xpubid) {
203         if (feedid == null) {
204             return;
205         }
206         instance.log(
207                 "DEL|" + pubid + "|" + feedid + "|" + subid + "|" + requrl + "|" + method + "|" + ctype + "|" + clen
208                         + "|" + user + "|" + status + "|" + xpubid);
209         eelfLogger.info("DEL|" + pubid + "|" + feedid + "|" + subid + "|" + requrl + "|" + method + "|" + ctype + "|" + clen
210                                 + "|" + user + "|" + status + "|" + xpubid);
211     }
212
213     /**
214      * Log delivery attempts expired.
215      *
216      * @param pubid The publish ID assigned by the node
217      * @param feedid The feed ID
218      * @param subid The (space delimited list of) subscription ID
219      * @param requrl The URL that would be delivered to
220      * @param method The method (DELETE or PUT) in the request
221      * @param ctype The content type (if method is PUT, not metaonly, and clen > 0)
222      * @param clen The content length (if PUT and not metaonly)
223      * @param reason The reason the attempts were discontinued
224      * @param attempts The number of attempts made
225      */
226     public static void logExp(String pubid, String feedid, String subid, String requrl, String method, String ctype,
227             long clen, String reason, int attempts) {
228         if (feedid == null) {
229             return;
230         }
231         instance.log(
232                 "EXP|" + pubid + "|" + feedid + "|" + subid + "|" + requrl + "|" + method + "|" + ctype + "|" + clen
233                         + "|" + reason + "|" + attempts);
234         eelfLogger.info("EXP|" + pubid + "|" + feedid + "|" + subid + "|" + requrl + "|" + method + "|" + ctype + "|" + clen
235                                 + "|" + reason + "|" + attempts);
236     }
237
238     /**
239      * Log extra statistics about unsuccessful delivery attempts.
240      *
241      * @param pubid The publish ID assigned by the node
242      * @param feedid The feed ID
243      * @param subid The (space delimited list of) subscription ID
244      * @param clen The content length
245      * @param sent The # of bytes sent or -1 if subscriber returned an error instead of 100 Continue, otherwise, the
246      *      number of bytes sent before an error occurred.
247      */
248     public static void logDelExtra(String pubid, String feedid, String subid, long clen, long sent) {
249         if (feedid == null) {
250             return;
251         }
252         instance.log("DLX|" + pubid + "|" + feedid + "|" + subid + "|" + clen + "|" + sent);
253         eelfLogger.info("DLX|" + pubid + "|" + feedid + "|" + subid + "|" + clen + "|" + sent);
254     }
255
256     private synchronized void checkRoll(long now) throws IOException {
257         if (now >= nexttime) {
258             if (os != null) {
259                 os.close();
260                 os = null;
261             }
262             intvl = parseInterval(config.getEventLogInterval(), 300000);
263             prefix = config.getEventLogPrefix();
264             suffix = config.getEventLogSuffix();
265             nexttime = now - now % intvl + intvl;
266             curfile = prefix + filedate.format(new Date(nexttime - intvl)) + suffix;
267             plainfile = prefix + suffix;
268             notify();
269         }
270     }
271
272     private synchronized void log(String string) {
273         try {
274             long now = System.currentTimeMillis();
275             checkRoll(now);
276             if (os == null) {
277                 os = new FileOutputStream(curfile, true);
278                 (new File(plainfile)).delete();
279                 Files.createLink(Paths.get(plainfile), Paths.get(curfile));
280             }
281             os.write((NodeUtils.logts(new Date(now)) + '|' + string + '\n').getBytes());
282             os.flush();
283         } catch (IOException ioe) {
284             eelfLogger.error("IOException", ioe);
285         }
286     }
287 }