-
Notifications
You must be signed in to change notification settings - Fork 215
/
PolicyEnforcerActor.java
81 lines (72 loc) · 3.78 KB
/
PolicyEnforcerActor.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
/*
* 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.policies.service.persistence.actors;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.function.Function;
import org.apache.pekko.actor.Props;
import org.eclipse.ditto.base.model.signals.Signal;
import org.eclipse.ditto.internal.utils.cache.entry.Entry;
import org.eclipse.ditto.policies.enforcement.AbstractPolicyLoadingEnforcerActor;
import org.eclipse.ditto.policies.enforcement.PolicyCacheLoader;
import org.eclipse.ditto.policies.enforcement.PolicyEnforcer;
import org.eclipse.ditto.policies.enforcement.PolicyEnforcerProvider;
import org.eclipse.ditto.policies.model.Policy;
import org.eclipse.ditto.policies.model.PolicyId;
import org.eclipse.ditto.policies.model.signals.commands.PolicyCommand;
import org.eclipse.ditto.policies.model.signals.commands.PolicyCommandResponse;
import org.eclipse.ditto.policies.model.signals.commands.modify.CreatePolicy;
import org.eclipse.ditto.policies.service.enforcement.PolicyCommandEnforcement;
/**
* Enforcer responsible for enforcing {@link PolicyCommand}s and filtering {@link PolicyCommandResponse}s utilizing the
* {@link PolicyCommandEnforcement}.
*/
public final class PolicyEnforcerActor extends
AbstractPolicyLoadingEnforcerActor<PolicyId, Signal<?>, PolicyCommandResponse<?>, PolicyCommandEnforcement> {
private static final String ENFORCEMENT_DISPATCHER = "enforcement-dispatcher";
@SuppressWarnings("unused")
private PolicyEnforcerActor(final PolicyId policyId, final PolicyCommandEnforcement policyCommandEnforcement,
final PolicyEnforcerProvider policyEnforcerProvider) {
super(policyId, policyCommandEnforcement, policyEnforcerProvider);
}
/**
* Creates Pekko configuration object Props for this Actor.
*
* @param policyId the PolicyId this enforcer actor is responsible for.
* @param policyCommandEnforcement the policy command enforcement logic to apply in the enforcer.
* @param policyEnforcerProvider the policy enforcer provider.
* @return the {@link Props} to create this actor.
*/
public static Props props(final PolicyId policyId, final PolicyCommandEnforcement policyCommandEnforcement,
final PolicyEnforcerProvider policyEnforcerProvider) {
return Props.create(PolicyEnforcerActor.class, policyId, policyCommandEnforcement, policyEnforcerProvider)
.withDispatcher(ENFORCEMENT_DISPATCHER);
}
@Override
protected CompletionStage<PolicyId> providePolicyIdForEnforcement(final Signal<?> signal) {
return CompletableFuture.completedFuture(entityId);
}
@Override
protected CompletionStage<Optional<PolicyEnforcer>> loadPolicyEnforcer(final Signal<?> signal) {
if (signal instanceof CreatePolicy createPolicy) {
final PolicyCacheLoader policyCacheLoader = PolicyCacheLoader.getSingletonInstance(getContext().system());
final Function<PolicyId, CompletionStage<Optional<Policy>>> importedPolicyResolver =
importedPolicyId -> policyCacheLoader.asyncLoad(importedPolicyId, getContext().dispatcher())
.thenApply(Entry::get);
return PolicyEnforcer.withResolvedImports(createPolicy.getPolicy(), importedPolicyResolver)
.thenApply(Optional::of);
}
return super.loadPolicyEnforcer(signal);
}
}