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