Search in sources :

Example 1 with ModelConvertingLogstashVisitor

use of org.opensearch.dataprepper.logstash.parser.ModelConvertingLogstashVisitor in project data-prepper by opensearch-project.

the class LogstashConfigConverter method convertLogstashConfigurationToPipeline.

public String convertLogstashConfigurationToPipeline(String logstashConfigurationPath, String outputDirectory) throws IOException {
    final Path configurationFilePath = Paths.get(logstashConfigurationPath);
    final String logstashConfigAsString = new String(Files.readAllBytes(configurationFilePath));
    LogstashLexer lexer = new LogstashLexer(CharStreams.fromString(logstashConfigAsString));
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    LogstashParser parser = new LogstashParser(tokens);
    final ParseTree tree = parser.config();
    ModelConvertingLogstashVisitor visitor = new ModelConvertingLogstashVisitor();
    LogstashConfiguration logstashConfiguration = (LogstashConfiguration) visitor.visit(tree);
    LogstashMapper logstashMapper = new LogstashMapper();
    PipelineModel pipelineModel = logstashMapper.mapPipeline(logstashConfiguration);
    PipelinesDataFlowModel pipelinesDataFlowModel = new PipelinesDataFlowModel(Collections.singletonMap("logstash-converted-pipeline", pipelineModel));
    ObjectMapper mapper = new ObjectMapper(YAMLFactory.builder().disable(YAMLGenerator.Feature.WRITE_DOC_START_MARKER).enable(YAMLGenerator.Feature.INDENT_ARRAYS_WITH_INDICATOR).enable(YAMLGenerator.Feature.USE_PLATFORM_LINE_BREAKS).disable(YAMLGenerator.Feature.SPLIT_LINES).enable(YAMLGenerator.Feature.LITERAL_BLOCK_STYLE).build());
    final String confFileName = configurationFilePath.getFileName().toString();
    final String yamlFileName = confFileName.substring(0, confFileName.lastIndexOf(".conf"));
    final Path yamlFilePath = Paths.get(outputDirectory, yamlFileName + ".yaml");
    mapper.writeValue(new File(String.valueOf(yamlFilePath)), pipelinesDataFlowModel);
    return String.valueOf(yamlFilePath);
}
Also used : Path(java.nio.file.Path) LogstashConfiguration(org.opensearch.dataprepper.logstash.model.LogstashConfiguration) LogstashMapper(org.opensearch.dataprepper.logstash.mapping.LogstashMapper) CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) PipelinesDataFlowModel(com.amazon.dataprepper.model.configuration.PipelinesDataFlowModel) PipelineModel(com.amazon.dataprepper.model.configuration.PipelineModel) ModelConvertingLogstashVisitor(org.opensearch.dataprepper.logstash.parser.ModelConvertingLogstashVisitor) File(java.io.File) ParseTree(org.antlr.v4.runtime.tree.ParseTree) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

PipelineModel (com.amazon.dataprepper.model.configuration.PipelineModel)1 PipelinesDataFlowModel (com.amazon.dataprepper.model.configuration.PipelinesDataFlowModel)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 File (java.io.File)1 Path (java.nio.file.Path)1 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)1 ParseTree (org.antlr.v4.runtime.tree.ParseTree)1 LogstashMapper (org.opensearch.dataprepper.logstash.mapping.LogstashMapper)1 LogstashConfiguration (org.opensearch.dataprepper.logstash.model.LogstashConfiguration)1 ModelConvertingLogstashVisitor (org.opensearch.dataprepper.logstash.parser.ModelConvertingLogstashVisitor)1