-
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.
Updated Logstash attributes mapper to use template pattern (#617)
* Updated Logstash attributes mapper to use template pattern Signed-off-by: Asif Sohail Mohammed <[email protected]> Co-authored-by: Asif Sohail Mohammed <[email protected]>
- Loading branch information
1 parent
169365c
commit 5f5cb41
Showing
5 changed files
with
154 additions
and
48 deletions.
There are no files selected for viewing
75 changes: 75 additions & 0 deletions
75
...a/org/opensearch/dataprepper/logstash/mapping/AbstractLogstashPluginAttributesMapper.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,75 @@ | ||
package org.opensearch.dataprepper.logstash.mapping; | ||
|
||
import org.opensearch.dataprepper.logstash.model.LogstashAttribute; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.Collection; | ||
import java.util.Objects; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.LinkedHashMap; | ||
import java.util.HashSet; | ||
|
||
/** | ||
* An abstract class which is responsible for mapping basic attributes | ||
* | ||
* @since 1.2 | ||
*/ | ||
public abstract class AbstractLogstashPluginAttributesMapper implements LogstashPluginAttributesMapper { | ||
private static final Logger LOG = LoggerFactory.getLogger(AbstractLogstashPluginAttributesMapper.class); | ||
|
||
@Override | ||
public Map<String, Object> mapAttributes(final List<LogstashAttribute> logstashAttributes, final LogstashAttributesMappings logstashAttributesMappings) { | ||
|
||
Objects.requireNonNull(logstashAttributes); | ||
Objects.requireNonNull(logstashAttributesMappings); | ||
Objects.requireNonNull(logstashAttributesMappings.getMappedAttributeNames()); | ||
Objects.requireNonNull(logstashAttributesMappings.getAdditionalAttributes()); | ||
|
||
final Map<String, Object> pluginSettings = new LinkedHashMap<>(logstashAttributesMappings.getAdditionalAttributes()); | ||
final Map<String, String> mappedAttributeNames = logstashAttributesMappings.getMappedAttributeNames(); | ||
|
||
Collection<String> customMappedAttributeNames = getCustomMappedAttributeNames(); | ||
|
||
logstashAttributes | ||
.stream() | ||
.filter(logstashAttribute -> !customMappedAttributeNames.contains(logstashAttribute.getAttributeName())) | ||
.forEach(logstashAttribute -> { | ||
if (mappedAttributeNames.containsKey(logstashAttribute.getAttributeName())) { | ||
pluginSettings.put( | ||
mappedAttributeNames.get(logstashAttribute.getAttributeName()), | ||
logstashAttribute.getAttributeValue().getValue() | ||
); | ||
} | ||
else { | ||
LOG.warn("Attribute name {} is not found in mapping file.", logstashAttribute.getAttributeName()); | ||
} | ||
}); | ||
|
||
if (!customMappedAttributeNames.isEmpty()) { | ||
mapCustomAttributes(logstashAttributes, logstashAttributesMappings, pluginSettings); | ||
} | ||
|
||
return pluginSettings; | ||
} | ||
|
||
/** | ||
* Map custom logstashAttributes from a Logstash plugin. | ||
* | ||
* @param logstashAttributes All the Logstash logstashAttributes for the plugin | ||
* @param logstashAttributesMappings The mappings for this Logstash plugin | ||
* @param pluginSettings A map of Data Prepper basic plugin settings. | ||
* | ||
* @since 1.2 | ||
*/ | ||
protected abstract void mapCustomAttributes(List<LogstashAttribute> logstashAttributes, LogstashAttributesMappings logstashAttributesMappings, Map<String, Object> pluginSettings); | ||
|
||
/** | ||
* Get custom logstashAttributes names from a Logstash plugin. | ||
* | ||
* @return A set of custom attributes | ||
* @since 1.2 | ||
*/ | ||
protected abstract HashSet<String> getCustomMappedAttributeNames(); | ||
} |
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
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
59 changes: 59 additions & 0 deletions
59
...g/opensearch/dataprepper/logstash/mapping/AbstractLogstashPluginAttributesMapperTest.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,59 @@ | ||
package org.opensearch.dataprepper.logstash.mapping; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.Mockito; | ||
import org.opensearch.dataprepper.logstash.model.LogstashAttribute; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.LinkedHashMap; | ||
import java.util.HashSet; | ||
|
||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.never; | ||
|
||
class AbstractLogstashPluginAttributesMapperTest { | ||
|
||
private List<LogstashAttribute> logstashAttributes; | ||
private LogstashAttributesMappings mappings; | ||
private Map<String, Object> pluginSettings; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
final LogstashAttribute logstashAttribute = mock(LogstashAttribute.class); | ||
|
||
logstashAttributes = Collections.singletonList(logstashAttribute); | ||
mappings = mock(LogstashAttributesMappings.class); | ||
pluginSettings = new LinkedHashMap<>(mappings.getAdditionalAttributes()); | ||
} | ||
|
||
@Test | ||
void mapAttributes_with_no_custom_attributes_does_not_invoke_mapCustomAttributes_Test() { | ||
AbstractLogstashPluginAttributesMapper abstractLogstashPluginAttributesMapper = Mockito | ||
.spy(AbstractLogstashPluginAttributesMapper.class); | ||
|
||
when(abstractLogstashPluginAttributesMapper.getCustomMappedAttributeNames()).thenReturn(new HashSet<>()); | ||
|
||
Map<String, Object> pluginSettings = abstractLogstashPluginAttributesMapper.mapAttributes(logstashAttributes, mappings); | ||
verify(abstractLogstashPluginAttributesMapper, never()).mapCustomAttributes( | ||
logstashAttributes, mappings, pluginSettings); | ||
} | ||
|
||
|
||
@Test | ||
void mapAttributes_with_custom_attributes_invokes_mapCustomAttributes_Test() { | ||
AbstractLogstashPluginAttributesMapper abstractLogstashPluginAttributesMapper = Mockito | ||
.spy(AbstractLogstashPluginAttributesMapper.class); | ||
|
||
when(abstractLogstashPluginAttributesMapper.getCustomMappedAttributeNames()) | ||
.thenReturn(new HashSet<>(Collections.singletonList("customAttribute"))); | ||
|
||
Map<String, Object> pluginSettings = abstractLogstashPluginAttributesMapper.mapAttributes(logstashAttributes, mappings); | ||
verify(abstractLogstashPluginAttributesMapper).mapCustomAttributes(logstashAttributes, mappings, pluginSettings); | ||
} | ||
|
||
} |
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