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);
}
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);
}
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();
}
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);
}
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();
}
Aggregations