-
Notifications
You must be signed in to change notification settings - Fork 207
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added configConverter which converts conf to yaml
Signed-off-by: Asif Sohail Mohammed <[email protected]>
- Loading branch information
1 parent
178712c
commit e032adf
Showing
3 changed files
with
99 additions
and
0 deletions.
There are no files selected for viewing
56 changes: 56 additions & 0 deletions
56
...ion/src/main/java/org/opensearch/dataprepper/logstash/parser/LogstashConfigConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
...src/test/java/org/opensearch/dataprepper/logstash/parser/LogstashConfigConverterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
...configuration/src/test/resources/org/opensearch/dataprepper/logstash/parser/logstash.conf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |