Search in sources :

Example 1 with StreamImpl

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));
}
Also used : StreamRule(org.graylog2.plugin.streams.StreamRule) Output(org.graylog2.plugin.streams.Output) NotFoundException(org.graylog2.database.NotFoundException) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject)

Example 2 with StreamImpl

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);
}
Also used : Entity(org.graylog2.contentpacks.model.entities.Entity) StreamEntity(org.graylog2.contentpacks.model.entities.StreamEntity) ObjectId(org.bson.types.ObjectId) StreamRule(org.graylog2.plugin.streams.StreamRule) EntityDescriptorIds(org.graylog2.contentpacks.EntityDescriptorIds) StreamEntity(org.graylog2.contentpacks.model.entities.StreamEntity) StreamRuleMock(org.graylog2.streams.matchers.StreamRuleMock) EntityV1(org.graylog2.contentpacks.model.entities.EntityV1) EntityDescriptor(org.graylog2.contentpacks.model.entities.EntityDescriptor) StreamImpl(org.graylog2.streams.StreamImpl) Output(org.graylog2.plugin.streams.Output) Test(org.junit.Test)

Example 3 with StreamImpl

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());
}
Also used : EntityExcerpt(org.graylog2.contentpacks.model.entities.EntityExcerpt) StreamImpl(org.graylog2.streams.StreamImpl) Test(org.junit.Test)

Example 4 with StreamImpl

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);
}
Also used : NativeEntity(org.graylog2.contentpacks.model.entities.NativeEntity) PivotEntity(org.graylog2.contentpacks.model.entities.PivotEntity) QueryEntity(org.graylog2.contentpacks.model.entities.QueryEntity) EventListEntity(org.graylog2.contentpacks.model.entities.EventListEntity) ViewEntity(org.graylog2.contentpacks.model.entities.ViewEntity) Entity(org.graylog2.contentpacks.model.entities.Entity) SearchEntity(org.graylog2.contentpacks.model.entities.SearchEntity) ViewStateEntity(org.graylog2.contentpacks.model.entities.ViewStateEntity) StreamEntity(org.graylog2.contentpacks.model.entities.StreamEntity) MessageListEntity(org.graylog2.contentpacks.model.entities.MessageListEntity) Query(org.graylog.plugins.views.search.Query) HashMap(java.util.HashMap) StreamFilter(org.graylog.plugins.views.search.filter.StreamFilter) EntityDescriptor(org.graylog2.contentpacks.model.entities.EntityDescriptor) ViewDTO(org.graylog.plugins.views.search.views.ViewDTO) PasswordAlgorithmFactory(org.graylog2.security.PasswordAlgorithmFactory) StreamImpl(org.graylog2.streams.StreamImpl) Search(org.graylog.plugins.views.search.Search) UserImpl(org.graylog2.users.UserImpl) Permissions(org.graylog2.shared.security.Permissions) MongoDBFixtures(org.graylog.testing.mongodb.MongoDBFixtures) Test(org.junit.Test)

Example 5 with StreamImpl

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;
}
Also used : ObjectId(org.bson.types.ObjectId) StreamImpl(org.graylog2.streams.StreamImpl) StreamRuleImpl(org.graylog2.streams.StreamRuleImpl) IndexSet(org.graylog2.indexer.IndexSet)

Aggregations

StreamImpl (org.graylog2.streams.StreamImpl)7 ObjectId (org.bson.types.ObjectId)5 HashMap (java.util.HashMap)3 IndexSet (org.graylog2.indexer.IndexSet)3 ValidationException (org.graylog2.plugin.database.ValidationException)3 Stream (org.graylog2.plugin.streams.Stream)3 Test (org.junit.Test)3 BasicDBObject (com.mongodb.BasicDBObject)2 DBObject (com.mongodb.DBObject)2 StreamFilter (org.graylog.plugins.views.search.filter.StreamFilter)2 ViewDTO (org.graylog.plugins.views.search.views.ViewDTO)2 Entity (org.graylog2.contentpacks.model.entities.Entity)2 EntityDescriptor (org.graylog2.contentpacks.model.entities.EntityDescriptor)2 StreamEntity (org.graylog2.contentpacks.model.entities.StreamEntity)2 MongoConnection (org.graylog2.database.MongoConnection)2 Output (org.graylog2.plugin.streams.Output)2 StreamRule (org.graylog2.plugin.streams.StreamRule)2 NamedType (com.fasterxml.jackson.databind.jsontype.NamedType)1 Strings (com.google.common.base.Strings)1 Strings.isNullOrEmpty (com.google.common.base.Strings.isNullOrEmpty)1