-
Notifications
You must be signed in to change notification settings - Fork 215
/
ThingCommandResponseAcknowledgementProvider.java
104 lines (90 loc) · 4.41 KB
/
ThingCommandResponseAcknowledgementProvider.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
/*
* 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.edge.service.acknowledgements.things;
import static org.eclipse.ditto.base.model.acks.DittoAcknowledgementLabel.LIVE_RESPONSE;
import static org.eclipse.ditto.base.model.acks.DittoAcknowledgementLabel.TWIN_PERSISTED;
import java.util.Optional;
import org.eclipse.ditto.base.model.acks.AcknowledgementLabel;
import org.eclipse.ditto.base.model.acks.CommandResponseAcknowledgementProvider;
import org.eclipse.ditto.base.model.exceptions.DittoInternalErrorException;
import org.eclipse.ditto.base.model.signals.Signal;
import org.eclipse.ditto.base.model.signals.WithOptionalEntity;
import org.eclipse.ditto.base.model.signals.acks.Acknowledgement;
import org.eclipse.ditto.base.model.signals.commands.CommandResponse;
import org.eclipse.ditto.json.JsonValue;
import org.eclipse.ditto.things.model.signals.acks.ThingAcknowledgementFactory;
import org.eclipse.ditto.things.model.signals.commands.ThingCommand;
import org.eclipse.ditto.things.model.signals.commands.ThingCommandResponse;
/**
* Provides Acknowledgements specific for {@link ThingCommandResponse}s.
*
* @since 3.0.0
*/
public final class ThingCommandResponseAcknowledgementProvider
implements CommandResponseAcknowledgementProvider<ThingCommand<?>> {
private static final ThingCommandResponseAcknowledgementProvider
INSTANCE = new ThingCommandResponseAcknowledgementProvider();
/**
* Returns an instance of {@code ThingCommandResponseAcknowledgementProvider}.
*
* @return the instance.
*/
public static ThingCommandResponseAcknowledgementProvider getInstance() {
return INSTANCE;
}
@Override
public Acknowledgement provideAcknowledgement(final ThingCommand<?> originatingSignal,
final CommandResponse<?> commandResponse) {
if (commandResponse instanceof ThingCommandResponse<?> thingCommandResponse) {
final AcknowledgementLabel acknowledgementLabel = getAckLabelOfResponse(originatingSignal);
return ThingAcknowledgementFactory.newAcknowledgement(acknowledgementLabel,
thingCommandResponse.getEntityId(),
thingCommandResponse.getHttpStatus(),
thingCommandResponse.getDittoHeaders(),
getPayload(thingCommandResponse).orElse(null));
} else {
throw DittoInternalErrorException.newBuilder()
.dittoHeaders(originatingSignal.getDittoHeaders())
.cause(new IllegalArgumentException("The given commandResponse was not of type" +
" ThingCommandResponse. Check by isApplicable before passing a command response to this " +
"method."))
.build();
}
}
@Override
public boolean isApplicable(final CommandResponse<?> commandResponse) {
return commandResponse instanceof ThingCommandResponse<?>;
}
@Override
public Class<?> getCommandClass() {
return ThingCommand.class;
}
private static AcknowledgementLabel getAckLabelOfResponse(final Signal<?> signal) {
// check originating signal for ack label of response
// because live commands may generate twin responses due to live timeout fallback strategy
// smart channel commands are treated as live channel commands
final boolean isChannelLive = Signal.isChannelLive(signal);
final boolean isChannelSmart = Signal.isChannelSmart(signal);
return isChannelLive || isChannelSmart ? LIVE_RESPONSE : TWIN_PERSISTED;
}
private static Optional<JsonValue> getPayload(final ThingCommandResponse<?> thingCommandResponse) {
final Optional<JsonValue> result;
if (thingCommandResponse instanceof WithOptionalEntity) {
final WithOptionalEntity withOptionalEntity = (WithOptionalEntity) thingCommandResponse;
result = withOptionalEntity.getEntity(thingCommandResponse.getImplementedSchemaVersion());
} else {
result = Optional.empty();
}
return result;
}
}