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