Disconnection improvements.
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / connection / OutboundQueueProviderImpl.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. 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
9 package org.opendaylight.openflowplugin.impl.connection;
10
11 import com.google.common.util.concurrent.FutureCallback;
12 import java.util.function.Function;
13 import javax.annotation.Nonnull;
14 import org.opendaylight.openflowjava.protocol.api.connection.OutboundQueue;
15 import org.opendaylight.openflowplugin.api.openflow.connection.OutboundQueueProvider;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierInput;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierInputBuilder;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 public class OutboundQueueProviderImpl implements OutboundQueueProvider {
23     private static final Logger LOG = LoggerFactory.getLogger(OutboundQueueProviderImpl.class);
24     private final short ofVersion;
25     private volatile OutboundQueue outboundQueue;
26
27     public OutboundQueueProviderImpl(final short ofVersion) {
28         this.ofVersion = ofVersion;
29     }
30
31     @Nonnull
32     @Override
33     public BarrierInput createBarrierRequest(@Nonnull final Long xid) {
34         final BarrierInputBuilder biBuilder = new BarrierInputBuilder();
35         biBuilder.setVersion(ofVersion);
36         biBuilder.setXid(xid);
37         return biBuilder.build();
38
39     }
40
41     @Override
42     public synchronized void onConnectionQueueChanged(final OutboundQueue queue) {
43         if (LOG.isDebugEnabled()) {
44             LOG.debug("Replacing queue {} with {}", outboundQueue, queue);
45         }
46         outboundQueue = queue;
47         notifyAll();
48     }
49
50     @Override
51     public Long reserveEntry() {
52         if (LOG.isDebugEnabled()) {
53             LOG.debug("Reserve entry with queue: {} in this {} implementation", outboundQueue, this);
54         }
55         for (;;) {
56             OutboundQueue queue = outboundQueue;
57             if (queue == null) {
58                 LOG.error("No queue present, failing request");
59                 return null;
60             }
61
62             final Long ret = queue.reserveEntry();
63             if (ret != null) {
64                 return ret;
65             }
66
67             LOG.debug("Reservation failed, trying to recover");
68             synchronized (this) {
69                 while (queue.equals(outboundQueue)) {
70                     LOG.debug("Queue {} is not replaced yet, going to sleep", queue);
71                     try {
72                         wait();
73                     } catch (InterruptedException e) {
74                         LOG.error("Interrupted while waiting for entry", e);
75                         return null;
76                     }
77                 }
78             }
79         }
80     }
81
82     @Override
83     public void commitEntry(final Long xid, final OfHeader message, final FutureCallback<OfHeader> callback) {
84         outboundQueue.commitEntry(xid, message, callback);
85     }
86
87     @Override
88     public void commitEntry(final Long xid, final OfHeader message, final FutureCallback<OfHeader> callback,
89             final Function<OfHeader, Boolean> isComplete) {
90         outboundQueue.commitEntry(xid, message, callback, isComplete);
91     }
92 }