Merge "Bug 6235 - Cookie compare in FlowRegistryKeyFactory"
[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 /**
22  * Created by Martin Bobak <mbobak@cisco.com> on 12.5.2015.
23  */
24 public class OutboundQueueProviderImpl implements OutboundQueueProvider {
25     private static final Logger LOG = LoggerFactory.getLogger(OutboundQueueProviderImpl.class);
26     private final short ofVersion;
27     private volatile OutboundQueue outboundQueue;
28
29     public OutboundQueueProviderImpl(final short ofVersion) {
30         this.ofVersion = ofVersion;
31     }
32
33     @Nonnull
34     @Override
35     public BarrierInput createBarrierRequest(@Nonnull final Long xid) {
36         final BarrierInputBuilder biBuilder = new BarrierInputBuilder();
37         biBuilder.setVersion(ofVersion);
38         biBuilder.setXid(xid);
39         return biBuilder.build();
40
41     }
42
43     @Override
44     public synchronized void onConnectionQueueChanged(final OutboundQueue queue) {
45         LOG.debug("Replacing queue {} with {}", outboundQueue, queue);
46         outboundQueue = queue;
47         notifyAll();
48     }
49
50     @Override
51     public Long reserveEntry() {
52         for (;;) {
53             OutboundQueue queue = outboundQueue;
54             if (queue == null) {
55                 LOG.error("No queue present, failing request");
56                 return null;
57             }
58
59             final Long ret = queue.reserveEntry();
60             if (ret != null) {
61                 return ret;
62             }
63
64             LOG.debug("Reservation failed, trying to recover");
65             synchronized (this) {
66                 while (queue.equals(outboundQueue)) {
67                     LOG.debug("Queue {} is not replaced yet, going to sleep", queue);
68                     try {
69                         wait();
70                     } catch (InterruptedException e) {
71                         LOG.error("Interrupted while waiting for entry", e);
72                         return null;
73                     }
74                 }
75             }
76         }
77     }
78
79     @Override
80     public void commitEntry(final Long xid, final OfHeader message, final FutureCallback<OfHeader> callback) {
81         outboundQueue.commitEntry(xid, message, callback);
82     }
83 }