Search in sources :

Example 11 with NativeEntity

use of org.graylog2.contentpacks.model.entities.NativeEntity in project graylog2-server by Graylog2.

the class LookupDataAdapterFacade method decode.

private NativeEntity<DataAdapterDto> decode(EntityV1 entity, final Map<String, ValueReference> parameters) {
    final LookupDataAdapterEntity lookupDataAdapterEntity = objectMapper.convertValue(entity.data(), LookupDataAdapterEntity.class);
    final LookupDataAdapterConfiguration configuration = objectMapper.convertValue(toValueMap(lookupDataAdapterEntity.configuration(), parameters), LookupDataAdapterConfiguration.class);
    final DataAdapterDto dataAdapterDto = DataAdapterDto.builder().name(lookupDataAdapterEntity.name().asString(parameters)).title(lookupDataAdapterEntity.title().asString(parameters)).description(lookupDataAdapterEntity.description().asString(parameters)).config(configuration).build();
    final DataAdapterDto savedDataAdapterDto = dataAdapterService.save(dataAdapterDto);
    return NativeEntity.create(entity.id(), savedDataAdapterDto.id(), TYPE_V1, savedDataAdapterDto.title(), savedDataAdapterDto);
}
Also used : LookupDataAdapterEntity(org.graylog2.contentpacks.model.entities.LookupDataAdapterEntity) DataAdapterDto(org.graylog2.lookup.dto.DataAdapterDto) LookupDataAdapterConfiguration(org.graylog2.plugin.lookup.LookupDataAdapterConfiguration)

Example 12 with NativeEntity

use of org.graylog2.contentpacks.model.entities.NativeEntity in project graylog2-server by Graylog2.

the class NotificationFacadeTest method loadNativeEntity.

@Test
@MongoDBFixtures("NotificationFacadeTest.json")
public void loadNativeEntity() {
    final NativeEntityDescriptor nativeEntityDescriptor = NativeEntityDescriptor.create(ModelId.of("content-pack-id"), ModelId.of("5d4d33753d27460ad18e0c4d"), ModelTypes.NOTIFICATION_V1, "title");
    final Optional<NativeEntity<NotificationDto>> optionalNativeEntity = facade.loadNativeEntity(nativeEntityDescriptor);
    assertThat(optionalNativeEntity).isPresent();
    final NativeEntity<NotificationDto> nativeEntity = optionalNativeEntity.get();
    assertThat(nativeEntity.entity()).isNotNull();
    final NotificationDto notificationDto = nativeEntity.entity();
    assertThat(notificationDto.id()).isEqualTo("5d4d33753d27460ad18e0c4d");
}
Also used : NotificationDto(org.graylog.events.notifications.NotificationDto) NativeEntityDescriptor(org.graylog2.contentpacks.model.entities.NativeEntityDescriptor) NativeEntity(org.graylog2.contentpacks.model.entities.NativeEntity) MongoDBFixtures(org.graylog.testing.mongodb.MongoDBFixtures) Test(org.junit.Test)

Example 13 with NativeEntity

use of org.graylog2.contentpacks.model.entities.NativeEntity in project graylog2-server by Graylog2.

the class PipelineFacade method decode.

private NativeEntity<PipelineDao> decode(EntityV1 entity, Map<String, ValueReference> parameters, Map<EntityDescriptor, Object> nativeEntities) {
    final DateTime now = Tools.nowUTC();
    final PipelineEntity pipelineEntity = objectMapper.convertValue(entity.data(), PipelineEntity.class);
    final ValueReference description = pipelineEntity.description();
    final PipelineDao pipelineDao = PipelineDao.builder().title(pipelineEntity.title().asString(parameters)).description(description == null ? null : description.asString(parameters)).source(pipelineEntity.source().asString(parameters)).createdAt(now).modifiedAt(now).build();
    final PipelineDao savedPipelineDao = pipelineService.save(pipelineDao);
    final String pipelineId = requireNonNull(savedPipelineDao.id(), "Saved pipeline ID must not be null");
    final Set<EntityDescriptor> connectedStreamEntities = pipelineEntity.connectedStreams().stream().map(valueReference -> valueReference.asString(parameters)).map(streamId -> EntityDescriptor.create(streamId, ModelTypes.STREAM_V1)).collect(Collectors.toSet());
    final Set<Stream> connectedStreams = connectedStreams(connectedStreamEntities, nativeEntities);
    createPipelineConnections(pipelineId, connectedStreams);
    return NativeEntity.create(entity.id(), pipelineId, TYPE_V1, savedPipelineDao.title(), savedPipelineDao);
}
Also used : PipelineConnections(org.graylog.plugins.pipelineprocessor.rest.PipelineConnections) EntityDescriptorIds(org.graylog2.contentpacks.EntityDescriptorIds) RuleDao(org.graylog.plugins.pipelineprocessor.db.RuleDao) RuleService(org.graylog.plugins.pipelineprocessor.db.RuleService) ImmutableGraph(com.google.common.graph.ImmutableGraph) NativeEntity(org.graylog2.contentpacks.model.entities.NativeEntity) Tools(org.graylog2.plugin.Tools) LoggerFactory(org.slf4j.LoggerFactory) Entity(org.graylog2.contentpacks.model.entities.Entity) Stage(org.graylog.plugins.pipelineprocessor.ast.Stage) ModelType(org.graylog2.contentpacks.model.ModelType) ValueReference(org.graylog2.contentpacks.model.entities.references.ValueReference) Inject(javax.inject.Inject) PipelineService(org.graylog.plugins.pipelineprocessor.db.PipelineService) EntityExcerpt(org.graylog2.contentpacks.model.entities.EntityExcerpt) PipelineEntity(org.graylog2.contentpacks.model.entities.PipelineEntity) PipelineStreamConnectionsService(org.graylog.plugins.pipelineprocessor.db.PipelineStreamConnectionsService) Map(java.util.Map) Objects.requireNonNull(java.util.Objects.requireNonNull) JsonNode(com.fasterxml.jackson.databind.JsonNode) PipelineRuleParser(org.graylog.plugins.pipelineprocessor.parser.PipelineRuleParser) NotFoundException(org.graylog2.database.NotFoundException) Pipeline(org.graylog.plugins.pipelineprocessor.ast.Pipeline) ImmutableSet(com.google.common.collect.ImmutableSet) Logger(org.slf4j.Logger) ModelId(org.graylog2.contentpacks.model.ModelId) MutableGraph(com.google.common.graph.MutableGraph) EntityDescriptor(org.graylog2.contentpacks.model.entities.EntityDescriptor) Collection(java.util.Collection) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) DateTime(org.joda.time.DateTime) MissingNativeEntityException(org.graylog2.contentpacks.exceptions.MissingNativeEntityException) Set(java.util.Set) PipelineDao(org.graylog.plugins.pipelineprocessor.db.PipelineDao) Collectors(java.util.stream.Collectors) GraphBuilder(com.google.common.graph.GraphBuilder) EntityV1(org.graylog2.contentpacks.model.entities.EntityV1) Objects(java.util.Objects) Stream(org.graylog2.plugin.streams.Stream) StreamService(org.graylog2.streams.StreamService) Optional(java.util.Optional) VisibleForTesting(com.google.common.annotations.VisibleForTesting) NativeEntityDescriptor(org.graylog2.contentpacks.model.entities.NativeEntityDescriptor) Collections(java.util.Collections) Graph(com.google.common.graph.Graph) ModelTypes(org.graylog2.contentpacks.model.ModelTypes) EntityDescriptor(org.graylog2.contentpacks.model.entities.EntityDescriptor) NativeEntityDescriptor(org.graylog2.contentpacks.model.entities.NativeEntityDescriptor) PipelineEntity(org.graylog2.contentpacks.model.entities.PipelineEntity) PipelineDao(org.graylog.plugins.pipelineprocessor.db.PipelineDao) Stream(org.graylog2.plugin.streams.Stream) DateTime(org.joda.time.DateTime) ValueReference(org.graylog2.contentpacks.model.entities.references.ValueReference)

Example 14 with NativeEntity

use of org.graylog2.contentpacks.model.entities.NativeEntity in project graylog2-server by Graylog2.

the class StreamFacade method decode.

private NativeEntity<Stream> decode(EntityV1 entity, Map<String, ValueReference> parameters, Map<EntityDescriptor, Object> nativeEntities, User user) {
    final StreamEntity streamEntity = objectMapper.convertValue(entity.data(), StreamEntity.class);
    final CreateStreamRequest createStreamRequest = CreateStreamRequest.create(streamEntity.title().asString(parameters), streamEntity.description().asString(parameters), // ignored
    null, null, streamEntity.matchingType().asString(parameters), streamEntity.removeMatches().asBoolean(parameters), indexSetService.getDefault().id());
    final Stream stream = streamService.create(createStreamRequest, user.getName());
    final List<StreamRule> streamRules = streamEntity.streamRules().stream().map(streamRuleEntity -> createStreamRuleRequest(streamRuleEntity, parameters)).map(request -> streamRuleService.create(DUMMY_STREAM_ID, request)).collect(Collectors.toList());
    // TODO: The creation of legacy alert conditions should be avoided and a new event definition should be created instead
    final List<AlertCondition> alertConditions = streamEntity.alertConditions().stream().map(alertCondition -> createStreamAlertConditionRequest(alertCondition, parameters)).map(request -> {
        try {
            return streamAlertService.fromRequest(request, stream, user.getName());
        } catch (ConfigurationException e) {
            throw new ContentPackException("Couldn't create entity " + entity.toEntityDescriptor(), e);
        }
    }).collect(Collectors.toList());
    // TODO: The creation of legacy alarm callback should be avoided and a new event notification should be created instead
    final List<AlarmCallbackConfiguration> alarmCallbacks = streamEntity.alarmCallbacks().stream().map(alarmCallback -> createStreamAlarmCallbackRequest(alarmCallback, parameters)).map(request -> alarmCallbackConfigurationService.create(stream.getId(), request, user.getName())).collect(Collectors.toList());
    final String savedStreamId;
    try {
        savedStreamId = streamService.saveWithRulesAndOwnership(stream, streamRules, user);
        for (final AlertCondition alertCondition : alertConditions) {
            streamService.addAlertCondition(stream, alertCondition);
        }
        for (final AlarmCallbackConfiguration alarmCallback : alarmCallbacks) {
            alarmCallbackConfigurationService.save(alarmCallback);
        }
    } catch (ValidationException e) {
        throw new ContentPackException("Couldn't create entity " + entity.toEntityDescriptor(), e);
    }
    final Set<ObjectId> outputIds = streamEntity.outputs().stream().map(valueReference -> valueReference.asString(parameters)).map(ModelId::of).map(modelId -> EntityDescriptor.create(modelId, ModelTypes.OUTPUT_V1)).map(descriptor -> findOutput(descriptor, nativeEntities)).map(Output::getId).map(ObjectId::new).collect(Collectors.toSet());
    streamService.addOutputs(new ObjectId(savedStreamId), outputIds);
    if (!alertConditions.isEmpty() || !alarmCallbacks.isEmpty()) {
        // TODO: Remove migration call once we updated the above code to directly create event definitions and notifications
        try {
            legacyAlertsMigration.upgrade();
        } catch (Exception e) {
            LOG.error("Couldn't run migration for newly created legacy alert conditions and/or alarm callbacks", e);
        }
    }
    return NativeEntity.create(entity.id(), savedStreamId, TYPE_V1, stream.getTitle(), stream);
}
Also used : ImmutableGraph(com.google.common.graph.ImmutableGraph) NativeEntity(org.graylog2.contentpacks.model.entities.NativeEntity) LoggerFactory(org.slf4j.LoggerFactory) CreateStreamRuleRequest(org.graylog2.rest.resources.streams.rules.requests.CreateStreamRuleRequest) AlarmCallbackConfiguration(org.graylog2.alarmcallbacks.AlarmCallbackConfiguration) AlertService(org.graylog2.alerts.AlertService) StreamRule(org.graylog2.plugin.streams.StreamRule) ModelType(org.graylog2.contentpacks.model.ModelType) ValueReference(org.graylog2.contentpacks.model.entities.references.ValueReference) StreamRuleService(org.graylog2.streams.StreamRuleService) Map(java.util.Map) JsonNode(com.fasterxml.jackson.databind.JsonNode) ModelId(org.graylog2.contentpacks.model.ModelId) MutableGraph(com.google.common.graph.MutableGraph) EntityDescriptor(org.graylog2.contentpacks.model.entities.EntityDescriptor) StreamAlarmCallbackEntity(org.graylog2.contentpacks.model.entities.StreamAlarmCallbackEntity) Set(java.util.Set) Collectors(java.util.stream.Collectors) GraphBuilder(com.google.common.graph.GraphBuilder) StreamRuleType(org.graylog2.plugin.streams.StreamRuleType) Objects(java.util.Objects) CreateStreamRequest(org.graylog2.rest.resources.streams.requests.CreateStreamRequest) CreateAlarmCallbackRequest(org.graylog2.rest.models.alarmcallbacks.requests.CreateAlarmCallbackRequest) List(java.util.List) IndexSetService(org.graylog2.indexer.indexset.IndexSetService) UserService(org.graylog2.shared.users.UserService) Stream(org.graylog2.plugin.streams.Stream) StreamService(org.graylog2.streams.StreamService) AlertCondition(org.graylog2.plugin.alarms.AlertCondition) CreateConditionRequest(org.graylog2.rest.models.streams.alerts.requests.CreateConditionRequest) Optional(java.util.Optional) ModelTypes(org.graylog2.contentpacks.model.ModelTypes) EntityDescriptorIds(org.graylog2.contentpacks.EntityDescriptorIds) Strings.nullToEmpty(com.google.common.base.Strings.nullToEmpty) Entity(org.graylog2.contentpacks.model.entities.Entity) ContentPackException(org.graylog2.contentpacks.exceptions.ContentPackException) StreamAlertConditionEntity(org.graylog2.contentpacks.model.entities.StreamAlertConditionEntity) Inject(javax.inject.Inject) ReferenceMapUtils(org.graylog2.contentpacks.model.entities.references.ReferenceMapUtils) V20190722150700_LegacyAlertConditionMigration(org.graylog.events.legacy.V20190722150700_LegacyAlertConditionMigration) EntityExcerpt(org.graylog2.contentpacks.model.entities.EntityExcerpt) ConfigurationException(org.graylog2.plugin.configuration.ConfigurationException) NotFoundException(org.graylog2.database.NotFoundException) Logger(org.slf4j.Logger) StreamEntity(org.graylog2.contentpacks.model.entities.StreamEntity) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) StreamRuleEntity(org.graylog2.contentpacks.model.entities.StreamRuleEntity) AlarmCallbackConfigurationService(org.graylog2.alarmcallbacks.AlarmCallbackConfigurationService) EntityV1(org.graylog2.contentpacks.model.entities.EntityV1) Output(org.graylog2.plugin.streams.Output) ValidationException(org.graylog2.plugin.database.ValidationException) ObjectId(org.bson.types.ObjectId) VisibleForTesting(com.google.common.annotations.VisibleForTesting) User(org.graylog2.plugin.database.users.User) NativeEntityDescriptor(org.graylog2.contentpacks.model.entities.NativeEntityDescriptor) Collections(java.util.Collections) Graph(com.google.common.graph.Graph) ContentPackException(org.graylog2.contentpacks.exceptions.ContentPackException) ValidationException(org.graylog2.plugin.database.ValidationException) ObjectId(org.bson.types.ObjectId) StreamRule(org.graylog2.plugin.streams.StreamRule) StreamEntity(org.graylog2.contentpacks.model.entities.StreamEntity) ContentPackException(org.graylog2.contentpacks.exceptions.ContentPackException) ConfigurationException(org.graylog2.plugin.configuration.ConfigurationException) NotFoundException(org.graylog2.database.NotFoundException) ValidationException(org.graylog2.plugin.database.ValidationException) ConfigurationException(org.graylog2.plugin.configuration.ConfigurationException) CreateStreamRequest(org.graylog2.rest.resources.streams.requests.CreateStreamRequest) Output(org.graylog2.plugin.streams.Output) AlertCondition(org.graylog2.plugin.alarms.AlertCondition) Stream(org.graylog2.plugin.streams.Stream) AlarmCallbackConfiguration(org.graylog2.alarmcallbacks.AlarmCallbackConfiguration)

Example 15 with NativeEntity

use of org.graylog2.contentpacks.model.entities.NativeEntity in project graylog2-server by Graylog2.

the class EventDefinitionFacadeTest method createNativeEntity.

@Test
public void createNativeEntity() {
    final EntityV1 entityV1 = createTestEntity();
    final NotificationDto notificationDto = NotificationDto.builder().config(HTTPEventNotificationConfig.builder().url("https://hulud.net").build()).title("Notify me Senpai").description("A notification for senpai").id("dead-beef").build();
    final EntityDescriptor entityDescriptor = EntityDescriptor.create("123123", ModelTypes.NOTIFICATION_V1);
    final ImmutableMap<EntityDescriptor, Object> nativeEntities = ImmutableMap.of(entityDescriptor, notificationDto);
    final JobDefinitionDto jobDefinitionDto = mock(JobDefinitionDto.class);
    final JobTriggerDto jobTriggerDto = mock(JobTriggerDto.class);
    when(jobDefinitionDto.id()).thenReturn("job-123123");
    when(jobSchedulerClock.nowUTC()).thenReturn(DateTime.now(DateTimeZone.UTC));
    when(jobDefinitionService.save(any(JobDefinitionDto.class))).thenReturn(jobDefinitionDto);
    when(jobTriggerService.create(any(JobTriggerDto.class))).thenReturn(jobTriggerDto);
    final UserImpl kmerzUser = new UserImpl(mock(PasswordAlgorithmFactory.class), new Permissions(ImmutableSet.of()), ImmutableMap.of("username", "kmerz"));
    when(userService.load("kmerz")).thenReturn(kmerzUser);
    final NativeEntity<EventDefinitionDto> nativeEntity = facade.createNativeEntity(entityV1, ImmutableMap.of(), nativeEntities, "kmerz");
    assertThat(nativeEntity).isNotNull();
    final EventDefinitionDto eventDefinitionDto = nativeEntity.entity();
    assertThat(eventDefinitionDto.title()).isEqualTo("title");
    assertThat(eventDefinitionDto.description()).isEqualTo("description");
    assertThat(eventDefinitionDto.config().type()).isEqualTo("aggregation-v1");
    // verify that ownership was registered for this entity
    verify(entityOwnershipService, times(1)).registerNewEventDefinition(nativeEntity.entity().id(), kmerzUser);
}
Also used : EntityV1(org.graylog2.contentpacks.model.entities.EntityV1) EntityDescriptor(org.graylog2.contentpacks.model.entities.EntityDescriptor) NativeEntityDescriptor(org.graylog2.contentpacks.model.entities.NativeEntityDescriptor) PasswordAlgorithmFactory(org.graylog2.security.PasswordAlgorithmFactory) NotificationDto(org.graylog.events.notifications.NotificationDto) EventDefinitionDto(org.graylog.events.processor.EventDefinitionDto) JobDefinitionDto(org.graylog.scheduler.JobDefinitionDto) UserImpl(org.graylog2.users.UserImpl) Permissions(org.graylog2.shared.security.Permissions) JobTriggerDto(org.graylog.scheduler.JobTriggerDto) Test(org.junit.Test)

Aggregations

NativeEntity (org.graylog2.contentpacks.model.entities.NativeEntity)30 Entity (org.graylog2.contentpacks.model.entities.Entity)28 Test (org.junit.Test)26 NativeEntityDescriptor (org.graylog2.contentpacks.model.entities.NativeEntityDescriptor)15 MongoDBFixtures (org.graylog.testing.mongodb.MongoDBFixtures)14 EntityDescriptor (org.graylog2.contentpacks.model.entities.EntityDescriptor)12 EntityV1 (org.graylog2.contentpacks.model.entities.EntityV1)7 LookupCacheEntity (org.graylog2.contentpacks.model.entities.LookupCacheEntity)7 LookupTableEntity (org.graylog2.contentpacks.model.entities.LookupTableEntity)7 ValueReference (org.graylog2.contentpacks.model.entities.references.ValueReference)7 HashMap (java.util.HashMap)6 LookupDataAdapterEntity (org.graylog2.contentpacks.model.entities.LookupDataAdapterEntity)6 CacheDto (org.graylog2.lookup.dto.CacheDto)6 Map (java.util.Map)5 RuleDao (org.graylog.plugins.pipelineprocessor.db.RuleDao)5 GrokPatternEntity (org.graylog2.contentpacks.model.entities.GrokPatternEntity)5 DataAdapterDto (org.graylog2.lookup.dto.DataAdapterDto)5 Graph (com.google.common.graph.Graph)4 GraphBuilder (com.google.common.graph.GraphBuilder)4 ImmutableGraph (com.google.common.graph.ImmutableGraph)4