Bump MRI upstreams
[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 java.util.Collections;
21 import java.util.List;
22 import java.util.Optional;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.junit.runner.RunWith;
26 import org.mockito.ArgumentMatchers;
27 import org.mockito.Mock;
28 import org.mockito.Mockito;
29 import org.mockito.junit.MockitoJUnitRunner;
30 import org.opendaylight.mdsal.binding.api.ReadTransaction;
31 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
32 import org.opendaylight.openflowplugin.api.OFConstants;
33 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
34 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
35 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
36 import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
37 import org.opendaylight.openflowplugin.api.openflow.registry.flow.DeviceFlowRegistry;
38 import org.opendaylight.openflowplugin.api.openflow.registry.flow.FlowDescriptor;
39 import org.opendaylight.openflowplugin.api.openflow.registry.flow.FlowRegistryKey;
40 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.EventIdentifier;
41 import org.opendaylight.openflowplugin.impl.datastore.MultipartWriterProviderFactory;
42 import org.opendaylight.openflowplugin.impl.rpc.AbstractRequestContext;
43 import org.opendaylight.openflowplugin.impl.services.multilayer.MultiLayerFlowMultipartRequestOnTheFlyCallback;
44 import org.opendaylight.openflowplugin.impl.statistics.ofpspecific.MessageIntelligenceAgencyImpl;
45 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorManager;
46 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorManagerFactory;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
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.binding.util.BindingMap;
70 import org.opendaylight.yangtools.yang.common.ErrorType;
71 import org.opendaylight.yangtools.yang.common.RpcError;
72 import org.opendaylight.yangtools.yang.common.RpcResult;
73 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
74 import org.opendaylight.yangtools.yang.common.Uint16;
75 import org.opendaylight.yangtools.yang.common.Uint32;
76 import org.opendaylight.yangtools.yang.common.Uint64;
77 import org.opendaylight.yangtools.yang.common.Uint8;
78
79 @RunWith(MockitoJUnitRunner.class)
80 public class MultipartRequestOnTheFlyCallbackTest {
81
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 Uint32 DUMMY_XID = Uint32.valueOf(55L);
86     private static final KeyedInstanceIdentifier<Node, NodeKey> NODE_PATH = KeyedInstanceIdentifier
87             .create(Nodes.class)
88             .child(Node.class, new NodeKey(new NodeId("uf-node:123")));
89
90     @Mock
91     private DeviceContext mockedDeviceContext;
92     @Mock
93     private ConnectionContext mockedPrimaryConnection;
94     @Mock
95     private NodeId mockedNodeId;
96     @Mock
97     private FeaturesReply mockedFeaturesReply;
98     @Mock
99     private DeviceState mockedDeviceState;
100     @Mock
101     private DeviceInfo mockedDeviceInfo;
102     @Mock
103     private GetFeaturesOutput mocketGetFeaturesOutput;
104     @Mock
105     private DeviceFlowRegistry mockedFlowRegistry;
106     @Mock
107     private FlowDescriptor mockedFlowDescriptor;
108     @Mock
109     private ReadTransaction mockedReadOnlyTx;
110
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 Uint8 tableId = Uint8.ZERO;
115
116     @Before
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);
121
122         when(mockedFlowDescriptor.getFlowId()).thenReturn(new FlowId("mookus"));
123
124         when(mockedDeviceContext.getPrimaryConnectionContext()).thenReturn(mockedPrimaryConnection);
125         when(mockedDeviceInfo.getNodeInstanceIdentifier()).thenReturn(NODE_PATH);
126         when(mockedDeviceInfo.getNodeId()).thenReturn(mockedNodeId);
127         when(mockedDeviceInfo.getDatapathId()).thenReturn(Uint64.valueOf(10));
128         when(mockedDeviceInfo.getVersion()).thenReturn(OFConstants.OFP_VERSION_1_3);
129
130         when(mockedDeviceContext.getDeviceInfo()).thenReturn(mockedDeviceInfo);
131         when(mockedDeviceContext.getDeviceFlowRegistry()).thenReturn(mockedFlowRegistry);
132         when(mockedFlowRegistry.retrieveDescriptor(any(FlowRegistryKey.class)))
133                 .thenReturn(mockedFlowDescriptor);
134
135         final InstanceIdentifier<FlowCapableNode> nodePath =
136                 mockedDeviceInfo.getNodeInstanceIdentifier().augmentation(FlowCapableNode.class);
137         final FlowCapableNodeBuilder flowNodeBuilder = new FlowCapableNodeBuilder();
138         flowNodeBuilder.setTable(Collections.emptyMap());
139         final Optional<FlowCapableNode> flowNodeOpt = Optional.of(flowNodeBuilder.build());
140         dummyRequestContext = new AbstractRequestContext<>(DUMMY_XID) {
141
142             @Override
143             public void close() {
144                 //NOOP
145             }
146         };
147
148         final ConvertorManager convertorManager = ConvertorManagerFactory.createDefaultManager();
149         multipartRequestOnTheFlyCallback = new MultiLayerFlowMultipartRequestOnTheFlyCallback<>(
150             dummyRequestContext,
151             String.class,
152             mockedDeviceContext,
153             dummyEventIdentifier,
154             MultipartWriterProviderFactory.createDefaultProvider(mockedDeviceContext),
155             convertorManager);
156     }
157
158
159     @Test
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());
168     }
169
170     @Test
171     public void testOnSuccessWithNotMultiNoMultipart() throws Exception {
172         final HelloMessage mockedHelloMessage = mock(HelloMessage.class);
173         multipartRequestOnTheFlyCallback.onSuccess(mockedHelloMessage);
174
175         final RpcResult<List<MultipartReply>> expectedRpcResult =
176                 RpcResultBuilder.<List<MultipartReply>>failed().withError(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());
181
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(), ErrorType.APPLICATION);
187         assertEquals(expectedRpcResult.getResult(), actualResult.getResult());
188         assertEquals(expectedRpcResult.isSuccessful(), actualResult.isSuccessful());
189
190         Mockito.verify(mockedDeviceContext, Mockito.never())
191                 .writeToTransaction(eq(LogicalDatastoreType.OPERATIONAL),
192                         ArgumentMatchers.<InstanceIdentifier>any(), ArgumentMatchers.any());
193         Mockito.verify(mockedDeviceContext).submitTransaction();
194     }
195
196     /**
197      * Not the last reply.
198      */
199     @Test
200     public void testOnSuccessWithValidMultipart1() {
201         final MatchBuilder matchBuilder = new MatchBuilder()
202                 .setMatchEntry(Collections.emptyList());
203         final FlowStatsBuilder flowStatsBuilder = new FlowStatsBuilder()
204                 .setTableId(tableId)
205                 .setPriority(Uint16.TWO)
206                 .setCookie(Uint64.ZERO)
207                 .setByteCount(Uint64.TEN)
208                 .setPacketCount(Uint64.ONE)
209                 .setDurationSec(Uint32.valueOf(11))
210                 .setDurationNsec(Uint32.valueOf(12))
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())
221                 .setXid(Uint32.valueOf(21));
222
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(BindingMap.of(tableDataBld.build()));
229         final Optional<FlowCapableNode> flowNodeOpt = Optional.of(flowNodeBuilder.build());
230
231         multipartRequestOnTheFlyCallback.onSuccess(mpReplyMessage.build());
232
233         verify(mockedReadOnlyTx, times(0)).read(LogicalDatastoreType.OPERATIONAL, nodePath);
234         verify(mockedReadOnlyTx, times(0)).close();
235         verify(mockedDeviceContext, times(1)).writeToTransaction(eq(LogicalDatastoreType.OPERATIONAL),
236                 any(), any());
237     }
238
239     /**
240      * The last reply.
241      */
242     @Test
243     public void testOnSuccessWithValidMultipart2() throws Exception {
244         final MultipartReplyMessageBuilder mpReplyMessage = new MultipartReplyMessageBuilder()
245                 .setType(MultipartType.OFPMPDESC)
246                 .setFlags(new MultipartRequestFlags(false));
247
248         multipartRequestOnTheFlyCallback.onSuccess(mpReplyMessage.build());
249
250         final RpcResult<List<MultipartReply>> actualResult = dummyRequestContext.getFuture().get();
251
252         // Nothing else than flow is supported by on the fly callback
253         assertNotNull(actualResult.getErrors());
254         assertFalse(actualResult.getErrors().isEmpty());
255         Mockito.verify(mockedFlowRegistry, Mockito.never()).store(any());
256         Mockito.verify(mockedDeviceContext, Mockito.never())
257                 .writeToTransaction(eq(LogicalDatastoreType.OPERATIONAL),
258                         ArgumentMatchers.<InstanceIdentifier>any(), ArgumentMatchers.any());
259     }
260 }