2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.common.datastructure;
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;
30 import java.util.TimeZone;
32 import org.codehaus.jettison.json.JSONException;
33 import org.codehaus.jettison.json.JSONObject;
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.
43 public class ESTimeBasedEvent {
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>();
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);
57 public static ESTimeBasedEvent createEventFromJson(String jsonString) throws JSONException {
59 ESTimeBasedEvent event = new ESTimeBasedEvent();
61 gsonObj = new JSONObject(jsonString);
62 Iterator keys = gsonObj.keys();
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);
74 public String calculateYearIndexSuffix() {
75 return timestamp.substring(0, 4);
78 public String calculateMonthIndexSuffix() {
79 return timestamp.substring(0, 7);
82 public String calculateDayIndexSuffix() {
83 return timestamp.substring(0, 10);
86 public String calculateHourIndexSuffix() {
87 return new StringBuilder().append(timestamp.substring(0, 10)).append("-").append(timestamp.substring(11, 13))
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();
96 protected String getFormattedString(String template, Object... params) {
98 StringBuilder sb = new StringBuilder();
99 Formatter formatter = new Formatter(sb, Locale.US);
101 formatter.format(template, params);
102 res = formatter.toString();
109 public String getTimestamp() {
113 public void setTimestamp(String timestamp) {
114 this.timestamp = timestamp;
117 public Map<String, Object> getFields() {
121 public void setFields(Map<String, Object> fields) {
122 this.fields = fields;