/
ResolvedPolicyCacheLoader.java
99 lines (87 loc) · 4.75 KB
/
ResolvedPolicyCacheLoader.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
* Copyright (c) 2022 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.ditto.thingsearch.service.persistence.write.streaming;
import java.util.HashSet;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.Executor;
import org.apache.pekko.japi.Pair;
import org.eclipse.ditto.internal.utils.cache.Cache;
import org.eclipse.ditto.internal.utils.cache.entry.Entry;
import org.eclipse.ditto.policies.api.PolicyTag;
import org.eclipse.ditto.policies.enforcement.PolicyCacheLoader;
import org.eclipse.ditto.policies.model.Policy;
import org.eclipse.ditto.policies.model.PolicyId;
import org.eclipse.ditto.policies.model.PolicyRevision;
import com.github.benmanes.caffeine.cache.AsyncCacheLoader;
final class ResolvedPolicyCacheLoader
implements AsyncCacheLoader<PolicyIdResolvingImports, Entry<Pair<Policy, Set<PolicyTag>>>> {
private final PolicyCacheLoader policyCacheLoader;
private final CompletableFuture<Cache<PolicyIdResolvingImports, Entry<Pair<Policy, Set<PolicyTag>>>>> cacheFuture;
ResolvedPolicyCacheLoader(final PolicyCacheLoader policyCacheLoader,
final CompletableFuture<Cache<PolicyIdResolvingImports, Entry<Pair<Policy, Set<PolicyTag>>>>> cacheFuture) {
this.policyCacheLoader = policyCacheLoader;
this.cacheFuture = cacheFuture;
}
@Override
public CompletableFuture<? extends Entry<Pair<Policy, Set<PolicyTag>>>> asyncLoad(
final PolicyIdResolvingImports policyIdResolvingImports,
final Executor executor) {
return policyCacheLoader.asyncLoad(policyIdResolvingImports.policyId(), executor)
.thenCompose(policyEntry -> {
if (policyEntry.exists()) {
final Policy policy = policyEntry.getValueOrThrow();
final long revision = policy.getRevision().map(PolicyRevision::toLong)
.orElseThrow(
() -> new IllegalStateException("Bad SudoRetrievePolicyResponse: no revision"));
final Set<PolicyTag> referencedPolicies = new HashSet<>();
if (policyIdResolvingImports.resolveImports()) {
return cacheFuture.thenCompose(cache ->
resolvePolicyImports(cache, policy, referencedPolicies)
)
.thenApply(resolvedPolicy ->
Entry.of(revision, new Pair<>(resolvedPolicy, referencedPolicies))
);
} else {
return CompletableFuture.completedFuture(
Entry.of(revision, new Pair<>(policy, referencedPolicies))
);
}
} else {
return CompletableFuture.completedFuture(Entry.nonexistent());
}
});
}
private static CompletionStage<Policy> resolvePolicyImports(
final Cache<PolicyIdResolvingImports, Entry<Pair<Policy, Set<PolicyTag>>>> cache,
final Policy policy,
final Set<PolicyTag> referencedPolicies) {
return policy.withResolvedImports(importedPolicyId ->
cache.get(new PolicyIdResolvingImports(importedPolicyId, false)) // don't transitively resolve imports, only 1 "level"
.thenApply(entry -> entry.flatMap(Entry::get))
.thenApply(optionalReferencedPolicy -> {
if (optionalReferencedPolicy.isPresent()) {
final Policy referencedPolicy = optionalReferencedPolicy.get().first();
final Optional<PolicyRevision> refRevision = referencedPolicy.getRevision();
final Optional<PolicyId> entityId = referencedPolicy.getEntityId();
if (refRevision.isPresent() && entityId.isPresent()) {
referencedPolicies.add(PolicyTag.of(entityId.get(), refRevision.get().toLong()));
}
}
return optionalReferencedPolicy.map(Pair::first);
})
);
}
}