2 * Copyright (c) 2015 Pantheon Technologies s.r.o. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.openflowjava.protocol.impl.core.connection;
10 import com.google.common.annotations.VisibleForTesting;
11 import com.google.common.base.Preconditions;
12 import com.google.common.util.concurrent.FutureCallback;
14 import java.util.function.Function;
16 import org.opendaylight.openflowjava.protocol.api.connection.OutboundQueueException;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierInput;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReplyMessage;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketOutInput;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
25 final class OutboundQueueEntry {
26 private static final Logger LOG = LoggerFactory.getLogger(OutboundQueueEntry.class);
27 public static final Function<OfHeader, Boolean> DEFAULT_IS_COMPLETE = new Function<OfHeader, Boolean>() {
30 public Boolean apply(final OfHeader message) {
31 if (message instanceof MultipartReplyMessage) {
32 return !((MultipartReplyMessage) message).getFlags().isOFPMPFREQMORE();
40 private FutureCallback<OfHeader> callback;
41 private OfHeader message;
42 private boolean completed;
43 private boolean barrier;
44 private volatile boolean committed;
45 private OutboundQueueException lastException = null;
46 private Function<OfHeader, Boolean> isCompletedFunction = DEFAULT_IS_COMPLETE;
48 void commit(final OfHeader message, final FutureCallback<OfHeader> callback) {
49 commit(message, callback, DEFAULT_IS_COMPLETE);
52 void commit(final OfHeader message, final FutureCallback<OfHeader> callback,
53 final Function<OfHeader, Boolean> isCompletedFunction) {
55 LOG.warn("Can't commit a completed message.");
56 if (callback != null) {
57 callback.onFailure(lastException);
60 this.message = message;
61 this.callback = callback;
62 this.barrier = message instanceof BarrierInput;
63 this.isCompletedFunction = isCompletedFunction;
65 // Volatile write, needs to be last
66 this.committed = true;
76 // Volatile write, needs to be last
84 boolean isCommitted() {
88 boolean isCompleted() {
92 OfHeader takeMessage() {
93 final OfHeader ret = message;
95 checkCompletionNeed();
101 private void checkCompletionNeed() {
102 if (callback == null || (message instanceof PacketOutInput)) {
104 if (callback != null) {
105 callback.onSuccess(null);
112 boolean complete(final OfHeader response) {
113 Preconditions.checkState(!completed, "Attempted to complete a completed message with response %s", response);
115 // Multipart requests are special, we have to look at them to see
116 // if there is something outstanding and adjust ourselves accordingly
117 final boolean reallyComplete = isCompletedFunction.apply(response);
119 completed = reallyComplete;
120 if (callback != null) {
121 callback.onSuccess(response);
122 if (reallyComplete) {
123 // We will not need the callback anymore, make sure it can be GC'd
127 LOG.debug("Entry {} completed {} with response {}", this, completed, response);
128 return reallyComplete;
131 void fail(final OutboundQueueException cause) {
133 lastException = cause;
135 if (callback != null) {
136 callback.onFailure(cause);
140 LOG.warn("Ignoring failure {} for completed message", cause);
145 /** This method is only for testing to prove that after queue entry is completed there is not callback future */
146 boolean hasCallback() {
147 return (callback != null);