use of org.graylog2.contentpacks.model.entities.references.ValueReference in project graylog2-server by Graylog2.
the class ContentPackService method validateParameters.
private ImmutableMap<String, ValueReference> validateParameters(Map<String, ValueReference> parameters, Set<Parameter> contentPackParameters) {
final Set<String> contentPackParameterNames = contentPackParameters.stream().map(Parameter::name).collect(Collectors.toSet());
checkUnknownParameters(parameters, contentPackParameterNames);
checkMissingParameters(parameters, contentPackParameterNames);
final Map<String, ValueType> contentPackParameterValueTypes = contentPackParameters.stream().collect(Collectors.toMap(Parameter::name, Parameter::valueType));
final Set<String> invalidParameters = parameters.entrySet().stream().filter(entry -> entry.getValue().valueType() != contentPackParameterValueTypes.get(entry.getKey())).map(Map.Entry::getKey).collect(Collectors.toSet());
if (!invalidParameters.isEmpty()) {
throw new InvalidParametersException(invalidParameters);
}
final ImmutableMap.Builder<String, ValueReference> validatedParameters = ImmutableMap.builder();
for (Parameter contentPackParameter : contentPackParameters) {
final String name = contentPackParameter.name();
final ValueReference providedParameter = parameters.get(name);
if (providedParameter == null) {
final Optional<?> defaultValue = contentPackParameter.defaultValue();
final Object value = defaultValue.orElseThrow(() -> new EmptyDefaultValueException(name));
final ValueReference valueReference = ValueReference.builder().valueType(contentPackParameter.valueType()).value(value).build();
validatedParameters.put(name, valueReference);
} else if (providedParameter.valueType() != contentPackParameter.valueType()) {
throw new InvalidParameterTypeException(contentPackParameter.valueType(), providedParameter.valueType());
} else {
validatedParameters.put(name, providedParameter);
}
}
return validatedParameters.build();
}
use of org.graylog2.contentpacks.model.entities.references.ValueReference in project graylog2-server by Graylog2.
the class ContentPackService method buildEntityGraph.
private ImmutableGraph<Entity> buildEntityGraph(Entity rootEntity, Set<Entity> entities, Map<String, ValueReference> parameters) {
final Map<EntityDescriptor, Entity> entityDescriptorMap = entities.stream().collect(Collectors.toMap(Entity::toEntityDescriptor, Function.identity()));
final MutableGraph<Entity> dependencyGraph = GraphBuilder.directed().allowsSelfLoops(false).expectedNodeCount(entities.size()).build();
for (Map.Entry<EntityDescriptor, Entity> entry : entityDescriptorMap.entrySet()) {
final EntityDescriptor entityDescriptor = entry.getKey();
final Entity entity = entry.getValue();
final EntityWithExcerptFacade<?, ?> facade = entityFacades.getOrDefault(entity.type(), UnsupportedEntityFacade.INSTANCE);
final Graph<Entity> entityGraph = facade.resolveForInstallation(entity, parameters, entityDescriptorMap);
LOG.trace("Dependencies of entity {}: {}", entityDescriptor, entityGraph);
dependencyGraph.putEdge(rootEntity, entity);
Graphs.merge(dependencyGraph, entityGraph);
LOG.trace("New dependency graph: {}", dependencyGraph);
}
final Set<Entity> unexpectedEntities = dependencyGraph.nodes().stream().filter(entity -> !rootEntity.equals(entity)).filter(entity -> !entities.contains(entity)).collect(Collectors.toSet());
if (!unexpectedEntities.isEmpty()) {
throw new UnexpectedEntitiesException(unexpectedEntities);
}
return ImmutableGraph.copyOf(dependencyGraph);
}
use of org.graylog2.contentpacks.model.entities.references.ValueReference 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);
}
use of org.graylog2.contentpacks.model.entities.references.ValueReference 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.entities.references.ValueReference in project graylog2-server by Graylog2.
the class PipelineRuleFacade method decode.
private NativeEntity<RuleDao> decode(EntityV1 entity, Map<String, ValueReference> parameters) {
final PipelineRuleEntity ruleEntity = objectMapper.convertValue(entity.data(), PipelineRuleEntity.class);
final String title = ruleEntity.title().asString(parameters);
final String source = ruleEntity.source().asString(parameters);
final DateTime now = Tools.nowUTC();
final ValueReference description = ruleEntity.description();
final RuleDao ruleDao = RuleDao.builder().title(title).description(description == null ? null : description.asString(parameters)).source(source).createdAt(now).modifiedAt(now).build();
final RuleDao savedRuleDao = ruleService.save(ruleDao);
return NativeEntity.create(entity.id(), savedRuleDao.id(), TYPE_V1, savedRuleDao.title(), savedRuleDao);
}
Aggregations