-
Notifications
You must be signed in to change notification settings - Fork 215
/
PolicyIdReferencePlaceholderResolver.java
190 lines (162 loc) · 9.61 KB
/
PolicyIdReferencePlaceholderResolver.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
* 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.enforcement.placeholders.references;
import java.util.EnumMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CompletionStage;
import java.util.stream.Collectors;
import javax.annotation.concurrent.Immutable;
import org.eclipse.ditto.base.model.exceptions.DittoInternalErrorException;
import org.eclipse.ditto.base.model.exceptions.DittoRuntimeException;
import org.eclipse.ditto.base.model.headers.DittoHeaders;
import org.eclipse.ditto.placeholders.PlaceholderReferenceNotSupportedException;
import org.eclipse.ditto.placeholders.PlaceholderReferenceUnknownFieldException;
import org.eclipse.ditto.internal.utils.akka.logging.AutoCloseableSlf4jLogger;
import org.eclipse.ditto.internal.utils.akka.logging.DittoLogger;
import org.eclipse.ditto.internal.utils.akka.logging.DittoLoggerFactory;
import org.eclipse.ditto.internal.utils.cacheloaders.AskWithRetry;
import org.eclipse.ditto.internal.utils.cacheloaders.config.AskWithRetryConfig;
import org.eclipse.ditto.json.JsonFieldDefinition;
import org.eclipse.ditto.json.JsonValue;
import org.eclipse.ditto.things.model.ThingId;
import org.eclipse.ditto.things.model.signals.commands.ThingErrorResponse;
import org.eclipse.ditto.things.model.signals.commands.query.RetrieveThing;
import org.eclipse.ditto.things.model.signals.commands.query.RetrieveThingResponse;
import akka.actor.ActorRef;
import akka.actor.ActorSystem;
/**
* Responsible for resolving a policy id of a referenced entity.
*/
@Immutable
public final class PolicyIdReferencePlaceholderResolver implements ReferencePlaceholderResolver<String> {
private static final DittoLogger LOGGER = DittoLoggerFactory.getLogger(PolicyIdReferencePlaceholderResolver.class);
private final ActorRef commandForwarderActor;
private final AskWithRetryConfig askWithRetryConfig;
private final ActorSystem actorSystem;
private final Map<ReferencePlaceholder.ReferencedEntityType, ResolveEntityReferenceStrategy>
supportedEntityTypesToActionMap = new EnumMap<>(ReferencePlaceholder.ReferencedEntityType.class);
private final Set<CharSequence> supportedEntityTypeNames;
private PolicyIdReferencePlaceholderResolver(final ActorRef commandForwarderActor,
final AskWithRetryConfig askWithRetryConfig, final ActorSystem actorSystem) {
this.commandForwarderActor = commandForwarderActor;
this.askWithRetryConfig = askWithRetryConfig;
this.actorSystem = actorSystem;
initializeSupportedEntityTypeReferences();
this.supportedEntityTypeNames =
this.supportedEntityTypesToActionMap.keySet().stream().map(Enum::name).collect(Collectors.toSet());
}
private void initializeSupportedEntityTypeReferences() {
this.supportedEntityTypesToActionMap.put(ReferencePlaceholder.ReferencedEntityType.THINGS,
this::handlePolicyIdReference);
}
/**
* Resolves the policy id of the entity of type {@link ReferencePlaceholder#getReferencedEntityType()} with id
* {@link ReferencePlaceholder#getReferencedEntityId()}.
*
* @param referencePlaceholder The placeholder holding the information about the referenced entity id.
* @param dittoHeaders The ditto headers.
* @return A completion stage of String that should eventually hold the policy id.
*/
@Override
public CompletionStage<String> resolve(final ReferencePlaceholder referencePlaceholder,
final DittoHeaders dittoHeaders) {
final var resolveEntityReferenceStrategy =
supportedEntityTypesToActionMap.get(referencePlaceholder.getReferencedEntityType());
try (final AutoCloseableSlf4jLogger logger = LOGGER.setCorrelationId(dittoHeaders)) {
if (null == resolveEntityReferenceStrategy) {
final String referencedEntityType = referencePlaceholder.getReferencedEntityType().name();
logger.info("Could not find a placeholder replacement strategy for entity type <{}> in supported" +
" entity types: {}", referencedEntityType, supportedEntityTypeNames);
throw notSupportedException(referencedEntityType, dittoHeaders);
}
logger.debug("Will resolve entity reference for placeholder: <{}>", referencePlaceholder);
}
return resolveEntityReferenceStrategy.handleEntityPolicyIdReference(referencePlaceholder, dittoHeaders);
}
private CompletionStage<String> handlePolicyIdReference(final ReferencePlaceholder referencePlaceholder,
final DittoHeaders dittoHeaders) {
final var thingId = ThingId.of(referencePlaceholder.getReferencedEntityId());
final var retrieveThingCommand = RetrieveThing.getBuilder(thingId, dittoHeaders)
.withSelectedFields(referencePlaceholder.getReferencedField().toFieldSelector())
.build();
return AskWithRetry.askWithRetry(commandForwarderActor, retrieveThingCommand, askWithRetryConfig, actorSystem,
response -> handleRetrieveThingResponse(response, referencePlaceholder, dittoHeaders)
);
}
private static String handleRetrieveThingResponse(final Object response,
final ReferencePlaceholder referencePlaceholder, final DittoHeaders dittoHeaders) {
if (response instanceof RetrieveThingResponse retrieveThingResponse) {
final JsonValue entity = retrieveThingResponse.getEntity();
if (!entity.isObject()) {
LOGGER.withCorrelationId(dittoHeaders)
.error("Expected RetrieveThingResponse to contain a JsonObject as Entity but was: {}", entity);
throw DittoInternalErrorException.newBuilder().dittoHeaders(dittoHeaders).build();
}
return entity.asObject()
.getValue(JsonFieldDefinition.ofString(referencePlaceholder.getReferencedField()))
.orElseThrow(() -> unknownFieldException(referencePlaceholder, dittoHeaders));
} else if (response instanceof ThingErrorResponse thingErrorResponse) {
LOGGER.withCorrelationId(dittoHeaders)
.info("Got ThingErrorResponse when waiting on RetrieveThingResponse when resolving policy ID" +
" placeholder reference <{}>: {}", referencePlaceholder, thingErrorResponse);
throw ((ThingErrorResponse) response).getDittoRuntimeException();
} else if (response instanceof DittoRuntimeException dre) {
// ignore warning that second argument isn't used. Runtime exceptions will have their stacktrace printed
// in the logs according to https://www.slf4j.org/faq.html#paramException
LOGGER.withCorrelationId(dittoHeaders)
.info("Got Exception when waiting on RetrieveThingResponse when resolving policy ID placeholder reference <{}> - {}: {}",
referencePlaceholder, dre.getClass().getSimpleName(),
dre.getMessage());
throw dre;
} else {
LOGGER.withCorrelationId(dittoHeaders)
.error("Did not retrieve expected RetrieveThingResponse when resolving policy ID placeholder reference <{}>: {}",
referencePlaceholder, response);
throw DittoInternalErrorException.newBuilder().dittoHeaders(dittoHeaders).build();
}
}
private static PlaceholderReferenceUnknownFieldException unknownFieldException(
final ReferencePlaceholder placeholder, final DittoHeaders headers) {
return PlaceholderReferenceUnknownFieldException.fromUnknownFieldAndEntityId(
placeholder.getReferencedField().toString(),
placeholder.getReferencedEntityId())
.dittoHeaders(headers)
.build();
}
private PlaceholderReferenceNotSupportedException notSupportedException(
final CharSequence referencedEntityType, final DittoHeaders headers) {
return PlaceholderReferenceNotSupportedException.fromUnsupportedEntityType(referencedEntityType,
supportedEntityTypeNames)
.dittoHeaders(headers)
.build();
}
/**
* Creates a new {@link PolicyIdReferencePlaceholderResolver} responsible for resolving a policy id of a referenced
* entity.
*
* @param commandForwarderActor the ActorRef of the {@code EdgeCommandForwarderActor} which to ask for "retrieve"
* commands.
* @param askWithRetryConfig the configuration for the "ask with retry" pattern applied when asking for retrieves.
* @param actorSystem the actorSystem to load scheduler and dispatcher to use for the "ask with retry" pattern from.
* @return the created PolicyIdReferencePlaceholderResolver instance.
*/
public static PolicyIdReferencePlaceholderResolver of(final ActorRef commandForwarderActor,
final AskWithRetryConfig askWithRetryConfig, final ActorSystem actorSystem) {
return new PolicyIdReferencePlaceholderResolver(commandForwarderActor, askWithRetryConfig, actorSystem);
}
interface ResolveEntityReferenceStrategy {
CompletionStage<String> handleEntityPolicyIdReference(ReferencePlaceholder referencePlaceholder,
DittoHeaders dittoHeaders);
}
}