5e613e8642489c0dce449fdcb242700406142f20
[controller.git] / opendaylight / protocol_plugins / openflow / src / main / java / org / opendaylight / controller / protocol_plugin / openflow / core / internal / SynchronousMessage.java
1
2 /*
3  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
4  *
5  * This program and the accompanying materials are made available under the
6  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
7  * and is available at http://www.eclipse.org/legal/epl-v10.html
8  */
9
10 package org.opendaylight.controller.protocol_plugin.openflow.core.internal;
11
12 import java.util.concurrent.Callable;
13 import java.util.concurrent.CountDownLatch;
14
15 import org.opendaylight.controller.protocol_plugin.openflow.core.ISwitch;
16 import org.openflow.protocol.OFBarrierRequest;
17 import org.openflow.protocol.OFError;
18 import org.openflow.protocol.OFMessage;
19
20 /**
21  * Implements the synchronous message send to a switch
22  * It sends the requested message to the switch followed by a barrier request message
23  * It returns the result once it gets the reply from the switch or after a timeout
24  * If the protocol does not dictate the switch to reply the processing status for a particular message
25  * the barrier request forces the switch to reply saying whether or not the message processing was
26  * successful for messages sent to the switch up to this point
27  *
28  *
29  *
30  */
31 public class SynchronousMessage implements Callable<Object> {
32     private ISwitch sw;
33     private Integer xid;
34     private OFMessage syncMsg;
35     protected CountDownLatch latch;
36     private Object result;
37
38     public SynchronousMessage(ISwitch sw, Integer xid, OFMessage msg) {
39         this.sw = sw;
40         this.xid = xid;
41         syncMsg = msg;
42         latch = new CountDownLatch(1);
43         result = null;
44     }
45
46     @Override
47     public Object call() throws Exception {
48         sw.asyncSend(syncMsg, xid);
49         OFBarrierRequest barrierMsg = new OFBarrierRequest();
50         sw.asyncSend(barrierMsg, xid);
51         latch.await();
52         return result;
53     }
54
55     public Integer getXid() {
56         return this.xid;
57     }
58
59     public void wakeup() {
60         this.latch.countDown();
61     }
62
63     public void wakeup(OFError e) {
64         result = e;
65         wakeup();
66     }
67
68 }