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.assertFalse;
12 import static org.junit.Assert.assertNotNull;
13 import static org.mockito.ArgumentMatchers.any;
14 import static org.mockito.ArgumentMatchers.eq;
15 import static org.mockito.Mockito.mock;
16 import static org.mockito.Mockito.times;
17 import static org.mockito.Mockito.verify;
18 import static org.mockito.Mockito.when;
20 import com.google.common.base.Optional;
21 import com.google.common.util.concurrent.CheckedFuture;
22 import com.google.common.util.concurrent.Futures;
23 import java.math.BigInteger;
24 import java.util.Collections;
25 import java.util.List;
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.junit.runner.RunWith;
29 import org.mockito.ArgumentMatchers;
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.registry.flow.DeviceFlowRegistry;
42 import org.opendaylight.openflowplugin.api.openflow.registry.flow.FlowDescriptor;
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.datastore.MultipartWriterProviderFactory;
46 import org.opendaylight.openflowplugin.impl.rpc.AbstractRequestContext;
47 import org.opendaylight.openflowplugin.impl.services.multilayer.MultiLayerFlowMultipartRequestOnTheFlyCallback;
48 import org.opendaylight.openflowplugin.impl.statistics.ofpspecific.MessageIntelligenceAgencyImpl;
49 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorManager;
50 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorManagerFactory;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeBuilder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableBuilder;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.FlowModFlags;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartRequestFlags;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntry;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.grouping.MatchBuilder;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloMessage;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReply;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReplyMessageBuilder;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyFlowCaseBuilder;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.flow._case.MultipartReplyFlowBuilder;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.flow._case.multipart.reply.flow.FlowStatsBuilder;
72 import org.opendaylight.yangtools.yang.binding.DataObject;
73 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
74 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
75 import org.opendaylight.yangtools.yang.common.RpcError;
76 import org.opendaylight.yangtools.yang.common.RpcResult;
77 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
79 @RunWith(MockitoJUnitRunner.class)
80 public class MultipartRequestOnTheFlyCallbackTest {
82 private static final String DUMMY_NODE_ID = "dummyNodeId";
83 private static final String DUMMY_EVENT_NAME = "dummy event name 1";
84 private static final String DUMMY_DEVICE_ID = "dummy device id 1";
85 private static final Long DUMMY_XID = 55L;
86 private static final KeyedInstanceIdentifier<Node, NodeKey> NODE_PATH = KeyedInstanceIdentifier
88 .child(Node.class, new NodeKey(new NodeId("uf-node:123")));
91 private DeviceContext mockedDeviceContext;
93 private ConnectionContext mockedPrimaryConnection;
95 private NodeId mockedNodeId;
97 private FeaturesReply mockedFeaturesReply;
99 private DeviceState mockedDeviceState;
101 private DeviceInfo mockedDeviceInfo;
103 private GetFeaturesOutput mocketGetFeaturesOutput;
105 private DeviceFlowRegistry mockedFlowRegistry;
107 private FlowDescriptor mockedFlowDescriptor;
109 private ReadOnlyTransaction mockedReadOnlyTx;
111 private AbstractRequestContext<List<MultipartReply>> dummyRequestContext;
112 private final EventIdentifier dummyEventIdentifier = new EventIdentifier(DUMMY_EVENT_NAME, DUMMY_DEVICE_ID);
113 private AbstractMultipartRequestOnTheFlyCallback<MultipartReply> multipartRequestOnTheFlyCallback;
114 private final short tableId = 0;
117 public void initialization() {
118 when(mockedDeviceContext.getMessageSpy()).thenReturn(new MessageIntelligenceAgencyImpl());
119 when(mockedNodeId.toString()).thenReturn(DUMMY_NODE_ID);
120 when(mockedPrimaryConnection.getFeatures()).thenReturn(mockedFeaturesReply);
122 when(mockedDeviceContext.getPrimaryConnectionContext()).thenReturn(mockedPrimaryConnection);
123 when(mockedDeviceInfo.getNodeInstanceIdentifier()).thenReturn(NODE_PATH);
124 when(mockedDeviceInfo.getNodeId()).thenReturn(mockedNodeId);
125 when(mockedDeviceInfo.getDatapathId()).thenReturn(BigInteger.TEN);
126 when(mockedDeviceInfo.getVersion()).thenReturn(OFConstants.OFP_VERSION_1_3);
128 when(mockedDeviceContext.getDeviceInfo()).thenReturn(mockedDeviceInfo);
129 when(mockedDeviceContext.getDeviceFlowRegistry()).thenReturn(mockedFlowRegistry);
130 when(mockedFlowRegistry.retrieveDescriptor(any(FlowRegistryKey.class)))
131 .thenReturn(mockedFlowDescriptor);
133 final InstanceIdentifier<FlowCapableNode> nodePath =
134 mockedDeviceInfo.getNodeInstanceIdentifier().augmentation(FlowCapableNode.class);
135 final FlowCapableNodeBuilder flowNodeBuilder = new FlowCapableNodeBuilder();
136 flowNodeBuilder.setTable(Collections.<Table>emptyList());
137 final Optional<FlowCapableNode> flowNodeOpt = Optional.of(flowNodeBuilder.build());
138 final CheckedFuture<Optional<FlowCapableNode>, ReadFailedException> flowNodeFuture =
139 Futures.immediateCheckedFuture(flowNodeOpt);
140 dummyRequestContext = new AbstractRequestContext<List<MultipartReply>>(DUMMY_XID) {
143 public void close() {
148 final ConvertorManager convertorManager = ConvertorManagerFactory.createDefaultManager();
149 multipartRequestOnTheFlyCallback = new MultiLayerFlowMultipartRequestOnTheFlyCallback<>(
153 dummyEventIdentifier,
154 MultipartWriterProviderFactory.createDefaultProvider(mockedDeviceContext),
160 public void testOnSuccessWithNull() throws Exception {
161 multipartRequestOnTheFlyCallback.onSuccess(null);
162 final RpcResult<List<MultipartReply>> expectedRpcResult =
163 RpcResultBuilder.success(Collections.<MultipartReply>emptyList()).build();
164 final RpcResult<List<MultipartReply>> actualResult = dummyRequestContext.getFuture().get();
165 assertEquals(expectedRpcResult.getErrors(), actualResult.getErrors());
166 assertEquals(expectedRpcResult.getResult(), actualResult.getResult());
167 assertEquals(expectedRpcResult.isSuccessful(), actualResult.isSuccessful());
171 public void testOnSuccessWithNotMultiNoMultipart() throws Exception {
172 final HelloMessage mockedHelloMessage = mock(HelloMessage.class);
173 multipartRequestOnTheFlyCallback.onSuccess(mockedHelloMessage);
175 final RpcResult<List<MultipartReply>> expectedRpcResult =
176 RpcResultBuilder.<List<MultipartReply>>failed().withError(RpcError.ErrorType.APPLICATION,
177 String.format("Unexpected response type received: %s.", mockedHelloMessage.getClass())).build();
178 final RpcResult<List<MultipartReply>> actualResult = dummyRequestContext.getFuture().get();
179 assertNotNull(actualResult.getErrors());
180 assertEquals(1, actualResult.getErrors().size());
182 final RpcError actualError = actualResult.getErrors().iterator().next();
183 assertEquals(actualError.getMessage(),
184 String.format("Unexpected response type received: %s.",
185 mockedHelloMessage.getClass()));
186 assertEquals(actualError.getErrorType(),RpcError.ErrorType.APPLICATION);
187 assertEquals(expectedRpcResult.getResult(), actualResult.getResult());
188 assertEquals(expectedRpcResult.isSuccessful(), actualResult.isSuccessful());
190 Mockito.verify(mockedDeviceContext, Mockito.never())
191 .writeToTransaction(eq(LogicalDatastoreType.OPERATIONAL),
192 ArgumentMatchers.<InstanceIdentifier>any(), ArgumentMatchers.<DataObject>any());
193 Mockito.verify(mockedDeviceContext).submitTransaction();
197 * Not the last reply.
200 public void testOnSuccessWithValidMultipart1() throws Exception {
201 final MatchBuilder matchBuilder = new MatchBuilder()
202 .setMatchEntry(Collections.<MatchEntry>emptyList());
203 final FlowStatsBuilder flowStatsBuilder = new FlowStatsBuilder()
206 .setCookie(BigInteger.ZERO)
207 .setByteCount(BigInteger.TEN)
208 .setPacketCount(BigInteger.ONE)
210 .setDurationNsec(12L)
211 .setMatch(matchBuilder.build())
212 .setFlags(new FlowModFlags(true, false, false, false, false));
213 final MultipartReplyFlowBuilder multipartReplyFlowBuilder = new MultipartReplyFlowBuilder()
214 .setFlowStats(Collections.singletonList(flowStatsBuilder.build()));
215 final MultipartReplyFlowCaseBuilder multipartReplyFlowCaseBuilder = new MultipartReplyFlowCaseBuilder()
216 .setMultipartReplyFlow(multipartReplyFlowBuilder.build());
217 final MultipartReplyMessageBuilder mpReplyMessage = new MultipartReplyMessageBuilder()
218 .setType(MultipartType.OFPMPFLOW)
219 .setFlags(new MultipartRequestFlags(true))
220 .setMultipartReplyBody(multipartReplyFlowCaseBuilder.build())
223 final InstanceIdentifier<FlowCapableNode> nodePath = mockedDeviceInfo.getNodeInstanceIdentifier()
224 .augmentation(FlowCapableNode.class);
225 final FlowCapableNodeBuilder flowNodeBuilder = new FlowCapableNodeBuilder();
226 final TableBuilder tableDataBld = new TableBuilder();
227 tableDataBld.setId(tableId);
228 flowNodeBuilder.setTable(Collections.singletonList(tableDataBld.build()));
229 final Optional<FlowCapableNode> flowNodeOpt = Optional.of(flowNodeBuilder.build());
230 final CheckedFuture<Optional<FlowCapableNode>, ReadFailedException> flowNodeFuture = Futures
231 .immediateCheckedFuture(flowNodeOpt);
233 multipartRequestOnTheFlyCallback.onSuccess(mpReplyMessage.build());
235 verify(mockedReadOnlyTx, times(0)).read(LogicalDatastoreType.OPERATIONAL, nodePath);
236 verify(mockedReadOnlyTx, times(0)).close();
237 verify(mockedDeviceContext, times(1)).writeToTransaction(eq(LogicalDatastoreType.OPERATIONAL),
245 public void testOnSuccessWithValidMultipart2() throws Exception {
246 final MultipartReplyMessageBuilder mpReplyMessage = new MultipartReplyMessageBuilder()
247 .setType(MultipartType.OFPMPDESC)
248 .setFlags(new MultipartRequestFlags(false));
250 multipartRequestOnTheFlyCallback.onSuccess(mpReplyMessage.build());
252 final RpcResult<List<MultipartReply>> actualResult = dummyRequestContext.getFuture().get();
254 // Nothing else than flow is supported by on the fly callback
255 assertNotNull(actualResult.getErrors());
256 assertFalse(actualResult.getErrors().isEmpty());
257 Mockito.verify(mockedFlowRegistry, Mockito.never()).store(any());
258 Mockito.verify(mockedDeviceContext, Mockito.never())
259 .writeToTransaction(eq(LogicalDatastoreType.OPERATIONAL),
260 ArgumentMatchers.<InstanceIdentifier>any(), ArgumentMatchers.<DataObject>any());