use of org.graylog.plugins.pipelineprocessor.parser.ParseException in project graylog2-server by Graylog2.
the class RuleResource method parse.
@ApiOperation(value = "Parse a processing rule without saving it", notes = "")
@POST
@Path("/parse")
@NoAuditEvent("only used to parse a rule, no changes made in the system")
public RuleSource parse(@ApiParam(name = "rule", required = true) @NotNull RuleSource ruleSource) throws ParseException {
final Rule rule;
try {
// be silent about parse errors here, many requests will result in invalid syntax
rule = pipelineRuleParser.parseRule(ruleSource.id(), ruleSource.source(), true);
} catch (ParseException e) {
throw new BadRequestException(Response.status(Response.Status.BAD_REQUEST).entity(e.getErrors()).build());
}
final DateTime now = DateTime.now(DateTimeZone.UTC);
return RuleSource.builder().title(rule.name()).description(ruleSource.description()).source(ruleSource.source()).createdAt(now).modifiedAt(now).build();
}
use of org.graylog.plugins.pipelineprocessor.parser.ParseException in project graylog2-server by Graylog2.
the class ConfigurationStateUpdater method reloadAndSave.
// only the singleton instance should mutate itself, others are welcome to reload a new state, but we don't
// currently allow direct global state updates from external sources (if you need to, send an event on the bus instead)
private synchronized PipelineInterpreter.State reloadAndSave() {
// read all rules and parse them
Map<String, Rule> ruleNameMap = Maps.newHashMap();
ruleService.loadAll().forEach(ruleDao -> {
Rule rule;
try {
rule = pipelineRuleParser.parseRule(ruleDao.id(), ruleDao.source(), false);
} catch (ParseException e) {
log.warn("Ignoring non parseable rule <{}/{}> with errors <{}>", ruleDao.title(), ruleDao.id(), e.getErrors());
rule = Rule.alwaysFalse("Failed to parse rule: " + ruleDao.id());
}
ruleNameMap.put(rule.name(), rule);
});
// read all pipelines and parse them
ImmutableMap.Builder<String, Pipeline> pipelineIdMap = ImmutableMap.builder();
pipelineService.loadAll().forEach(pipelineDao -> {
Pipeline pipeline;
try {
pipeline = pipelineRuleParser.parsePipeline(pipelineDao.id(), pipelineDao.source());
} catch (ParseException e) {
pipeline = Pipeline.empty("Failed to parse pipeline" + pipelineDao.id());
}
// noinspection ConstantConditions
pipelineIdMap.put(pipelineDao.id(), resolvePipeline(pipeline, ruleNameMap));
});
final ImmutableMap<String, Pipeline> currentPipelines = pipelineIdMap.build();
// read all stream connections of those pipelines to allow processing messages through them
final HashMultimap<String, Pipeline> connections = HashMultimap.create();
for (PipelineConnections streamConnection : pipelineStreamConnectionsService.loadAll()) {
streamConnection.pipelineIds().stream().map(currentPipelines::get).filter(Objects::nonNull).forEach(pipeline -> connections.put(streamConnection.streamId(), pipeline));
}
ImmutableSetMultimap<String, Pipeline> streamPipelineConnections = ImmutableSetMultimap.copyOf(connections);
final RuleMetricsConfigDto ruleMetricsConfig = ruleMetricsConfigService.get();
final PipelineInterpreter.State newState = stateFactory.newState(currentPipelines, streamPipelineConnections, ruleMetricsConfig);
latestState.set(newState);
return newState;
}
use of org.graylog.plugins.pipelineprocessor.parser.ParseException in project graylog2-server by Graylog2.
the class PipelineResource method update.
@ApiOperation(value = "Modify a processing pipeline", notes = "It can take up to a second until the change is applied")
@Path("/{id}")
@PUT
@AuditEvent(type = PipelineProcessorAuditEventTypes.PIPELINE_UPDATE)
public PipelineSource update(@ApiParam(name = "id") @PathParam("id") String id, @ApiParam(name = "pipeline", required = true) @NotNull PipelineSource update) throws NotFoundException {
checkPermission(PipelineRestPermissions.PIPELINE_EDIT, id);
final PipelineDao dao = pipelineService.load(id);
final Pipeline pipeline;
try {
pipeline = pipelineRuleParser.parsePipeline(update.id(), update.source());
} catch (ParseException e) {
throw new BadRequestException(Response.status(Response.Status.BAD_REQUEST).entity(e.getErrors()).build());
}
final PipelineDao toSave = dao.toBuilder().title(pipeline.name()).description(update.description()).source(update.source()).modifiedAt(DateTime.now(DateTimeZone.UTC)).build();
final PipelineDao savedPipeline = pipelineService.save(toSave);
return PipelineSource.fromDao(pipelineRuleParser, savedPipeline);
}
use of org.graylog.plugins.pipelineprocessor.parser.ParseException in project graylog2-server by Graylog2.
the class PipelineSource method fromDao.
public static PipelineSource fromDao(PipelineRuleParser parser, PipelineDao dao) {
Set<ParseError> errors = null;
Pipeline pipeline = null;
try {
pipeline = parser.parsePipeline(dao.id(), dao.source());
} catch (ParseException e) {
errors = e.getErrors();
}
final List<StageSource> stageSources = (pipeline == null) ? Collections.emptyList() : pipeline.stages().stream().map(stage -> StageSource.builder().match(stage.match()).rules(stage.ruleReferences()).stage(stage.stage()).build()).collect(Collectors.toList());
return builder().id(dao.id()).title(dao.title()).description(dao.description()).source(dao.source()).createdAt(dao.createdAt()).modifiedAt(dao.modifiedAt()).stages(stageSources).errors(errors).build();
}
use of org.graylog.plugins.pipelineprocessor.parser.ParseException in project graylog2-server by Graylog2.
the class PipelineResourceTest method shouldNotParseAPipelineSuccessfullyIfRaisingAnError.
@Test
public void shouldNotParseAPipelineSuccessfullyIfRaisingAnError() {
final PipelineSource pipelineSource = PipelineSource.builder().source("foo").stages(Collections.emptyList()).title("Graylog Git Pipeline").build();
when(pipelineRuleParser.parsePipeline(pipelineSource.id(), pipelineSource.source())).thenThrow(new ParseException(Collections.emptySet()));
assertThatExceptionOfType(BadRequestException.class).isThrownBy(() -> this.pipelineResource.parse(pipelineSource));
}
Aggregations