Skip to content

Commit

Permalink
Added configConverter which converts conf to yaml
Browse files Browse the repository at this point in the history
Signed-off-by: Asif Sohail Mohammed <[email protected]>
  • Loading branch information
asifsmohammed committed Nov 12, 2021
1 parent 178712c commit e032adf
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package org.opensearch.dataprepper.logstash.parser;

import com.amazon.dataprepper.model.configuration.PipelineModel;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.tree.ParseTree;
import org.opensearch.dataprepper.logstash.LogstashLexer;
import org.opensearch.dataprepper.logstash.LogstashParser;
import org.opensearch.dataprepper.logstash.mapping.LogstashMapper;
import org.opensearch.dataprepper.logstash.model.LogstashConfiguration;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Collections;
import java.util.Map;

/**
* Converts Logstash configuration file and returns YAML file location
*
* @since 1.2
*/
public class LogstashConfigConverter {
public String convertLogstashConfigurationToPipeline(String logstashConfigurationPath, String outputDirectory) throws IOException {
final String logstashConfigAsString = new String(Files.readAllBytes(Paths.get(logstashConfigurationPath)));

LogstashLexer lexer = new LogstashLexer(CharStreams.fromString(logstashConfigAsString));
CommonTokenStream tokens = new CommonTokenStream(lexer);
LogstashParser parser = new LogstashParser(tokens);
ParseTree tree = parser.config();

LogstashVisitor visitor = new LogstashVisitor();
LogstashConfiguration logstashConfiguration = (LogstashConfiguration) visitor.visit(tree);

LogstashMapper logstashMapper = new LogstashMapper();
PipelineModel pipelineModel = logstashMapper.mapPipeline(logstashConfiguration);
Map<String, Object> pipeline = Collections.singletonMap("log-pipeline", pipelineModel);

YAMLFactory factory = new YAMLFactory();
factory.disable(YAMLGenerator.Feature.WRITE_DOC_START_MARKER);
factory.enable(YAMLGenerator.Feature.INDENT_ARRAYS_WITH_INDICATOR);
factory.disable(YAMLGenerator.Feature.SPLIT_LINES);
factory.enable(YAMLGenerator.Feature.LITERAL_BLOCK_STYLE);

ObjectMapper mapper = new ObjectMapper(factory);

String yamlFilePath = outputDirectory + "logstash.yaml";
mapper.writeValue(new File(yamlFilePath), pipeline);

return yamlFilePath;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.opensearch.dataprepper.logstash.parser;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.io.IOException;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;

class LogstashConfigConverterTest {
private LogstashConfigConverter logstashConfigConverter;

@BeforeEach
void createObjectUnderTest() {
logstashConfigConverter = new LogstashConfigConverter();
}

@Test
void convertLogstashConfigurationToPipeline() throws IOException {

String outputDirectory = "build/resources/test/org/opensearch/dataprepper/logstash/parser/";
String confPath = "src/test/resources/org/opensearch/dataprepper/logstash/parser/logstash.conf";

String actualYamlPath = logstashConfigConverter.convertLogstashConfigurationToPipeline(confPath, outputDirectory);
String expectedYamlPath = "build/resources/test/org/opensearch/dataprepper/logstash/parser/logstash.yaml";

assertThat(actualYamlPath, equalTo(expectedYamlPath));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
input {
http {
port => 8000
threads => 100
max_pending_requests => 128
}
}
output {
elasticsearch {
hosts => ["https://localhost:9200"]
user => admin
password => admin
}
}

0 comments on commit e032adf

Please sign in to comment.