-
Notifications
You must be signed in to change notification settings - Fork 215
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Concierge: Add an actor to handle live thing query commands with cust…
…om acknowledgement requests. Signed-off-by: Yufei Cai <yufei.cai@bosch.io>
- Loading branch information
Showing
4 changed files
with
176 additions
and
6 deletions.
There are no files selected for viewing
132 changes: 132 additions & 0 deletions
132
...a/org/eclipse/ditto/concierge/service/actors/LiveResponseAndAcknowledgementForwarder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
/* | ||
* Copyright (c) 2021 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.concierge.service.actors; | ||
|
||
import java.time.Duration; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
import org.eclipse.ditto.base.model.acks.AcknowledgementLabel; | ||
import org.eclipse.ditto.base.model.signals.Signal; | ||
import org.eclipse.ditto.base.model.signals.acks.Acknowledgement; | ||
import org.eclipse.ditto.base.model.signals.acks.Acknowledgements; | ||
import org.eclipse.ditto.internal.utils.akka.logging.DittoDiagnosticLoggingAdapter; | ||
import org.eclipse.ditto.internal.utils.akka.logging.DittoLoggerFactory; | ||
import org.eclipse.ditto.things.model.signals.commands.query.ThingQueryCommandResponse; | ||
|
||
import akka.actor.AbstractActor; | ||
import akka.actor.ActorRef; | ||
import akka.actor.Props; | ||
import akka.actor.ReceiveTimeout; | ||
import akka.japi.pf.ReceiveBuilder; | ||
|
||
/** | ||
* An actor to deal with a live thing query command expecting a response and requesting custom acknowledgements. | ||
* The sender of the live command is an actor in Concierge in order to apply policy enforcement on the response. | ||
* As a result, custom acknowledgements are also sent to Concierge, which must forward them to the acknowledgement | ||
* aggregator actor. | ||
*/ | ||
public final class LiveResponseAndAcknowledgementForwarder extends AbstractActor { | ||
|
||
private static final Duration DEFAULT_TIMEOUT = Duration.ofSeconds(60); | ||
|
||
private final DittoDiagnosticLoggingAdapter logger; | ||
private final ActorRef messageReceiver; | ||
private final ActorRef acknowledgementReceiver; | ||
private final Set<AcknowledgementLabel> pendingAcknowledgements; | ||
private boolean responseReceived = false; | ||
private ActorRef messageSender; | ||
|
||
private LiveResponseAndAcknowledgementForwarder(final Signal<?> liveSignal, | ||
final ActorRef messageReceiver, | ||
final ActorRef acknowledgementReceiver) { | ||
|
||
logger = DittoLoggerFactory.getDiagnosticLoggingAdapter(this); | ||
pendingAcknowledgements = new HashSet<>(); | ||
this.messageReceiver = messageReceiver; | ||
this.acknowledgementReceiver = acknowledgementReceiver; | ||
getContext().setReceiveTimeout(liveSignal.getDittoHeaders().getTimeout().orElse(DEFAULT_TIMEOUT)); | ||
for (final var ackRequest : liveSignal.getDittoHeaders().getAcknowledgementRequests()) { | ||
pendingAcknowledgements.add(ackRequest.getLabel()); | ||
} | ||
} | ||
|
||
/** | ||
* Create Props object for this actor. | ||
* | ||
* @param liveSignal The live signal whose acknowledgements and responses this actor listens for. | ||
* @param messageReceiver Receiver of the message to publish. | ||
* @param acknowledgementReceiver Receiver of acknowledgements. | ||
* @return The Props object. | ||
*/ | ||
public static Props props(final Signal<?> liveSignal, | ||
final ActorRef messageReceiver, | ||
final ActorRef acknowledgementReceiver) { | ||
return Props.create(LiveResponseAndAcknowledgementForwarder.class, liveSignal, messageReceiver, | ||
acknowledgementReceiver); | ||
} | ||
|
||
@Override | ||
public Receive createReceive() { | ||
return ReceiveBuilder.create() | ||
.match(ThingQueryCommandResponse.class, this::onQueryCommandResponse) | ||
.match(Acknowledgement.class, this::onAcknowledgement) | ||
.match(Acknowledgements.class, this::onAcknowledgements) | ||
.match(ReceiveTimeout.class, this::stopSelf) | ||
.matchAny(this::sendMessage) | ||
.build(); | ||
} | ||
|
||
private void sendMessage(final Object message) { | ||
logger.debug("Got message to send <{}>", message); | ||
messageSender = getSender(); | ||
messageReceiver.tell(message, getSelf()); | ||
} | ||
|
||
private void onAcknowledgement(final Acknowledgement ack) { | ||
logger.debug("Got <{}>", ack); | ||
pendingAcknowledgements.remove(ack.getLabel()); | ||
acknowledgementReceiver.forward(ack, getContext()); | ||
} | ||
|
||
private void onAcknowledgements(final Acknowledgements acks) { | ||
logger.debug("Got <{}>", acks); | ||
for (final var ack : acks) { | ||
pendingAcknowledgements.remove(ack.getLabel()); | ||
} | ||
acknowledgementReceiver.forward(acks, getContext()); | ||
} | ||
|
||
private void onQueryCommandResponse(final ThingQueryCommandResponse<?> response) { | ||
logger.debug("Got <{}>", response); | ||
responseReceived = true; | ||
if (messageSender != null) { | ||
messageSender.forward(response, getContext()); | ||
checkCompletion(); | ||
} else { | ||
logger.error("Got response without receiving command"); | ||
stopSelf("Message sender not found"); | ||
} | ||
} | ||
|
||
private void checkCompletion() { | ||
if (responseReceived && pendingAcknowledgements.isEmpty()) { | ||
stopSelf("All responses and acknowledgements delivered"); | ||
} | ||
} | ||
|
||
private void stopSelf(final Object trigger) { | ||
logger.debug("Stopping due to <{}>", trigger); | ||
getContext().stop(getSelf()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters