Merge "BUG-2329 Add test for anyxmls inside rpc resonse for netcfon-connector"
[controller.git] / opendaylight / adsal / protocol_plugins / openflow / 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     private ISwitch sw;
32     private Integer xid;
33     private OFMessage syncMsg;
34     protected CountDownLatch latch;
35     private Object result;
36     private boolean syncRequest;
37
38     public SynchronousMessage(ISwitch sw, Integer xid, OFMessage msg,
39             boolean syncRequest) {
40         this.sw = sw;
41         this.xid = xid;
42         syncMsg = msg;
43         latch = new CountDownLatch(1);
44         result = null;
45         this.syncRequest = syncRequest;
46     }
47
48     @Override
49     public Object call() throws Exception {
50         /*
51          * Send out message only if syncRequest is set to true. Otherwise, just
52          * wait for the Barrier response back.
53          */
54         if (syncRequest) {
55             sw.asyncSend(syncMsg, xid);
56             if (!(syncMsg instanceof OFBarrierRequest)) {
57                 OFBarrierRequest barrierMsg = new OFBarrierRequest();
58                 sw.asyncSend(barrierMsg, xid);
59             }
60         }
61         latch.await();
62         return result;
63     }
64
65     public Integer getXid() {
66         return this.xid;
67     }
68
69     public void wakeup() {
70         this.latch.countDown();
71     }
72
73     public void wakeup(OFError e) {
74         result = e;
75         wakeup();
76     }
77
78 }