Search in sources :

Example 21 with ModelId

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

the class ContentPackResourceTest method deleteContentPack.

@Test
public void deleteContentPack() throws Exception {
    final ModelId id = ModelId.of("1");
    when(contentPackPersistenceService.deleteById(id)).thenReturn(1);
    contentPackResource.deleteContentPack(id);
    verify(contentPackPersistenceService, times(1)).deleteById(id);
    when(contentPackPersistenceService.deleteByIdAndRevision(id, 1)).thenReturn(1);
    contentPackResource.deleteContentPack(id, 1);
    verify(contentPackPersistenceService, times(1)).deleteByIdAndRevision(id, 1);
}
Also used : ModelId(org.graylog2.contentpacks.model.ModelId) Test(org.junit.Test)

Example 22 with ModelId

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

the class ContentPackResourceTest method listAndLatest.

@Test
public void listAndLatest() throws Exception {
    final ContentPack contentPack = objectMapper.readValue(CONTENT_PACK, ContentPack.class);
    final Set<ContentPack> contentPacks = Collections.singleton(contentPack);
    final Map<ModelId, Map<Integer, ContentPackMetadata>> metaDataMap = Collections.emptyMap();
    final ContentPackList expectedList = ContentPackList.create(contentPacks.size(), contentPacks, metaDataMap);
    when(contentPackPersistenceService.loadAll()).thenReturn(Collections.singleton(contentPack));
    final ContentPackList contentPackList = contentPackResource.listContentPacks();
    verify(contentPackPersistenceService, times(1)).loadAll();
    assertThat(contentPackList).isEqualTo(expectedList);
    when(contentPackPersistenceService.loadAllLatest()).thenReturn(Collections.singleton(contentPack));
    final ContentPackList contentPackLatest = contentPackResource.listLatestContentPacks();
    verify(contentPackPersistenceService, times(1)).loadAll();
    assertThat(contentPackLatest).isEqualTo(expectedList);
}
Also used : ContentPackList(org.graylog2.rest.models.system.contentpacks.responses.ContentPackList) ContentPack(org.graylog2.contentpacks.model.ContentPack) Map(java.util.Map) ModelId(org.graylog2.contentpacks.model.ModelId) Test(org.junit.Test)

Example 23 with ModelId

use of org.graylog2.contentpacks.model.ModelId 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)

Example 24 with ModelId

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

the class ViewFacade method resolveNativeEntity.

@SuppressWarnings("UnstableApiUsage")
@Override
public Graph<EntityDescriptor> resolveNativeEntity(EntityDescriptor entityDescriptor) {
    final MutableGraph<EntityDescriptor> mutableGraph = GraphBuilder.directed().build();
    mutableGraph.addNode(entityDescriptor);
    final ModelId modelId = entityDescriptor.id();
    final ViewSummaryDTO viewSummaryDTO = viewSummaryService.get(modelId.id()).orElseThrow(() -> new NoSuchElementException("Could not find view with id " + modelId.id()));
    final Search search = searchDbService.get(viewSummaryDTO.searchId()).orElseThrow(() -> new NoSuchElementException("Could not find search with id " + viewSummaryDTO.searchId()));
    search.usedStreamIds().stream().map(s -> EntityDescriptor.create(s, ModelTypes.STREAM_V1)).forEach(streamDescriptor -> mutableGraph.putEdge(entityDescriptor, streamDescriptor));
    return ImmutableGraph.copyOf(mutableGraph);
}
Also used : EntityDescriptorIds(org.graylog2.contentpacks.EntityDescriptorIds) ImmutableGraph(com.google.common.graph.ImmutableGraph) NativeEntity(org.graylog2.contentpacks.model.entities.NativeEntity) LoggerFactory(org.slf4j.LoggerFactory) Entity(org.graylog2.contentpacks.model.entities.Entity) ModelType(org.graylog2.contentpacks.model.ModelType) ViewDTO(org.graylog.plugins.views.search.views.ViewDTO) ValueReference(org.graylog2.contentpacks.model.entities.references.ValueReference) Inject(javax.inject.Inject) LinkedHashMap(java.util.LinkedHashMap) SearchEntity(org.graylog2.contentpacks.model.entities.SearchEntity) ViewStateEntity(org.graylog2.contentpacks.model.entities.ViewStateEntity) EntityExcerpt(org.graylog2.contentpacks.model.entities.EntityExcerpt) Map(java.util.Map) JsonNode(com.fasterxml.jackson.databind.JsonNode) Search(org.graylog.plugins.views.search.Search) ViewSummaryService(org.graylog.plugins.views.search.views.ViewSummaryService) NoSuchElementException(java.util.NoSuchElementException) Logger(org.slf4j.Logger) ModelId(org.graylog2.contentpacks.model.ModelId) MutableGraph(com.google.common.graph.MutableGraph) EntityDescriptor(org.graylog2.contentpacks.model.entities.EntityDescriptor) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Set(java.util.Set) ViewSummaryDTO(org.graylog.plugins.views.search.views.ViewSummaryDTO) Collectors(java.util.stream.Collectors) GraphBuilder(com.google.common.graph.GraphBuilder) ViewEntity(org.graylog2.contentpacks.model.entities.ViewEntity) EntityV1(org.graylog2.contentpacks.model.entities.EntityV1) Objects(java.util.Objects) Stream(java.util.stream.Stream) UserService(org.graylog2.shared.users.UserService) ViewStateDTO(org.graylog.plugins.views.search.views.ViewStateDTO) Optional(java.util.Optional) ViewService(org.graylog.plugins.views.search.views.ViewService) SearchDbService(org.graylog.plugins.views.search.db.SearchDbService) User(org.graylog2.plugin.database.users.User) NativeEntityDescriptor(org.graylog2.contentpacks.model.entities.NativeEntityDescriptor) Graph(com.google.common.graph.Graph) ModelTypes(org.graylog2.contentpacks.model.ModelTypes) EntityDescriptor(org.graylog2.contentpacks.model.entities.EntityDescriptor) NativeEntityDescriptor(org.graylog2.contentpacks.model.entities.NativeEntityDescriptor) Search(org.graylog.plugins.views.search.Search) ViewSummaryDTO(org.graylog.plugins.views.search.views.ViewSummaryDTO) ModelId(org.graylog2.contentpacks.model.ModelId) NoSuchElementException(java.util.NoSuchElementException)

Example 25 with ModelId

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

the class ViewFacade method exportEntity.

@Override
public Optional<Entity> exportEntity(EntityDescriptor entityDescriptor, EntityDescriptorIds entityDescriptorIds) {
    final ModelId modelId = entityDescriptor.id();
    final Optional<ViewDTO> optionalView = viewService.get(modelId.id());
    if (optionalView.isPresent()) {
        return Optional.of(exportNativeEntity(optionalView.get(), entityDescriptorIds));
    }
    LOG.debug("Couldn't find view {}", entityDescriptor);
    return Optional.empty();
}
Also used : ViewDTO(org.graylog.plugins.views.search.views.ViewDTO) 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