2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.openflowplugin.impl.services;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertTrue;
13 import static org.mockito.Matchers.eq;
14 import static org.mockito.Mockito.mock;
15 import static org.mockito.Mockito.times;
16 import static org.mockito.Mockito.verify;
17 import static org.mockito.Mockito.when;
19 import com.google.common.base.Optional;
20 import com.google.common.util.concurrent.CheckedFuture;
21 import com.google.common.util.concurrent.Futures;
22 import java.math.BigInteger;
23 import java.util.Collections;
24 import java.util.List;
25 import java.util.concurrent.ExecutionException;
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.junit.runner.RunWith;
29 import org.mockito.Matchers;
30 import org.mockito.Mock;
31 import org.mockito.Mockito;
32 import org.mockito.runners.MockitoJUnitRunner;
33 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
34 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
35 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
36 import org.opendaylight.openflowplugin.api.OFConstants;
37 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
38 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
39 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
40 import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
41 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
42 import org.opendaylight.openflowplugin.api.openflow.registry.flow.DeviceFlowRegistry;
43 import org.opendaylight.openflowplugin.api.openflow.registry.flow.FlowRegistryKey;
44 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.EventIdentifier;
45 import org.opendaylight.openflowplugin.impl.rpc.AbstractRequestContext;
46 import org.opendaylight.openflowplugin.impl.statistics.ofpspecific.MessageIntelligenceAgencyImpl;
47 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorManager;
48 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorManagerFactory;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableBuilder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.FlowModFlags;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartRequestFlags;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntry;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.grouping.MatchBuilder;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloMessage;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReply;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReplyMessageBuilder;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyFlowCaseBuilder;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.flow._case.MultipartReplyFlowBuilder;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.flow._case.multipart.reply.flow.FlowStatsBuilder;
71 import org.opendaylight.yangtools.yang.binding.DataObject;
72 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
73 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
74 import org.opendaylight.yangtools.yang.common.RpcError;
75 import org.opendaylight.yangtools.yang.common.RpcResult;
76 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
78 @RunWith(MockitoJUnitRunner.class)
79 public class MultipartRequestOnTheFlyCallbackTest {
81 private static final String DUMMY_NODE_ID = "dummyNodeId";
82 private static final String DUMMY_EVENT_NAME = "dummy event name 1";
83 private static final String DUMMY_DEVICE_ID = "dummy device id 1";
84 private static final Long DUMMY_XID = 55L;
85 private static final KeyedInstanceIdentifier<Node, NodeKey> NODE_PATH = KeyedInstanceIdentifier
87 .child(Node.class, new NodeKey(new NodeId("uf-node:123")));
90 private DeviceContext mockedDeviceContext;
92 private RequestContext<List<MultipartReply>> mockedRequestContext;
94 private ConnectionContext mockedPrimaryConnection;
96 private NodeId mockedNodeId;
98 private FeaturesReply mockedFeaturesReply;
100 private DeviceState mockedDeviceState;
102 private DeviceInfo mockedDeviceInfo;
104 private GetFeaturesOutput mocketGetFeaturesOutput;
106 private DeviceFlowRegistry mockedFlowRegistry;
108 private ReadOnlyTransaction mockedReadOnlyTx;
110 private AbstractRequestContext<List<MultipartReply>> dummyRequestContext;
111 private final EventIdentifier dummyEventIdentifier = new EventIdentifier(DUMMY_EVENT_NAME, DUMMY_DEVICE_ID);
112 private MultipartRequestOnTheFlyCallback multipartRequestOnTheFlyCallback;
113 private final short tableId = 0;
116 public void initialization() {
117 when(mockedDeviceContext.getMessageSpy()).thenReturn(new MessageIntelligenceAgencyImpl());
118 when(mockedNodeId.toString()).thenReturn(DUMMY_NODE_ID);
119 when(mockedPrimaryConnection.getNodeId()).thenReturn(mockedNodeId);
120 when(mockedPrimaryConnection.getFeatures()).thenReturn(mockedFeaturesReply);
121 when(mockedFeaturesReply.getVersion()).thenReturn(OFConstants.OFP_VERSION_1_3);
122 when(mockedFeaturesReply.getDatapathId()).thenReturn(BigInteger.valueOf(123L));
124 when(mocketGetFeaturesOutput.getTables()).thenReturn(tableId);
125 when(mocketGetFeaturesOutput.getVersion()).thenReturn(OFConstants.OFP_VERSION_1_3);
126 when(mocketGetFeaturesOutput.getDatapathId()).thenReturn(BigInteger.valueOf(123L));
128 when(mockedDeviceContext.getPrimaryConnectionContext()).thenReturn(mockedPrimaryConnection);
129 when(mockedDeviceInfo.getNodeInstanceIdentifier()).thenReturn(NODE_PATH);
130 when(mockedDeviceInfo.getNodeId()).thenReturn(mockedNodeId);
131 when(mockedDeviceInfo.getDatapathId()).thenReturn(BigInteger.TEN);
132 when(mockedDeviceInfo.getVersion()).thenReturn(OFConstants.OFP_VERSION_1_3);
134 when(mockedDeviceContext.getDeviceState()).thenReturn(mockedDeviceState);
135 when(mockedDeviceContext.getDeviceInfo()).thenReturn(mockedDeviceInfo);
136 when(mockedDeviceContext.getDeviceFlowRegistry()).thenReturn(mockedFlowRegistry);
138 final InstanceIdentifier<FlowCapableNode> nodePath = mockedDeviceInfo.getNodeInstanceIdentifier().augmentation(FlowCapableNode.class);
139 final FlowCapableNodeBuilder flowNodeBuilder = new FlowCapableNodeBuilder();
140 flowNodeBuilder.setTable(Collections.<Table> emptyList());
141 final Optional<FlowCapableNode> flowNodeOpt = Optional.of(flowNodeBuilder.build());
142 final CheckedFuture<Optional<FlowCapableNode>, ReadFailedException> flowNodeFuture = Futures.immediateCheckedFuture(flowNodeOpt);
143 when(mockedReadOnlyTx.read(LogicalDatastoreType.OPERATIONAL, nodePath)).thenReturn(flowNodeFuture);
144 when(mockedDeviceContext.getReadTransaction()).thenReturn(mockedReadOnlyTx);
146 dummyRequestContext = new AbstractRequestContext<List<MultipartReply>>(DUMMY_XID) {
149 public void close() {
154 final ConvertorManager convertorManager = ConvertorManagerFactory.createDefaultManager();
155 multipartRequestOnTheFlyCallback = new MultipartRequestOnTheFlyCallback(dummyRequestContext, String.class,
156 mockedDeviceContext.getMessageSpy(),dummyEventIdentifier, mockedDeviceInfo,
157 mockedDeviceContext.getDeviceFlowRegistry(), mockedDeviceContext, convertorManager);
162 public void testOnSuccessWithNull() throws Exception {
163 multipartRequestOnTheFlyCallback.onSuccess(null);
164 final RpcResult<List<MultipartReply>> expectedRpcResult = RpcResultBuilder.success(Collections.<MultipartReply>emptyList()).build();
165 final RpcResult<List<MultipartReply>> actualResult = dummyRequestContext.getFuture().get();
166 assertEquals(expectedRpcResult.getErrors(), actualResult.getErrors());
167 assertEquals(expectedRpcResult.getResult(), actualResult.getResult());
168 assertEquals(expectedRpcResult.isSuccessful(), actualResult.isSuccessful());
172 public void testOnSuccessWithNotMultiNoMultipart() throws Exception {
173 final HelloMessage mockedHelloMessage = mock(HelloMessage.class);
174 multipartRequestOnTheFlyCallback.onSuccess(mockedHelloMessage);
176 final RpcResult<List<MultipartReply>> expectedRpcResult =
177 RpcResultBuilder.<List<MultipartReply>>failed().withError(RpcError.ErrorType.APPLICATION,
178 String.format("Unexpected response type received %s.", mockedHelloMessage.getClass())).build();
179 final RpcResult<List<MultipartReply>> actualResult = dummyRequestContext.getFuture().get();
180 assertNotNull(actualResult.getErrors());
181 assertEquals(1, actualResult.getErrors().size());
183 final RpcError actualError = actualResult.getErrors().iterator().next();
184 assertEquals(actualError.getMessage(), String.format("Unexpected response type received %s.", mockedHelloMessage.getClass()));
185 assertEquals(actualError.getErrorType(),RpcError.ErrorType.APPLICATION);
186 assertEquals(expectedRpcResult.getResult(), actualResult.getResult());
187 assertEquals(expectedRpcResult.isSuccessful(), actualResult.isSuccessful());
189 Mockito.verify(mockedDeviceContext, Mockito.never()).writeToTransaction(Matchers.eq(LogicalDatastoreType.OPERATIONAL),
190 Matchers.<InstanceIdentifier>any(), Matchers.<DataObject>any());
191 Mockito.verify(mockedDeviceContext).submitTransaction();
197 * @throws ExecutionException
198 * @throws InterruptedException
201 public void testOnSuccessWithValidMultipart1() throws Exception {
202 final MatchBuilder matchBuilder = new MatchBuilder()
203 .setMatchEntry(Collections.<MatchEntry>emptyList());
204 final FlowStatsBuilder flowStatsBuilder = new FlowStatsBuilder()
207 .setCookie(BigInteger.ZERO)
208 .setByteCount(BigInteger.TEN)
209 .setPacketCount(BigInteger.ONE)
211 .setDurationNsec(12L)
212 .setMatch(matchBuilder.build())
213 .setFlags(new FlowModFlags(true, false, false, false, false));
214 final MultipartReplyFlowBuilder multipartReplyFlowBuilder = new MultipartReplyFlowBuilder()
215 .setFlowStats(Collections.singletonList(flowStatsBuilder.build()));
216 final MultipartReplyFlowCaseBuilder multipartReplyFlowCaseBuilder = new MultipartReplyFlowCaseBuilder()
217 .setMultipartReplyFlow(multipartReplyFlowBuilder.build());
218 final MultipartReplyMessageBuilder mpReplyMessage = new MultipartReplyMessageBuilder()
219 .setType(MultipartType.OFPMPFLOW)
220 .setFlags(new MultipartRequestFlags(true))
221 .setMultipartReplyBody(multipartReplyFlowCaseBuilder.build())
224 final InstanceIdentifier<FlowCapableNode> nodePath = mockedDeviceInfo.getNodeInstanceIdentifier()
225 .augmentation(FlowCapableNode.class);
226 final FlowCapableNodeBuilder flowNodeBuilder = new FlowCapableNodeBuilder();
227 final TableBuilder tableDataBld = new TableBuilder();
228 tableDataBld.setId(tableId);
229 flowNodeBuilder.setTable(Collections.singletonList(tableDataBld.build()));
230 final Optional<FlowCapableNode> flowNodeOpt = Optional.of(flowNodeBuilder.build());
231 final CheckedFuture<Optional<FlowCapableNode>, ReadFailedException> flowNodeFuture = Futures
232 .immediateCheckedFuture(flowNodeOpt);
233 when(mockedReadOnlyTx.read(LogicalDatastoreType.OPERATIONAL, nodePath)).thenReturn(flowNodeFuture);
234 when(mockedDeviceContext.getReadTransaction()).thenReturn(mockedReadOnlyTx);
236 multipartRequestOnTheFlyCallback.onSuccess(mpReplyMessage.build());
237 final InstanceIdentifier<Table> tableIdent = nodePath.child(Table.class, new TableKey(tableId));
239 verify(mockedReadOnlyTx, times(1)).read(LogicalDatastoreType.OPERATIONAL, nodePath);
240 verify(mockedReadOnlyTx, times(1)).close();
241 verify(mockedFlowRegistry).storeIfNecessary(Matchers.<FlowRegistryKey> any());
242 verify(mockedDeviceContext, times(1)).writeToTransaction(eq(LogicalDatastoreType.OPERATIONAL),
243 eq(tableIdent), Matchers.<Table> any());
245 * One call for Table one call for Flow
246 * we are not able to create Flow InstanceIdentifier because we are missing FlowId
248 verify(mockedDeviceContext, times(2)).writeToTransaction(eq(LogicalDatastoreType.OPERATIONAL),
249 Matchers.<InstanceIdentifier> any(), Matchers.<DataObject> any());
255 * @throws ExecutionException
256 * @throws InterruptedException
259 public void testOnSuccessWithValidMultipart2() throws Exception {
260 final MultipartReplyMessageBuilder mpReplyMessage = new MultipartReplyMessageBuilder()
261 .setType(MultipartType.OFPMPDESC)
262 .setFlags(new MultipartRequestFlags(false));
264 multipartRequestOnTheFlyCallback.onSuccess(mpReplyMessage.build());
266 final RpcResult<List<MultipartReply>> actualResult = dummyRequestContext.getFuture().get();
267 assertNotNull(actualResult.getErrors());
268 assertTrue(actualResult.getErrors().isEmpty());
269 assertNotNull(actualResult.getResult());
270 assertTrue(actualResult.getResult().isEmpty());
272 Mockito.verify(mockedFlowRegistry, Mockito.never()).storeIfNecessary(Matchers.any());
273 Mockito.verify(mockedDeviceContext, Mockito.never()).writeToTransaction(Matchers.eq(LogicalDatastoreType.OPERATIONAL),
274 Matchers.<InstanceIdentifier>any(), Matchers.<DataObject>any());