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