Search in sources :

Example 6 with LookupTableDto

use of org.graylog2.lookup.dto.LookupTableDto in project graylog2-server by Graylog2.

the class LookupTableResource method validateTable.

@POST
@Path("tables/validate")
@NoAuditEvent("Validation only")
@ApiOperation(value = "Validate the lookup table config")
@RequiresPermissions(RestPermissions.LOOKUP_TABLES_READ)
public ValidationResult validateTable(@Valid @ApiParam LookupTableApi toValidate) {
    final ValidationResult validation = new ValidationResult();
    final Optional<LookupTableDto> dtoOptional = dbTableService.get(toValidate.name());
    if (dtoOptional.isPresent()) {
        // a table exist with the given name, check that the IDs are the same, this might be an update
        final LookupTableDto tableDto = dtoOptional.get();
        // noinspection ConstantConditions
        if (!tableDto.id().equals(toValidate.id())) {
            // a table exists with a different id, so the name is already in use, fail validation
            validation.addError("name", "The lookup table name is already in use.");
        }
    }
    try {
        LookupDefaultSingleValue.create(toValidate.defaultSingleValue(), toValidate.defaultSingleValueType());
    } catch (Exception e) {
        validation.addError(LookupTableApi.FIELD_DEFAULT_SINGLE_VALUE, e.getMessage());
    }
    try {
        LookupDefaultMultiValue.create(toValidate.defaultMultiValue(), toValidate.defaultMultiValueType());
    } catch (Exception e) {
        validation.addError(LookupTableApi.FIELD_DEFAULT_MULTI_VALUE, e.getMessage());
    }
    return validation;
}
Also used : LookupTableDto(org.graylog2.lookup.dto.LookupTableDto) ValidationResult(org.graylog2.plugin.rest.ValidationResult) BadRequestException(javax.ws.rs.BadRequestException) NotFoundException(javax.ws.rs.NotFoundException) DuplicateKeyException(com.mongodb.DuplicateKeyException) Path(javax.ws.rs.Path) RequiresPermissions(org.apache.shiro.authz.annotation.RequiresPermissions) POST(javax.ws.rs.POST) ApiOperation(io.swagger.annotations.ApiOperation) NoAuditEvent(org.graylog2.audit.jersey.NoAuditEvent)

Example 7 with LookupTableDto

use of org.graylog2.lookup.dto.LookupTableDto in project graylog2-server by Graylog2.

the class LookupTableResource method updateTable.

@PUT
@Path("tables/{idOrName}")
@AuditEvent(type = AuditEventTypes.LOOKUP_TABLE_UPDATE)
@ApiOperation(value = "Update the given lookup table")
public LookupTableApi updateTable(@ApiParam(name = "idOrName") @PathParam("idOrName") @NotEmpty String idOrName, @Valid @ApiParam LookupTableApi toUpdate) {
    checkLookupTableId(idOrName, toUpdate);
    checkPermission(RestPermissions.LOOKUP_TABLES_EDIT, toUpdate.id());
    LookupTableDto saved = dbTableService.save(toUpdate.toDto());
    return LookupTableApi.fromDto(saved);
}
Also used : LookupTableDto(org.graylog2.lookup.dto.LookupTableDto) Path(javax.ws.rs.Path) ApiOperation(io.swagger.annotations.ApiOperation) NoAuditEvent(org.graylog2.audit.jersey.NoAuditEvent) AuditEvent(org.graylog2.audit.jersey.AuditEvent) PUT(javax.ws.rs.PUT)

Example 8 with LookupTableDto

use of org.graylog2.lookup.dto.LookupTableDto in project graylog2-server by Graylog2.

the class LookupTableService method handleCacheUpdate.

@Subscribe
public void handleCacheUpdate(CachesUpdated updated) {
    scheduler.schedule(() -> {
        // first we create the new cache instance and start it
        // then we retrieve the old one so we can safely stop it later
        // then we build a new lookup table instance with the new cache instance
        // last we can remove the old lookup table instance and stop the original cache
        // collect old cache instances
        final ImmutableSet.Builder<LookupCache> existingCaches = ImmutableSet.builder();
        // create new cache and lookup table instances
        final Map<CacheDto, LookupCache> newCaches = createCaches(configService.findCachesForIds(updated.ids()));
        final CountDownLatch runningLatch = new CountDownLatch(newCaches.size());
        newCaches.forEach((cacheDto, cache) -> {
            cache.addListener(new CacheListener(cacheDto, cache, runningLatch, existingCaches::add), scheduler);
            cache.startAsync();
        });
        // wait until everything is either running or failed before starting the
        awaitUninterruptibly(runningLatch);
        // when a cache is updated, the lookup tables that use it need to be updated as well
        final Collection<LookupTableDto> tablesToUpdate = configService.findTablesForCacheIds(updated.ids());
        tablesToUpdate.forEach(this::createLookupTable);
        // stop old caches
        existingCaches.build().forEach(AbstractIdleService::stopAsync);
    }, 0, TimeUnit.SECONDS);
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) LookupCache(org.graylog2.plugin.lookup.LookupCache) LookupTableDto(org.graylog2.lookup.dto.LookupTableDto) AbstractIdleService(com.google.common.util.concurrent.AbstractIdleService) CountDownLatch(java.util.concurrent.CountDownLatch) CacheDto(org.graylog2.lookup.dto.CacheDto) Subscribe(com.google.common.eventbus.Subscribe)

Example 9 with LookupTableDto

use of org.graylog2.lookup.dto.LookupTableDto in project graylog2-server by Graylog2.

the class LookupTableService method handleAdapterUpdate.

@Subscribe
public void handleAdapterUpdate(DataAdaptersUpdated updated) {
    scheduler.schedule(() -> {
        // first we create the new adapter instance and start it
        // then we retrieve the old one so we can safely stop it later
        // then we build a new lookup table instance with the new adapter instance
        // last we can remove the old lookup table instance and stop the original adapter
        // collect old adapter instances
        final ImmutableSet.Builder<LookupDataAdapter> existingAdapters = ImmutableSet.builder();
        // create new adapter and lookup table instances
        final Map<DataAdapterDto, LookupDataAdapter> newAdapters = createAdapters(configService.findDataAdaptersForIds(updated.ids()));
        final CountDownLatch runningLatch = new CountDownLatch(newAdapters.size());
        newAdapters.forEach((dto, adapter) -> {
            adapter.addListener(new DataAdapterListener(dto, adapter, runningLatch, existingAdapters::add), scheduler);
            adapter.startAsync();
        });
        // wait until everything is either running or failed before starting the
        awaitUninterruptibly(runningLatch);
        // when a data adapter is updated, the lookup tables that use it need to be updated as well
        final Collection<LookupTableDto> tablesToUpdate = configService.findTablesForDataAdapterIds(updated.ids());
        tablesToUpdate.forEach(this::createLookupTable);
        // stop old adapters
        existingAdapters.build().forEach(AbstractIdleService::stopAsync);
    }, 0, TimeUnit.SECONDS);
}
Also used : LookupDataAdapter(org.graylog2.plugin.lookup.LookupDataAdapter) DataAdapterDto(org.graylog2.lookup.dto.DataAdapterDto) ImmutableSet(com.google.common.collect.ImmutableSet) LookupTableDto(org.graylog2.lookup.dto.LookupTableDto) AbstractIdleService(com.google.common.util.concurrent.AbstractIdleService) CountDownLatch(java.util.concurrent.CountDownLatch) Subscribe(com.google.common.eventbus.Subscribe)

Example 10 with LookupTableDto

use of org.graylog2.lookup.dto.LookupTableDto in project graylog2-server by Graylog2.

the class LookupTableFacadeTest method createNativeEntity.

@Test
public void createNativeEntity() {
    final Entity entity = EntityV1.builder().id(ModelId.of("1")).type(ModelTypes.LOOKUP_TABLE_V1).data(objectMapper.convertValue(LookupTableEntity.create(ValueReference.of("http-dsv-no-cache"), ValueReference.of("HTTP DSV without Cache"), ValueReference.of("HTTP DSV without Cache"), ValueReference.of("no-op-cache"), ValueReference.of("http-dsv"), ValueReference.of("Default single value"), ValueReference.of(LookupDefaultValue.Type.STRING), ValueReference.of("Default multi value"), ValueReference.of(LookupDefaultValue.Type.OBJECT)), JsonNode.class)).build();
    final EntityDescriptor cacheDescriptor = EntityDescriptor.create("no-op-cache", ModelTypes.LOOKUP_CACHE_V1);
    final CacheDto cacheDto = CacheDto.builder().id("5adf24b24b900a0fdb4e0001").name("no-op-cache").title("No-op cache").description("No-op cache").config(new FallbackCacheConfig()).build();
    final EntityDescriptor dataAdapterDescriptor = EntityDescriptor.create("http-dsv", ModelTypes.LOOKUP_ADAPTER_V1);
    final DataAdapterDto dataAdapterDto = DataAdapterDto.builder().id("5adf24a04b900a0fdb4e0002").name("http-dsv").title("HTTP DSV").description("HTTP DSV").config(new FallbackAdapterConfig()).build();
    final Map<EntityDescriptor, Object> nativeEntities = ImmutableMap.of(cacheDescriptor, cacheDto, dataAdapterDescriptor, dataAdapterDto);
    assertThat(lookupTableService.findAll()).isEmpty();
    final NativeEntity<LookupTableDto> nativeEntity = facade.createNativeEntity(entity, Collections.emptyMap(), nativeEntities, "username");
    assertThat(nativeEntity.descriptor().type()).isEqualTo(ModelTypes.LOOKUP_TABLE_V1);
    assertThat(nativeEntity.entity().name()).isEqualTo("http-dsv-no-cache");
    assertThat(nativeEntity.entity().title()).isEqualTo("HTTP DSV without Cache");
    assertThat(nativeEntity.entity().description()).isEqualTo("HTTP DSV without Cache");
    assertThat(nativeEntity.entity().cacheId()).isEqualTo("5adf24b24b900a0fdb4e0001");
    assertThat(nativeEntity.entity().dataAdapterId()).isEqualTo("5adf24a04b900a0fdb4e0002");
    assertThat(nativeEntity.entity().defaultSingleValue()).isEqualTo("Default single value");
    assertThat(nativeEntity.entity().defaultSingleValueType()).isEqualTo(LookupDefaultValue.Type.STRING);
    assertThat(nativeEntity.entity().defaultMultiValue()).isEqualTo("Default multi value");
    assertThat(nativeEntity.entity().defaultMultiValueType()).isEqualTo(LookupDefaultValue.Type.OBJECT);
    assertThat(lookupTableService.findAll()).hasSize(1);
}
Also used : NativeEntity(org.graylog2.contentpacks.model.entities.NativeEntity) Entity(org.graylog2.contentpacks.model.entities.Entity) LookupCacheEntity(org.graylog2.contentpacks.model.entities.LookupCacheEntity) LookupDataAdapterEntity(org.graylog2.contentpacks.model.entities.LookupDataAdapterEntity) LookupTableEntity(org.graylog2.contentpacks.model.entities.LookupTableEntity) EntityDescriptor(org.graylog2.contentpacks.model.entities.EntityDescriptor) DataAdapterDto(org.graylog2.lookup.dto.DataAdapterDto) LookupTableDto(org.graylog2.lookup.dto.LookupTableDto) CacheDto(org.graylog2.lookup.dto.CacheDto) FallbackAdapterConfig(org.graylog2.plugin.lookup.FallbackAdapterConfig) FallbackCacheConfig(org.graylog2.plugin.lookup.FallbackCacheConfig) Test(org.junit.Test)

Aggregations

LookupTableDto (org.graylog2.lookup.dto.LookupTableDto)14 LookupTableEntity (org.graylog2.contentpacks.model.entities.LookupTableEntity)8 Entity (org.graylog2.contentpacks.model.entities.Entity)5 EntityDescriptor (org.graylog2.contentpacks.model.entities.EntityDescriptor)5 NativeEntity (org.graylog2.contentpacks.model.entities.NativeEntity)5 Test (org.junit.Test)5 ImmutableSet (com.google.common.collect.ImmutableSet)4 ApiOperation (io.swagger.annotations.ApiOperation)4 Path (javax.ws.rs.Path)4 LookupCacheEntity (org.graylog2.contentpacks.model.entities.LookupCacheEntity)4 LookupDataAdapterEntity (org.graylog2.contentpacks.model.entities.LookupDataAdapterEntity)4 NativeEntityDescriptor (org.graylog2.contentpacks.model.entities.NativeEntityDescriptor)3 CacheDto (org.graylog2.lookup.dto.CacheDto)3 DataAdapterDto (org.graylog2.lookup.dto.DataAdapterDto)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 Subscribe (com.google.common.eventbus.Subscribe)2 AbstractIdleService (com.google.common.util.concurrent.AbstractIdleService)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 GET (javax.ws.rs.GET)2