Initial OpenECOMP SDC commit
[sdc.git] / common-app-api / src / main / java / org / openecomp / sdc / common / datastructure / ESTimeBasedEvent.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 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
21 package org.openecomp.sdc.common.datastructure;
22
23 import java.text.SimpleDateFormat;
24 import java.util.Date;
25 import java.util.Formatter;
26 import java.util.HashMap;
27 import java.util.Iterator;
28 import java.util.Locale;
29 import java.util.Map;
30 import java.util.TimeZone;
31
32 import org.codehaus.jettison.json.JSONException;
33 import org.codehaus.jettison.json.JSONObject;
34
35 /**
36  * Extending this class enforces the objects of implementing classes to have a
37  * timestamp, so that like in logstash, we can derive the index name for those
38  * object from the timestamp.
39  * 
40  * @author paharoni
41  *
42  */
43 public class ESTimeBasedEvent {
44
45         protected static String dateFormatPattern = "yyyy-MM-dd HH:mm:ss.SSS z";
46         protected String timestamp;
47         protected Map<String, Object> fields = new HashMap<String, Object>();
48
49         public ESTimeBasedEvent() {
50                 SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dateFormatPattern);
51                 simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
52                 this.timestamp = simpleDateFormat.format(new Date());
53                 fields.put(AuditingFieldsKeysEnum.AUDIT_TIMESTAMP.getDisplayName(), this.timestamp);
54
55         }
56
57         public static ESTimeBasedEvent createEventFromJson(String jsonString) throws JSONException {
58
59                 ESTimeBasedEvent event = new ESTimeBasedEvent();
60                 JSONObject gsonObj;
61                 gsonObj = new JSONObject(jsonString);
62                 Iterator keys = gsonObj.keys();
63
64                 while (keys.hasNext()) {
65                         String key = (String) keys.next();
66                         event.fields.put(key, gsonObj.get(key));
67                         if (key.equals(AuditingFieldsKeysEnum.AUDIT_TIMESTAMP.getDisplayName())) {
68                                 event.timestamp = (String) gsonObj.get(key);
69                         }
70                 }
71                 return event;
72         }
73
74         public String calculateYearIndexSuffix() {
75                 return timestamp.substring(0, 4);
76         }
77
78         public String calculateMonthIndexSuffix() {
79                 return timestamp.substring(0, 7);
80         }
81
82         public String calculateDayIndexSuffix() {
83                 return timestamp.substring(0, 10);
84         }
85
86         public String calculateHourIndexSuffix() {
87                 return new StringBuilder().append(timestamp.substring(0, 10)).append("-").append(timestamp.substring(11, 13))
88                                 .toString();
89         }
90
91         public String calculateMinuteIndexSuffix() {
92                 return new StringBuilder().append(timestamp.substring(0, 10)).append("-").append(timestamp.substring(11, 13))
93                                 .append("-").append(timestamp.substring(14, 16)).toString();
94         }
95
96         protected String getFormattedString(String template, Object... params) {
97                 String res = null;
98                 StringBuilder sb = new StringBuilder();
99                 Formatter formatter = new Formatter(sb, Locale.US);
100                 try {
101                         formatter.format(template, params);
102                         res = formatter.toString();
103                 } finally {
104                         formatter.close();
105                 }
106                 return res;
107         }
108
109         public String getTimestamp() {
110                 return timestamp;
111         }
112
113         public void setTimestamp(String timestamp) {
114                 this.timestamp = timestamp;
115         }
116
117         public Map<String, Object> getFields() {
118                 return fields;
119         }
120
121         public void setFields(Map<String, Object> fields) {
122                 this.fields = fields;
123         }
124
125 }