Merge "BUG-6890: Enabling statistics rpc config through openflowplugin.cfg"
[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 javax.annotation.Nonnull;
13 import org.opendaylight.openflowjava.protocol.api.connection.OutboundQueue;
14 import org.opendaylight.openflowplugin.api.openflow.connection.OutboundQueueProvider;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierInput;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierInputBuilder;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20
21 public class OutboundQueueProviderImpl implements OutboundQueueProvider {
22     private static final Logger LOG = LoggerFactory.getLogger(OutboundQueueProviderImpl.class);
23     private final short ofVersion;
24     private volatile OutboundQueue outboundQueue;
25
26     public OutboundQueueProviderImpl(final short ofVersion) {
27         this.ofVersion = ofVersion;
28     }
29
30     @Nonnull
31     @Override
32     public BarrierInput createBarrierRequest(@Nonnull final Long xid) {
33         final BarrierInputBuilder biBuilder = new BarrierInputBuilder();
34         biBuilder.setVersion(ofVersion);
35         biBuilder.setXid(xid);
36         return biBuilder.build();
37
38     }
39
40     @Override
41     public synchronized void onConnectionQueueChanged(final OutboundQueue queue) {
42         LOG.debug("Replacing queue {} with {}", outboundQueue, queue);
43         outboundQueue = queue;
44         notifyAll();
45     }
46
47     @Override
48     public Long reserveEntry() {
49         for (;;) {
50             OutboundQueue queue = outboundQueue;
51             if (queue == null) {
52                 LOG.error("No queue present, failing request");
53                 return null;
54             }
55
56             final Long ret = queue.reserveEntry();
57             if (ret != null) {
58                 return ret;
59             }
60
61             LOG.debug("Reservation failed, trying to recover");
62             synchronized (this) {
63                 while (queue.equals(outboundQueue)) {
64                     LOG.debug("Queue {} is not replaced yet, going to sleep", queue);
65                     try {
66                         wait();
67                     } catch (InterruptedException e) {
68                         LOG.error("Interrupted while waiting for entry", e);
69                         return null;
70                     }
71                 }
72             }
73         }
74     }
75
76     @Override
77     public void commitEntry(final Long xid, final OfHeader message, final FutureCallback<OfHeader> callback) {
78         outboundQueue.commitEntry(xid, message, callback);
79     }
80 }