-
Notifications
You must be signed in to change notification settings - Fork 1k
/
StreamImpl.java
277 lines (236 loc) · 9.19 KB
/
StreamImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/**
* This file is part of Graylog.
*
* Graylog is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Graylog is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Graylog. If not, see <http://www.gnu.org/licenses/>.
*/
package org.graylog2.streams;
import com.fasterxml.jackson.annotation.JsonValue;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.bson.types.ObjectId;
import org.graylog2.database.CollectionName;
import org.graylog2.database.PersistedImpl;
import org.graylog2.database.validators.DateValidator;
import org.graylog2.database.validators.FilledStringValidator;
import org.graylog2.database.validators.MapValidator;
import org.graylog2.database.validators.OptionalStringValidator;
import org.graylog2.indexer.IndexSet;
import org.graylog2.plugin.Tools;
import org.graylog2.plugin.database.validators.Validator;
import org.graylog2.plugin.streams.Output;
import org.graylog2.plugin.streams.Stream;
import org.graylog2.plugin.streams.StreamRule;
import org.joda.time.DateTime;
import javax.annotation.Nullable;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* Representing a single stream from the streams collection. Also provides method
* to get all streams of this collection.
*/
@CollectionName("streams")
public class StreamImpl extends PersistedImpl implements Stream {
public static final String FIELD_TITLE = "title";
public static final String FIELD_DESCRIPTION = "description";
public static final String FIELD_RULES = "rules";
public static final String FIELD_OUTPUTS = "outputs";
public static final String FIELD_CONTENT_PACK = "content_pack";
public static final String FIELD_ALERT_RECEIVERS = "alert_receivers";
public static final String FIELD_DISABLED = "disabled";
public static final String FIELD_CREATED_AT = "created_at";
public static final String FIELD_CREATOR_USER_ID = "creator_user_id";
public static final String FIELD_MATCHING_TYPE = "matching_type";
public static final String FIELD_DEFAULT_STREAM = "is_default_stream";
public static final String FIELD_REMOVE_MATCHES_FROM_DEFAULT_STREAM = "remove_matches_from_default_stream";
public static final String FIELD_INDEX_SET_ID = "index_set_id";
public static final String EMBEDDED_ALERT_CONDITIONS = "alert_conditions";
private final List<StreamRule> streamRules;
private final Set<Output> outputs;
private final IndexSet indexSet;
public StreamImpl(Map<String, Object> fields) {
super(fields);
this.streamRules = null;
this.outputs = null;
this.indexSet = null;
}
public StreamImpl(Map<String, Object> fields, IndexSet indexSet) {
super(fields);
this.streamRules = null;
this.outputs = null;
this.indexSet = indexSet;
}
protected StreamImpl(ObjectId id, Map<String, Object> fields) {
super(id, fields);
this.streamRules = null;
this.outputs = null;
this.indexSet = null;
}
public StreamImpl(ObjectId id, Map<String, Object> fields, List<StreamRule> streamRules, Set<Output> outputs, @Nullable IndexSet indexSet) {
super(id, fields);
this.streamRules = streamRules;
this.outputs = outputs;
this.indexSet = indexSet;
}
@Override
public String toString() {
return this.id.toString() + ": \"" + this.getTitle() + "\"";
}
@Override
public List<StreamRule> getStreamRules() {
return this.streamRules;
}
@Override
public Set<Output> getOutputs() {
return this.outputs;
}
@Override
public String getTitle() {
return (String) fields.get(FIELD_TITLE);
}
@Override
public String getDescription() {
return (String) fields.get(FIELD_DESCRIPTION);
}
@Override
public void setTitle(String title) {
fields.put(FIELD_TITLE, title);
}
@Override
public void setDescription(String description) {
fields.put(FIELD_DESCRIPTION, description);
}
@Override
public Boolean getDisabled() {
return (Boolean) fields.get(FIELD_DISABLED);
}
@Override
public void setDisabled(Boolean disabled) {
fields.put(FIELD_DISABLED, disabled);
}
@Override
public String getContentPack() {
return (String) fields.get(FIELD_CONTENT_PACK);
}
@Override
public void setContentPack(String contentPack) {
fields.put(FIELD_CONTENT_PACK, contentPack);
}
@Override
public Boolean isPaused() {
Boolean disabled = getDisabled();
return (disabled != null && disabled);
}
@Override
public Map<String, Object> asMap(List<StreamRule> streamRules) {
Map<String, Object> result = asMap();
List<Map<String, Object>> streamRulesMap = Lists.newArrayList();
for (StreamRule streamRule : streamRules) {
streamRulesMap.add(streamRule.asMap());
}
result.put(FIELD_RULES, streamRulesMap);
return result;
}
@JsonValue
@Override
public Map<String, Object> asMap() {
// We work on the result a bit to allow correct JSON serializing.
Map<String, Object> result = Maps.newHashMap(fields);
result.remove("_id");
result.put("id", ((ObjectId) fields.get("_id")).toHexString());
result.remove(FIELD_CREATED_AT);
result.put(FIELD_CREATED_AT, (Tools.getISO8601String((DateTime) fields.get(FIELD_CREATED_AT))));
result.put(FIELD_RULES, streamRules);
result.put(FIELD_OUTPUTS, outputs);
result.put(FIELD_MATCHING_TYPE, getMatchingType());
result.put(FIELD_DEFAULT_STREAM, isDefaultStream());
result.put(FIELD_REMOVE_MATCHES_FROM_DEFAULT_STREAM, getRemoveMatchesFromDefaultStream());
result.put(FIELD_INDEX_SET_ID, getIndexSetId());
return result;
}
@Override
public Map<String, Validator> getValidations() {
return ImmutableMap.<String, Validator>builder()
.put(FIELD_TITLE, new FilledStringValidator())
.put(FIELD_CREATOR_USER_ID, new FilledStringValidator())
.put(FIELD_CREATED_AT, new DateValidator())
.put(FIELD_CONTENT_PACK, new OptionalStringValidator())
.put(FIELD_INDEX_SET_ID, new FilledStringValidator())
.build();
}
@Override
public Map<String, Validator> getEmbeddedValidations(String key) {
if (key.equals(EMBEDDED_ALERT_CONDITIONS)) {
return ImmutableMap.of(
"id", new FilledStringValidator(),
"parameters", new MapValidator());
}
return Collections.emptyMap();
}
@Override
public Map<String, List<String>> getAlertReceivers() {
@SuppressWarnings("unchecked")
final Map<String, List<String>> alertReceivers =
(Map<String, List<String>>) fields.getOrDefault(FIELD_ALERT_RECEIVERS, Collections.emptyMap());
return alertReceivers;
}
@Override
public MatchingType getMatchingType() {
final String matchingTypeString = (String) fields.get(FIELD_MATCHING_TYPE);
if (matchingTypeString == null) {
return MatchingType.AND;
} else {
return MatchingType.valueOf(matchingTypeString);
}
}
@Override
public void setMatchingType(MatchingType matchingType) {
fields.put(FIELD_MATCHING_TYPE, matchingType.toString());
}
@Override
public boolean isDefaultStream() {
return (boolean) fields.getOrDefault(FIELD_DEFAULT_STREAM, false);
}
@Override
public void setDefaultStream(boolean defaultStream) {
fields.put(FIELD_DEFAULT_STREAM, defaultStream);
}
@Override
public boolean getRemoveMatchesFromDefaultStream() {
return (boolean) fields.getOrDefault(FIELD_REMOVE_MATCHES_FROM_DEFAULT_STREAM, false);
}
@Override
public void setRemoveMatchesFromDefaultStream(boolean removeMatchesFromDefaultStream) {
fields.put(FIELD_REMOVE_MATCHES_FROM_DEFAULT_STREAM, removeMatchesFromDefaultStream);
}
@Override
public IndexSet getIndexSet() {
// The indexSet might be null because of backwards compatibility but it shouldn't be for regular streams.
// Throw an exception if indexSet is not set to avoid losing messages!
if (indexSet == null) {
throw new IllegalStateException("index set must not be null! (stream id=" + getId() + " title=\"" + getTitle() + "\")");
}
return indexSet;
}
@Override
public String getIndexSetId() {
return (String) fields.get(FIELD_INDEX_SET_ID);
}
@Override
public void setIndexSetId(String indexSetId) {
fields.put(FIELD_INDEX_SET_ID, indexSetId);
}
}