Search in sources :

Example 1 with ImmutableGraph

use of com.google.common.graph.ImmutableGraph 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 ImmutableGraph

use of com.google.common.graph.ImmutableGraph in project graylog2-server by Graylog2.

the class ContentPackService method buildEntityGraph.

private ImmutableGraph<Entity> buildEntityGraph(Entity rootEntity, Set<Entity> entities, Map<String, ValueReference> parameters) {
    final Map<EntityDescriptor, Entity> entityDescriptorMap = entities.stream().collect(Collectors.toMap(Entity::toEntityDescriptor, Function.identity()));
    final MutableGraph<Entity> dependencyGraph = GraphBuilder.directed().allowsSelfLoops(false).expectedNodeCount(entities.size()).build();
    for (Map.Entry<EntityDescriptor, Entity> entry : entityDescriptorMap.entrySet()) {
        final EntityDescriptor entityDescriptor = entry.getKey();
        final Entity entity = entry.getValue();
        final EntityWithExcerptFacade<?, ?> facade = entityFacades.getOrDefault(entity.type(), UnsupportedEntityFacade.INSTANCE);
        final Graph<Entity> entityGraph = facade.resolveForInstallation(entity, parameters, entityDescriptorMap);
        LOG.trace("Dependencies of entity {}: {}", entityDescriptor, entityGraph);
        dependencyGraph.putEdge(rootEntity, entity);
        Graphs.merge(dependencyGraph, entityGraph);
        LOG.trace("New dependency graph: {}", dependencyGraph);
    }
    final Set<Entity> unexpectedEntities = dependencyGraph.nodes().stream().filter(entity -> !rootEntity.equals(entity)).filter(entity -> !entities.contains(entity)).collect(Collectors.toSet());
    if (!unexpectedEntities.isEmpty()) {
        throw new UnexpectedEntitiesException(unexpectedEntities);
    }
    return ImmutableGraph.copyOf(dependencyGraph);
}
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) EntityDescriptor(org.graylog2.contentpacks.model.entities.EntityDescriptor) NativeEntityDescriptor(org.graylog2.contentpacks.model.entities.NativeEntityDescriptor) NativeEntity(org.graylog2.contentpacks.model.entities.NativeEntity) Entity(org.graylog2.contentpacks.model.entities.Entity) UnexpectedEntitiesException(org.graylog2.contentpacks.exceptions.UnexpectedEntitiesException) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap)

Example 3 with ImmutableGraph

use of com.google.common.graph.ImmutableGraph in project graylog2-server by Graylog2.

the class ContentPackService method uninstallContentPack.

private ContentPackUninstallation uninstallContentPack(ContentPackInstallation installation, ContentPackV1 contentPack) {
    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> removedEntities = new HashSet<>();
    final Set<NativeEntityDescriptor> failedEntities = new HashSet<>();
    final Set<NativeEntityDescriptor> skippedEntities = new HashSet<>();
    try {
        for (Entity entity : entitiesInOrder) {
            if (entity.equals(rootEntity)) {
                continue;
            }
            final Optional<NativeEntityDescriptor> nativeEntityDescriptorOptional = installation.entities().stream().filter(descriptor -> entity.id().equals(descriptor.contentPackEntityId())).findFirst();
            final EntityWithExcerptFacade facade = entityFacades.getOrDefault(entity.type(), UnsupportedEntityFacade.INSTANCE);
            if (nativeEntityDescriptorOptional.isPresent()) {
                final NativeEntityDescriptor nativeEntityDescriptor = nativeEntityDescriptorOptional.get();
                final Optional nativeEntityOptional = facade.loadNativeEntity(nativeEntityDescriptor);
                final ModelId entityId = nativeEntityDescriptor.id();
                final long installCount = contentPackInstallationPersistenceService.countInstallationOfEntityById(entityId);
                final long systemFoundCount = contentPackInstallationPersistenceService.countInstallationOfEntityByIdAndFoundOnSystem(entityId);
                if (installCount > 1 || (installCount == 1 && systemFoundCount >= 1)) {
                    skippedEntities.add(nativeEntityDescriptor);
                    LOG.debug("Did not remove entity since other content pack installations still use them: {}", nativeEntityDescriptor);
                } else if (nativeEntityOptional.isPresent()) {
                    final Object nativeEntity = nativeEntityOptional.get();
                    LOG.trace("Removing existing native entity for {} ({})", nativeEntityDescriptor);
                    try {
                        // The EntityFacade#delete() method expects the actual entity object
                        // noinspection unchecked
                        facade.delete(((NativeEntity) nativeEntity).entity());
                        removedEntities.add(nativeEntityDescriptor);
                    } catch (Exception e) {
                        LOG.warn("Couldn't remove native entity {}", nativeEntity);
                        failedEntities.add(nativeEntityDescriptor);
                    }
                } else {
                    LOG.trace("Couldn't find existing native entity for {} ({})", nativeEntityDescriptor);
                }
            }
        }
    } catch (Exception e) {
        throw new ContentPackException("Failed to remove content pack <" + contentPack.id() + "/" + contentPack.revision() + ">", e);
    }
    final int deletedInstallations = contentPackInstallationPersistenceService.deleteById(installation.id());
    LOG.debug("Deleted {} installation(s) of content pack {}", deletedInstallations, contentPack.id());
    return ContentPackUninstallation.builder().entities(ImmutableSet.copyOf(removedEntities)).skippedEntities(ImmutableSet.copyOf(skippedEntities)).failedEntities(ImmutableSet.copyOf(failedEntities)).build();
}
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) ContentPackException(org.graylog2.contentpacks.exceptions.ContentPackException) NativeEntity(org.graylog2.contentpacks.model.entities.NativeEntity) Entity(org.graylog2.contentpacks.model.entities.Entity) Optional(java.util.Optional) FailedConstraintsException(org.graylog2.contentpacks.exceptions.FailedConstraintsException) InvalidParameterTypeException(org.graylog2.contentpacks.exceptions.InvalidParameterTypeException) MissingParametersException(org.graylog2.contentpacks.exceptions.MissingParametersException) InvalidParametersException(org.graylog2.contentpacks.exceptions.InvalidParametersException) EmptyDefaultValueException(org.graylog2.contentpacks.exceptions.EmptyDefaultValueException) ContentPackException(org.graylog2.contentpacks.exceptions.ContentPackException) UnexpectedEntitiesException(org.graylog2.contentpacks.exceptions.UnexpectedEntitiesException) Constraint(org.graylog2.contentpacks.model.constraints.Constraint) NativeEntityDescriptor(org.graylog2.contentpacks.model.entities.NativeEntityDescriptor) EntityWithExcerptFacade(org.graylog2.contentpacks.facades.EntityWithExcerptFacade) NativeEntity(org.graylog2.contentpacks.model.entities.NativeEntity) ModelId(org.graylog2.contentpacks.model.ModelId) ValueReference(org.graylog2.contentpacks.model.entities.references.ValueReference) HashSet(java.util.HashSet)

Aggregations

ImmutableList (com.google.common.collect.ImmutableList)3 ImmutableMap (com.google.common.collect.ImmutableMap)3 ImmutableSet (com.google.common.collect.ImmutableSet)3 Sets (com.google.common.collect.Sets)3 ElementOrder (com.google.common.graph.ElementOrder)3 Graph (com.google.common.graph.Graph)3 GraphBuilder (com.google.common.graph.GraphBuilder)3 ImmutableGraph (com.google.common.graph.ImmutableGraph)3 MutableGraph (com.google.common.graph.MutableGraph)3 Traverser (com.google.common.graph.Traverser)3 Instant (java.time.Instant)3 Collection (java.util.Collection)3 Collections (java.util.Collections)3 HashMap (java.util.HashMap)3 HashSet (java.util.HashSet)3 LinkedHashMap (java.util.LinkedHashMap)3 Map (java.util.Map)3 Optional (java.util.Optional)3 Set (java.util.Set)3 Function (java.util.function.Function)3