|
| 1 | +package dev.openfeature.contrib.providers.optimizely; |
| 2 | + |
| 3 | +import com.optimizely.ab.Optimizely; |
| 4 | +import com.optimizely.ab.OptimizelyUserContext; |
| 5 | +import com.optimizely.ab.optimizelydecision.OptimizelyDecision; |
| 6 | +import com.optimizely.ab.optimizelyjson.OptimizelyJSON; |
| 7 | +import dev.openfeature.sdk.EvaluationContext; |
| 8 | +import dev.openfeature.sdk.EventProvider; |
| 9 | +import dev.openfeature.sdk.Metadata; |
| 10 | +import dev.openfeature.sdk.ProviderEvaluation; |
| 11 | +import dev.openfeature.sdk.Structure; |
| 12 | +import dev.openfeature.sdk.Value; |
| 13 | +import java.util.List; |
| 14 | +import java.util.Map; |
| 15 | +import lombok.Getter; |
| 16 | +import lombok.SneakyThrows; |
| 17 | +import lombok.extern.slf4j.Slf4j; |
| 18 | + |
| 19 | +/** Provider implementation for Optimizely. */ |
| 20 | +@Slf4j |
| 21 | +public class OptimizelyProvider extends EventProvider { |
| 22 | + |
| 23 | + @Getter |
| 24 | + private static final String NAME = "Optimizely"; |
| 25 | + |
| 26 | + private OptimizelyProviderConfig optimizelyProviderConfig; |
| 27 | + |
| 28 | + @Getter |
| 29 | + private Optimizely optimizely; |
| 30 | + |
| 31 | + private ContextTransformer contextTransformer; |
| 32 | + |
| 33 | + /** |
| 34 | + * Constructor. |
| 35 | + * |
| 36 | + * @param optimizelyProviderConfig configuration for the provider |
| 37 | + */ |
| 38 | + public OptimizelyProvider(OptimizelyProviderConfig optimizelyProviderConfig) { |
| 39 | + this.optimizelyProviderConfig = optimizelyProviderConfig; |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Initialize the provider. |
| 44 | + * |
| 45 | + * @param evaluationContext evaluation context |
| 46 | + * @throws Exception on error |
| 47 | + */ |
| 48 | + @Override |
| 49 | + public void initialize(EvaluationContext evaluationContext) throws Exception { |
| 50 | + optimizely = Optimizely.builder() |
| 51 | + .withConfigManager(optimizelyProviderConfig.getProjectConfigManager()) |
| 52 | + .withEventProcessor(optimizelyProviderConfig.getEventProcessor()) |
| 53 | + .withDatafile(optimizelyProviderConfig.getDatafile()) |
| 54 | + .withDefaultDecideOptions(optimizelyProviderConfig.getDefaultDecideOptions()) |
| 55 | + .withErrorHandler(optimizelyProviderConfig.getErrorHandler()) |
| 56 | + .withODPManager(optimizelyProviderConfig.getOdpManager()) |
| 57 | + .withUserProfileService(optimizelyProviderConfig.getUserProfileService()) |
| 58 | + .build(); |
| 59 | + contextTransformer = ContextTransformer.builder().optimizely(optimizely).build(); |
| 60 | + log.info("finished initializing provider"); |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public Metadata getMetadata() { |
| 65 | + return () -> NAME; |
| 66 | + } |
| 67 | + |
| 68 | + @SneakyThrows |
| 69 | + @Override |
| 70 | + public ProviderEvaluation<Boolean> getBooleanEvaluation(String key, Boolean defaultValue, EvaluationContext ctx) { |
| 71 | + OptimizelyUserContext userContext = contextTransformer.transform(ctx); |
| 72 | + OptimizelyDecision decision = userContext.decide(key); |
| 73 | + String variationKey = decision.getVariationKey(); |
| 74 | + String reasonsString = null; |
| 75 | + if (variationKey == null) { |
| 76 | + List<String> reasons = decision.getReasons(); |
| 77 | + reasonsString = String.join(", ", reasons); |
| 78 | + } |
| 79 | + |
| 80 | + boolean enabled = decision.getEnabled(); |
| 81 | + return ProviderEvaluation.<Boolean>builder() |
| 82 | + .value(enabled) |
| 83 | + .reason(reasonsString) |
| 84 | + .build(); |
| 85 | + } |
| 86 | + |
| 87 | + @SneakyThrows |
| 88 | + @Override |
| 89 | + public ProviderEvaluation<String> getStringEvaluation(String key, String defaultValue, EvaluationContext ctx) { |
| 90 | + throw new UnsupportedOperationException("String evaluation is not directly supported by Optimizely provider," |
| 91 | + + "use getObjectEvaluation instead."); |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + public ProviderEvaluation<Integer> getIntegerEvaluation(String key, Integer defaultValue, EvaluationContext ctx) { |
| 96 | + throw new UnsupportedOperationException("Integer evaluation is not directly supported by Optimizely provider," |
| 97 | + + "use getObjectEvaluation instead."); |
| 98 | + } |
| 99 | + |
| 100 | + @Override |
| 101 | + public ProviderEvaluation<Double> getDoubleEvaluation(String key, Double defaultValue, EvaluationContext ctx) { |
| 102 | + throw new UnsupportedOperationException("Double evaluation is not directly supported by Optimizely provider," |
| 103 | + + "use getObjectEvaluation instead."); |
| 104 | + } |
| 105 | + |
| 106 | + @SneakyThrows |
| 107 | + @Override |
| 108 | + public ProviderEvaluation<Value> getObjectEvaluation(String key, Value defaultValue, EvaluationContext ctx) { |
| 109 | + OptimizelyUserContext userContext = contextTransformer.transform(ctx); |
| 110 | + OptimizelyDecision decision = userContext.decide(key); |
| 111 | + String variationKey = decision.getVariationKey(); |
| 112 | + String reasonsString = null; |
| 113 | + if (variationKey == null) { |
| 114 | + List<String> reasons = decision.getReasons(); |
| 115 | + reasonsString = String.join(", ", reasons); |
| 116 | + } |
| 117 | + |
| 118 | + Value evaluatedValue = defaultValue; |
| 119 | + boolean enabled = decision.getEnabled(); |
| 120 | + if (enabled) { |
| 121 | + OptimizelyJSON variables = decision.getVariables(); |
| 122 | + evaluatedValue = toValue(variables); |
| 123 | + } |
| 124 | + |
| 125 | + return ProviderEvaluation.<Value>builder() |
| 126 | + .value(evaluatedValue) |
| 127 | + .reason(reasonsString) |
| 128 | + .variant(variationKey) |
| 129 | + .build(); |
| 130 | + } |
| 131 | + |
| 132 | + @SneakyThrows |
| 133 | + private Value toValue(OptimizelyJSON optimizelyJson) { |
| 134 | + Map<String, Object> map = optimizelyJson.toMap(); |
| 135 | + Structure structure = Structure.mapToStructure(map); |
| 136 | + return new Value(structure); |
| 137 | + } |
| 138 | + |
| 139 | + @SneakyThrows |
| 140 | + @Override |
| 141 | + public void shutdown() { |
| 142 | + log.info("shutdown"); |
| 143 | + if (optimizely != null) { |
| 144 | + optimizely.close(); |
| 145 | + } |
| 146 | + } |
| 147 | +} |
0 commit comments