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.base.Preconditions;
11 import com.google.common.util.concurrent.FutureCallback;
12 import org.opendaylight.openflowjava.protocol.api.connection.OutboundQueueException;
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierInput;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReplyMessage;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketOutInput;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
20 final class OutboundQueueEntry {
21 private static final Logger LOG = LoggerFactory.getLogger(OutboundQueueEntry.class);
22 private FutureCallback<OfHeader> callback;
23 private OfHeader message;
24 private boolean completed;
25 private boolean barrier;
26 private volatile boolean committed;
28 void commit(final OfHeader message, final FutureCallback<OfHeader> callback) {
29 this.message = message;
30 this.callback = callback;
31 this.barrier = message instanceof BarrierInput;
33 // Volatile write, needs to be last
43 // Volatile write, needs to be last
51 boolean isCommitted() {
55 boolean isCompleted() {
59 OfHeader takeMessage() {
60 final OfHeader ret = message;
62 checkCompletionNeed();
68 private void checkCompletionNeed() {
69 if (callback == null || (message instanceof PacketOutInput)) {
71 if (callback != null) {
72 callback.onSuccess(null);
79 boolean complete(final OfHeader response) {
80 Preconditions.checkState(!completed, "Attempted to complete a completed message with response %s", response);
82 // Multipart requests are special, we have to look at them to see
83 // if there is something outstanding and adjust ourselves accordingly
84 final boolean reallyComplete;
85 if (response instanceof MultipartReplyMessage) {
86 reallyComplete = !((MultipartReplyMessage) response).getFlags().isOFPMPFREQMORE();
87 LOG.debug("Multipart reply {}", response);
89 reallyComplete = true;
92 completed = reallyComplete;
93 if (callback != null) {
94 callback.onSuccess(response);
96 // We will not need the callback anymore, make sure it can be GC'd
100 LOG.debug("Entry {} completed {} with response {}", this, completed, response);
101 return reallyComplete;
104 void fail(final OutboundQueueException cause) {
107 if (callback != null) {
108 callback.onFailure(cause);
112 LOG.warn("Ignoring failure {} for completed message", cause);