/
MqttConsumerActor.java
171 lines (152 loc) · 7.31 KB
/
MqttConsumerActor.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
/*
* 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.services.connectivity.messaging.mqtt;
import static org.eclipse.ditto.services.connectivity.messaging.mqtt.MqttClientActor.ConsumerStreamMessage.STREAM_ACK;
import java.util.HashMap;
import java.util.Objects;
import javax.annotation.Nullable;
import org.eclipse.ditto.model.base.auth.AuthorizationContext;
import org.eclipse.ditto.model.connectivity.Enforcement;
import org.eclipse.ditto.model.placeholders.EnforcementFactoryFactory;
import org.eclipse.ditto.model.placeholders.EnforcementFilter;
import org.eclipse.ditto.model.placeholders.EnforcementFilterFactory;
import org.eclipse.ditto.model.placeholders.PlaceholderFactory;
import org.eclipse.ditto.services.connectivity.messaging.BaseConsumerActor;
import org.eclipse.ditto.services.connectivity.messaging.internal.RetrieveAddressStatus;
import org.eclipse.ditto.services.models.connectivity.ExternalMessage;
import org.eclipse.ditto.services.models.connectivity.ExternalMessageFactory;
import org.eclipse.ditto.services.utils.akka.LogUtil;
import akka.actor.ActorRef;
import akka.actor.Props;
import akka.event.DiagnosticLoggingAdapter;
import akka.japi.Creator;
import akka.routing.ConsistentHashingRouter;
import akka.stream.alpakka.mqtt.MqttMessage;
/**
* Actor which receives message from a MQTT broker and forwards them to a {@code MessageMappingProcessorActor}.
*/
public final class MqttConsumerActor extends BaseConsumerActor {
static final String ACTOR_NAME_PREFIX = "mqttConsumer-";
private static final String MQTT_TOPIC_HEADER = "mqtt.topic";
private final DiagnosticLoggingAdapter log = LogUtil.obtain(this);
private final ActorRef deadLetters;
private final boolean dryRun;
@Nullable private final EnforcementFilterFactory<String, String> topicEnforcementFilterFactory;
private MqttConsumerActor(final String connectionId, final ActorRef messageMappingProcessor,
final AuthorizationContext sourceAuthorizationContext, @Nullable final Enforcement enforcement,
final boolean dryRun, final String sourceAddress) {
super(connectionId, sourceAddress, messageMappingProcessor, sourceAuthorizationContext, null);
this.dryRun = dryRun;
deadLetters = getContext().system().deadLetters();
if (enforcement != null) {
this.topicEnforcementFilterFactory = EnforcementFactoryFactory.newEnforcementFilterFactory(enforcement,
PlaceholderFactory.newSourceAddressPlaceholder());
} else {
topicEnforcementFilterFactory = null;
}
}
/**
* Creates Akka configuration object for this actor.
*
* @param connectionId ID of the connection this consumer is belongs to
* @param messageMappingProcessor the ActorRef to the {@code MessageMappingProcessor}
* @param sourceAuthorizationContext the {@link AuthorizationContext} of the source
* @param enforcement the optional Enforcement to apply
* @param dryRun whether this is a dry-run/connection test or not
* @param topic the topic for which this consumer receives messages
* @return the Akka configuration Props object.
*/
static Props props(final String connectionId, final ActorRef messageMappingProcessor,
final AuthorizationContext sourceAuthorizationContext,
@Nullable final Enforcement enforcement,
final boolean dryRun, final String topic) {
return Props.create(MqttConsumerActor.class, new Creator<MqttConsumerActor>() {
private static final long serialVersionUID = 1L;
@Override
public MqttConsumerActor create() {
return new MqttConsumerActor(connectionId, messageMappingProcessor, sourceAuthorizationContext,
enforcement,
dryRun, topic);
}
});
}
@Override
public Receive createReceive() {
return receiveBuilder()
.match(MqttMessage.class, this::isDryRun,
message -> log.info("Dropping message in dryRun mode: {}", message))
.match(MqttMessage.class, this::handleMqttMessage)
.match(RetrieveAddressStatus.class, ram -> getSender().tell(getCurrentSourceStatus(), getSelf()))
.match(MqttClientActor.ConsumerStreamMessage.class, this::handleConsumerStreamMessage)
.matchAny(unhandled -> {
log.info("Unhandled message: {}", unhandled);
unhandled(unhandled);
})
.build();
}
private void handleMqttMessage(final MqttMessage message) {
try {
if (log.isDebugEnabled()) {
log.debug("Received MQTT message on topic <{}>: {}", message.topic(),
message.payload().utf8String());
}
final HashMap<String, String> headers = new HashMap<>();
headers.put(MQTT_TOPIC_HEADER, message.topic());
final ExternalMessage externalMessage = ExternalMessageFactory.newExternalMessageBuilder(headers)
.withBytes(message.payload().toByteBuffer())
.withAuthorizationContext(authorizationContext)
.withEnforcement(getEnforcementFilter(message.topic()))
.withSourceAddress(sourceAddress)
.build();
inboundCounter.recordSuccess();
final Object msg = new ConsistentHashingRouter.ConsistentHashableEnvelope(externalMessage, message.topic());
messageMappingProcessor.tell(msg, getSelf());
replyStreamAck();
} catch (final Exception e) {
inboundCounter.recordFailure();
log.info("Failed to handle MQTT message: {}", e.getMessage());
}
}
@Nullable
private EnforcementFilter getEnforcementFilter(final String topic) {
if (topicEnforcementFilterFactory != null) {
return topicEnforcementFilterFactory.getFilter(topic);
} else {
return null;
}
}
private boolean isDryRun(final Object message) {
return dryRun;
}
private void handleConsumerStreamMessage(final MqttClientActor.ConsumerStreamMessage message) {
switch (message) {
case STREAM_STARTED:
replyStreamAck();
break;
case STREAM_ENDED:
// sometimes Akka sends STREAM_ENDED out-of-band before the last stream element
log.info("Underlying stream completed, waiting for shutdown by parent.");
break;
case STREAM_ACK:
log.error("Protocol violation: STREAM_ACK");
break;
}
}
private void replyStreamAck() {
final ActorRef sender = getSender();
// check sender against deadLetters because stream actor terminates itself before waiting for the final ACK
if (!Objects.equals(sender, deadLetters)) {
sender.tell(STREAM_ACK, getSelf());
}
}
}