/
MqttConsumerActor.java
176 lines (152 loc) · 7.48 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
172
173
174
175
176
/*
* Copyright (c) 2017-2018 Bosch Software Innovations GmbH.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/org/documents/epl-2.0/index.php
* 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.time.Instant;
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.AddressMetric;
import org.eclipse.ditto.model.connectivity.ConnectionStatus;
import org.eclipse.ditto.model.connectivity.ConnectivityModelFactory;
import org.eclipse.ditto.model.connectivity.Enforcement;
import org.eclipse.ditto.services.connectivity.messaging.internal.RetrieveAddressMetric;
import org.eclipse.ditto.services.models.connectivity.ExternalMessage;
import org.eclipse.ditto.services.models.connectivity.ExternalMessageFactory;
import org.eclipse.ditto.services.models.connectivity.placeholder.EnforcementFactoryFactory;
import org.eclipse.ditto.services.models.connectivity.placeholder.EnforcementFilter;
import org.eclipse.ditto.services.models.connectivity.placeholder.EnforcementFilterFactory;
import org.eclipse.ditto.services.utils.akka.LogUtil;
import akka.actor.AbstractActor;
import akka.actor.ActorRef;
import akka.actor.Props;
import akka.event.DiagnosticLoggingAdapter;
import akka.japi.Creator;
import akka.stream.alpakka.mqtt.MqttMessage;
public class MqttConsumerActor extends AbstractActor {
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 messageMappingProcessor;
private final AuthorizationContext sourceAuthorizationContext;
// private final Enforcement enforcement;
private long consumedMessages = 0L;
private Instant lastMessageConsumedAt;
private final AddressMetric addressMetric;
private final ActorRef deadLetters;
private final boolean dryRun;
@Nullable private final EnforcementFilterFactory<String, String> enforcementFilterFactory;
private MqttConsumerActor(final ActorRef messageMappingProcessor,
final AuthorizationContext sourceAuthorizationContext,
final Enforcement enforcement,
final boolean dryRun) {
this.messageMappingProcessor = messageMappingProcessor;
this.sourceAuthorizationContext = sourceAuthorizationContext;
// this.enforcement = enforcement;
this.dryRun = dryRun;
addressMetric =
ConnectivityModelFactory.newAddressMetric(ConnectionStatus.OPEN, "Started at " + Instant.now(),
0, null);
deadLetters = getContext().system().deadLetters();
if (enforcement != null) {
this.enforcementFilterFactory = EnforcementFactoryFactory.newThingIdEnforcementFactory(enforcement,
TopicPlaceholder.INSTANCE);
} else {
enforcementFilterFactory = null;
}
}
static Props props(final ActorRef messageMappingProcessor,
final AuthorizationContext sourceAuthorizationContext,
final Enforcement enforcement,
final boolean dryRun) {
return Props.create(MqttConsumerActor.class, new Creator<MqttConsumerActor>() {
private static final long serialVersionUID = 1L;
@Override
public MqttConsumerActor create() {
return new MqttConsumerActor(messageMappingProcessor, sourceAuthorizationContext, enforcement,
dryRun);
}
});
}
@Override
public Receive createReceive() {
return receiveBuilder()
.match(MqttMessage.class, this::isDryRun, message -> {
log.info("Dropping message in dryRun mode: {}", message);
})
.match(MqttMessage.class, message -> {
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(sourceAuthorizationContext)
.withEnforcement(getEnforcementFilter(message.topic()))
.build();
lastMessageConsumedAt = Instant.now();
consumedMessages++;
messageMappingProcessor.tell(externalMessage, getSelf());
replyStreamAck();
})
.match(RetrieveAddressMetric.class, ram -> {
final AddressMetric addressMetric = ConnectivityModelFactory.newAddressMetric(
this.addressMetric != null ? this.addressMetric.getStatus() : ConnectionStatus.UNKNOWN,
this.addressMetric != null ? this.addressMetric.getStatusDetails().orElse(null) : null,
consumedMessages, lastMessageConsumedAt);
log.debug("addressMetric: {}", addressMetric);
getSender().tell(addressMetric, getSelf());
})
.match(MqttClientActor.ConsumerStreamMessage.class, this::handleConsumerStreamMessage)
.matchAny(unhandled -> {
log.info("Unhandled message: {}", unhandled);
unhandled(unhandled);
})
.build();
}
@Nullable
private EnforcementFilter<String> getEnforcementFilter(final String topic) {
if (enforcementFilterFactory != null) {
return enforcementFilterFactory.getFilter(topic);
// TODO error message
} 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());
}
}
private static String getIdEnforcementErrorMessage(final String topic) {
return String.format("The MQTT topic ''%s'' of the Ditto protocol message does not match any message filter " +
"configured for the connection.", topic);
}
}