-
Notifications
You must be signed in to change notification settings - Fork 219
/
CreatePolicyStrategy.java
117 lines (103 loc) · 5.4 KB
/
CreatePolicyStrategy.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*
* Copyright (c) 2019 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.strategies.commands;
import java.time.Instant;
import java.util.Optional;
import java.util.Set;
import javax.annotation.Nullable;
import org.eclipse.ditto.base.model.entity.metadata.Metadata;
import org.eclipse.ditto.base.model.headers.DittoHeaders;
import org.eclipse.ditto.base.model.headers.DittoHeadersBuilder;
import org.eclipse.ditto.base.model.headers.WithDittoHeaders;
import org.eclipse.ditto.base.model.headers.entitytag.EntityTag;
import org.eclipse.ditto.internal.utils.persistentactors.results.Result;
import org.eclipse.ditto.internal.utils.persistentactors.results.ResultFactory;
import org.eclipse.ditto.policies.api.PoliciesValidator;
import org.eclipse.ditto.policies.model.Policy;
import org.eclipse.ditto.policies.model.PolicyBuilder;
import org.eclipse.ditto.policies.model.PolicyEntry;
import org.eclipse.ditto.policies.model.PolicyId;
import org.eclipse.ditto.policies.model.PolicyLifecycle;
import org.eclipse.ditto.policies.model.signals.commands.modify.CreatePolicy;
import org.eclipse.ditto.policies.model.signals.commands.modify.CreatePolicyResponse;
import org.eclipse.ditto.policies.model.signals.events.PolicyCreated;
import org.eclipse.ditto.policies.model.signals.events.PolicyEvent;
import org.eclipse.ditto.policies.service.common.config.PolicyConfig;
/**
* This strategy handles the {@link org.eclipse.ditto.policies.model.signals.commands.modify.CreatePolicy} command for a
* new Policy.
*/
final class CreatePolicyStrategy extends AbstractPolicyCommandStrategy<CreatePolicy, PolicyEvent<?>> {
CreatePolicyStrategy(final PolicyConfig policyConfig) {
super(CreatePolicy.class, policyConfig);
}
@Override
protected Result<PolicyEvent<?>> doApply(final Context<PolicyId> context,
@Nullable final Policy entity,
final long nextRevision,
final CreatePolicy command,
@Nullable final Metadata metadata) {
// Policy not yet created - do so ..
final Policy newPolicy = command.getPolicy();
final DittoHeadersBuilder<? ,?> adjustedHeadersBuilder = command.getDittoHeaders().toBuilder();
final Set<PolicyEntry> adjustedEntries = potentiallyAdjustPolicyEntries(command.getPolicy().getEntriesSet());
final DittoHeaders adjustedHeaders = adjustedHeadersBuilder.build();
final PolicyBuilder newPolicyBuilder = newPolicy.toBuilder().setId(newPolicy.getEntityId().orElseThrow())
.setAll(adjustedEntries);
final Policy adjustedPolicy = newPolicyBuilder.build();
final CreatePolicy adjustedCommand = CreatePolicy.of(adjustedPolicy, adjustedHeaders);
if (newPolicy.getLifecycle().isEmpty()) {
newPolicyBuilder.setLifecycle(PolicyLifecycle.ACTIVE);
}
final Policy newPolicyWithLifecycle = newPolicyBuilder.build();
final Optional<Result<PolicyEvent<?>>> alreadyExpiredSubject =
checkForAlreadyExpiredSubject(newPolicyWithLifecycle, adjustedHeaders, command);
if (alreadyExpiredSubject.isPresent()) {
return alreadyExpiredSubject.get();
}
final PoliciesValidator validator = PoliciesValidator.newInstance(newPolicyWithLifecycle);
if (validator.isValid()) {
final Instant timestamp = getEventTimestamp();
final Policy newPolicyWithImplicits = newPolicyWithLifecycle.toBuilder()
.setModified(timestamp)
.setCreated(timestamp)
.setRevision(nextRevision)
.setMetadata(metadata)
.build();
final PolicyCreated policyCreated =
PolicyCreated.of(newPolicyWithImplicits, nextRevision, timestamp, adjustedHeaders, metadata);
final WithDittoHeaders response = appendETagHeaderIfProvided(adjustedCommand,
CreatePolicyResponse.of(context.getState(), newPolicyWithImplicits, adjustedHeaders),
newPolicyWithImplicits);
context.getLog().withCorrelationId(adjustedCommand)
.debug("Created new Policy with ID <{}>.", context.getState());
return ResultFactory.newMutationResult(adjustedCommand, policyCreated, response, true, false);
} else {
return ResultFactory.newErrorResult(
policyInvalid(context.getState(), validator.getReason().orElse(null), adjustedHeaders),
command);
}
}
@Override
public Optional<EntityTag> previousEntityTag(final CreatePolicy command, @Nullable final Policy previousEntity) {
return Optional.ofNullable(previousEntity).flatMap(EntityTag::fromEntity);
}
@Override
public Optional<EntityTag> nextEntityTag(final CreatePolicy command, @Nullable final Policy newEntity) {
return Optional.ofNullable(newEntity).flatMap(EntityTag::fromEntity);
}
@Override
public boolean isDefined(final Context<PolicyId> ctx, @Nullable final Policy policy, final CreatePolicy cmd) {
return true;
}
}