b6efbffee913890254928c9d67c6f61e1188d9d4
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / services / SalFlowServiceImplTest.java
1 package org.opendaylight.openflowplugin.impl.services;
2
3
4 import static org.mockito.Mockito.mock;
5 import static org.mockito.Mockito.when;
6
7 import com.google.common.util.concurrent.Futures;
8 import java.math.BigInteger;
9 import java.util.concurrent.ExecutionException;
10 import java.util.concurrent.Future;
11 import junit.framework.TestCase;
12 import org.junit.Before;
13 import org.junit.Test;
14 import org.junit.runner.RunWith;
15 import org.mockito.Matchers;
16 import org.mockito.Mock;
17 import org.mockito.Mockito;
18 import org.mockito.runners.MockitoJUnitRunner;
19 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
20 import org.opendaylight.openflowjava.protocol.api.connection.OutboundQueue;
21 import org.opendaylight.openflowplugin.api.OFConstants;
22 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
23 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
24 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
25 import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
26 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
27 import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
28 import org.opendaylight.openflowplugin.api.openflow.device.Xid;
29 import org.opendaylight.openflowplugin.api.openflow.registry.flow.DeviceFlowRegistry;
30 import org.opendaylight.openflowplugin.api.openflow.registry.flow.FlowDescriptor;
31 import org.opendaylight.openflowplugin.api.openflow.registry.flow.FlowRegistryKey;
32 import org.opendaylight.openflowplugin.api.openflow.rpc.listener.ItemLifecycleListener;
33 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInput;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowOutput;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowInput;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowOutput;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.UpdateFlowInput;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.flow.update.OriginalFlow;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.flow.update.UpdatedFlow;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowRef;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
55 import org.opendaylight.yangtools.yang.binding.DataObject;
56 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
57 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
58 import org.opendaylight.yangtools.yang.common.RpcResult;
59 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
60
61 @RunWith(MockitoJUnitRunner.class)
62 public class SalFlowServiceImplTest extends TestCase {
63
64     private static final BigInteger DUMMY_DATAPATH_ID = new BigInteger("444");
65     private static final Short DUMMY_VERSION = OFConstants.OFP_VERSION_1_3;
66     private static final String DUMMY_NODE_ID = "dummyNodeID";
67     private static final String DUMMY_FLOW_ID = "dummyFlowID";
68     private static final Short DUMMY_TABLE_ID = (short) 0;
69
70     private static final KeyedInstanceIdentifier<Node, NodeKey> NODE_II
71             = InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(new NodeId(DUMMY_NODE_ID)));
72
73     private static final KeyedInstanceIdentifier<Table, TableKey> TABLE_II
74             = NODE_II.augmentation(FlowCapableNode.class).child(Table.class, new TableKey(DUMMY_TABLE_ID));
75
76     @Mock
77     private RequestContextStack mockedRequestContextStack;
78     @Mock
79     private DeviceContext mockedDeviceContext;
80     @Mock
81     private ConnectionContext mockedPrimConnectionContext;
82     @Mock
83     private FeaturesReply mockedFeatures;
84     @Mock
85     private ConnectionAdapter mockedConnectionAdapter;
86     @Mock
87     private MessageSpy mockedMessagSpy;
88     @Mock
89     private RequestContext<Object> requestContext;
90     @Mock
91     private OutboundQueue outboundQueue;
92     @Mock
93     private Match match;
94     private SalFlowServiceImpl salFlowService;
95
96     @Mock
97     private DeviceState mockedDeviceState;
98     @Mock
99     private DeviceInfo mockedDeviceInfo;
100     @Mock
101     private DeviceFlowRegistry deviceFlowRegistry;
102     @Mock
103     private GetFeaturesOutput mockedFeaturesOutput;
104
105     @Before
106     public void initialization() {
107         when(mockedFeatures.getDatapathId()).thenReturn(DUMMY_DATAPATH_ID);
108         when(mockedFeatures.getVersion()).thenReturn(DUMMY_VERSION);
109         when(mockedFeaturesOutput.getDatapathId()).thenReturn(DUMMY_DATAPATH_ID);
110         when(mockedFeaturesOutput.getVersion()).thenReturn(DUMMY_VERSION);
111
112         when(mockedPrimConnectionContext.getFeatures()).thenReturn(mockedFeatures);
113         when(mockedPrimConnectionContext.getConnectionAdapter()).thenReturn(mockedConnectionAdapter);
114         when(mockedPrimConnectionContext.getOutboundQueueProvider()).thenReturn(outboundQueue);
115
116         when(mockedDeviceContext.getPrimaryConnectionContext()).thenReturn(mockedPrimConnectionContext);
117
118         when(mockedDeviceContext.getMessageSpy()).thenReturn(mockedMessagSpy);
119         when(mockedDeviceContext.getDeviceFlowRegistry()).thenReturn(deviceFlowRegistry);
120         when(mockedRequestContextStack.createRequestContext()).thenReturn(requestContext);
121
122         when(requestContext.getXid()).thenReturn(new Xid(84L));
123         when(requestContext.getFuture()).thenReturn(RpcResultBuilder.success().buildFuture());
124
125         when(mockedDeviceInfo.getNodeInstanceIdentifier()).thenReturn(NODE_II);
126         when(mockedDeviceInfo.getDatapathId()).thenReturn(DUMMY_DATAPATH_ID);
127         when(mockedDeviceInfo.getVersion()).thenReturn(DUMMY_VERSION);
128         when(mockedDeviceContext.getDeviceState()).thenReturn(mockedDeviceState);
129         when(mockedDeviceContext.getDeviceInfo()).thenReturn(mockedDeviceInfo);
130
131         salFlowService = new SalFlowServiceImpl(mockedRequestContextStack, mockedDeviceContext);
132     }
133
134     @Test
135     public void testAddFlow() throws Exception {
136         addFlow(null);
137     }
138
139     @Test
140     public void testAddFlowFailCallback() throws Exception {
141         AddFlowInput mockedAddFlowInput = createFlowMock(AddFlowInput.class);
142         Mockito.doReturn(Futures.<RequestContext<Object>>immediateFailedFuture(new Exception("ut-failed-response")))
143                 .when(requestContext).getFuture();
144
145         final Future<RpcResult<AddFlowOutput>> rpcResultFuture = salFlowService.addFlow(mockedAddFlowInput);
146
147         assertNotNull(rpcResultFuture);
148         final RpcResult<?> addFlowOutputRpcResult = rpcResultFuture.get();
149         assertNotNull(addFlowOutputRpcResult);
150         assertFalse(addFlowOutputRpcResult.isSuccessful());
151     }
152
153     @Test
154     public void testRemoveFlowFailCallback() throws Exception {
155         RemoveFlowInput mockedRemoveFlowInput = createFlowMock(RemoveFlowInput.class);
156         Mockito.doReturn(Futures.<RequestContext<Object>>immediateFailedFuture(new Exception("ut-failed-response")))
157                 .when(requestContext).getFuture();
158
159         final Future<RpcResult<RemoveFlowOutput>> rpcResultFuture = salFlowService.removeFlow(mockedRemoveFlowInput);
160
161         assertNotNull(rpcResultFuture);
162         final RpcResult<?> removeFlowOutputRpcResult = rpcResultFuture.get();
163         assertNotNull(removeFlowOutputRpcResult);
164         assertFalse(removeFlowOutputRpcResult.isSuccessful());
165     }
166
167     @Test
168     public void testAddFlowWithItemLifecycle() throws Exception {
169         addFlow(mock(ItemLifecycleListener.class));
170     }
171
172     private void addFlow(final ItemLifecycleListener itemLifecycleListener) throws ExecutionException, InterruptedException {
173         AddFlowInput mockedAddFlowInput = createFlowMock(AddFlowInput.class);
174         salFlowService.setItemLifecycleListener(itemLifecycleListener);
175
176         verifyOutput(salFlowService.addFlow(mockedAddFlowInput));
177         if (itemLifecycleListener != null) {
178             Mockito.verify(itemLifecycleListener).onAdded(Matchers.<KeyedInstanceIdentifier<Flow, FlowKey>>any(), Matchers.<Flow>any());
179         }
180     }
181
182     @Test
183     public void testRemoveFlow() throws Exception {
184         removeFlow(null);
185     }
186
187     @Test
188     public void testRemoveFlowWithItemLifecycle() throws Exception {
189         removeFlow(mock(ItemLifecycleListener.class));
190     }
191
192     private void removeFlow(final ItemLifecycleListener itemLifecycleListener) throws Exception {
193         RemoveFlowInput mockedRemoveFlowInput = createFlowMock(RemoveFlowInput.class);
194
195         if (itemLifecycleListener != null) {
196             salFlowService.setItemLifecycleListener(itemLifecycleListener);
197             mockingFlowRegistryLookup();
198
199         }
200
201         verifyOutput(salFlowService.removeFlow(mockedRemoveFlowInput));
202         if (itemLifecycleListener != null) {
203             Mockito.verify(itemLifecycleListener).onRemoved(Matchers.<KeyedInstanceIdentifier<Flow, FlowKey>>any());
204         }
205
206     }
207
208     @Test
209     public void testUpdateFlow() throws Exception {
210         updateFlow(null);
211     }
212
213     @Test
214     public void testUpdateFlowWithItemLifecycle() throws Exception {
215         updateFlow(mock(ItemLifecycleListener.class));
216     }
217
218     private void updateFlow(final ItemLifecycleListener itemLifecycleListener) throws Exception {
219         UpdateFlowInput mockedUpdateFlowInput = mock(UpdateFlowInput.class);
220
221         UpdatedFlow mockedUpdateFlow = createFlowMock(UpdatedFlow.class);
222         when(mockedUpdateFlowInput.getUpdatedFlow()).thenReturn(mockedUpdateFlow);
223
224         FlowRef mockedFlowRef = mock(FlowRef.class);
225         Mockito.doReturn(TABLE_II.child(Flow.class, new FlowKey(new FlowId(DUMMY_FLOW_ID)))).when(mockedFlowRef).getValue();
226         when(mockedUpdateFlowInput.getFlowRef()).thenReturn(mockedFlowRef);
227
228         OriginalFlow mockedOriginalFlow = createFlowMock(OriginalFlow.class);
229         when(mockedUpdateFlowInput.getOriginalFlow()).thenReturn(mockedOriginalFlow);
230
231         if (itemLifecycleListener != null) {
232             salFlowService.setItemLifecycleListener(itemLifecycleListener);
233             mockingFlowRegistryLookup();
234         }
235
236         verifyOutput(salFlowService.updateFlow(mockedUpdateFlowInput));
237
238         if (itemLifecycleListener != null) {
239             Mockito.verify(itemLifecycleListener).onUpdated(Matchers.<KeyedInstanceIdentifier<Flow, FlowKey>>any(), Matchers.<Flow>any());
240         }
241
242     }
243
244     private void mockingFlowRegistryLookup() {
245         FlowDescriptor mockedFlowDescriptor = mock(FlowDescriptor.class);
246         when(mockedFlowDescriptor.getFlowId()).thenReturn(new FlowId(DUMMY_FLOW_ID));
247         when(mockedFlowDescriptor.getTableKey()).thenReturn(new TableKey(DUMMY_TABLE_ID));
248
249         when(deviceFlowRegistry.retrieveIdForFlow(Matchers.any(FlowRegistryKey.class))).thenReturn(mockedFlowDescriptor);
250     }
251
252     private <T extends DataObject> void verifyOutput(Future<RpcResult<T>> rpcResultFuture) throws ExecutionException, InterruptedException {
253         assertNotNull(rpcResultFuture);
254         final RpcResult<?> addFlowOutputRpcResult = rpcResultFuture.get();
255         assertNotNull(addFlowOutputRpcResult);
256         assertTrue(addFlowOutputRpcResult.isSuccessful());
257     }
258
259     private <T extends org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.Flow> T createFlowMock(Class<T> flowClazz) {
260         T mockedFlow = mock(flowClazz);
261         when(mockedFlow.getMatch()).thenReturn(match);
262         return mockedFlow;
263     }
264 }