Search in sources :

Example 11 with ContentPack

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

the class ContentPackPersistenceServiceTest method insert.

@Test
public void insert() {
    final ContentPackV1 contentPack = ContentPackV1.builder().id(ModelId.of("id")).revision(1).name("name").description("description").summary("summary").vendor("vendor").url(URI.create("https://www.graylog.org/")).entities(ImmutableSet.of()).build();
    final Optional<ContentPack> savedContentPack = contentPackPersistenceService.insert(contentPack);
    assertThat(savedContentPack).isPresent().get().isEqualToIgnoringGivenFields(contentPack, "_id");
}
Also used : ContentPackV1(org.graylog2.contentpacks.model.ContentPackV1) ContentPack(org.graylog2.contentpacks.model.ContentPack) Test(org.junit.Test)

Example 12 with ContentPack

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

the class ContentPackInstallationPersistenceServiceTest method findByContentPackIdAndRevision.

@Test
@MongoDBFixtures("ContentPackInstallationPersistenceServiceTest.json")
public void findByContentPackIdAndRevision() {
    final ModelId id = ModelId.of("4e3d7025-881e-6870-da03-cafebabe0001");
    final Set<ContentPackInstallation> contentPack = persistenceService.findByContentPackIdAndRevision(id, 1);
    assertThat(contentPack).hasSize(1).anySatisfy(c -> assertThat(c.contentPackId()).isEqualTo(id));
}
Also used : ContentPackInstallation(org.graylog2.contentpacks.model.ContentPackInstallation) ModelId(org.graylog2.contentpacks.model.ModelId) MongoDBFixtures(org.graylog.testing.mongodb.MongoDBFixtures) Test(org.junit.Test)

Example 13 with ContentPack

use of org.graylog2.contentpacks.model.ContentPack 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 14 with ContentPack

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

the class ContentPackResourceTest method getContentPack.

@Test
public void getContentPack() throws Exception {
    final ContentPack contentPack = objectMapper.readValue(CONTENT_PACK, ContentPack.class);
    final Set<ContentPack> contentPackSet = Collections.singleton(contentPack);
    final Set<ConstraintCheckResult> constraints = Collections.emptySet();
    final Map<Integer, ContentPack> contentPacks = Collections.singletonMap(1, contentPack);
    final Map<Integer, Set<ConstraintCheckResult>> constraintMap = Collections.singletonMap(1, constraints);
    final ContentPackRevisions expectedRevisions = ContentPackRevisions.create(contentPacks, constraintMap);
    final ModelId id = ModelId.of("1");
    when(contentPackPersistenceService.findAllById(id)).thenReturn(contentPackSet);
    final ContentPackRevisions contentPackRevisions = contentPackResource.listContentPackRevisions(id);
    verify(contentPackPersistenceService, times(1)).findAllById(id);
    assertThat(contentPackRevisions).isEqualTo(expectedRevisions);
    when(contentPackPersistenceService.findByIdAndRevision(id, 1)).thenReturn(Optional.ofNullable(contentPack));
    final ContentPackResponse contentPackResponse = contentPackResource.getContentPackRevisions(id, 1);
    verify(contentPackPersistenceService, times(1)).findByIdAndRevision(id, 1);
    assertThat(contentPackResponse.contentPack()).isEqualTo(contentPack);
}
Also used : ConstraintCheckResult(org.graylog2.contentpacks.model.constraints.ConstraintCheckResult) Set(java.util.Set) ContentPackRevisions(org.graylog2.rest.models.system.contentpacks.responses.ContentPackRevisions) ContentPack(org.graylog2.contentpacks.model.ContentPack) ContentPackResponse(org.graylog2.rest.models.system.contentpacks.responses.ContentPackResponse) ModelId(org.graylog2.contentpacks.model.ModelId) Test(org.junit.Test)

Example 15 with ContentPack

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

the class ContentPackService method installContentPack.

private ContentPackInstallation installContentPack(ContentPackV1 contentPack, Map<String, ValueReference> parameters, String comment, String user) {
    ensureConstraints(contentPack.constraints());
    final Entity rootEntity = EntityV1.createRoot(contentPack);
    final ImmutableMap<String, ValueReference> validatedParameters = validateParameters(parameters, contentPack.parameters());
    final ImmutableGraph<Entity> dependencyGraph = buildEntityGraph(rootEntity, contentPack.entities(), validatedParameters);
    final Traverser<Entity> entityTraverser = Traverser.forGraph(dependencyGraph);
    final Iterable<Entity> entitiesInOrder = entityTraverser.depthFirstPostOrder(rootEntity);
    // Insertion order is important for created entities so we can roll back in order!
    final Map<EntityDescriptor, Object> createdEntities = new LinkedHashMap<>();
    final Map<EntityDescriptor, Object> allEntities = new HashMap<>();
    final ImmutableSet.Builder<NativeEntityDescriptor> allEntityDescriptors = ImmutableSet.builder();
    try {
        for (Entity entity : entitiesInOrder) {
            if (entity.equals(rootEntity)) {
                continue;
            }
            final EntityDescriptor entityDescriptor = entity.toEntityDescriptor();
            final EntityWithExcerptFacade facade = entityFacades.getOrDefault(entity.type(), UnsupportedEntityFacade.INSTANCE);
            @SuppressWarnings({ "rawtypes", "unchecked" }) final Optional<NativeEntity> existingEntity = facade.findExisting(entity, parameters);
            if (existingEntity.isPresent()) {
                LOG.trace("Found existing entity for {}", entityDescriptor);
                final NativeEntity<?> nativeEntity = existingEntity.get();
                final NativeEntityDescriptor nativeEntityDescriptor = nativeEntity.descriptor();
                /* Found entity on the system or we found a other installation which stated that */
                if (contentPackInstallationPersistenceService.countInstallationOfEntityById(nativeEntityDescriptor.id()) <= 0 || contentPackInstallationPersistenceService.countInstallationOfEntityByIdAndFoundOnSystem(nativeEntityDescriptor.id()) > 0) {
                    final NativeEntityDescriptor serverDescriptor = nativeEntityDescriptor.toBuilder().foundOnSystem(true).build();
                    allEntityDescriptors.add(serverDescriptor);
                } else {
                    allEntityDescriptors.add(nativeEntity.descriptor());
                }
                allEntities.put(entityDescriptor, nativeEntity.entity());
            } else {
                LOG.trace("Creating new entity for {}", entityDescriptor);
                final NativeEntity<?> createdEntity = facade.createNativeEntity(entity, validatedParameters, allEntities, user);
                allEntityDescriptors.add(createdEntity.descriptor());
                createdEntities.put(entityDescriptor, createdEntity.entity());
                allEntities.put(entityDescriptor, createdEntity.entity());
            }
        }
    } catch (Exception e) {
        rollback(createdEntities);
        throw new ContentPackException("Failed to install content pack <" + contentPack.id() + "/" + contentPack.revision() + ">", e);
    }
    final ContentPackInstallation installation = ContentPackInstallation.builder().contentPackId(contentPack.id()).contentPackRevision(contentPack.revision()).parameters(validatedParameters).comment(comment).entities(allEntityDescriptors.build()).createdAt(Instant.now()).createdBy(user).build();
    return contentPackInstallationPersistenceService.insert(installation);
}
Also used : ContentPackException(org.graylog2.contentpacks.exceptions.ContentPackException) NativeEntity(org.graylog2.contentpacks.model.entities.NativeEntity) Entity(org.graylog2.contentpacks.model.entities.Entity) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) 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) LinkedHashMap(java.util.LinkedHashMap) EntityDescriptor(org.graylog2.contentpacks.model.entities.EntityDescriptor) NativeEntityDescriptor(org.graylog2.contentpacks.model.entities.NativeEntityDescriptor) ContentPackInstallation(org.graylog2.contentpacks.model.ContentPackInstallation) ImmutableSet(com.google.common.collect.ImmutableSet) NativeEntityDescriptor(org.graylog2.contentpacks.model.entities.NativeEntityDescriptor) EntityWithExcerptFacade(org.graylog2.contentpacks.facades.EntityWithExcerptFacade) NativeEntity(org.graylog2.contentpacks.model.entities.NativeEntity) ValueReference(org.graylog2.contentpacks.model.entities.references.ValueReference)

Aggregations

ContentPack (org.graylog2.contentpacks.model.ContentPack)21 Test (org.junit.Test)11 ContentPackInstallation (org.graylog2.contentpacks.model.ContentPackInstallation)10 HashMap (java.util.HashMap)7 ContentPackV1 (org.graylog2.contentpacks.model.ContentPackV1)7 ModelId (org.graylog2.contentpacks.model.ModelId)7 Timed (com.codahale.metrics.annotation.Timed)6 JsonView (com.fasterxml.jackson.annotation.JsonView)6 ApiOperation (io.swagger.annotations.ApiOperation)6 ApiResponses (io.swagger.annotations.ApiResponses)6 ImmutableSet (com.google.common.collect.ImmutableSet)5 URL (java.net.URL)5 Map (java.util.Map)5 NotFoundException (javax.ws.rs.NotFoundException)5 Path (javax.ws.rs.Path)5 ContentPackException (org.graylog2.contentpacks.exceptions.ContentPackException)5 Entity (org.graylog2.contentpacks.model.entities.Entity)5 ObjectId (org.bson.types.ObjectId)4 EntityWithExcerptFacade (org.graylog2.contentpacks.facades.EntityWithExcerptFacade)4 ContentPackUninstallation (org.graylog2.contentpacks.model.ContentPackUninstallation)4