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.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 org.junit.Before;
26 import org.junit.Test;
27 import org.junit.runner.RunWith;
28 import org.mockito.Matchers;
29 import org.mockito.Mock;
30 import org.mockito.Mockito;
31 import org.mockito.runners.MockitoJUnitRunner;
32 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
33 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
34 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
35 import org.opendaylight.openflowplugin.api.OFConstants;
36 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
37 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
38 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
39 import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
40 import org.opendaylight.openflowplugin.api.openflow.registry.flow.DeviceFlowRegistry;
41 import org.opendaylight.openflowplugin.api.openflow.registry.flow.FlowDescriptor;
42 import org.opendaylight.openflowplugin.api.openflow.registry.flow.FlowRegistryKey;
43 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.EventIdentifier;
44 import org.opendaylight.openflowplugin.impl.datastore.MultipartWriterProviderFactory;
45 import org.opendaylight.openflowplugin.impl.rpc.AbstractRequestContext;
46 import org.opendaylight.openflowplugin.impl.services.multilayer.MultiLayerFlowMultipartRequestOnTheFlyCallback;
47 import org.opendaylight.openflowplugin.impl.statistics.ofpspecific.MessageIntelligenceAgencyImpl;
48 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorManager;
49 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorManagerFactory;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableBuilder;
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 ConnectionContext mockedPrimaryConnection;
94 private NodeId mockedNodeId;
96 private FeaturesReply mockedFeaturesReply;
98 private DeviceState mockedDeviceState;
100 private DeviceInfo mockedDeviceInfo;
102 private GetFeaturesOutput mocketGetFeaturesOutput;
104 private DeviceFlowRegistry mockedFlowRegistry;
106 private FlowDescriptor mockedFlowDescriptor;
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 AbstractMultipartRequestOnTheFlyCallback<MultipartReply> 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);
137 when(mockedFlowRegistry.retrieveDescriptor(Matchers.any(FlowRegistryKey.class)))
138 .thenReturn(mockedFlowDescriptor);
140 final InstanceIdentifier<FlowCapableNode> nodePath =
141 mockedDeviceInfo.getNodeInstanceIdentifier().augmentation(FlowCapableNode.class);
142 final FlowCapableNodeBuilder flowNodeBuilder = new FlowCapableNodeBuilder();
143 flowNodeBuilder.setTable(Collections.<Table>emptyList());
144 final Optional<FlowCapableNode> flowNodeOpt = Optional.of(flowNodeBuilder.build());
145 final CheckedFuture<Optional<FlowCapableNode>, ReadFailedException> flowNodeFuture =
146 Futures.immediateCheckedFuture(flowNodeOpt);
147 when(mockedReadOnlyTx.read(LogicalDatastoreType.OPERATIONAL, nodePath)).thenReturn(flowNodeFuture);
148 when(mockedDeviceContext.getReadTransaction()).thenReturn(mockedReadOnlyTx);
150 dummyRequestContext = new AbstractRequestContext<List<MultipartReply>>(DUMMY_XID) {
153 public void close() {
158 final ConvertorManager convertorManager = ConvertorManagerFactory.createDefaultManager();
159 multipartRequestOnTheFlyCallback = new MultiLayerFlowMultipartRequestOnTheFlyCallback<>(
163 dummyEventIdentifier,
164 MultipartWriterProviderFactory.createDefaultProvider(mockedDeviceContext),
170 public void testOnSuccessWithNull() throws Exception {
171 multipartRequestOnTheFlyCallback.onSuccess(null);
172 final RpcResult<List<MultipartReply>> expectedRpcResult =
173 RpcResultBuilder.success(Collections.<MultipartReply>emptyList()).build();
174 final RpcResult<List<MultipartReply>> actualResult = dummyRequestContext.getFuture().get();
175 assertEquals(expectedRpcResult.getErrors(), actualResult.getErrors());
176 assertEquals(expectedRpcResult.getResult(), actualResult.getResult());
177 assertEquals(expectedRpcResult.isSuccessful(), actualResult.isSuccessful());
181 public void testOnSuccessWithNotMultiNoMultipart() throws Exception {
182 final HelloMessage mockedHelloMessage = mock(HelloMessage.class);
183 multipartRequestOnTheFlyCallback.onSuccess(mockedHelloMessage);
185 final RpcResult<List<MultipartReply>> expectedRpcResult =
186 RpcResultBuilder.<List<MultipartReply>>failed().withError(RpcError.ErrorType.APPLICATION,
187 String.format("Unexpected response type received: %s.", mockedHelloMessage.getClass())).build();
188 final RpcResult<List<MultipartReply>> actualResult = dummyRequestContext.getFuture().get();
189 assertNotNull(actualResult.getErrors());
190 assertEquals(1, actualResult.getErrors().size());
192 final RpcError actualError = actualResult.getErrors().iterator().next();
193 assertEquals(actualError.getMessage(),
194 String.format("Unexpected response type received: %s.",
195 mockedHelloMessage.getClass()));
196 assertEquals(actualError.getErrorType(),RpcError.ErrorType.APPLICATION);
197 assertEquals(expectedRpcResult.getResult(), actualResult.getResult());
198 assertEquals(expectedRpcResult.isSuccessful(), actualResult.isSuccessful());
200 Mockito.verify(mockedDeviceContext, Mockito.never())
201 .writeToTransaction(Matchers.eq(LogicalDatastoreType.OPERATIONAL),
202 Matchers.<InstanceIdentifier>any(), Matchers.<DataObject>any());
203 Mockito.verify(mockedDeviceContext).submitTransaction();
207 * Not the last reply.
210 public void testOnSuccessWithValidMultipart1() throws Exception {
211 final MatchBuilder matchBuilder = new MatchBuilder()
212 .setMatchEntry(Collections.<MatchEntry>emptyList());
213 final FlowStatsBuilder flowStatsBuilder = new FlowStatsBuilder()
216 .setCookie(BigInteger.ZERO)
217 .setByteCount(BigInteger.TEN)
218 .setPacketCount(BigInteger.ONE)
220 .setDurationNsec(12L)
221 .setMatch(matchBuilder.build())
222 .setFlags(new FlowModFlags(true, false, false, false, false));
223 final MultipartReplyFlowBuilder multipartReplyFlowBuilder = new MultipartReplyFlowBuilder()
224 .setFlowStats(Collections.singletonList(flowStatsBuilder.build()));
225 final MultipartReplyFlowCaseBuilder multipartReplyFlowCaseBuilder = new MultipartReplyFlowCaseBuilder()
226 .setMultipartReplyFlow(multipartReplyFlowBuilder.build());
227 final MultipartReplyMessageBuilder mpReplyMessage = new MultipartReplyMessageBuilder()
228 .setType(MultipartType.OFPMPFLOW)
229 .setFlags(new MultipartRequestFlags(true))
230 .setMultipartReplyBody(multipartReplyFlowCaseBuilder.build())
233 final InstanceIdentifier<FlowCapableNode> nodePath = mockedDeviceInfo.getNodeInstanceIdentifier()
234 .augmentation(FlowCapableNode.class);
235 final FlowCapableNodeBuilder flowNodeBuilder = new FlowCapableNodeBuilder();
236 final TableBuilder tableDataBld = new TableBuilder();
237 tableDataBld.setId(tableId);
238 flowNodeBuilder.setTable(Collections.singletonList(tableDataBld.build()));
239 final Optional<FlowCapableNode> flowNodeOpt = Optional.of(flowNodeBuilder.build());
240 final CheckedFuture<Optional<FlowCapableNode>, ReadFailedException> flowNodeFuture = Futures
241 .immediateCheckedFuture(flowNodeOpt);
242 when(mockedReadOnlyTx.read(LogicalDatastoreType.OPERATIONAL, nodePath)).thenReturn(flowNodeFuture);
243 when(mockedDeviceContext.getReadTransaction()).thenReturn(mockedReadOnlyTx);
245 multipartRequestOnTheFlyCallback.onSuccess(mpReplyMessage.build());
247 verify(mockedReadOnlyTx, times(0)).read(LogicalDatastoreType.OPERATIONAL, nodePath);
248 verify(mockedReadOnlyTx, times(0)).close();
249 verify(mockedDeviceContext, times(1)).writeToTransaction(eq(LogicalDatastoreType.OPERATIONAL),
250 Matchers.any(), Matchers.any());
257 public void testOnSuccessWithValidMultipart2() throws Exception {
258 final MultipartReplyMessageBuilder mpReplyMessage = new MultipartReplyMessageBuilder()
259 .setType(MultipartType.OFPMPDESC)
260 .setFlags(new MultipartRequestFlags(false));
262 multipartRequestOnTheFlyCallback.onSuccess(mpReplyMessage.build());
264 final RpcResult<List<MultipartReply>> actualResult = dummyRequestContext.getFuture().get();
266 // Nothing else than flow is supported by on the fly callback
267 assertNotNull(actualResult.getErrors());
268 assertFalse(actualResult.getErrors().isEmpty());
269 Mockito.verify(mockedFlowRegistry, Mockito.never()).store(Matchers.any());
270 Mockito.verify(mockedDeviceContext, Mockito.never())
271 .writeToTransaction(Matchers.eq(LogicalDatastoreType.OPERATIONAL),
272 Matchers.<InstanceIdentifier>any(), Matchers.<DataObject>any());