use of org.graylog2.streams.StreamImpl in project graylog2-server by Graylog2.
the class StreamServiceImpl method load.
public Stream load(ObjectId id) throws NotFoundException {
final DBObject o = get(StreamImpl.class, id);
if (o == null) {
throw new NotFoundException("Stream <" + id + "> not found!");
}
final List<StreamRule> streamRules = streamRuleService.loadForStreamId(id.toHexString());
final Set<Output> outputs = loadOutputsForRawStream(o);
@SuppressWarnings("unchecked") final Map<String, Object> fields = o.toMap();
return new StreamImpl((ObjectId) o.get("_id"), fields, streamRules, outputs, getIndexSet(o));
}
use of org.graylog2.streams.StreamImpl in project graylog2-server by Graylog2.
the class StreamCatalogTest method encode.
@Test
public void encode() {
final ImmutableMap<String, Object> streamFields = ImmutableMap.of(StreamImpl.FIELD_TITLE, "Stream Title", StreamImpl.FIELD_DESCRIPTION, "Stream Description", StreamImpl.FIELD_DISABLED, false);
final ImmutableMap<String, Object> streamRuleFields = ImmutableMap.<String, Object>builder().put("_id", "1234567890").put(StreamRuleImpl.FIELD_TYPE, StreamRuleType.EXACT.getValue()).put(StreamRuleImpl.FIELD_DESCRIPTION, "description").put(StreamRuleImpl.FIELD_FIELD, "field").put(StreamRuleImpl.FIELD_VALUE, "value").put(StreamRuleImpl.FIELD_INVERTED, false).put(StreamRuleImpl.FIELD_STREAM_ID, "1234567890").build();
final ImmutableList<StreamRule> streamRules = ImmutableList.of(new StreamRuleMock(streamRuleFields));
final ImmutableSet<Output> outputs = ImmutableSet.of();
final ObjectId streamId = new ObjectId();
final StreamImpl stream = new StreamImpl(streamId, streamFields, streamRules, outputs, null);
final EntityDescriptor descriptor = EntityDescriptor.create(stream.getId(), ModelTypes.STREAM_V1);
final EntityDescriptorIds entityDescriptorIds = EntityDescriptorIds.of(descriptor);
final Entity entity = facade.exportNativeEntity(stream, entityDescriptorIds);
assertThat(entity).isInstanceOf(EntityV1.class);
assertThat(entity.id()).isEqualTo(ModelId.of(entityDescriptorIds.get(descriptor).orElse(null)));
assertThat(entity.type()).isEqualTo(ModelTypes.STREAM_V1);
final EntityV1 entityV1 = (EntityV1) entity;
final StreamEntity streamEntity = objectMapper.convertValue(entityV1.data(), StreamEntity.class);
assertThat(streamEntity.title()).isEqualTo(ValueReference.of("Stream Title"));
assertThat(streamEntity.description()).isEqualTo(ValueReference.of("Stream Description"));
assertThat(streamEntity.disabled()).isEqualTo(ValueReference.of(false));
assertThat(streamEntity.streamRules()).hasSize(1);
}
use of org.graylog2.streams.StreamImpl in project graylog2-server by Graylog2.
the class StreamCatalogTest method createExcerpt.
@Test
public void createExcerpt() {
final ImmutableMap<String, Object> fields = ImmutableMap.of("title", "Stream Title");
final StreamImpl stream = new StreamImpl(fields);
final EntityExcerpt excerpt = facade.createExcerpt(stream);
assertThat(excerpt.id()).isEqualTo(ModelId.of(stream.getId()));
assertThat(excerpt.type()).isEqualTo(ModelTypes.STREAM_V1);
assertThat(excerpt.title()).isEqualTo(stream.getTitle());
}
use of org.graylog2.streams.StreamImpl in project graylog2-server by Graylog2.
the class ViewFacadeTest method itShouldCreateADTOFromAnEntity.
@Test
@MongoDBFixtures("ViewFacadeTest.json")
public void itShouldCreateADTOFromAnEntity() throws Exception {
final StreamImpl stream = new StreamImpl(Collections.emptyMap());
final Entity viewEntity = createViewEntity();
final Map<EntityDescriptor, Object> nativeEntities = new HashMap<>(1);
nativeEntities.put(EntityDescriptor.create(newStreamId, ModelTypes.STREAM_V1), stream);
final UserImpl fakeUser = new UserImpl(mock(PasswordAlgorithmFactory.class), new Permissions(ImmutableSet.of()), ImmutableMap.of("username", "testuser"));
when(userService.load("testuser")).thenReturn(fakeUser);
final NativeEntity<ViewDTO> nativeEntity = facade.createNativeEntity(viewEntity, Collections.emptyMap(), nativeEntities, "testuser");
assertThat(nativeEntity.descriptor().title()).isEqualTo("title");
assertThat(nativeEntity.descriptor().type()).isEqualTo(ModelTypes.SEARCH_V1);
Optional<ViewDTO> resultedView = viewService.get(nativeEntity.descriptor().id().id());
assertThat(resultedView).isPresent();
Optional<Search> search = searchDbService.get(resultedView.get().searchId());
assertThat(search).isPresent();
final Query query = search.get().queries().iterator().next();
assertThat(query.filter()).isNotNull();
assertThat(query.filter().filters()).isNotEmpty();
final StreamFilter streamFilter = (StreamFilter) query.filter().filters().iterator().next();
assertThat(streamFilter.streamId()).doesNotMatch(newStreamId);
}
use of org.graylog2.streams.StreamImpl in project graylog2-server by Graylog2.
the class BundleImporter method createStream.
private org.graylog2.plugin.streams.Stream createStream(final String bundleId, final Stream streamDescription, final String userName) throws ValidationException {
// We cannot create streams without having a default index set.
final IndexSet indexSet = indexSetRegistry.getDefault();
final Map<String, Object> streamData = ImmutableMap.<String, Object>builder().put(StreamImpl.FIELD_TITLE, streamDescription.getTitle()).put(StreamImpl.FIELD_DESCRIPTION, streamDescription.getDescription()).put(StreamImpl.FIELD_DISABLED, streamDescription.isDisabled()).put(StreamImpl.FIELD_MATCHING_TYPE, streamDescription.getMatchingType().name()).put(StreamImpl.FIELD_CREATOR_USER_ID, userName).put(StreamImpl.FIELD_CREATED_AT, Tools.nowUTC()).put(StreamImpl.FIELD_CONTENT_PACK, bundleId).put(StreamImpl.FIELD_DEFAULT_STREAM, streamDescription.isDefaultStream()).put(StreamImpl.FIELD_INDEX_SET_ID, indexSet.getConfig().id()).build();
final String defaultStreamId = org.graylog2.plugin.streams.Stream.DEFAULT_STREAM_ID;
final ObjectId id = streamDescription.isDefaultStream() ? new ObjectId(defaultStreamId) : new ObjectId(streamDescription.getId());
final org.graylog2.plugin.streams.Stream stream = new StreamImpl(id, streamData, Collections.emptyList(), Collections.emptySet(), indexSet);
final String streamId = streamService.save(stream);
if (streamDescription.getStreamRules() != null) {
for (StreamRule streamRule : streamDescription.getStreamRules()) {
final Map<String, Object> streamRuleData = ImmutableMap.<String, Object>builder().put(StreamRuleImpl.FIELD_TYPE, streamRule.getType().toInteger()).put(StreamRuleImpl.FIELD_VALUE, streamRule.getValue()).put(StreamRuleImpl.FIELD_FIELD, streamRule.getField()).put(StreamRuleImpl.FIELD_INVERTED, streamRule.isInverted()).put(StreamRuleImpl.FIELD_STREAM_ID, new ObjectId(streamId)).put(StreamRuleImpl.FIELD_CONTENT_PACK, bundleId).put(StreamRuleImpl.FIELD_DESCRIPTION, streamRule.getDescription()).build();
streamRuleService.save(new StreamRuleImpl(streamRuleData));
}
}
for (final String outputId : streamDescription.getOutputs()) {
if (isNullOrEmpty(outputId)) {
LOG.warn("Couldn't find referenced output <{}> for stream <{}>", outputId, streamDescription.getTitle());
} else {
streamService.addOutput(stream, outputsByReferenceId.get(outputId));
}
}
return stream;
}
Aggregations