CSIT Fix for SDC-2585
[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.Date;
28 import java.util.Formatter;
29 import java.util.HashMap;
30 import java.util.Iterator;
31 import java.util.Locale;
32 import java.util.Map;
33 import java.util.TimeZone;
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 public class ESTimeBasedEvent {
43
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;
51
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<>();
56
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);
62
63     }
64
65     public static ESTimeBasedEvent createEventFromJson(String jsonString) throws JSONException {
66
67         ESTimeBasedEvent event = new ESTimeBasedEvent();
68         JSONObject gsonObj;
69         gsonObj = new JSONObject(jsonString);
70         Iterator keys = gsonObj.keys();
71
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);
77             }
78         }
79         return event;
80     }
81
82     public String calculateYearIndexSuffix() {
83         return timestamp.substring(0, TIMESTAMP_YEAR_SUBSTRING);
84     }
85
86     public String calculateMonthIndexSuffix() {
87         return timestamp.substring(0, TIMESTAMP_MONTH_SUBSTRING);
88     }
89
90     public String calculateDayIndexSuffix() {
91         return timestamp.substring(0, TIMESTAMP_DAY_SUBSTRING);
92     }
93
94     public String calculateHourIndexSuffix() {
95         return calculateBaseIndexSuffix().toString();
96     }
97
98     public String calculateMinuteIndexSuffix() {
99         return calculateBaseIndexSuffix().append("-").append(timestamp, TIMESTAMP_MINUTES_START, TIMESTAMP_MINUTES_END).toString();
100     }
101
102     private StringBuilder calculateBaseIndexSuffix() {
103         return new StringBuilder().append(timestamp, 0, TIMESTAMP_DAY_SUBSTRING).append("-").append(timestamp, TIMESTAMP_HOURS_START, TIMESTAMP_HOURS_END);
104     }
105
106     protected String getFormattedString(String template, Object... params) {
107         String res;
108         StringBuilder sb = new StringBuilder();
109         try (Formatter formatter = new Formatter(sb, Locale.US)) {
110             formatter.format(template, params);
111             res = formatter.toString();
112         }
113         return res;
114     }
115
116     public String getTimestamp() {
117         return timestamp;
118     }
119
120     public void setTimestamp(String timestamp) {
121         this.timestamp = timestamp;
122     }
123
124     public Map<String, Object> getFields() {
125         return fields;
126     }
127
128     public void setFields(Map<String, Object> fields) {
129         this.fields = fields;
130     }
131
132 }