1 package evel_javalibrary.att.com;
\r
2 /**************************************************************************//**
\r
6 * This file implements the Evel Syslog Event class which is intended to provide a
\r
7 * simple wrapper around the complexity of AT&T's Vendor Event Listener API so
\r
8 * that VNFs can use it to send Syslog events.
\r
12 * Unless otherwise specified, all software contained herein is
\r
13 * Licensed under the Apache License, Version 2.0 (the "License");
\r
14 * you may not use this file except in compliance with the License.
\r
15 * You may obtain a copy of the License at
\r
16 * http://www.apache.org/licenses/LICENSE-2.0
\r
18 * Unless required by applicable law or agreed to in writing, software
\r
19 * distributed under the License is distributed on an "AS IS" BASIS,
\r
20 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
21 * See the License for the specific language governing permissions and
\r
22 * limitations under the License.
\r
23 *****************************************************************************/
\r
25 import java.text.MessageFormat;
\r
26 import java.util.ArrayList;
\r
28 import javax.json.Json;
\r
29 import javax.json.JsonArrayBuilder;
\r
30 import javax.json.JsonObject;
\r
31 import javax.json.JsonObjectBuilder;
\r
33 import org.apache.log4j.Logger;
\r
34 import org.slf4j.helpers.MessageFormatter;
\r
36 import evel_javalibrary.att.com.EvelFault.EVEL_SEVERITIES;
\r
39 public class EvelSyslog extends EvelHeader {
\r
41 int major_version = 1;
\r
42 int minor_version = 2;
\r
44 /**************************************************************************//**
\r
46 * JSON equivalent fields: newState, oldState
\r
47 *****************************************************************************/
\r
48 /**************************************************************************//**
\r
49 * Syslog facilities.
\r
50 * JSON equivalent field: syslogFacility
\r
51 *****************************************************************************/
\r
52 public enum EVEL_SYSLOG_FACILITIES{
\r
53 EVEL_SYSLOG_FACILITY_KERNEL,
\r
54 EVEL_SYSLOG_FACILITY_USER,
\r
55 EVEL_SYSLOG_FACILITY_MAIL,
\r
56 EVEL_SYSLOG_FACILITY_SYSTEM_DAEMON,
\r
57 EVEL_SYSLOG_FACILITY_SECURITY_AUTH,
\r
58 EVEL_SYSLOG_FACILITY_INTERNAL,
\r
59 EVEL_SYSLOG_FACILITY_LINE_PRINTER,
\r
60 EVEL_SYSLOG_FACILITY_NETWORK_NEWS,
\r
61 EVEL_SYSLOG_FACILITY_UUCP,
\r
62 EVEL_SYSLOG_FACILITY_CLOCK_DAEMON,
\r
63 EVEL_SYSLOG_FACILITY_SECURITY_AUTH2,
\r
64 EVEL_SYSLOG_FACILITY_FTP_DAEMON,
\r
65 EVEL_SYSLOG_FACILITY_NTP,
\r
66 EVEL_SYSLOG_FACILITY_LOG_AUDIT,
\r
67 EVEL_SYSLOG_FACILITY_LOG_ALERT,
\r
68 EVEL_SYSLOG_FACILITY_CLOCK_DAEMON2,
\r
69 EVEL_SYSLOG_FACILITY_LOCAL0,
\r
70 EVEL_SYSLOG_FACILITY_LOCAL1,
\r
71 EVEL_SYSLOG_FACILITY_LOCAL2,
\r
72 EVEL_SYSLOG_FACILITY_LOCAL3,
\r
73 EVEL_SYSLOG_FACILITY_LOCAL4,
\r
74 EVEL_SYSLOG_FACILITY_LOCAL5,
\r
75 EVEL_SYSLOG_FACILITY_LOCAL6,
\r
76 EVEL_SYSLOG_FACILITY_LOCAL7,
\r
77 EVEL_MAX_SYSLOG_FACILITIES
\r
82 /***************************************************************************/
\r
83 /* Mandatory fields */
\r
84 /***************************************************************************/
\r
85 EvelFault.EVEL_SOURCE_TYPES event_source_type;
\r
89 /***************************************************************************/
\r
90 /* Optional fields */
\r
91 /***************************************************************************/
\r
92 EvelOptionString additional_filters;
\r
93 EvelOptionString event_source_host;
\r
94 EvelOptionInt syslog_facility;
\r
95 EvelOptionInt syslog_priority;
\r
96 EvelOptionString syslog_proc;
\r
97 EvelOptionInt syslog_proc_id;
\r
98 EvelOptionString syslog_s_data;
\r
99 EvelOptionString syslog_sdid;
\r
100 EvelOptionString syslog_severity;
\r
101 double syslog_fver;
\r
102 EvelOptionInt syslog_ver;
\r
105 private static final Logger LOGGER = Logger.getLogger( EvelSyslog.class.getName() );
\r
107 /**************************************************************************//**
\r
108 * Create a new Syslog event.
\r
111 * @param syslg_msg The Syslog message string.
\r
112 * @param syslg_tag The Syslog tag string.
\r
113 * @param interface The card or port name of the reporting entity.
\r
115 *****************************************************************************/
\r
116 public EvelSyslog(String evname, String evid,
\r
117 EvelFault.EVEL_SOURCE_TYPES evt_source_type,
\r
121 super(evname,evid);
\r
122 event_domain = EvelHeader.DOMAINS.EVEL_DOMAIN_SYSLOG;
\r
123 assert(EvelFault.EVEL_SOURCE_TYPES.EVEL_MAX_SOURCE_TYPES.compareTo(event_source_type) < 0);
\r
124 assert(syslog_msg != null);
\r
125 assert(syslog_tag != null);
\r
127 event_source_type = evt_source_type;
\r
128 syslog_msg = syslg_msg;
\r
129 syslog_tag = syslg_tag;
\r
130 syslog_facility = new EvelOptionInt();
\r
131 syslog_priority = new EvelOptionInt();
\r
132 syslog_proc_id = new EvelOptionInt();
\r
133 syslog_ver = new EvelOptionInt();
\r
134 additional_filters = new EvelOptionString();
\r
135 event_source_host = new EvelOptionString();
\r
136 syslog_proc = new EvelOptionString();
\r
137 syslog_s_data = new EvelOptionString();
\r
138 syslog_sdid = new EvelOptionString();
\r
139 syslog_severity = new EvelOptionString();
\r
142 /**************************************************************************//**
\r
143 * Set the Event Type property of the Syslog.
\r
145 * @param type The Event Type to be set. ASCIIZ string. The caller
\r
146 * does not need to preserve the value once the function
\r
148 *****************************************************************************/
\r
149 public void evel_syslog_type_set(String typ)
\r
153 /***************************************************************************/
\r
154 /* Check preconditions and call evel_header_type_set. */
\r
155 /***************************************************************************/
\r
156 assert(event_domain == EvelHeader.DOMAINS.EVEL_DOMAIN_SYSLOG);
\r
157 evel_header_type_set(typ);
\r
162 /**************************************************************************//**
\r
163 * Add an additional value name/value pair to the Syslog.
\r
165 * The name and value are null delimited ASCII strings. The library takes
\r
166 * a copy so the caller does not have to preserve values after the function
\r
170 * @param name ASCIIZ string with the attribute's name. The caller
\r
171 * does not need to preserve the value once the function
\r
173 * @param value ASCIIZ string with the attribute's value. The caller
\r
174 * does not need to preserve the value once the function
\r
176 *****************************************************************************/
\r
177 public void evel_syslog_addl_filter_set(String filter)
\r
181 /***************************************************************************/
\r
182 /* Check preconditions. */
\r
183 /***************************************************************************/
\r
184 assert(event_domain == EvelHeader.DOMAINS.EVEL_DOMAIN_SYSLOG);
\r
185 assert(filter != null);
\r
187 additional_filters.SetValuePr(
\r
189 "Syslog filter string");
\r
194 /**************************************************************************//**
\r
195 * Set the Event Source Host property of the Syslog.
\r
197 * @note The property is treated as immutable: it is only valid to call
\r
198 * the setter once. However, we don't assert if the caller tries to
\r
199 * overwrite, just ignoring the update instead.
\r
202 * @param host The Event Source Host to be set. ASCIIZ string. The caller
\r
203 * does not need to preserve the value once the function
\r
205 *****************************************************************************/
\r
206 public void evel_syslog_event_source_host_set(String host)
\r
210 /***************************************************************************/
\r
211 /* Check preconditions. */
\r
212 /***************************************************************************/
\r
213 assert(event_domain == EvelHeader.DOMAINS.EVEL_DOMAIN_SYSLOG);
\r
214 assert(host != null);
\r
216 event_source_host.SetValuePr(
\r
218 "Event Source Host");
\r
222 /**************************************************************************//**
\r
223 * Set the Facility property of the Syslog.
\r
225 * @note The property is treated as immutable: it is only valid to call
\r
226 * the setter once. However, we don't assert if the caller tries to
\r
227 * overwrite, just ignoring the update instead.
\r
229 * @param syslog Pointer to the Syslog.
\r
230 * @param facility The Syslog Facility to be set. ASCIIZ string. The caller
\r
231 * does not need to preserve the value once the function
\r
233 *****************************************************************************/
\r
234 public void evel_syslog_facility_set(
\r
235 EVEL_SYSLOG_FACILITIES facility)
\r
239 /***************************************************************************/
\r
240 /* Check preconditions. */
\r
241 /***************************************************************************/
\r
242 assert(event_domain == EvelHeader.DOMAINS.EVEL_DOMAIN_SYSLOG);
\r
243 assert(facility.compareTo(EVEL_SYSLOG_FACILITIES.EVEL_MAX_SYSLOG_FACILITIES) < 0);
\r
244 int num = facility.compareTo(EVEL_SYSLOG_FACILITIES.EVEL_SYSLOG_FACILITY_KERNEL);
\r
246 syslog_facility.SetValuePr(
\r
252 /**************************************************************************//**
\r
253 * Set the Process property of the Syslog.
\r
255 * @note The property is treated as immutable: it is only valid to call
\r
256 * the setter once. However, we don't assert if the caller tries to
\r
257 * overwrite, just ignoring the update instead.
\r
260 * @param proc The Process to be set. ASCIIZ string. The caller does not
\r
261 * need to preserve the value once the function returns.
\r
262 *****************************************************************************/
\r
263 public void evel_syslog_proc_set(String proc)
\r
267 /***************************************************************************/
\r
268 /* Check preconditions. */
\r
269 /***************************************************************************/
\r
270 assert(event_domain == EvelHeader.DOMAINS.EVEL_DOMAIN_SYSLOG);
\r
271 assert(proc != null);
\r
273 syslog_proc.SetValuePr(proc, "Process");
\r
277 /**************************************************************************//**
\r
278 * Set the Process ID property of the Syslog.
\r
280 * @note The property is treated as immutable: it is only valid to call
\r
281 * the setter once. However, we don't assert if the caller tries to
\r
282 * overwrite, just ignoring the update instead.
\r
285 * @param proc_id The Process ID to be set. ASCIIZ string. The caller does
\r
286 * not need to preserve the value once the function returns.
\r
287 *****************************************************************************/
\r
288 public void evel_syslog_proc_id_set(int proc_id)
\r
292 /***************************************************************************/
\r
293 /* Check preconditions. */
\r
294 /***************************************************************************/
\r
295 assert(event_domain == EvelHeader.DOMAINS.EVEL_DOMAIN_SYSLOG);
\r
296 assert(proc_id > 0);
\r
298 syslog_proc_id.SetValuePr(
\r
304 /**************************************************************************//**
\r
305 * Set the Version property of the Syslog.
\r
307 * @note The property is treated as immutable: it is only valid to call
\r
308 * the setter once. However, we don't assert if the caller tries to
\r
309 * overwrite, just ignoring the update instead.
\r
312 * @param version The Version to be set. ASCIIZ string. The caller does not
\r
313 * need to preserve the value once the function returns.
\r
314 *****************************************************************************/
\r
315 public void evel_syslog_version_set(int version)
\r
319 /***************************************************************************/
\r
320 /* Check preconditions. */
\r
321 /***************************************************************************/
\r
322 assert(event_domain == EvelHeader.DOMAINS.EVEL_DOMAIN_SYSLOG);
\r
323 assert(version >= 0);
\r
325 syslog_ver.SetValuePr(
\r
331 /**************************************************************************//**
\r
332 * Set the Structured Data property of the Syslog.
\r
334 * @note The property is treated as immutable: it is only valid to call
\r
335 * the setter once. However, we don't assert if the caller tries to
\r
336 * overwrite, just ignoring the update instead.
\r
339 * @param s_data The Structured Data to be set. ASCIIZ string. The caller
\r
340 * does not need to preserve the value once the function
\r
342 *****************************************************************************/
\r
343 public void evel_syslog_s_data_set(String s_data)
\r
347 /***************************************************************************/
\r
348 /* Check preconditions. */
\r
349 /***************************************************************************/
\r
350 assert(event_domain == EvelHeader.DOMAINS.EVEL_DOMAIN_SYSLOG);
\r
351 assert(s_data != null);
\r
353 syslog_s_data.SetValuePr(
\r
355 "Structured Data");
\r
359 /**************************************************************************//**
\r
360 * Set the Structured SDID property of the Syslog.
\r
362 * @note The property is treated as immutable: it is only valid to call
\r
363 * the setter once. However, we don't assert if the caller tries to
\r
364 * overwrite, just ignoring the update instead.
\r
367 * @param sdid The Structured Data to be set. ASCIIZ string. name@number
\r
368 * Caller does not need to preserve the value once the function
\r
370 *****************************************************************************/
\r
371 public void evel_syslog_sdid_set(String sdid)
\r
375 /***************************************************************************/
\r
376 /* Check preconditions. */
\r
377 /***************************************************************************/
\r
378 assert(event_domain == EvelHeader.DOMAINS.EVEL_DOMAIN_SYSLOG);
\r
379 assert(sdid != null);
\r
381 syslog_sdid.SetValuePr(
\r
387 /**************************************************************************//**
\r
388 * Set the Structured Severity property of the Syslog.
\r
390 * @note The property is treated as immutable: it is only valid to call
\r
391 * the setter once. However, we don't assert if the caller tries to
\r
392 * overwrite, just ignoring the update instead.
\r
395 * @param sdid The Structured Data to be set. ASCIIZ string.
\r
396 * Caller does not need to preserve the value once the function
\r
398 *****************************************************************************/
\r
399 public void evel_syslog_severity_set(String severty)
\r
403 /***************************************************************************/
\r
404 /* Check preconditions. */
\r
405 /***************************************************************************/
\r
406 assert(event_domain == EvelHeader.DOMAINS.EVEL_DOMAIN_SYSLOG);
\r
407 assert(severty != null);
\r
409 if( severty.equals("Alert") || severty.equals("Critical") || severty.equals("Debug") ||
\r
410 severty.equals("Emergency") || severty.equals("Error") || severty.equals("Info") ||
\r
411 severty.equals("Notice") || severty.equals("Warning") )
\r
413 syslog_severity.SetValuePr(
\r
421 /**************************************************************************//**
\r
422 * Encode the Syslog in JSON according to AT&T's schema.
\r
424 *****************************************************************************/
\r
425 JsonObjectBuilder evelSyslogObject()
\r
427 String evt_source_type;
\r
428 double version = major_version+(double)minor_version/10;
\r
432 assert(event_domain == EvelHeader.DOMAINS.EVEL_DOMAIN_SYSLOG);
\r
434 /***************************************************************************/
\r
435 /* Mandatory fields */
\r
436 /***************************************************************************/
\r
437 JsonObjectBuilder obj = Json.createObjectBuilder()
\r
438 .add("eventSourceType", event_source_type.toString())
\r
439 .add("syslogMsg", syslog_msg)
\r
440 .add("syslogTag", syslog_tag)
\r
441 .add("syslogFieldsVersion",version);
\r
443 /***************************************************************************/
\r
444 /* Optional fields */
\r
445 /***************************************************************************/
\r
446 event_source_host.encJsonValue(obj, "eventSourceHost");
\r
447 syslog_facility.encJsonValue(obj,"syslogFacility");
\r
448 syslog_priority.encJsonValue(obj,"syslogPri");
\r
449 syslog_proc.encJsonValue(obj, "syslogProc");
\r
450 syslog_proc_id.encJsonValue(obj, "syslogProcId");
\r
451 syslog_s_data.encJsonValue(obj, "syslogSData");
\r
452 syslog_sdid.encJsonValue(obj, "syslogSdId");
\r
453 syslog_severity.encJsonValue(obj, "syslogSev");
\r
454 syslog_ver.encJsonValue(obj, "syslogVer");
\r
461 /**************************************************************************//**
\r
462 * Encode the event as a JSON event object according to AT&T's schema.
\r
463 * retval : String of JSON syslog event message
\r
464 *****************************************************************************/
\r
465 String evel_json_encode_event()
\r
469 assert(event_domain == EvelHeader.DOMAINS.EVEL_DOMAIN_SYSLOG);
\r
471 JsonObject obj = Json.createObjectBuilder()
\r
472 .add("event", Json.createObjectBuilder()
\r
473 .add( "commonEventHeader",eventHeaderObject() )
\r
474 .add( "syslogFields",evelSyslogObject() )
\r
479 return obj.toString();
\r