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