Skip to content

Commit

Permalink
Remove composite predicates from transformations
Browse files Browse the repository at this point in the history
Signed-off-by: Andre Kurait <[email protected]>
  • Loading branch information
AndreKurait committed Oct 24, 2024
1 parent 5f64ef7 commit 18824d0
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 109 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
package org.opensearch.migrations.transform;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.opensearch.migrations.transform.JsonCompositePredicate.CompositeOperation;

import io.burt.jmespath.BaseRuntime;
import io.burt.jmespath.jcf.JcfRuntime;

Expand All @@ -20,41 +16,30 @@ public JsonJMESPathPredicateProvider() {

@Override
public IJsonPredicate createPredicate(Object jsonConfig) {
var transformers = new ArrayList<JsonJMESPathPredicate>();
var configs = new ArrayList<Map<String, Object>>();
try {
if (jsonConfig instanceof Map) {
configs.add((Map<String, Object>) jsonConfig);
} else if (jsonConfig instanceof List) {
for (var c : (List) jsonConfig) {
configs.add((Map<String, Object>) c);
}
} else {
throw new IllegalArgumentException(getConfigUsageStr());
}
for (var c : configs) {
if (c.size() != 1) {
@SuppressWarnings("unchecked")
var jsonConfigMap = (Map<String, Object>) jsonConfig;
if (jsonConfigMap.size() != 1) {
throw new IllegalArgumentException(getConfigUsageStr());

Check warning on line 24 in TrafficCapture/transformationPlugins/jsonMessageTransformers/jsonJMESPathMessageTransformerProvider/src/main/java/org/opensearch/migrations/transform/JsonJMESPathPredicateProvider.java

View check run for this annotation

Codecov / codecov/patch

TrafficCapture/transformationPlugins/jsonMessageTransformers/jsonJMESPathMessageTransformerProvider/src/main/java/org/opensearch/migrations/transform/JsonJMESPathPredicateProvider.java#L24

Added line #L24 was not covered by tests
}
var scriptValue = c.get(SCRIPT_KEY);
var scriptValue = jsonConfigMap.get(SCRIPT_KEY);
if (!(scriptValue instanceof String)) {
throw new IllegalArgumentException(getConfigUsageStr());

Check warning on line 28 in TrafficCapture/transformationPlugins/jsonMessageTransformers/jsonJMESPathMessageTransformerProvider/src/main/java/org/opensearch/migrations/transform/JsonJMESPathPredicateProvider.java

View check run for this annotation

Codecov / codecov/patch

TrafficCapture/transformationPlugins/jsonMessageTransformers/jsonJMESPathMessageTransformerProvider/src/main/java/org/opensearch/migrations/transform/JsonJMESPathPredicateProvider.java#L28

Added line #L28 was not covered by tests
}
transformers.add(new JsonJMESPathPredicate(adapterRuntime, (String) scriptValue));
return new JsonJMESPathPredicate(adapterRuntime, (String) scriptValue);
}
throw new IllegalArgumentException(getConfigUsageStr());
} catch (ClassCastException e) {
throw new IllegalArgumentException(getConfigUsageStr(), e);

Check warning on line 34 in TrafficCapture/transformationPlugins/jsonMessageTransformers/jsonJMESPathMessageTransformerProvider/src/main/java/org/opensearch/migrations/transform/JsonJMESPathPredicateProvider.java

View check run for this annotation

Codecov / codecov/patch

TrafficCapture/transformationPlugins/jsonMessageTransformers/jsonJMESPathMessageTransformerProvider/src/main/java/org/opensearch/migrations/transform/JsonJMESPathPredicateProvider.java#L32-L34

Added lines #L32 - L34 were not covered by tests
}
return new JsonCompositePredicate(CompositeOperation.ALL,
transformers.toArray(IJsonPredicate[]::new));
}

private String getConfigUsageStr() {
return this.getClass().getName()

Check warning on line 39 in TrafficCapture/transformationPlugins/jsonMessageTransformers/jsonJMESPathMessageTransformerProvider/src/main/java/org/opensearch/migrations/transform/JsonJMESPathPredicateProvider.java

View check run for this annotation

Codecov / codecov/patch

TrafficCapture/transformationPlugins/jsonMessageTransformers/jsonJMESPathMessageTransformerProvider/src/main/java/org/opensearch/migrations/transform/JsonJMESPathPredicateProvider.java#L39

Added line #L39 was not covered by tests
+ " expects the incoming configuration "
+ "to be a Map<String,Object> or a List<Map<String,Object>>. "
+ "to be a Map<String,Object>. "
+ "Each of the Maps should have one key-value of \"script\": \"...\". "
+ "Script values should be a fully-formed inlined JsonPath queries encoded as a json value. "
+ "All of the values within a configuration will be concatenated into one chained Predicate.";
+ "Script values should be a fully-formed inlined JsonPath queries.";
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
import java.util.List;
import java.util.stream.Collectors;

import org.opensearch.migrations.transform.JsonCompositePredicate.CompositeOperation;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class JsonConditionalTransformerProvider implements IJsonTransformerProvider {

public JsonConditionalTransformerProvider() {
Expand All @@ -21,20 +21,17 @@ public IJsonTransformer createTransformer(Object jsonConfig) {
if (configs.size() != 2) {
throw new IllegalArgumentException(getConfigUsageStr());

Check warning on line 22 in TrafficCapture/transformationPlugins/jsonMessageTransformers/jsonMessageTransformerLoaders/src/main/java/org/opensearch/migrations/transform/JsonConditionalTransformerProvider.java

View check run for this annotation

Codecov / codecov/patch

TrafficCapture/transformationPlugins/jsonMessageTransformers/jsonMessageTransformerLoaders/src/main/java/org/opensearch/migrations/transform/JsonConditionalTransformerProvider.java#L22

Added line #L22 was not covered by tests
}
var PredicateConfig = configs.get(0);
var predicateConfig = configs.get(0);
var transformerConfig = configs.get(1);
@SuppressWarnings("unchecked")
List<IJsonPredicate> Predicate = new PredicateLoader()
.getTransformerFactoryFromServiceLoaderParsed((List<Object>) PredicateConfig)
.collect(Collectors.toList());
IJsonPredicate predicate = new PredicateLoader()
.getPredicateFactoryFromServiceLoaderParsed(predicateConfig);
@SuppressWarnings("unchecked")
List<IJsonTransformer> transformer = new TransformationLoader()
.getTransformerFactoryFromServiceLoaderParsed((List<Object>) transformerConfig)
.collect(Collectors.toList());

return new JsonConditionalTransformer(
new JsonCompositePredicate(CompositeOperation.ALL,
Predicate.toArray(IJsonPredicate[]::new)),
predicate,
new JsonCompositeTransformer(transformer.toArray(IJsonTransformer[]::new)));
}
throw new IllegalArgumentException(getConfigUsageStr());

Check warning on line 37 in TrafficCapture/transformationPlugins/jsonMessageTransformers/jsonMessageTransformerLoaders/src/main/java/org/opensearch/migrations/transform/JsonConditionalTransformerProvider.java

View check run for this annotation

Codecov / codecov/patch

TrafficCapture/transformationPlugins/jsonMessageTransformers/jsonMessageTransformerLoaders/src/main/java/org/opensearch/migrations/transform/JsonConditionalTransformerProvider.java#L37

Added line #L37 was not covered by tests
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@
import java.util.ServiceLoader;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.opensearch.migrations.transform.JsonCompositePredicate.CompositeOperation;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
Expand All @@ -20,18 +17,18 @@
@Slf4j
public class PredicateLoader {
public static final String WRONG_JSON_STRUCTURE_MESSAGE =
"Must specify the top-level configuration list with a sequence of "
+ "maps that have only one key each, where the key is the name of the transformer to be configured.";
"Must specify the top-level configuration map"
+ " that only has one key, where the key is the name of the Predicate to be configured.";
public static final Pattern CLASS_NAME_PATTERN = Pattern.compile("^[^{}]*$");
private final List<IJsonPredicateProvider> providers;
ObjectMapper objMapper = new ObjectMapper();

public PredicateLoader() {
ServiceLoader<IJsonPredicateProvider> transformerProviders = ServiceLoader.load(
ServiceLoader<IJsonPredicateProvider> predicateProviders = ServiceLoader.load(
IJsonPredicateProvider.class
);
var inProgressProviders = new ArrayList<IJsonPredicateProvider>();
for (var provider : transformerProviders) {
for (var provider : predicateProviders) {
log.info("Adding IJsonPredicateProvider: " + provider);
inProgressProviders.add(provider);
}
Expand All @@ -44,38 +41,34 @@ public PredicateLoader() {
.log();
}

List<Map<String, Object>> parseFullConfig(String fullConfig) throws JsonProcessingException {
Map<String, Object> parseFullConfig(String fullConfig) throws JsonProcessingException {
if (CLASS_NAME_PATTERN.matcher(fullConfig).matches()) {
return List.of(Collections.singletonMap(fullConfig, null));
return Collections.singletonMap(fullConfig, null);

Check warning on line 46 in TrafficCapture/transformationPlugins/jsonMessageTransformers/jsonMessageTransformerLoaders/src/main/java/org/opensearch/migrations/transform/PredicateLoader.java

View check run for this annotation

Codecov / codecov/patch

TrafficCapture/transformationPlugins/jsonMessageTransformers/jsonMessageTransformerLoaders/src/main/java/org/opensearch/migrations/transform/PredicateLoader.java#L46

Added line #L46 was not covered by tests
} else {
return objMapper.readValue(fullConfig, new TypeReference<>() {
});
return objMapper.readValue(fullConfig, new TypeReference<>() {});

Check warning on line 48 in TrafficCapture/transformationPlugins/jsonMessageTransformers/jsonMessageTransformerLoaders/src/main/java/org/opensearch/migrations/transform/PredicateLoader.java

View check run for this annotation

Codecov / codecov/patch

TrafficCapture/transformationPlugins/jsonMessageTransformers/jsonMessageTransformerLoaders/src/main/java/org/opensearch/migrations/transform/PredicateLoader.java#L48

Added line #L48 was not covered by tests
}
}

protected Stream<IJsonPredicate> getTransformerFactoryFromServiceLoader(String fullConfig)
protected IJsonPredicate getPredicateFactoryFromServiceLoader(String fullConfig)
throws JsonProcessingException {
var configList = fullConfig == null ? List.of() : parseFullConfig(fullConfig);
if (configList.isEmpty() || providers.isEmpty()) {
log.warn("No transformer configuration specified. No custom transformations will be performed");
return Stream.of();
} else {
return configList.stream().map(c -> configureTransformerFromConfig((Map<String, Object>) c));
}
Map<String, Object> configMap = fullConfig == null ? Map.of() : parseFullConfig(fullConfig);
return getPredicateFactoryFromServiceLoaderParsed(configMap);

Check warning on line 55 in TrafficCapture/transformationPlugins/jsonMessageTransformers/jsonMessageTransformerLoaders/src/main/java/org/opensearch/migrations/transform/PredicateLoader.java

View check run for this annotation

Codecov / codecov/patch

TrafficCapture/transformationPlugins/jsonMessageTransformers/jsonMessageTransformerLoaders/src/main/java/org/opensearch/migrations/transform/PredicateLoader.java#L55

Added line #L55 was not covered by tests
}

public Stream<IJsonPredicate> getTransformerFactoryFromServiceLoaderParsed(List<Object> configList) {
if (configList.isEmpty() || providers.isEmpty()) {
log.warn("No transformer configuration specified. No custom transformations will be performed");
return Stream.of();
public IJsonPredicate getPredicateFactoryFromServiceLoaderParsed(Object config) {
@SuppressWarnings("unchecked")
var configMap = (Map<String, Object>) config;
if (configMap.isEmpty() || providers.isEmpty()) {
log.atError().setMessage("No predicate configuration found for configuration {}")
.addArgument(config).log();
throw new IllegalArgumentException("No predicate configuration found for configuration " + config);

Check warning on line 64 in TrafficCapture/transformationPlugins/jsonMessageTransformers/jsonMessageTransformerLoaders/src/main/java/org/opensearch/migrations/transform/PredicateLoader.java

View check run for this annotation

Codecov / codecov/patch

TrafficCapture/transformationPlugins/jsonMessageTransformers/jsonMessageTransformerLoaders/src/main/java/org/opensearch/migrations/transform/PredicateLoader.java#L62-L64

Added lines #L62 - L64 were not covered by tests
} else {
return configList.stream().map(c -> configureTransformerFromConfig((Map<String, Object>) c));
return configurePredicateFromConfig(configMap);
}
}


@SneakyThrows // JsonProcessingException should be impossible since the contents are those that were just parsed

Check warning on line 70 in TrafficCapture/transformationPlugins/jsonMessageTransformers/jsonMessageTransformerLoaders/src/main/java/org/opensearch/migrations/transform/PredicateLoader.java

View check run for this annotation

Codecov / codecov/patch

TrafficCapture/transformationPlugins/jsonMessageTransformers/jsonMessageTransformerLoaders/src/main/java/org/opensearch/migrations/transform/PredicateLoader.java#L70

Added line #L70 was not covered by tests
private IJsonPredicate configureTransformerFromConfig(Map<String, Object> c) {
private IJsonPredicate configurePredicateFromConfig(Map<String, Object> c) {
var keys = c.keySet();
if (keys.size() != 1) {
throw new IllegalArgumentException(WRONG_JSON_STRUCTURE_MESSAGE);

Check warning on line 74 in TrafficCapture/transformationPlugins/jsonMessageTransformers/jsonMessageTransformerLoaders/src/main/java/org/opensearch/migrations/transform/PredicateLoader.java

View check run for this annotation

Codecov / codecov/patch

TrafficCapture/transformationPlugins/jsonMessageTransformers/jsonMessageTransformerLoaders/src/main/java/org/opensearch/migrations/transform/PredicateLoader.java#L74

Added line #L74 was not covered by tests
Expand All @@ -84,12 +77,12 @@ private IJsonPredicate configureTransformerFromConfig(Map<String, Object> c) {
.findFirst()
.orElseThrow(() -> new IllegalArgumentException(WRONG_JSON_STRUCTURE_MESSAGE));
for (var p : providers) {
var transformerName = p.getName();
if (transformerName.equals(key)) {
var predicateName = p.getName();
if (predicateName.equals(key)) {
var configuration = c.get(key);
log.atInfo()
.setMessage(
() -> "Creating a transformer through provider=" + p + " with configuration=" + configuration
() -> "Creating a Predicate through provider=" + p + " with configuration=" + configuration
)
.log();
return p.createPredicate(configuration);
Expand All @@ -98,15 +91,11 @@ private IJsonPredicate configureTransformerFromConfig(Map<String, Object> c) {
throw new IllegalArgumentException("Could not find a provider named: " + key);

Check warning on line 91 in TrafficCapture/transformationPlugins/jsonMessageTransformers/jsonMessageTransformerLoaders/src/main/java/org/opensearch/migrations/transform/PredicateLoader.java

View check run for this annotation

Codecov / codecov/patch

TrafficCapture/transformationPlugins/jsonMessageTransformers/jsonMessageTransformerLoaders/src/main/java/org/opensearch/migrations/transform/PredicateLoader.java#L90-L91

Added lines #L90 - L91 were not covered by tests
}

public IJsonPredicate getTransformerFactoryLoader(String fullConfig) {
public IJsonPredicate getPredicateFactoryLoader(String fullConfig) {
try {
var loadedTransformers = getTransformerFactoryFromServiceLoader(fullConfig);
return new JsonCompositePredicate(
CompositeOperation.ALL,
loadedTransformers.toArray(IJsonPredicate[]::new)
);
return getPredicateFactoryFromServiceLoader(fullConfig);
} catch (JsonProcessingException e) {
throw new IllegalArgumentException("Could not parse the transformer configuration as a json list", e);
throw new IllegalArgumentException("Could not parse the Predicate configuration as a json list", e);

Check warning on line 98 in TrafficCapture/transformationPlugins/jsonMessageTransformers/jsonMessageTransformerLoaders/src/main/java/org/opensearch/migrations/transform/PredicateLoader.java

View check run for this annotation

Codecov / codecov/patch

TrafficCapture/transformationPlugins/jsonMessageTransformers/jsonMessageTransformerLoaders/src/main/java/org/opensearch/migrations/transform/PredicateLoader.java#L96-L98

Added lines #L96 - L98 were not covered by tests
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public void testAddGzip() throws Exception {
public void testAddGzipAndCustom() throws Exception {
final var addGzip = "["
+ "{\"JsonConditionalTransformerProvider\": ["
+ " [{\"JsonJMESPathPredicateProvider\": { \"script\": \"" + "URI == '/testindex/_search'" + "\"}}],"
+ " {\"JsonJMESPathPredicateProvider\": { \"script\": \"" + "URI == '/testindex/_search'" + "\"}},"
+ " [{\"JsonJoltTransformerProvider\": { \"canned\": \"ADD_GZIP\" }}]"
+ "]},"
+ "{ \"JsonJoltTransformerProvider\":"
Expand Down

0 comments on commit 18824d0

Please sign in to comment.