Search in sources :

Example 1 with NativeEntityDescriptor

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

the class ContentPackService method getUninstallDetails.

private ContentPackUninstallDetails getUninstallDetails(ContentPackV1 contentPack, ContentPackInstallation installation) {
    final Entity rootEntity = EntityV1.createRoot(contentPack);
    final ImmutableMap<String, ValueReference> parameters = installation.parameters();
    final ImmutableGraph<Entity> dependencyGraph = buildEntityGraph(rootEntity, contentPack.entities(), parameters);
    final Traverser<Entity> entityTraverser = Traverser.forGraph(dependencyGraph);
    final Iterable<Entity> entitiesInOrder = entityTraverser.breadthFirst(rootEntity);
    final Set<NativeEntityDescriptor> nativeEntityDescriptors = new HashSet<>();
    entitiesInOrder.forEach((entity -> {
        if (entity.equals(rootEntity)) {
            return;
        }
        final Optional<NativeEntityDescriptor> nativeEntityDescriptorOptional = installation.entities().stream().filter(descriptor -> entity.id().equals(descriptor.contentPackEntityId())).findFirst();
        if (nativeEntityDescriptorOptional.isPresent()) {
            NativeEntityDescriptor nativeEntityDescriptor = nativeEntityDescriptorOptional.get();
            if (contentPackInstallationPersistenceService.countInstallationOfEntityById(nativeEntityDescriptor.id()) <= 1) {
                nativeEntityDescriptors.add(nativeEntityDescriptor);
            }
        }
    }));
    return ContentPackUninstallDetails.create(nativeEntityDescriptors);
}
Also used : ElementOrder(com.google.common.graph.ElementOrder) Graphs(org.graylog2.utilities.Graphs) Constraint(org.graylog2.contentpacks.model.constraints.Constraint) ImmutableGraph(com.google.common.graph.ImmutableGraph) NativeEntity(org.graylog2.contentpacks.model.entities.NativeEntity) ContentPack(org.graylog2.contentpacks.model.ContentPack) LoggerFactory(org.slf4j.LoggerFactory) ConstraintCheckResult(org.graylog2.contentpacks.model.constraints.ConstraintCheckResult) ModelType(org.graylog2.contentpacks.model.ModelType) ValueReference(org.graylog2.contentpacks.model.entities.references.ValueReference) LegacyContentPack(org.graylog2.contentpacks.model.LegacyContentPack) Map(java.util.Map) FailedConstraintsException(org.graylog2.contentpacks.exceptions.FailedConstraintsException) InvalidParameterTypeException(org.graylog2.contentpacks.exceptions.InvalidParameterTypeException) MissingParametersException(org.graylog2.contentpacks.exceptions.MissingParametersException) UnsupportedEntityFacade(org.graylog2.contentpacks.facades.UnsupportedEntityFacade) ImmutableSet(com.google.common.collect.ImmutableSet) ModelId(org.graylog2.contentpacks.model.ModelId) ImmutableMap(com.google.common.collect.ImmutableMap) Predicate(java.util.function.Predicate) MutableGraph(com.google.common.graph.MutableGraph) EntityDescriptor(org.graylog2.contentpacks.model.entities.EntityDescriptor) Collection(java.util.Collection) EntityWithExcerptFacade(org.graylog2.contentpacks.facades.EntityWithExcerptFacade) Set(java.util.Set) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) GraphBuilder(com.google.common.graph.GraphBuilder) InvalidParametersException(org.graylog2.contentpacks.exceptions.InvalidParametersException) Parameter(org.graylog2.contentpacks.model.parameters.Parameter) Optional(java.util.Optional) ConstraintChecker(org.graylog2.contentpacks.constraints.ConstraintChecker) ContentPackUninstallDetails(org.graylog2.contentpacks.model.ContentPackUninstallDetails) EmptyDefaultValueException(org.graylog2.contentpacks.exceptions.EmptyDefaultValueException) ContentPackV1(org.graylog2.contentpacks.model.ContentPackV1) Entity(org.graylog2.contentpacks.model.entities.Entity) HashMap(java.util.HashMap) ContentPackException(org.graylog2.contentpacks.exceptions.ContentPackException) ValueType(org.graylog2.contentpacks.model.entities.references.ValueType) Singleton(javax.inject.Singleton) Function(java.util.function.Function) Inject(javax.inject.Inject) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) ImmutableList(com.google.common.collect.ImmutableList) EntityExcerpt(org.graylog2.contentpacks.model.entities.EntityExcerpt) EntityFacade(org.graylog2.contentpacks.facades.EntityFacade) Logger(org.slf4j.Logger) ContentPackUninstallation(org.graylog2.contentpacks.model.ContentPackUninstallation) ContentPackInstallation(org.graylog2.contentpacks.model.ContentPackInstallation) UnexpectedEntitiesException(org.graylog2.contentpacks.exceptions.UnexpectedEntitiesException) EntityV1(org.graylog2.contentpacks.model.entities.EntityV1) Traverser(com.google.common.graph.Traverser) NativeEntityDescriptor(org.graylog2.contentpacks.model.entities.NativeEntityDescriptor) Collections(java.util.Collections) Graph(com.google.common.graph.Graph) NativeEntity(org.graylog2.contentpacks.model.entities.NativeEntity) Entity(org.graylog2.contentpacks.model.entities.Entity) Optional(java.util.Optional) NativeEntityDescriptor(org.graylog2.contentpacks.model.entities.NativeEntityDescriptor) ValueReference(org.graylog2.contentpacks.model.entities.references.ValueReference) HashSet(java.util.HashSet)

Example 2 with NativeEntityDescriptor

use of org.graylog2.contentpacks.model.entities.NativeEntityDescriptor 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 3 with NativeEntityDescriptor

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

the class GrokPatternFacadeTest method createNativeEntity.

@Test
public void createNativeEntity() throws NotFoundException {
    final Entity grokPatternEntity = EntityV1.builder().id(ModelId.of("1")).type(ModelTypes.GROK_PATTERN_V1).data(objectMapper.convertValue(GrokPatternEntity.create("Test", "[a-z]+"), JsonNode.class)).build();
    final NativeEntity<GrokPattern> nativeEntity = facade.createNativeEntity(grokPatternEntity, Collections.emptyMap(), Collections.emptyMap(), "admin");
    final GrokPattern expectedGrokPattern = GrokPattern.create("1", "Test", "[a-z]+", null);
    final NativeEntityDescriptor expectedDescriptor = NativeEntityDescriptor.create("1", "1", ModelTypes.GROK_PATTERN_V1, "Test");
    assertThat(nativeEntity.descriptor().title()).isEqualTo(expectedDescriptor.title());
    assertThat(nativeEntity.descriptor().type()).isEqualTo(expectedDescriptor.type());
    assertThat(nativeEntity.entity()).isEqualTo(expectedGrokPattern);
    assertThat(grokPatternService.load("1")).isEqualTo(expectedGrokPattern);
}
Also used : NativeEntity(org.graylog2.contentpacks.model.entities.NativeEntity) Entity(org.graylog2.contentpacks.model.entities.Entity) GrokPatternEntity(org.graylog2.contentpacks.model.entities.GrokPatternEntity) GrokPattern(org.graylog2.grok.GrokPattern) NativeEntityDescriptor(org.graylog2.contentpacks.model.entities.NativeEntityDescriptor) Test(org.junit.Test)

Example 4 with NativeEntityDescriptor

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

the class GrokPatternFacadeTest method findExisting.

@Test
public void findExisting() throws ValidationException {
    final GrokPattern grokPattern = grokPatternService.save(GrokPattern.create("Test", "[a-z]+"));
    final Entity grokPatternEntity = EntityV1.builder().id(ModelId.of("1")).type(ModelTypes.GROK_PATTERN_V1).data(objectMapper.convertValue(GrokPatternEntity.create("Test", "[a-z]+"), JsonNode.class)).build();
    final Optional<NativeEntity<GrokPattern>> existingGrokPattern = facade.findExisting(grokPatternEntity, Collections.emptyMap());
    final NativeEntityDescriptor expectedDescriptor = NativeEntityDescriptor.create(grokPatternEntity.id(), "1", ModelTypes.GROK_PATTERN_V1, grokPattern.name(), false);
    assertThat(existingGrokPattern).isPresent().get().satisfies(nativeEntity -> {
        assertThat(nativeEntity.descriptor()).isEqualTo(expectedDescriptor);
        assertThat(nativeEntity.entity()).isEqualTo(grokPattern);
    });
}
Also used : NativeEntity(org.graylog2.contentpacks.model.entities.NativeEntity) Entity(org.graylog2.contentpacks.model.entities.Entity) GrokPatternEntity(org.graylog2.contentpacks.model.entities.GrokPatternEntity) GrokPattern(org.graylog2.grok.GrokPattern) NativeEntityDescriptor(org.graylog2.contentpacks.model.entities.NativeEntityDescriptor) NativeEntity(org.graylog2.contentpacks.model.entities.NativeEntity) Test(org.junit.Test)

Example 5 with NativeEntityDescriptor

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

the class LookupCacheFacadeTest method createNativeEntity.

@Test
public void createNativeEntity() {
    final Entity entity = EntityV1.builder().id(ModelId.of("1")).type(ModelTypes.LOOKUP_CACHE_V1).data(objectMapper.convertValue(LookupCacheEntity.create(ValueReference.of("no-op-cache"), ValueReference.of("No-op cache"), ValueReference.of("No-op cache"), ReferenceMapUtils.toReferenceMap(ImmutableMap.of("type", "none"))), JsonNode.class)).build();
    assertThat(cacheService.findAll()).isEmpty();
    final NativeEntity<CacheDto> nativeEntity = facade.createNativeEntity(entity, Collections.emptyMap(), Collections.emptyMap(), "username");
    final NativeEntityDescriptor descriptor = nativeEntity.descriptor();
    final CacheDto cacheDto = nativeEntity.entity();
    assertThat(nativeEntity.descriptor().id()).isNotNull();
    assertThat(descriptor.type()).isEqualTo(ModelTypes.LOOKUP_CACHE_V1);
    assertThat(cacheDto.name()).isEqualTo("no-op-cache");
    assertThat(cacheDto.title()).isEqualTo("No-op cache");
    assertThat(cacheDto.description()).isEqualTo("No-op cache");
    assertThat(cacheDto.config().type()).isEqualTo("none");
    assertThat(cacheService.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) NativeEntityDescriptor(org.graylog2.contentpacks.model.entities.NativeEntityDescriptor) CacheDto(org.graylog2.lookup.dto.CacheDto) Test(org.junit.Test)

Aggregations

NativeEntityDescriptor (org.graylog2.contentpacks.model.entities.NativeEntityDescriptor)12 NativeEntity (org.graylog2.contentpacks.model.entities.NativeEntity)11 Entity (org.graylog2.contentpacks.model.entities.Entity)10 Test (org.junit.Test)7 HashMap (java.util.HashMap)4 MongoDBFixtures (org.graylog.testing.mongodb.MongoDBFixtures)4 EntityWithExcerptFacade (org.graylog2.contentpacks.facades.EntityWithExcerptFacade)4 ImmutableSet (com.google.common.collect.ImmutableSet)3 LinkedHashMap (java.util.LinkedHashMap)3 ImmutableList (com.google.common.collect.ImmutableList)2 ImmutableMap (com.google.common.collect.ImmutableMap)2 Sets (com.google.common.collect.Sets)2 ElementOrder (com.google.common.graph.ElementOrder)2 Graph (com.google.common.graph.Graph)2 GraphBuilder (com.google.common.graph.GraphBuilder)2 ImmutableGraph (com.google.common.graph.ImmutableGraph)2 MutableGraph (com.google.common.graph.MutableGraph)2 Traverser (com.google.common.graph.Traverser)2 Instant (java.time.Instant)2 Collection (java.util.Collection)2