1 package org.opendaylight.openflowplugin.impl.device;
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.fail;
5 import com.google.common.util.concurrent.SettableFuture;
6 import java.util.Collections;
7 import java.util.concurrent.ExecutionException;
8 import java.util.concurrent.TimeUnit;
9 import java.util.concurrent.TimeoutException;
10 import org.junit.Assert;
11 import org.junit.Before;
12 import org.junit.Test;
13 import org.junit.runner.RunWith;
14 import org.mockito.Mock;
15 import org.mockito.Mockito;
16 import org.mockito.runners.MockitoJUnitRunner;
17 import org.opendaylight.controller.md.sal.binding.api.BindingTransactionChain;
18 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
19 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
20 import org.opendaylight.controller.md.sal.binding.api.ReadTransaction;
21 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
22 import org.opendaylight.openflowplugin.api.OFConstants;
23 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
24 import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
25 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
26 import org.opendaylight.openflowplugin.api.openflow.device.Xid;
27 import org.opendaylight.openflowplugin.api.openflow.device.XidGenerator;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetAsyncOutput;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetAsyncOutputBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetAsyncReply;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.async.body.grouping.FlowRemovedMask;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.async.body.grouping.PacketInMask;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.async.body.grouping.PortStatusMask;
34 import org.opendaylight.yangtools.yang.common.RpcResult;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
38 @RunWith(MockitoJUnitRunner.class)
39 public class DeviceContextImplTest {
40 private static final Logger LOG = LoggerFactory
41 .getLogger(DeviceContextImplTest.class);
43 DeviceContextImpl deviceContext;
45 RequestContext<GetAsyncReply> requestContext;
47 ConnectionContext connectionContext;
49 DeviceState deviceState;
51 DataBroker dataBroker;
55 ReadOnlyTransaction rTx;
57 BindingTransactionChain txChainFactory;
61 Mockito.when(txChainFactory.newWriteOnlyTransaction()).thenReturn(wTx);
62 Mockito.when(dataBroker.createTransactionChain(Mockito.any(DeviceContextImpl.class))).thenReturn(txChainFactory);
63 Mockito.when(dataBroker.newReadOnlyTransaction()).thenReturn(rTx);
64 deviceContext = new DeviceContextImpl(connectionContext, deviceState, dataBroker);
65 xidGen = new XidGenerator();
66 final SettableFuture<RpcResult<GetAsyncReply>> settableFuture = SettableFuture.create();
67 Mockito.when(requestContext.getFuture()).thenReturn(settableFuture);
68 deviceContext.hookRequestCtx(deviceContext.getNextXid(), requestContext);
71 @Test(expected=NullPointerException.class)
72 public void testDeviceContextImplConstructorNullConnectionContext() {
73 new DeviceContextImpl(null, deviceState, dataBroker);
76 @Test(expected=NullPointerException.class)
77 public void testDeviceContextImplConstructorNullDataBroker() {
78 new DeviceContextImpl(connectionContext, deviceState, null);
81 @Test(expected=NullPointerException.class)
82 public void testDeviceContextImplConstructorNullDeviceState() {
83 new DeviceContextImpl(connectionContext, null, dataBroker);
87 public void testGetDeviceState() {
88 final DeviceState deviceSt = deviceContext.getDeviceState();
89 Assert.assertNotNull(deviceSt);
90 Assert.assertEquals(deviceState, deviceSt);
94 public void testGetReadTransaction() {
95 final ReadTransaction readTx = deviceContext.getReadTransaction();
96 Assert.assertNotNull(readTx);
97 Assert.assertEquals(rTx, readTx);
101 public void testGetWriteTransaction() {
102 final WriteTransaction writeTx = deviceContext.getWriteTransaction();
103 Assert.assertNotNull(writeTx);
104 Assert.assertEquals(wTx, writeTx);
108 public void testProcessReply() {
109 final Xid xid = xidGen.generate();
110 final GetAsyncOutput asyncOutput = createAsyncOutput(xid);
111 LOG.info("Hooking RequestContext");
112 deviceContext.hookRequestCtx(xid, requestContext);
113 Assert.assertEquals(requestContext, deviceContext.getRequests().get(xid));
115 Assert.assertFalse(requestContext.getFuture().isDone());
116 LOG.info("Sending reply from device");
117 deviceContext.processReply(xid, asyncOutput);
118 Assert.assertTrue(requestContext.getFuture().isDone());
120 LOG.info("Checking RequestContext.future");
122 final Object object = requestContext.getFuture().get(1L, TimeUnit.SECONDS);
123 final GetAsyncOutput getAsyncOutput = (GetAsyncOutput) object;
124 assertEquals(asyncOutput.getVersion(), getAsyncOutput.getVersion());
125 } catch (InterruptedException | ExecutionException | TimeoutException e) {
126 LOG.error("Test failed when checking RequestContext.future", e);
131 private GetAsyncOutput createAsyncOutput(final Xid xid) {
132 final GetAsyncOutputBuilder asyncOutputBuilder = new GetAsyncOutputBuilder();
133 asyncOutputBuilder.setFlowRemovedMask(Collections.<FlowRemovedMask> emptyList());
134 asyncOutputBuilder.setPacketInMask(Collections.<PacketInMask> emptyList());
135 asyncOutputBuilder.setPortStatusMask(Collections.<PortStatusMask> emptyList());
136 asyncOutputBuilder.setVersion(OFConstants.OFP_VERSION_1_3);
137 asyncOutputBuilder.setXid(xid.getValue());
138 return asyncOutputBuilder.build();