SONAR TD - DeviceFlowRegistryImpl, FlowDescriptorFactory, FlowRegistryKeyFactory
[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         mockingFlowRegistryLookup();
146         final Future<RpcResult<AddFlowOutput>> rpcResultFuture = salFlowService.addFlow(mockedAddFlowInput);
147
148         assertNotNull(rpcResultFuture);
149         final RpcResult<?> addFlowOutputRpcResult = rpcResultFuture.get();
150         assertNotNull(addFlowOutputRpcResult);
151         assertFalse(addFlowOutputRpcResult.isSuccessful());
152     }
153
154     @Test
155     public void testRemoveFlowFailCallback() throws Exception {
156         RemoveFlowInput mockedRemoveFlowInput = createFlowMock(RemoveFlowInput.class);
157         Mockito.doReturn(Futures.<RequestContext<Object>>immediateFailedFuture(new Exception("ut-failed-response")))
158                 .when(requestContext).getFuture();
159
160         final Future<RpcResult<RemoveFlowOutput>> rpcResultFuture = salFlowService.removeFlow(mockedRemoveFlowInput);
161
162         assertNotNull(rpcResultFuture);
163         final RpcResult<?> removeFlowOutputRpcResult = rpcResultFuture.get();
164         assertNotNull(removeFlowOutputRpcResult);
165         assertFalse(removeFlowOutputRpcResult.isSuccessful());
166     }
167
168     @Test
169     public void testAddFlowWithItemLifecycle() throws Exception {
170         addFlow(mock(ItemLifecycleListener.class));
171     }
172
173     private void addFlow(final ItemLifecycleListener itemLifecycleListener) throws ExecutionException, InterruptedException {
174         AddFlowInput mockedAddFlowInput = createFlowMock(AddFlowInput.class);
175         salFlowService.setItemLifecycleListener(itemLifecycleListener);
176
177         mockingFlowRegistryLookup();
178         verifyOutput(salFlowService.addFlow(mockedAddFlowInput));
179         if (itemLifecycleListener != null) {
180             Mockito.verify(itemLifecycleListener).onAdded(Matchers.<KeyedInstanceIdentifier<Flow, FlowKey>>any(), Matchers.<Flow>any());
181         }
182     }
183
184     @Test
185     public void testRemoveFlow() throws Exception {
186         removeFlow(null);
187     }
188
189     @Test
190     public void testRemoveFlowWithItemLifecycle() throws Exception {
191         removeFlow(mock(ItemLifecycleListener.class));
192     }
193
194     private void removeFlow(final ItemLifecycleListener itemLifecycleListener) throws Exception {
195         RemoveFlowInput mockedRemoveFlowInput = createFlowMock(RemoveFlowInput.class);
196
197         if (itemLifecycleListener != null) {
198             salFlowService.setItemLifecycleListener(itemLifecycleListener);
199             mockingFlowRegistryLookup();
200
201         }
202
203         verifyOutput(salFlowService.removeFlow(mockedRemoveFlowInput));
204         if (itemLifecycleListener != null) {
205             Mockito.verify(itemLifecycleListener).onRemoved(Matchers.<KeyedInstanceIdentifier<Flow, FlowKey>>any());
206         }
207
208     }
209
210     @Test
211     public void testUpdateFlow() throws Exception {
212         updateFlow(null);
213     }
214
215     @Test
216     public void testUpdateFlowWithItemLifecycle() throws Exception {
217         updateFlow(mock(ItemLifecycleListener.class));
218     }
219
220     private void updateFlow(final ItemLifecycleListener itemLifecycleListener) throws Exception {
221         UpdateFlowInput mockedUpdateFlowInput = mock(UpdateFlowInput.class);
222
223         UpdatedFlow mockedUpdateFlow = createFlowMock(UpdatedFlow.class);
224         when(mockedUpdateFlowInput.getUpdatedFlow()).thenReturn(mockedUpdateFlow);
225
226         FlowRef mockedFlowRef = mock(FlowRef.class);
227         Mockito.doReturn(TABLE_II.child(Flow.class, new FlowKey(new FlowId(DUMMY_FLOW_ID)))).when(mockedFlowRef).getValue();
228         when(mockedUpdateFlowInput.getFlowRef()).thenReturn(mockedFlowRef);
229
230         OriginalFlow mockedOriginalFlow = createFlowMock(OriginalFlow.class);
231         when(mockedUpdateFlowInput.getOriginalFlow()).thenReturn(mockedOriginalFlow);
232
233         if (itemLifecycleListener != null) {
234             salFlowService.setItemLifecycleListener(itemLifecycleListener);
235             mockingFlowRegistryLookup();
236         }
237
238         verifyOutput(salFlowService.updateFlow(mockedUpdateFlowInput));
239
240         if (itemLifecycleListener != null) {
241             Mockito.verify(itemLifecycleListener).onUpdated(Matchers.<KeyedInstanceIdentifier<Flow, FlowKey>>any(), Matchers.<Flow>any());
242         }
243
244     }
245
246     private void mockingFlowRegistryLookup() {
247         FlowDescriptor mockedFlowDescriptor = mock(FlowDescriptor.class);
248         FlowId flowId = new FlowId(DUMMY_FLOW_ID);
249         when(mockedFlowDescriptor.getFlowId()).thenReturn(flowId);
250         when(mockedFlowDescriptor.getTableKey()).thenReturn(new TableKey(DUMMY_TABLE_ID));
251
252         when(deviceFlowRegistry.storeIfNecessary(Matchers.any(FlowRegistryKey.class))).thenReturn(flowId);
253         when(deviceFlowRegistry.retrieveIdForFlow(Matchers.any(FlowRegistryKey.class))).thenReturn(mockedFlowDescriptor);
254     }
255
256     private <T extends DataObject> void verifyOutput(Future<RpcResult<T>> rpcResultFuture) throws ExecutionException, InterruptedException {
257         assertNotNull(rpcResultFuture);
258         final RpcResult<?> addFlowOutputRpcResult = rpcResultFuture.get();
259         assertNotNull(addFlowOutputRpcResult);
260         assertTrue(addFlowOutputRpcResult.isSuccessful());
261     }
262
263     private <T extends org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.Flow> T createFlowMock(Class<T> flowClazz) {
264         T mockedFlow = mock(flowClazz);
265         when(mockedFlow.getMatch()).thenReturn(match);
266         return mockedFlow;
267     }
268 }