-
Notifications
You must be signed in to change notification settings - Fork 214
/
SignalFilter.java
273 lines (249 loc) · 13.9 KB
/
SignalFilter.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/*
* Copyright (c) 2017 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.connectivity.service.messaging.persistence;
import static org.eclipse.ditto.protocol.TopicPath.Criterion.COMMANDS;
import static org.eclipse.ditto.protocol.TopicPath.Criterion.EVENTS;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.function.Predicate;
import javax.annotation.Nullable;
import org.eclipse.ditto.base.model.auth.AuthorizationContext;
import org.eclipse.ditto.base.model.entity.id.EntityId;
import org.eclipse.ditto.base.model.entity.id.WithEntityId;
import org.eclipse.ditto.base.model.headers.DittoHeaders;
import org.eclipse.ditto.base.model.namespaces.NamespaceReader;
import org.eclipse.ditto.base.model.signals.Signal;
import org.eclipse.ditto.base.model.signals.WithResource;
import org.eclipse.ditto.base.model.signals.commands.Command;
import org.eclipse.ditto.base.model.signals.commands.CommandResponse;
import org.eclipse.ditto.base.model.signals.events.Event;
import org.eclipse.ditto.connectivity.model.Connection;
import org.eclipse.ditto.connectivity.model.FilteredTopic;
import org.eclipse.ditto.connectivity.model.Target;
import org.eclipse.ditto.connectivity.model.Topic;
import org.eclipse.ditto.connectivity.model.signals.announcements.ConnectivityAnnouncement;
import org.eclipse.ditto.connectivity.service.messaging.monitoring.ConnectionMonitor;
import org.eclipse.ditto.connectivity.service.messaging.monitoring.ConnectionMonitorRegistry;
import org.eclipse.ditto.edge.service.placeholders.EntityIdPlaceholder;
import org.eclipse.ditto.edge.service.placeholders.FeaturePlaceholder;
import org.eclipse.ditto.edge.service.placeholders.ThingPlaceholder;
import org.eclipse.ditto.json.JsonFieldSelector;
import org.eclipse.ditto.json.JsonPointer;
import org.eclipse.ditto.messages.model.signals.commands.MessageCommand;
import org.eclipse.ditto.messages.model.signals.commands.MessageCommandResponse;
import org.eclipse.ditto.placeholders.PlaceholderFactory;
import org.eclipse.ditto.placeholders.PlaceholderResolver;
import org.eclipse.ditto.placeholders.TimePlaceholder;
import org.eclipse.ditto.policies.model.signals.announcements.PolicyAnnouncement;
import org.eclipse.ditto.protocol.TopicPath;
import org.eclipse.ditto.protocol.adapter.DittoProtocolAdapter;
import org.eclipse.ditto.protocol.placeholders.ResourcePlaceholder;
import org.eclipse.ditto.protocol.placeholders.TopicPathPlaceholder;
import org.eclipse.ditto.rql.parser.RqlPredicateParser;
import org.eclipse.ditto.rql.query.criteria.Criteria;
import org.eclipse.ditto.rql.query.filter.QueryFilterCriteriaFactory;
import org.eclipse.ditto.things.model.Thing;
import org.eclipse.ditto.things.model.ThingId;
import org.eclipse.ditto.things.model.signals.events.ThingEvent;
import org.eclipse.ditto.things.model.signals.events.ThingEventToThingConverter;
/**
* Filters a set of targets by
* <ul>
* <li>removing those targets that do not want to receive a signal</li>
* <li>removing those targets that are not allowed to read a signal</li>
* </ul>
*/
public final class SignalFilter {
private static final DittoProtocolAdapter DITTO_PROTOCOL_ADAPTER = DittoProtocolAdapter.newInstance();
private static final TopicPathPlaceholder TOPIC_PATH_PLACEHOLDER = TopicPathPlaceholder.getInstance();
private static final EntityIdPlaceholder ENTITY_ID_PLACEHOLDER = EntityIdPlaceholder.getInstance();
private static final ThingPlaceholder THING_PLACEHOLDER = ThingPlaceholder.getInstance();
private static final FeaturePlaceholder FEATURE_PLACEHOLDER = FeaturePlaceholder.getInstance();
private static final ResourcePlaceholder RESOURCE_PLACEHOLDER = ResourcePlaceholder.getInstance();
private static final TimePlaceholder TIME_PLACEHOLDER = TimePlaceholder.getInstance();
private final Connection connection;
private final ConnectionMonitorRegistry<ConnectionMonitor> connectionMonitorRegistry;
SignalFilter(final Connection connection,
final ConnectionMonitorRegistry<ConnectionMonitor> connectionMonitorRegistry) {
this.connection = connection;
this.connectionMonitorRegistry = connectionMonitorRegistry;
}
/**
* Constructs a new SignalFilter instance with the given {@code connection}.
*
* @param connection the connection to filter the signals on.
* @param connectionMonitorRegistry the monitor registry.
* @return the signal filter.
*/
public static SignalFilter of(final Connection connection,
final ConnectionMonitorRegistry<ConnectionMonitor> connectionMonitorRegistry) {
return new SignalFilter(connection, connectionMonitorRegistry);
}
/**
* Filters the passed {@code signal} by extracting those {@link Target}s which should receive the signal.
* Fields are ignored if they occur as "extra targets" to be evaluated later after signal enrichment.
*
* @param signal the signal to filter / determine the {@link org.eclipse.ditto.connectivity.model.Target}s for
* @return the determined Targets for the passed in {@code signal}
* @throws org.eclipse.ditto.base.model.exceptions.InvalidRqlExpressionException if the optional filter string of a
* Target cannot be mapped to a valid criterion
*/
@SuppressWarnings("squid:S3864")
public List<Target> filter(final Signal<?> signal) {
return connection.getTargets().stream()
.filter(t -> isTargetAuthorized(t, signal)) // this is cheaper, so check this first
.filter(t -> isTargetSubscribedForTopicGenerally(t, signal))
// count authorized targets which generally are interested in the topic (e.g. "live messages")
.peek(authorizedTarget -> connectionMonitorRegistry.forOutboundDispatched(connection,
authorizedTarget.getAddress())
.success(signal))
.filter(t -> isTargetSubscribedForTopicWithFiltering(t, signal))
// count authorized + filtered targets
.peek(filteredTarget -> connectionMonitorRegistry.forOutboundFiltered(connection,
filteredTarget.getAddress())
.success(signal))
.toList();
}
private static boolean isTargetAuthorized(final Target target, final Signal<?> signal) {
if (signal instanceof PolicyAnnouncement || signal instanceof ConnectivityAnnouncement) {
return true;
} else {
final AuthorizationContext authorizationContext = target.getAuthorizationContext();
final DittoHeaders headers = signal.getDittoHeaders();
return authorizationContext.isAuthorized(headers.getReadGrantedSubjects(),
headers.getReadRevokedSubjects());
}
}
private static boolean isTargetSubscribedForTopicGenerally(final Target target, final Signal<?> signal) {
return target.getTopics().stream()
.anyMatch(applyTopicFilter(signal));
}
private static boolean isTargetSubscribedForTopicWithFiltering(final Target target, final Signal<?> signal) {
return target.getTopics().stream()
.filter(applyTopicFilter(signal))
.filter(applyNamespaceFilter(signal))
.anyMatch(filteredTopic -> matchesFilterBeforeEnrichment(filteredTopic, signal));
}
private static Predicate<FilteredTopic> applyTopicFilter(final Signal<?> signal) {
return t -> t.getTopic().equals(topicFromSignal(signal).orElse(null));
}
private static Predicate<FilteredTopic> applyNamespaceFilter(final Signal<?> signal) {
return t -> t.getNamespaces().isEmpty() ||
(signal instanceof WithEntityId withEntityId && t.getNamespaces().contains(namespaceFromId(withEntityId)));
}
@Nullable
private static String namespaceFromId(final WithEntityId withEntityId) {
return NamespaceReader.fromEntityId(withEntityId.getEntityId()).orElse(null);
}
private static boolean matchesFilterBeforeEnrichment(final FilteredTopic filteredTopic, final Signal<?> signal) {
final Optional<String> filterOptional = filteredTopic.getFilter();
if (filterOptional.isPresent()) {
// match filter ignoring "extraFields"
final TopicPath topicPath = DITTO_PROTOCOL_ADAPTER.toTopicPath(signal);
final PlaceholderResolver<TopicPath> topicPathPlaceholderResolver =
PlaceholderFactory.newPlaceholderResolver(TOPIC_PATH_PLACEHOLDER, topicPath);
final PlaceholderResolver<EntityId> entityIdPlaceholderResolver = PlaceholderFactory
.newPlaceholderResolver(ENTITY_ID_PLACEHOLDER,
(signal instanceof WithEntityId withEntityId) ? withEntityId.getEntityId() : null);
final PlaceholderResolver<EntityId> thingPlaceholderResolver = PlaceholderFactory
.newPlaceholderResolver(THING_PLACEHOLDER,
(signal instanceof WithEntityId withEntityId) ? withEntityId.getEntityId() : null);
final PlaceholderResolver<Signal<?>> featurePlaceholderResolver = PlaceholderFactory
.newPlaceholderResolver(FEATURE_PLACEHOLDER, signal);
final PlaceholderResolver<WithResource> resourcePlaceholderResolver = PlaceholderFactory
.newPlaceholderResolver(RESOURCE_PLACEHOLDER, signal);
final PlaceholderResolver<Object> timePlaceholderResolver = PlaceholderFactory
.newPlaceholderResolver(TIME_PLACEHOLDER, new Object());
final Criteria criteria = parseCriteria(filterOptional.get(), signal.getDittoHeaders(),
topicPathPlaceholderResolver, entityIdPlaceholderResolver, thingPlaceholderResolver,
featurePlaceholderResolver, resourcePlaceholderResolver, timePlaceholderResolver);
final Set<JsonPointer> extraFields = filteredTopic.getExtraFields()
.map(JsonFieldSelector::getPointers)
.orElse(Collections.emptySet());
if (signal instanceof ThingEvent) {
return ThingEventToThingConverter.thingEventToThing((ThingEvent<?>) signal)
.filter(thing -> Thing3ValuePredicateVisitor.couldBeTrue(criteria, extraFields, thing,
topicPathPlaceholderResolver, entityIdPlaceholderResolver, thingPlaceholderResolver,
featurePlaceholderResolver, resourcePlaceholderResolver, timePlaceholderResolver))
.isPresent();
} else {
final Thing emptyThing = Thing.newBuilder().build();
return Thing3ValuePredicateVisitor.couldBeTrue(criteria, extraFields, emptyThing,
topicPathPlaceholderResolver, entityIdPlaceholderResolver, thingPlaceholderResolver,
featurePlaceholderResolver, resourcePlaceholderResolver, timePlaceholderResolver);
}
} else {
return true;
}
}
/**
* @throws org.eclipse.ditto.base.model.exceptions.InvalidRqlExpressionException if the filter string cannot be
* mapped to a valid criterion
*/
private static Criteria parseCriteria(final String filter, final DittoHeaders dittoHeaders,
final PlaceholderResolver<?>... placeholderResolvers) {
return QueryFilterCriteriaFactory.modelBased(RqlPredicateParser.getInstance(), placeholderResolvers)
.filterCriteria(filter, dittoHeaders);
}
private static Optional<Topic> topicFromSignal(final Signal<?> signal) {
if (signal instanceof PolicyAnnouncement) {
return Optional.of(Topic.POLICY_ANNOUNCEMENTS);
}
if (signal instanceof ConnectivityAnnouncement) {
return Optional.of(Topic.CONNECTION_ANNOUNCEMENTS);
}
// check things group
final TopicPath.Group group =
WithEntityId.getEntityIdOfType(ThingId.class, signal).isPresent() ? TopicPath.Group.THINGS : null;
final TopicPath.Channel channel = signal.getDittoHeaders()
.getChannel()
.flatMap(TopicPath.Channel::forName)
.orElse(TopicPath.Channel.TWIN);
final TopicPath.Criterion criterion = getCriterionOfSignal(signal);
if (TopicPath.Group.THINGS.equals(group)) {
if (TopicPath.Channel.TWIN.equals(channel)) {
if (EVENTS.equals(criterion)) {
return Optional.of(Topic.TWIN_EVENTS);
}
} else if (TopicPath.Channel.LIVE.equals(channel) && criterion != null) {
switch (criterion) {
case COMMANDS:
return Optional.of(Topic.LIVE_COMMANDS);
case EVENTS:
return Optional.of(Topic.LIVE_EVENTS);
case MESSAGES:
return Optional.of(Topic.LIVE_MESSAGES);
default:
return Optional.empty();
}
}
}
return Optional.empty();
}
@Nullable
private static TopicPath.Criterion getCriterionOfSignal(final Signal<?> signal) {
final TopicPath.Criterion criterion;
if (signal instanceof MessageCommand || signal instanceof MessageCommandResponse) {
criterion = TopicPath.Criterion.MESSAGES;
} else if (signal instanceof Command || signal instanceof CommandResponse) {
criterion = COMMANDS;
} else if (signal instanceof Event) {
criterion = EVENTS;
} else {
criterion = null;
}
return criterion;
}
}