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;
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.
37 public class ESTimeBasedEvent {
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<>();
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);
52 public static ESTimeBasedEvent createEventFromJson(String jsonString) throws JSONException {
54 ESTimeBasedEvent event = new ESTimeBasedEvent();
56 gsonObj = new JSONObject(jsonString);
57 Iterator keys = gsonObj.keys();
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);
69 public String calculateYearIndexSuffix() {
70 return timestamp.substring(0, 4);
73 public String calculateMonthIndexSuffix() {
74 return timestamp.substring(0, 7);
77 public String calculateDayIndexSuffix() {
78 return timestamp.substring(0, 10);
81 public String calculateHourIndexSuffix() {
82 return new StringBuilder().append(timestamp.substring(0, 10)).append("-").append(timestamp.substring(11, 13))
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();
91 protected String getFormattedString(String template, Object... params) {
93 StringBuilder sb = new StringBuilder();
94 Formatter formatter = new Formatter(sb, Locale.US);
96 formatter.format(template, params);
97 res = formatter.toString();
104 public String getTimestamp() {
108 public void setTimestamp(String timestamp) {
109 this.timestamp = timestamp;
112 public Map<String, Object> getFields() {
116 public void setFields(Map<String, Object> fields) {
117 this.fields = fields;