c0c2f764f3aa88c9b12da74ed5878f298d74fa0b
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / core / connection / OutboundQueueEntry.java
1 /*
2  * Copyright (c) 2015 Pantheon Technologies s.r.o. and others. All rights reserved.
3  *
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
7  */
8 package org.opendaylight.openflowjava.protocol.impl.core.connection;
9
10 import com.google.common.base.Preconditions;
11 import com.google.common.util.concurrent.FutureCallback;
12 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierInput;
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReplyMessage;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
15 import org.slf4j.Logger;
16 import org.slf4j.LoggerFactory;
17
18 final class OutboundQueueEntry {
19     private static final Logger LOG = LoggerFactory.getLogger(OutboundQueueEntry.class);
20     private FutureCallback<OfHeader> callback;
21     private OfHeader message;
22     private boolean completed;
23     private volatile boolean committed;
24
25     void commit(final OfHeader message, final FutureCallback<OfHeader> callback) {
26         this.message = message;
27         this.callback = callback;
28
29         // Volatile write, needs to be last
30         committed = true;
31     }
32
33     void reset() {
34         callback = null;
35         message = null;
36         completed = false;
37
38         // Volatile write, needs to be last
39         committed = false;
40     }
41
42     boolean isBarrier() {
43         return message instanceof BarrierInput;
44     }
45
46     boolean isCommitted() {
47         return committed;
48     }
49
50     boolean isCompleted() {
51         return completed;
52     }
53
54     OfHeader getMessage() {
55         return message;
56     }
57
58     boolean complete(final OfHeader response) {
59         Preconditions.checkState(!completed, "Attempted to complete a completed message %s with response %s", message, response);
60
61         // Multipart requests are special, we have to look at them to see
62         // if there is something outstanding and adjust ourselves accordingly
63         final boolean reallyComplete;
64         if (response instanceof MultipartReplyMessage) {
65             reallyComplete = !((MultipartReplyMessage) response).getFlags().isOFPMPFREQMORE();
66             LOG.debug("Multipart reply {}", response);
67         } else {
68             reallyComplete = true;
69         }
70
71         completed = reallyComplete;
72         if (callback != null) {
73             callback.onSuccess(response);
74         }
75         LOG.debug("Entry {} completed {} with response {}", this, completed, response);
76         return reallyComplete;
77     }
78
79     void fail(final Throwable cause) {
80         if (!completed) {
81             completed = true;
82             if (callback != null) {
83                 callback.onFailure(cause);
84             }
85         } else {
86             LOG.warn("Ignoring failure {} for completed message {}", cause, message);
87         }
88     }
89
90 }