-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathInputImpl.java
More file actions
259 lines (210 loc) · 9.13 KB
/
Copy pathInputImpl.java
File metadata and controls
259 lines (210 loc) · 9.13 KB
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
/*
* Copyright (C) 2020 Graylog, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the Server Side Public License, version 1,
* as published by MongoDB, Inc.
*
* This program 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
* Server Side Public License for more details.
*
* You should have received a copy of the Server Side Public License
* along with this program. If not, see
* <http://www.mongodb.com/licensing/server-side-public-license>.
*/
package org.graylog2.inputs;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.google.auto.value.AutoValue;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import org.graylog2.database.DbEntity;
import org.graylog2.database.MongoEntity;
import org.graylog2.plugin.IOState;
import org.graylog2.shared.security.RestPermissions;
import org.joda.time.DateTime;
import org.mongojack.Id;
import org.mongojack.ObjectId;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.Nullable;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import static org.graylog2.inputs.InputImpl.FIELD_CREATED_AT;
import static org.graylog2.inputs.InputImpl.FIELD_CREATOR_USER_ID;
import static org.graylog2.inputs.InputImpl.FIELD_GLOBAL;
import static org.graylog2.inputs.InputImpl.FIELD_NAME;
import static org.graylog2.inputs.InputImpl.FIELD_NODE_ID;
import static org.graylog2.inputs.InputImpl.FIELD_TITLE;
import static org.graylog2.inputs.InputImpl.FIELD_TYPE;
import static org.graylog2.shared.security.EntityPermissionsUtils.ID_FIELD;
@AutoValue
@JsonDeserialize(builder = InputImpl.Builder.class)
@DbEntity(collection = InputServiceImpl.COLLECTION_NAME, readPermission = RestPermissions.INPUTS_READ,
readableFields = {ID_FIELD, FIELD_TITLE, FIELD_TYPE, FIELD_CREATOR_USER_ID, FIELD_CREATED_AT,
FIELD_GLOBAL, FIELD_NODE_ID, FIELD_NAME})
public abstract class InputImpl implements Input, MongoEntity {
private static final Logger LOG = LoggerFactory.getLogger(InputImpl.class);
public static final String FIELD_ID = "_id";
public static final String FIELD_TYPE = "type";
public static final String FIELD_NODE_ID = "node_id";
public static final String FIELD_NAME = "name";
public static final String FIELD_TITLE = "title";
public static final String FIELD_CONFIGURATION = "configuration";
public static final String FIELD_CREATOR_USER_ID = "creator_user_id";
public static final String FIELD_CREATED_AT = "created_at";
public static final String FIELD_GLOBAL = "global";
public static final String EMBEDDED_EXTRACTORS = "extractors";
public static final String EMBEDDED_STATIC_FIELDS = "static_fields";
public static final String FIELD_STATIC_FIELD_KEY = "key";
public static final String FIELD_STATIC_FIELD_VALUE = "value";
public static final String FIELD_DESIRED_STATE = "desired_state";
public static final String FIELD_CONTENT_PACK = "content_pack";
@Id
@ObjectId
@JsonProperty(FIELD_ID)
@Nullable
public abstract String getId();
@Override
public String id() {
return getId();
}
@NotBlank
@JsonProperty(FIELD_TITLE)
public abstract String getTitle();
@NotNull
@JsonProperty(FIELD_CREATED_AT)
public abstract DateTime getCreatedAt();
@NotNull
@JsonProperty(FIELD_CONFIGURATION)
public abstract Map<String, Object> getConfiguration();
@JsonProperty(EMBEDDED_STATIC_FIELDS)
public abstract List<Map<String, String>> getEmbeddedStaticFields();
public Map<String, String> getStaticFields() {
final List<Map<String, String>> embeddedStaticFields = getEmbeddedStaticFields();
if (embeddedStaticFields == null || embeddedStaticFields.isEmpty()) {
return Map.of();
}
final Map<String, String> result = new LinkedHashMap<>(embeddedStaticFields.size());
for (Map<String, String> map : embeddedStaticFields) {
final String key = map.get(FIELD_STATIC_FIELD_KEY);
final String value = map.get(FIELD_STATIC_FIELD_VALUE);
if (key != null && value != null) {
if (result.put(key, value) != null) {
LOG.warn("Duplicate static field key '{}' found in input [{}], keeping last value", key, getId());
}
}
}
return result;
}
/**
* The embedded extractor documents. They are modified through targeted update operations (see
* {@code InputServiceImpl#addExtractor} etc.) and only modeled here so that they survive full document
* replacements when saving an input.
*/
@Nullable
@JsonProperty(EMBEDDED_EXTRACTORS)
public abstract List<Map<String, Object>> getEmbeddedExtractors();
@NotNull
@JsonProperty(FIELD_TYPE)
public abstract String getType();
@NotNull
@JsonProperty(FIELD_CREATOR_USER_ID)
public abstract String getCreatorUserId();
@JsonProperty(FIELD_GLOBAL)
public abstract boolean isGlobal();
@Nullable
@JsonProperty(FIELD_CONTENT_PACK)
public abstract String getContentPack();
@Nullable
@JsonProperty(FIELD_NODE_ID)
public abstract String getNodeId();
@Nullable
@JsonProperty(FIELD_DESIRED_STATE)
public abstract IOState.Type getPersistedDesiredState();
@Override
public IOState.Type getDesiredState() {
final IOState.Type persistedDesiredState = getPersistedDesiredState();
return persistedDesiredState != null ? persistedDesiredState : IOState.Type.RUNNING;
}
public static Builder builder() {
return Builder.create();
}
public abstract Builder toBuilder();
@AutoValue.Builder
@JsonIgnoreProperties(ignoreUnknown = true)
public abstract static class Builder {
@JsonCreator
public static Builder create() {
return new AutoValue_InputImpl.Builder().setEmbeddedStaticFields(List.of())
.setGlobal(false);
}
@JsonProperty(FIELD_ID)
public abstract Builder setId(String id);
@JsonProperty(FIELD_TITLE)
public abstract Builder setTitle(String title);
@JsonProperty(FIELD_CREATED_AT)
public abstract Builder setCreatedAt(DateTime createdAt);
@JsonProperty(FIELD_CONFIGURATION)
public abstract Builder setConfiguration(Map<String, Object> configuration);
@JsonProperty(EMBEDDED_STATIC_FIELDS)
public abstract Builder setEmbeddedStaticFields(List<Map<String, String>> staticFields);
@JsonProperty(EMBEDDED_EXTRACTORS)
public abstract Builder setEmbeddedExtractors(List<Map<String, Object>> extractors);
@JsonProperty(FIELD_TYPE)
public abstract Builder setType(String type);
@JsonProperty(FIELD_CREATOR_USER_ID)
public abstract Builder setCreatorUserId(String creatorUserId);
@JsonProperty(FIELD_GLOBAL)
public abstract Builder setGlobal(boolean isGlobal);
@JsonProperty(FIELD_CONTENT_PACK)
public abstract Builder setContentPack(String contentPack);
@JsonProperty(FIELD_NODE_ID)
public abstract Builder setNodeId(String nodeId);
@JsonProperty(FIELD_DESIRED_STATE)
public abstract Builder setPersistedDesiredState(IOState.Type desiredState);
public abstract InputImpl build();
}
@Override
public Input withDesiredState(IOState.Type desiredState) {
return toBuilder().setPersistedDesiredState(desiredState).build();
}
@JsonIgnore
@Override
public Map<String, Object> getFields() {
final Map<String, Object> doc = new java.util.LinkedHashMap<>();
if (getId() != null) {
doc.put(FIELD_ID, getId());
}
doc.put(FIELD_TYPE, getType());
doc.put(FIELD_TITLE, getTitle());
doc.put(FIELD_CREATOR_USER_ID, getCreatorUserId());
doc.put(FIELD_CREATED_AT, getCreatedAt());
doc.put(FIELD_GLOBAL, isGlobal());
doc.put(FIELD_CONFIGURATION, getConfiguration());
final List<Map<String, String>> staticFields = getEmbeddedStaticFields();
if (staticFields != null && !getStaticFields().isEmpty()) {
doc.put(EMBEDDED_STATIC_FIELDS, getEmbeddedStaticFields());
}
final List<Map<String, Object>> extractors = getEmbeddedExtractors();
if (extractors != null && !extractors.isEmpty()) {
doc.put(EMBEDDED_EXTRACTORS, extractors);
}
if (getContentPack() != null) {
doc.put(FIELD_CONTENT_PACK, getContentPack());
}
if (getNodeId() != null && !getNodeId().isBlank()) {
doc.put(FIELD_NODE_ID, getNodeId());
}
if (getDesiredState() != null) {
doc.put(FIELD_DESIRED_STATE, getDesiredState().name());
}
return doc;
}
}