Search in sources :

Example 6 with ModelId

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

the class ContentPackPersistenceService method loadAllLatest.

public Set<ContentPack> loadAllLatest() {
    final Set<ContentPack> allContentPacks = loadAll();
    final ImmutableMultimap.Builder<ModelId, ContentPack> byIdBuilder = ImmutableMultimap.builder();
    for (ContentPack contentPack : allContentPacks) {
        byIdBuilder.put(contentPack.id(), contentPack);
    }
    final ImmutableMultimap<ModelId, ContentPack> contentPacksById = byIdBuilder.build();
    final ImmutableSet.Builder<ContentPack> latestContentPacks = ImmutableSet.builderWithExpectedSize(contentPacksById.keySet().size());
    for (ModelId id : contentPacksById.keySet()) {
        final ImmutableCollection<ContentPack> contentPacks = contentPacksById.get(id);
        final ContentPack latestContentPackRevision = Collections.max(contentPacks, Comparator.comparingInt(Revisioned::revision));
        latestContentPacks.add(latestContentPackRevision);
    }
    return latestContentPacks.build();
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) ContentPack(org.graylog2.contentpacks.model.ContentPack) ImmutableMultimap(com.google.common.collect.ImmutableMultimap) ModelId(org.graylog2.contentpacks.model.ModelId)

Example 7 with ModelId

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

the class ContentPackInstallationPersistenceService method getInstallationMetadata.

public Map<ModelId, Map<Integer, ContentPackMetadata>> getInstallationMetadata(Set<ModelId> ids) {
    final Set<ContentPackInstallation> contentPackInstallations = findByContentPackIds(ids);
    Map<ModelId, Map<Integer, ContentPackMetadata>> installationMetaData = new HashMap<>();
    for (ContentPackInstallation installation : contentPackInstallations) {
        Map<Integer, ContentPackMetadata> metadataMap = installationMetaData.get(installation.contentPackId());
        if (metadataMap == null) {
            metadataMap = new HashMap<>();
        }
        ContentPackMetadata metadata = metadataMap.get(installation.contentPackRevision());
        int count = 1;
        if (metadata != null) {
            count = metadata.installationCount() + 1;
        }
        ContentPackMetadata newMetadata = ContentPackMetadata.create(count);
        metadataMap.put(installation.contentPackRevision(), newMetadata);
        installationMetaData.put(installation.contentPackId(), metadataMap);
    }
    return installationMetaData;
}
Also used : ContentPackInstallation(org.graylog2.contentpacks.model.ContentPackInstallation) HashMap(java.util.HashMap) HashMap(java.util.HashMap) Map(java.util.Map) ModelId(org.graylog2.contentpacks.model.ModelId) ContentPackMetadata(org.graylog2.rest.models.system.contentpacks.responses.ContentPackMetadata)

Example 8 with ModelId

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

the class ContentPackResource method deleteContentPackInstallationById.

@DELETE
@Path("{contentPackId}/installations/{installationId}")
@Timed
@ApiOperation(value = "Uninstall a content pack installation")
@ApiResponses(value = { @ApiResponse(code = 500, message = "Error loading content packs") })
@AuditEvent(type = AuditEventTypes.CONTENT_PACK_UNINSTALL)
@JsonView(ContentPackView.HttpView.class)
public Response deleteContentPackInstallationById(@ApiParam(name = "contentPackId", value = "Content pack ID", required = true) @PathParam("contentPackId") ModelId contentPackId, @ApiParam(name = "installationId", value = "Installation ID", required = true) @PathParam("installationId") String installationId) {
    checkPermission(RestPermissions.CONTENT_PACK_UNINSTALL, contentPackId.toString());
    final ContentPackInstallation installation = contentPackInstallationPersistenceService.findById(new ObjectId(installationId)).orElseThrow(() -> new NotFoundException("Couldn't find installation " + installationId));
    final ContentPack contentPack = contentPackPersistenceService.findByIdAndRevision(installation.contentPackId(), installation.contentPackRevision()).orElseThrow(() -> new NotFoundException("Couldn't find content pack " + installation.contentPackId() + " rev " + installation.contentPackRevision()));
    final ContentPackUninstallation removedInstallation = contentPackService.uninstallContentPack(contentPack, installation);
    return Response.ok(ImmutableMap.of("content_pack", contentPack, "uninstalled", removedInstallation)).build();
}
Also used : ContentPackInstallation(org.graylog2.contentpacks.model.ContentPackInstallation) ObjectId(org.bson.types.ObjectId) ContentPackUninstallation(org.graylog2.contentpacks.model.ContentPackUninstallation) ContentPack(org.graylog2.contentpacks.model.ContentPack) NotFoundException(javax.ws.rs.NotFoundException) Path(javax.ws.rs.Path) DELETE(javax.ws.rs.DELETE) Timed(com.codahale.metrics.annotation.Timed) ApiOperation(io.swagger.annotations.ApiOperation) JsonView(com.fasterxml.jackson.annotation.JsonView) AuditEvent(org.graylog2.audit.jersey.AuditEvent) ApiResponses(io.swagger.annotations.ApiResponses)

Example 9 with ModelId

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

the class ContentPackResource method getContentPackRevisions.

@GET
@Path("{contentPackId}/{revision}")
@Timed
@ApiOperation(value = "Get a revision of a content pack")
@ApiResponses(value = { @ApiResponse(code = 500, message = "Error loading content packs") })
@JsonView(ContentPackView.HttpView.class)
public ContentPackResponse getContentPackRevisions(@ApiParam(name = "contentPackId", value = "Content pack ID", required = true) @PathParam("contentPackId") ModelId id, @ApiParam(name = "revision", value = "Content pack revision", required = true) @PathParam("revision") int revision) {
    checkPermission(RestPermissions.CONTENT_PACK_READ);
    ContentPack contentPack = contentPackPersistenceService.findByIdAndRevision(id, revision).orElseThrow(() -> new NotFoundException("Content pack " + id + " with revision " + revision + " not found!"));
    Set<ConstraintCheckResult> constraints = contentPackService.checkConstraints(contentPack);
    return ContentPackResponse.create(contentPack, constraints);
}
Also used : ConstraintCheckResult(org.graylog2.contentpacks.model.constraints.ConstraintCheckResult) ContentPack(org.graylog2.contentpacks.model.ContentPack) NotFoundException(javax.ws.rs.NotFoundException) Path(javax.ws.rs.Path) Timed(com.codahale.metrics.annotation.Timed) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation) JsonView(com.fasterxml.jackson.annotation.JsonView) ApiResponses(io.swagger.annotations.ApiResponses)

Example 10 with ModelId

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

the class PipelineFacade method resolveForInstallation.

private Graph<Entity> resolveForInstallation(EntityV1 entity, Map<String, ValueReference> parameters, Map<EntityDescriptor, Entity> entities) {
    final MutableGraph<Entity> mutableGraph = GraphBuilder.directed().build();
    mutableGraph.addNode(entity);
    final PipelineEntity pipelineEntity = objectMapper.convertValue(entity.data(), PipelineEntity.class);
    final String source = pipelineEntity.source().asString(parameters);
    final Collection<String> referencedRules = referencedRules(source);
    referencedRules.stream().map(ModelId::of).map(modelId -> EntityDescriptor.create(modelId, ModelTypes.PIPELINE_RULE_V1)).map(entities::get).filter(Objects::nonNull).forEach(ruleEntity -> mutableGraph.putEdge(entity, ruleEntity));
    pipelineEntity.connectedStreams().stream().map(valueReference -> valueReference.asString(parameters)).map(ModelId::of).map(modelId -> EntityDescriptor.create(modelId, ModelTypes.STREAM_V1)).map(entities::get).filter(Objects::nonNull).forEach(streamEntity -> mutableGraph.putEdge(entity, streamEntity));
    return ImmutableGraph.copyOf(mutableGraph);
}
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) NativeEntity(org.graylog2.contentpacks.model.entities.NativeEntity) Entity(org.graylog2.contentpacks.model.entities.Entity) PipelineEntity(org.graylog2.contentpacks.model.entities.PipelineEntity) PipelineEntity(org.graylog2.contentpacks.model.entities.PipelineEntity) ModelId(org.graylog2.contentpacks.model.ModelId)

Aggregations

ModelId (org.graylog2.contentpacks.model.ModelId)31 EntityDescriptor (org.graylog2.contentpacks.model.entities.EntityDescriptor)16 NativeEntityDescriptor (org.graylog2.contentpacks.model.entities.NativeEntityDescriptor)16 Entity (org.graylog2.contentpacks.model.entities.Entity)11 EntityV1 (org.graylog2.contentpacks.model.entities.EntityV1)11 NativeEntity (org.graylog2.contentpacks.model.entities.NativeEntity)11 Map (java.util.Map)10 Set (java.util.Set)10 EntityDescriptorIds (org.graylog2.contentpacks.EntityDescriptorIds)10 Test (org.junit.Test)10 Optional (java.util.Optional)9 ContentPack (org.graylog2.contentpacks.model.ContentPack)9 Collectors (java.util.stream.Collectors)8 Inject (javax.inject.Inject)8 ContentPackInstallation (org.graylog2.contentpacks.model.ContentPackInstallation)8 NotFoundException (org.graylog2.database.NotFoundException)8 JsonNode (com.fasterxml.jackson.databind.JsonNode)7 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)7 Graph (com.google.common.graph.Graph)7 GraphBuilder (com.google.common.graph.GraphBuilder)7