Update License text
[vnfsdk/compliance.git] / veslibrary / ves_javalibrary / evel_javalib2 / src / evel_javalibrary / att / com / EvelSyslog.java
1 package evel_javalibrary.att.com;\r
2 /**************************************************************************//**\r
3  * @file\r
4  * Evel Syslog class\r
5  *\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
9  *\r
10  * License\r
11  * -------\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
17  *\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
24 \r
25 import java.text.MessageFormat;\r
26 import java.util.ArrayList;\r
27 \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
32 \r
33 import org.apache.log4j.Logger;\r
34 import org.slf4j.helpers.MessageFormatter;\r
35 \r
36 import evel_javalibrary.att.com.EvelFault.EVEL_SEVERITIES;\r
37 \r
38 \r
39 public class EvelSyslog extends EvelHeader {\r
40         \r
41         int major_version = 1;\r
42         int minor_version = 2;\r
43         \r
44         /**************************************************************************//**\r
45          * Alert types.\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
78         }\r
79 \r
80 \r
81         \r
82         /***************************************************************************/\r
83         /* Mandatory fields                                                        */\r
84         /***************************************************************************/\r
85           EvelFault.EVEL_SOURCE_TYPES event_source_type;\r
86           String syslog_msg;\r
87           String syslog_tag;\r
88 \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
103 \r
104         \r
105           private static final Logger LOGGER = Logger.getLogger( EvelSyslog.class.getName() );\r
106 \r
107           /**************************************************************************//**\r
108            * Create a new Syslog event.\r
109            *\r
110            *\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
114            *\r
115            *****************************************************************************/\r
116         public EvelSyslog(String evname, String evid,\r
117                                   EvelFault.EVEL_SOURCE_TYPES evt_source_type,\r
118                       String syslg_msg,\r
119                       String syslg_tag)\r
120         {\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
126 \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
140         }         \r
141           \r
142           /**************************************************************************//**\r
143            * Set the Event Type property of the Syslog.\r
144            *\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
147            *                    returns.\r
148            *****************************************************************************/\r
149           public void evel_syslog_type_set(String typ)\r
150           {\r
151             EVEL_ENTER();\r
152 \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
158 \r
159             EVEL_EXIT();\r
160           }\r
161 \r
162           /**************************************************************************//**\r
163            * Add an additional value name/value pair to the Syslog.\r
164            *\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
167            * returns.\r
168            *\r
169            * \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
172            *                  returns.\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
175            *                  returns.\r
176            *****************************************************************************/\r
177           public void evel_syslog_addl_filter_set(String filter)\r
178           {\r
179             EVEL_ENTER();\r
180 \r
181             /***************************************************************************/\r
182             /* Check preconditions.                                                    */\r
183             /***************************************************************************/\r
184             assert(event_domain == EvelHeader.DOMAINS.EVEL_DOMAIN_SYSLOG);\r
185             assert(filter != null);\r
186 \r
187             additional_filters.SetValuePr(\r
188                                    filter,\r
189                                    "Syslog filter string");\r
190 \r
191             EVEL_EXIT();\r
192           }\r
193 \r
194           /**************************************************************************//**\r
195            * Set the Event Source Host property of the Syslog.\r
196            *\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
200            *\r
201            * \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
204            *                   returns.\r
205            *****************************************************************************/\r
206           public void evel_syslog_event_source_host_set(String host)\r
207           {\r
208             EVEL_ENTER();\r
209 \r
210             /***************************************************************************/\r
211             /* Check preconditions.                                                    */\r
212             /***************************************************************************/\r
213             assert(event_domain == EvelHeader.DOMAINS.EVEL_DOMAIN_SYSLOG);\r
214             assert(host != null);\r
215 \r
216             event_source_host.SetValuePr(\r
217                                    host,\r
218                                    "Event Source Host");\r
219             EVEL_EXIT();\r
220           }\r
221 \r
222           /**************************************************************************//**\r
223            * Set the Facility property of the Syslog.\r
224            *\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
228            *\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
232            *                    returns.\r
233            *****************************************************************************/\r
234           public void evel_syslog_facility_set(\r
235                                         EVEL_SYSLOG_FACILITIES facility)\r
236           {\r
237             EVEL_ENTER();\r
238 \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
245 \r
246             syslog_facility.SetValuePr(\r
247                                 num,\r
248                                 "Facility");\r
249             EVEL_EXIT();\r
250           }\r
251 \r
252           /**************************************************************************//**\r
253            * Set the Process property of the Syslog.\r
254            *\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
258            *\r
259            * \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
264           {\r
265             EVEL_ENTER();\r
266 \r
267             /***************************************************************************/\r
268             /* Check preconditions.                                                    */\r
269             /***************************************************************************/\r
270             assert(event_domain == EvelHeader.DOMAINS.EVEL_DOMAIN_SYSLOG);\r
271             assert(proc != null);\r
272 \r
273             syslog_proc.SetValuePr(proc, "Process");\r
274             EVEL_EXIT();\r
275           }\r
276 \r
277           /**************************************************************************//**\r
278            * Set the Process ID property of the Syslog.\r
279            *\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
283            *\r
284            * \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
289           {\r
290             EVEL_ENTER();\r
291 \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
297 \r
298             syslog_proc_id.SetValuePr(\r
299                                 proc_id,\r
300                                 "Process ID");\r
301             EVEL_EXIT();\r
302           }\r
303 \r
304           /**************************************************************************//**\r
305            * Set the Version property of the Syslog.\r
306            *\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
310            *\r
311            * \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
316           {\r
317             EVEL_ENTER();\r
318 \r
319             /***************************************************************************/\r
320             /* Check preconditions.                                                    */\r
321             /***************************************************************************/\r
322             assert(event_domain == EvelHeader.DOMAINS.EVEL_DOMAIN_SYSLOG);\r
323             assert(version >= 0);\r
324 \r
325             syslog_ver.SetValuePr(\r
326                                 version,\r
327                                 "Version");\r
328             EVEL_EXIT();\r
329           }\r
330 \r
331           /**************************************************************************//**\r
332            * Set the Structured Data property of the Syslog.\r
333            *\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
337            *\r
338            * \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
341            *                   returns.\r
342            *****************************************************************************/\r
343           public void evel_syslog_s_data_set(String s_data)\r
344           {\r
345             EVEL_ENTER();\r
346 \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
352 \r
353             syslog_s_data.SetValuePr(\r
354                                    s_data,\r
355                                    "Structured Data");\r
356             EVEL_EXIT();\r
357           }\r
358 \r
359           /**************************************************************************//**\r
360            * Set the Structured SDID property of the Syslog.\r
361            *\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
365            *\r
366            * \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
369            *                   returns.\r
370            *****************************************************************************/\r
371           public void evel_syslog_sdid_set(String sdid)\r
372           {\r
373             EVEL_ENTER();\r
374 \r
375             /***************************************************************************/\r
376             /* Check preconditions.                                                    */\r
377             /***************************************************************************/\r
378             assert(event_domain == EvelHeader.DOMAINS.EVEL_DOMAIN_SYSLOG);\r
379             assert(sdid != null);\r
380 \r
381             syslog_sdid.SetValuePr(\r
382                                    sdid,\r
383                                    "SdId set");\r
384             EVEL_EXIT();\r
385           }\r
386 \r
387           /**************************************************************************//**\r
388            * Set the Structured Severity property of the Syslog.\r
389            *\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
393            *\r
394            * \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
397            *                   returns.\r
398            *****************************************************************************/\r
399           public void evel_syslog_severity_set(String severty)\r
400           {\r
401             EVEL_ENTER();\r
402 \r
403             /***************************************************************************/\r
404             /* Check preconditions.                                                    */\r
405             /***************************************************************************/\r
406             assert(event_domain == EvelHeader.DOMAINS.EVEL_DOMAIN_SYSLOG);\r
407             assert(severty != null);\r
408 \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
412             {\r
413                syslog_severity.SetValuePr(\r
414                                    severty,\r
415                                    "Severity set");\r
416             }\r
417             EVEL_EXIT();\r
418           }\r
419         \r
420           \r
421                 /**************************************************************************//**\r
422                  * Encode the Syslog in JSON according to AT&T's schema.\r
423                  *\r
424                  *****************************************************************************/\r
425                  JsonObjectBuilder evelSyslogObject()\r
426                  {\r
427                   String evt_source_type;\r
428                   double version = major_version+(double)minor_version/10;\r
429 \r
430                   EVEL_ENTER();\r
431                   \r
432                   assert(event_domain == EvelHeader.DOMAINS.EVEL_DOMAIN_SYSLOG);\r
433                   \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
442 \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
455 \r
456                   EVEL_EXIT();\r
457                   \r
458                   return obj;\r
459                 }       \r
460         \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
466           {\r
467                 EVEL_ENTER();\r
468                 \r
469                 assert(event_domain == EvelHeader.DOMAINS.EVEL_DOMAIN_SYSLOG);\r
470                 \r
471             JsonObject obj = Json.createObjectBuilder()\r
472                      .add("event", Json.createObjectBuilder()\r
473                                  .add( "commonEventHeader",eventHeaderObject() )\r
474                                  .add( "syslogFields",evelSyslogObject() )\r
475                                  ).build();\r
476 \r
477             EVEL_EXIT();\r
478             \r
479             return obj.toString();\r
480 \r
481           }\r
482           \r
483           \r
484         \r
485 \r
486 }\r