DeviceState changes
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / services / MultipartRequestOnTheFlyCallbackTest.java
1 package org.opendaylight.openflowplugin.impl.services;
2
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertNotNull;
5 import static org.junit.Assert.assertTrue;
6 import static org.mockito.Matchers.eq;
7 import static org.mockito.Mockito.mock;
8 import static org.mockito.Mockito.times;
9 import static org.mockito.Mockito.verify;
10 import static org.mockito.Mockito.when;
11 import com.google.common.base.Optional;
12 import com.google.common.util.concurrent.CheckedFuture;
13 import com.google.common.util.concurrent.Futures;
14 import java.math.BigInteger;
15 import java.util.Collections;
16 import java.util.List;
17 import java.util.concurrent.ExecutionException;
18 import org.junit.Before;
19 import org.junit.Test;
20 import org.junit.runner.RunWith;
21 import org.mockito.Matchers;
22 import org.mockito.Mock;
23 import org.mockito.Mockito;
24 import org.mockito.runners.MockitoJUnitRunner;
25 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
26 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
27 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
28 import org.opendaylight.openflowplugin.api.OFConstants;
29 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
30 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
31 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
32 import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
33 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
34 import org.opendaylight.openflowplugin.api.openflow.registry.flow.DeviceFlowRegistry;
35 import org.opendaylight.openflowplugin.api.openflow.registry.flow.FlowRegistryKey;
36 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.EventIdentifier;
37 import org.opendaylight.openflowplugin.impl.rpc.AbstractRequestContext;
38 import org.opendaylight.openflowplugin.impl.statistics.ofpspecific.MessageIntelligenceAgencyImpl;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.FlowModFlags;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartRequestFlags;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntry;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.grouping.MatchBuilder;
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.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloMessage;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReply;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReplyMessageBuilder;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyFlowCaseBuilder;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.flow._case.MultipartReplyFlowBuilder;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.flow._case.multipart.reply.flow.FlowStatsBuilder;
61 import org.opendaylight.yangtools.yang.binding.DataObject;
62 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
63 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
64 import org.opendaylight.yangtools.yang.common.RpcError;
65 import org.opendaylight.yangtools.yang.common.RpcResult;
66 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
67
68 @RunWith(MockitoJUnitRunner.class)
69 public class MultipartRequestOnTheFlyCallbackTest {
70
71     private static final String DUMMY_NODE_ID = "dummyNodeId";
72     private static final String DUMMY_EVENT_NAME = "dummy event name 1";
73     private static final String DUMMY_DEVICE_ID = "dummy device id 1";
74     private static final Long DUMMY_XID = 55L;
75     private static final KeyedInstanceIdentifier<Node, NodeKey> NODE_PATH = KeyedInstanceIdentifier
76             .create(Nodes.class)
77             .child(Node.class, new NodeKey(new NodeId("uf-node:123")));
78
79     @Mock
80     private DeviceContext mockedDeviceContext;
81     @Mock
82     private RequestContext<List<MultipartReply>> mockedRequestContext;
83     @Mock
84     private ConnectionContext mockedPrimaryConnection;
85     @Mock
86     private NodeId mockedNodeId;
87     @Mock
88     private FeaturesReply mockedFeaturesReply;
89     @Mock
90     private DeviceState mockedDeviceState;
91     @Mock
92     private DeviceInfo mockedDeviceInfo;
93     @Mock
94     private GetFeaturesOutput mocketGetFeaturesOutput;
95     @Mock
96     private DeviceFlowRegistry mockedFlowRegistry;
97     @Mock
98     private ReadOnlyTransaction mockedReadOnlyTx;
99
100     private AbstractRequestContext<List<MultipartReply>> dummyRequestContext;
101     private final EventIdentifier dummyEventIdentifier = new EventIdentifier(DUMMY_EVENT_NAME, DUMMY_DEVICE_ID);
102     private MultipartRequestOnTheFlyCallback multipartRequestOnTheFlyCallback;
103     private final short tableId = 0;
104
105     @Before
106     public void initialization() {
107         when(mockedDeviceContext.getMessageSpy()).thenReturn(new MessageIntelligenceAgencyImpl());
108         when(mockedNodeId.toString()).thenReturn(DUMMY_NODE_ID);
109         when(mockedPrimaryConnection.getNodeId()).thenReturn(mockedNodeId);
110         when(mockedPrimaryConnection.getFeatures()).thenReturn(mockedFeaturesReply);
111         when(mockedFeaturesReply.getVersion()).thenReturn(OFConstants.OFP_VERSION_1_3);
112         when(mockedFeaturesReply.getDatapathId()).thenReturn(BigInteger.valueOf(123L));
113
114         when(mocketGetFeaturesOutput.getTables()).thenReturn(tableId);
115         when(mocketGetFeaturesOutput.getVersion()).thenReturn(OFConstants.OFP_VERSION_1_3);
116         when(mocketGetFeaturesOutput.getDatapathId()).thenReturn(BigInteger.valueOf(123L));
117
118         when(mockedDeviceContext.getPrimaryConnectionContext()).thenReturn(mockedPrimaryConnection);
119         when(mockedDeviceInfo.getNodeInstanceIdentifier()).thenReturn(NODE_PATH);
120         when(mockedDeviceState.deviceSynchronized()).thenReturn(true);
121         when(mockedDeviceInfo.getNodeId()).thenReturn(mockedNodeId);
122         when(mockedDeviceInfo.getDatapathId()).thenReturn(BigInteger.TEN);
123
124         when(mockedDeviceContext.getDeviceState()).thenReturn(mockedDeviceState);
125         when(mockedDeviceContext.getDeviceInfo()).thenReturn(mockedDeviceInfo);
126         when(mockedDeviceContext.getDeviceFlowRegistry()).thenReturn(mockedFlowRegistry);
127
128         final InstanceIdentifier<FlowCapableNode> nodePath = mockedDeviceInfo.getNodeInstanceIdentifier().augmentation(FlowCapableNode.class);
129         final FlowCapableNodeBuilder flowNodeBuilder = new FlowCapableNodeBuilder();
130         flowNodeBuilder.setTable(Collections.<Table> emptyList());
131         final Optional<FlowCapableNode> flowNodeOpt = Optional.of(flowNodeBuilder.build());
132         final CheckedFuture<Optional<FlowCapableNode>, ReadFailedException> flowNodeFuture = Futures.immediateCheckedFuture(flowNodeOpt);
133         when(mockedReadOnlyTx.read(LogicalDatastoreType.OPERATIONAL, nodePath)).thenReturn(flowNodeFuture);
134         when(mockedDeviceContext.getReadTransaction()).thenReturn(mockedReadOnlyTx);
135
136         dummyRequestContext = new AbstractRequestContext<List<MultipartReply>>(DUMMY_XID) {
137
138             @Override
139             public void close() {
140                 //NOOP
141             }
142         };
143         multipartRequestOnTheFlyCallback = new MultipartRequestOnTheFlyCallback(dummyRequestContext, String.class,
144                 mockedDeviceContext.getMessageSpy(),dummyEventIdentifier, mockedDeviceInfo,
145                 mockedDeviceContext.getDeviceFlowRegistry(), mockedDeviceContext, mockedDeviceState);
146     }
147
148
149     @Test
150     public void testOnSuccessWithNull() throws Exception {
151         multipartRequestOnTheFlyCallback.onSuccess(null);
152         final RpcResult<List<MultipartReply>> expectedRpcResult = RpcResultBuilder.success(Collections.<MultipartReply>emptyList()).build();
153         final RpcResult<List<MultipartReply>> actualResult = dummyRequestContext.getFuture().get();
154         assertEquals(expectedRpcResult.getErrors(), actualResult.getErrors());
155         assertEquals(expectedRpcResult.getResult(), actualResult.getResult());
156         assertEquals(expectedRpcResult.isSuccessful(), actualResult.isSuccessful());
157     }
158
159     @Test
160     public void testOnSuccessWithNotMultiNoMultipart() throws Exception {
161         final HelloMessage mockedHelloMessage = mock(HelloMessage.class);
162         multipartRequestOnTheFlyCallback.onSuccess(mockedHelloMessage);
163
164         final RpcResult<List<MultipartReply>> expectedRpcResult =
165                 RpcResultBuilder.<List<MultipartReply>>failed().withError(RpcError.ErrorType.APPLICATION,
166                         String.format("Unexpected response type received %s.", mockedHelloMessage.getClass())).build();
167         final RpcResult<List<MultipartReply>> actualResult = dummyRequestContext.getFuture().get();
168         assertNotNull(actualResult.getErrors());
169         assertEquals(1, actualResult.getErrors().size());
170
171         final RpcError actualError = actualResult.getErrors().iterator().next();
172         assertEquals(actualError.getMessage(), String.format("Unexpected response type received %s.", mockedHelloMessage.getClass()));
173         assertEquals(actualError.getErrorType(),RpcError.ErrorType.APPLICATION);
174         assertEquals(expectedRpcResult.getResult(), actualResult.getResult());
175         assertEquals(expectedRpcResult.isSuccessful(), actualResult.isSuccessful());
176
177         Mockito.verify(mockedDeviceContext, Mockito.never()).writeToTransaction(Matchers.eq(LogicalDatastoreType.OPERATIONAL),
178                 Matchers.<InstanceIdentifier>any(), Matchers.<DataObject>any());
179         Mockito.verify(mockedDeviceContext).submitTransaction();
180     }
181
182     /**
183      * not the last reply
184      *
185      * @throws ExecutionException
186      * @throws InterruptedException
187      */
188     @Test
189     public void testOnSuccessWithValidMultipart1() throws Exception {
190         final MatchBuilder matchBuilder = new MatchBuilder()
191                 .setMatchEntry(Collections.<MatchEntry>emptyList());
192         final FlowStatsBuilder flowStatsBuilder = new FlowStatsBuilder()
193 .setTableId(tableId)
194                 .setPriority(2)
195                 .setCookie(BigInteger.ZERO)
196                 .setByteCount(BigInteger.TEN)
197                 .setPacketCount(BigInteger.ONE)
198                 .setDurationSec(11L)
199                 .setDurationNsec(12L)
200                 .setMatch(matchBuilder.build())
201                 .setFlags(new FlowModFlags(true, false, false, false, false));
202         final MultipartReplyFlowBuilder multipartReplyFlowBuilder = new MultipartReplyFlowBuilder()
203                 .setFlowStats(Collections.singletonList(flowStatsBuilder.build()));
204         final MultipartReplyFlowCaseBuilder multipartReplyFlowCaseBuilder = new MultipartReplyFlowCaseBuilder()
205                 .setMultipartReplyFlow(multipartReplyFlowBuilder.build());
206         final MultipartReplyMessageBuilder mpReplyMessage = new MultipartReplyMessageBuilder()
207                 .setType(MultipartType.OFPMPFLOW)
208                 .setFlags(new MultipartRequestFlags(true))
209                 .setMultipartReplyBody(multipartReplyFlowCaseBuilder.build())
210                 .setXid(21L);
211
212         final InstanceIdentifier<FlowCapableNode> nodePath = mockedDeviceInfo.getNodeInstanceIdentifier()
213                 .augmentation(FlowCapableNode.class);
214         final FlowCapableNodeBuilder flowNodeBuilder = new FlowCapableNodeBuilder();
215         final TableBuilder tableDataBld = new TableBuilder();
216         tableDataBld.setId(tableId);
217         flowNodeBuilder.setTable(Collections.singletonList(tableDataBld.build()));
218         final Optional<FlowCapableNode> flowNodeOpt = Optional.of(flowNodeBuilder.build());
219         final CheckedFuture<Optional<FlowCapableNode>, ReadFailedException> flowNodeFuture = Futures
220                 .immediateCheckedFuture(flowNodeOpt);
221         when(mockedReadOnlyTx.read(LogicalDatastoreType.OPERATIONAL, nodePath)).thenReturn(flowNodeFuture);
222         when(mockedDeviceContext.getReadTransaction()).thenReturn(mockedReadOnlyTx);
223
224         multipartRequestOnTheFlyCallback.onSuccess(mpReplyMessage.build());
225         final InstanceIdentifier<Table> tableIdent = nodePath.child(Table.class, new TableKey(tableId));
226
227         verify(mockedReadOnlyTx, times(1)).read(LogicalDatastoreType.OPERATIONAL, nodePath);
228         verify(mockedReadOnlyTx, times(1)).close();
229         verify(mockedFlowRegistry).storeIfNecessary(Matchers.<FlowRegistryKey> any(), Matchers.anyShort());
230         verify(mockedDeviceContext, times(1)).writeToTransaction(eq(LogicalDatastoreType.OPERATIONAL),
231                 eq(tableIdent), Matchers.<Table> any());
232         /*
233          * One call for Table one call for Flow
234          * we are not able to create Flow InstanceIdentifier because we are missing FlowId
235          */
236         verify(mockedDeviceContext, times(2)).writeToTransaction(eq(LogicalDatastoreType.OPERATIONAL),
237                 Matchers.<InstanceIdentifier> any(), Matchers.<DataObject> any());
238     }
239
240     /**
241      * the last reply
242      *
243      * @throws ExecutionException
244      * @throws InterruptedException
245      */
246     @Test
247     public void testOnSuccessWithValidMultipart2() throws Exception {
248         final MultipartReplyMessageBuilder mpReplyMessage = new MultipartReplyMessageBuilder()
249                 .setType(MultipartType.OFPMPDESC)
250                 .setFlags(new MultipartRequestFlags(false));
251
252         multipartRequestOnTheFlyCallback.onSuccess(mpReplyMessage.build());
253
254         final RpcResult<List<MultipartReply>> actualResult = dummyRequestContext.getFuture().get();
255         assertNotNull(actualResult.getErrors());
256         assertTrue(actualResult.getErrors().isEmpty());
257         assertNotNull(actualResult.getResult());
258         assertTrue(actualResult.getResult().isEmpty());
259
260         Mockito.verify(mockedFlowRegistry, Mockito.never()).storeIfNecessary(Matchers.<FlowRegistryKey>any(), Matchers.anyShort());
261         Mockito.verify(mockedDeviceContext, Mockito.never()).writeToTransaction(Matchers.eq(LogicalDatastoreType.OPERATIONAL),
262                 Matchers.<InstanceIdentifier>any(), Matchers.<DataObject>any());
263     }
264 }