04c8dd4d80da3b366f5d05c22cc62b815530f828
[controller.git] / opendaylight / protocol_plugins / openflow_netty / src / main / java / org / opendaylight / controller / protocol_plugin / openflow / core / internal / SynchronousMessage.java
1 /*
2  * Copyright (c) 2013 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.controller.protocol_plugin.openflow.core.internal;
10
11 import java.util.concurrent.Callable;
12 import java.util.concurrent.CountDownLatch;
13
14 import org.opendaylight.controller.protocol_plugin.openflow.core.ISwitch;
15 import org.openflow.protocol.OFBarrierRequest;
16 import org.openflow.protocol.OFError;
17 import org.openflow.protocol.OFMessage;
18
19 /**
20  * This class implements synchronous operations on message send to a switch. If
21  * syncRequest is set to true, it sends the requested message to the switch
22  * followed by a Barrier request message. It returns the result once it gets the
23  * reply from the switch or after a timeout. If the protocol does not dictate
24  * the switch to reply the processing status for a particular message, the
25  * Barrier request forces the switch to reply saying whether or not the message
26  * processing was successful for messages sent to the switch up to this point.
27  * If syncRequest is false, it simply skips the message send and just waits for
28  * the response back.
29  */
30 public class SynchronousMessage implements Callable<Object> {
31
32
33
34
35     private ISwitch sw;
36     private Integer xid;
37     private OFMessage syncMsg;
38     protected CountDownLatch latch;
39     private Object result;
40     private boolean syncRequest;
41
42     public SynchronousMessage(ISwitch sw, Integer xid, OFMessage msg,
43             boolean syncRequest) {
44         this.sw = sw;
45         this.xid = xid;
46         syncMsg = msg;
47         latch = new CountDownLatch(1);
48         result = null;
49         this.syncRequest = syncRequest;
50     }
51
52     @Override
53     public Object call() throws Exception {
54         /*
55          * Send out message only if syncRequest is set to true. Otherwise, just
56          * wait for the Barrier response back.
57          */
58
59         if (syncRequest) {
60             sw.asyncSend(syncMsg, xid);
61             if (!(syncMsg instanceof OFBarrierRequest)) {
62                 OFBarrierRequest barrierMsg = new OFBarrierRequest();
63                 sw.asyncSend(barrierMsg, xid);
64             }
65         }
66         latch.await();
67         return result;
68     }
69
70     public Integer getXid() {
71         return this.xid;
72     }
73
74     public void wakeup() {
75         this.latch.countDown();
76     }
77
78     public void wakeup(OFError e) {
79         result = e;
80         wakeup();
81     }
82
83
84 }