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