Search in sources :

Example 1 with EntityWithExcerptFacade

use of org.graylog2.contentpacks.facades.EntityWithExcerptFacade in project graylog2-server by Graylog2.

the class ContentPackService method resolveDependencyGraph.

private MutableGraph<EntityDescriptor> resolveDependencyGraph(Graph<EntityDescriptor> dependencyGraph, Set<EntityDescriptor> resolvedEntities) {
    final MutableGraph<EntityDescriptor> mutableGraph = GraphBuilder.from(dependencyGraph).build();
    Graphs.merge(mutableGraph, dependencyGraph);
    for (EntityDescriptor entityDescriptor : dependencyGraph.nodes()) {
        LOG.debug("Resolving entity {}", entityDescriptor);
        if (resolvedEntities.contains(entityDescriptor)) {
            LOG.debug("Entity {} already resolved, skipping.", entityDescriptor);
            continue;
        }
        final EntityWithExcerptFacade<?, ?> facade = entityFacades.getOrDefault(entityDescriptor.type(), UnsupportedEntityFacade.INSTANCE);
        final Graph<EntityDescriptor> graph = facade.resolveNativeEntity(entityDescriptor);
        LOG.trace("Dependencies of entity {}: {}", entityDescriptor, graph);
        Graphs.merge(mutableGraph, graph);
        LOG.trace("New dependency graph: {}", mutableGraph);
        resolvedEntities.add(entityDescriptor);
        final Graph<EntityDescriptor> result = resolveDependencyGraph(mutableGraph, resolvedEntities);
        Graphs.merge(mutableGraph, result);
    }
    return mutableGraph;
}
Also used : EntityDescriptor(org.graylog2.contentpacks.model.entities.EntityDescriptor) NativeEntityDescriptor(org.graylog2.contentpacks.model.entities.NativeEntityDescriptor)

Example 2 with EntityWithExcerptFacade

use of org.graylog2.contentpacks.facades.EntityWithExcerptFacade in project graylog2-server by Graylog2.

the class ContentPackService method collectEntities.

public ImmutableSet<Entity> collectEntities(Collection<EntityDescriptor> resolvedEntities) {
    // It's important to only compute the EntityDescriptor IDs once per #collectEntities call! Otherwise we
    // will get broken references between the entities.
    final EntityDescriptorIds entityDescriptorIds = EntityDescriptorIds.of(resolvedEntities);
    final ImmutableSet.Builder<Entity> entities = ImmutableSet.builder();
    for (EntityDescriptor entityDescriptor : resolvedEntities) {
        if (EntityDescriptorIds.isSystemStreamDescriptor(entityDescriptor)) {
            continue;
        }
        final EntityWithExcerptFacade<?, ?> facade = entityFacades.getOrDefault(entityDescriptor.type(), UnsupportedEntityFacade.INSTANCE);
        facade.exportEntity(entityDescriptor, entityDescriptorIds).ifPresent(entities::add);
    }
    return entities.build();
}
Also used : NativeEntity(org.graylog2.contentpacks.model.entities.NativeEntity) Entity(org.graylog2.contentpacks.model.entities.Entity) EntityDescriptor(org.graylog2.contentpacks.model.entities.EntityDescriptor) NativeEntityDescriptor(org.graylog2.contentpacks.model.entities.NativeEntityDescriptor) ImmutableSet(com.google.common.collect.ImmutableSet)

Example 3 with EntityWithExcerptFacade

use of org.graylog2.contentpacks.facades.EntityWithExcerptFacade in project graylog2-server by Graylog2.

the class ContentPackServiceTest method setUp.

@Before
public void setUp() throws Exception {
    final ContentPackInstallationPersistenceService contentPackInstallationPersistenceService = contentPackInstallService;
    final Set<ConstraintChecker> constraintCheckers = Collections.emptySet();
    pluginMetaData = new HashSet<>();
    outputFactories = new HashMap<>();
    outputFactories2 = new HashMap<>();
    final Map<ModelType, EntityWithExcerptFacade<?, ?>> entityFacades = ImmutableMap.of(ModelTypes.GROK_PATTERN_V1, new GrokPatternFacade(objectMapper, patternService), ModelTypes.STREAM_V1, new StreamFacade(objectMapper, streamService, streamRuleService, alertService, alarmCallbackConfigurationService, legacyAlertConditionMigration, indexSetService, userService), ModelTypes.OUTPUT_V1, new OutputFacade(objectMapper, outputService, pluginMetaData, outputFactories, outputFactories2));
    contentPackService = new ContentPackService(contentPackInstallationPersistenceService, constraintCheckers, entityFacades);
    Map<String, String> entityData = new HashMap<>(2);
    entityData.put("name", "NAME");
    entityData.put("pattern", "\\w");
    grokPattern = GrokPattern.builder().pattern("\\w").name("NAME").build();
    JsonNode jsonData = objectMapper.convertValue(entityData, JsonNode.class);
    EntityV1 entityV1 = EntityV1.builder().id(ModelId.of("12345")).type(ModelTypes.GROK_PATTERN_V1).data(jsonData).build();
    ImmutableSet<Entity> entities = ImmutableSet.of(entityV1);
    NativeEntityDescriptor nativeEntityDescriptor = NativeEntityDescriptor.create(ModelId.of("12345"), "dead-beef1", ModelTypes.GROK_PATTERN_V1, "NAME");
    nativeEntityDescriptors = ImmutableSet.of(nativeEntityDescriptor);
    contentPack = ContentPackV1.builder().description("test").entities(entities).name("test").revision(1).summary("").vendor("").url(URI.create("http://graylog.com")).id(ModelId.of("dead-beef")).build();
    contentPackInstallation = ContentPackInstallation.builder().contentPackId(ModelId.of("dead-beef")).contentPackRevision(1).entities(nativeEntityDescriptors).comment("Installed").parameters(ImmutableMap.copyOf(Collections.emptyMap())).createdAt(Instant.now()).createdBy("me").build();
}
Also used : Entity(org.graylog2.contentpacks.model.entities.Entity) StreamFacade(org.graylog2.contentpacks.facades.StreamFacade) HashMap(java.util.HashMap) JsonNode(com.fasterxml.jackson.databind.JsonNode) EntityV1(org.graylog2.contentpacks.model.entities.EntityV1) EntityWithExcerptFacade(org.graylog2.contentpacks.facades.EntityWithExcerptFacade) NativeEntityDescriptor(org.graylog2.contentpacks.model.entities.NativeEntityDescriptor) ModelType(org.graylog2.contentpacks.model.ModelType) ConstraintChecker(org.graylog2.contentpacks.constraints.ConstraintChecker) GrokPatternFacade(org.graylog2.contentpacks.facades.GrokPatternFacade) OutputFacade(org.graylog2.contentpacks.facades.OutputFacade) Before(org.junit.Before)

Example 4 with EntityWithExcerptFacade

use of org.graylog2.contentpacks.facades.EntityWithExcerptFacade in project graylog2-server by Graylog2.

the class CatalogResourceTest method setUp.

@Before
public void setUp() {
    final ContentPackInstallationPersistenceService contentPackInstallationPersistenceService = mock(ContentPackInstallationPersistenceService.class);
    final Set<ConstraintChecker> constraintCheckers = Collections.emptySet();
    final Map<ModelType, EntityWithExcerptFacade<?, ?>> entityFacades = Collections.singletonMap(ModelType.of("test", "1"), mockEntityFacade);
    contentPackService = new ContentPackService(contentPackInstallationPersistenceService, constraintCheckers, entityFacades);
    catalogResource = new CatalogResource(contentPackService);
}
Also used : ContentPackService(org.graylog2.contentpacks.ContentPackService) ContentPackInstallationPersistenceService(org.graylog2.contentpacks.ContentPackInstallationPersistenceService) EntityWithExcerptFacade(org.graylog2.contentpacks.facades.EntityWithExcerptFacade) ModelType(org.graylog2.contentpacks.model.ModelType) ConstraintChecker(org.graylog2.contentpacks.constraints.ConstraintChecker) Before(org.junit.Before)

Example 5 with EntityWithExcerptFacade

use of org.graylog2.contentpacks.facades.EntityWithExcerptFacade 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)

Aggregations

NativeEntityDescriptor (org.graylog2.contentpacks.model.entities.NativeEntityDescriptor)7 EntityWithExcerptFacade (org.graylog2.contentpacks.facades.EntityWithExcerptFacade)6 EntityDescriptor (org.graylog2.contentpacks.model.entities.EntityDescriptor)6 HashMap (java.util.HashMap)5 Entity (org.graylog2.contentpacks.model.entities.Entity)5 ImmutableSet (com.google.common.collect.ImmutableSet)4 LinkedHashMap (java.util.LinkedHashMap)4 ConstraintChecker (org.graylog2.contentpacks.constraints.ConstraintChecker)4 ModelType (org.graylog2.contentpacks.model.ModelType)4 NativeEntity (org.graylog2.contentpacks.model.entities.NativeEntity)4 ImmutableMap (com.google.common.collect.ImmutableMap)3 Map (java.util.Map)3 ContentPackException (org.graylog2.contentpacks.exceptions.ContentPackException)3 EmptyDefaultValueException (org.graylog2.contentpacks.exceptions.EmptyDefaultValueException)3 FailedConstraintsException (org.graylog2.contentpacks.exceptions.FailedConstraintsException)3 InvalidParameterTypeException (org.graylog2.contentpacks.exceptions.InvalidParameterTypeException)3 InvalidParametersException (org.graylog2.contentpacks.exceptions.InvalidParametersException)3 MissingParametersException (org.graylog2.contentpacks.exceptions.MissingParametersException)3 UnexpectedEntitiesException (org.graylog2.contentpacks.exceptions.UnexpectedEntitiesException)3 ContentPackInstallation (org.graylog2.contentpacks.model.ContentPackInstallation)3