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 org.codehaus.jettison.json.JSONException;
24 import org.codehaus.jettison.json.JSONObject;
26 import java.text.SimpleDateFormat;
27 import java.util.Date;
28 import java.util.Formatter;
29 import java.util.HashMap;
30 import java.util.Iterator;
31 import java.util.Locale;
33 import java.util.TimeZone;
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.
42 public class ESTimeBasedEvent {
44 private static final int TIMESTAMP_YEAR_SUBSTRING = 4;
45 private static final int TIMESTAMP_MONTH_SUBSTRING = 7;
46 private static final int TIMESTAMP_DAY_SUBSTRING = 10;
47 private static final int TIMESTAMP_HOURS_START = 11;
48 private static final int TIMESTAMP_HOURS_END = 13;
49 private static final int TIMESTAMP_MINUTES_START = 14;
50 private static final int TIMESTAMP_MINUTES_END = 16;
52 protected SimpleDateFormat simpleDateFormat;
53 protected static String dateFormatPattern = "yyyy-MM-dd HH:mm:ss.SSS z";
54 protected String timestamp;
55 protected Map<String, Object> fields = new HashMap<>();
57 public ESTimeBasedEvent() {
58 simpleDateFormat = new SimpleDateFormat(dateFormatPattern);
59 simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
60 this.timestamp = simpleDateFormat.format(new Date());
61 fields.put(AuditingFieldsKey.AUDIT_TIMESTAMP.getDisplayName(), this.timestamp);
65 public static ESTimeBasedEvent createEventFromJson(String jsonString) throws JSONException {
67 ESTimeBasedEvent event = new ESTimeBasedEvent();
69 gsonObj = new JSONObject(jsonString);
70 Iterator keys = gsonObj.keys();
72 while (keys.hasNext()) {
73 String key = (String) keys.next();
74 event.fields.put(key, gsonObj.get(key));
75 if (key.equals(AuditingFieldsKey.AUDIT_TIMESTAMP.getDisplayName())) {
76 event.timestamp = (String) gsonObj.get(key);
82 public String calculateYearIndexSuffix() {
83 return timestamp.substring(0, TIMESTAMP_YEAR_SUBSTRING);
86 public String calculateMonthIndexSuffix() {
87 return timestamp.substring(0, TIMESTAMP_MONTH_SUBSTRING);
90 public String calculateDayIndexSuffix() {
91 return timestamp.substring(0, TIMESTAMP_DAY_SUBSTRING);
94 public String calculateHourIndexSuffix() {
95 return calculateBaseIndexSuffix().toString();
98 public String calculateMinuteIndexSuffix() {
99 return calculateBaseIndexSuffix().append("-").append(timestamp, TIMESTAMP_MINUTES_START, TIMESTAMP_MINUTES_END).toString();
102 private StringBuilder calculateBaseIndexSuffix() {
103 return new StringBuilder().append(timestamp, 0, TIMESTAMP_DAY_SUBSTRING).append("-").append(timestamp, TIMESTAMP_HOURS_START, TIMESTAMP_HOURS_END);
106 protected String getFormattedString(String template, Object... params) {
108 StringBuilder sb = new StringBuilder();
109 try (Formatter formatter = new Formatter(sb, Locale.US)) {
110 formatter.format(template, params);
111 res = formatter.toString();
116 public String getTimestamp() {
120 public void setTimestamp(String timestamp) {
121 this.timestamp = timestamp;
124 public Map<String, Object> getFields() {
128 public void setFields(Map<String, Object> fields) {
129 this.fields = fields;