Update multipart request services for single layer
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / statistics / services / direct / multilayer / FlowDirectStatisticsServiceTest.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies s.r.o. 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
9 package org.opendaylight.openflowplugin.impl.statistics.services.direct.multilayer;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertTrue;
13 import static org.mockito.Matchers.any;
14 import static org.mockito.Matchers.eq;
15 import static org.mockito.Mockito.mock;
16 import static org.mockito.Mockito.verify;
17 import static org.mockito.Mockito.when;
18
19 import java.math.BigInteger;
20 import java.util.Arrays;
21 import java.util.Collections;
22 import java.util.List;
23 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
24 import org.opendaylight.openflowplugin.api.openflow.device.Xid;
25 import org.opendaylight.openflowplugin.api.openflow.registry.flow.DeviceFlowRegistry;
26 import org.opendaylight.openflowplugin.impl.registry.flow.FlowDescriptorFactory;
27 import org.opendaylight.openflowplugin.impl.statistics.services.direct.AbstractDirectStatisticsServiceTest;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.direct.statistics.rev160511.GetFlowStatisticsInput;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.direct.statistics.rev160511.GetFlowStatisticsOutput;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowAndStatisticsMap;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.flow.and.statistics.map.list.FlowAndStatisticsMapList;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.FlowModFlags;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReply;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartRequestInput;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyFlowCase;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.flow._case.MultipartReplyFlow;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.flow._case.multipart.reply.flow.FlowStats;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.flow._case.multipart.reply.flow.FlowStatsBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.MultipartRequestFlowCase;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.multipart.request.flow._case.MultipartRequestFlow;
44
45 public class FlowDirectStatisticsServiceTest extends AbstractDirectStatisticsServiceTest {
46     static final Short TABLE_NO = 1;
47     private FlowDirectStatisticsService service;
48
49     @Override
50     public void setUp() throws Exception {
51         service = new FlowDirectStatisticsService(requestContextStack, deviceContext, convertorManager, multipartWriterProvider);
52         final DeviceFlowRegistry registry = mock(DeviceFlowRegistry.class);
53         when(registry.retrieveDescriptor(any())).thenReturn(FlowDescriptorFactory.create(TABLE_NO, new FlowId("1")));
54         when(deviceContext.getDeviceFlowRegistry()).thenReturn(registry);
55     }
56
57     @Override
58     public void testBuildRequestBody() throws Exception {
59         final GetFlowStatisticsInput input = mock(GetFlowStatisticsInput.class);
60
61         when(input.getNode()).thenReturn(createNodeRef(NODE_ID));
62         when(input.getTableId()).thenReturn(TABLE_NO);
63
64         final MultipartRequestFlowCase body = (MultipartRequestFlowCase) ((MultipartRequestInput) service
65             .buildRequest(new Xid(42L), input))
66             .getMultipartRequestBody();
67
68         final MultipartRequestFlow flow = body.getMultipartRequestFlow();
69
70         assertEquals(TABLE_NO, flow.getTableId());
71     }
72
73     @Override
74     public void testBuildReply() throws Exception {
75         final MultipartReply reply = mock(MultipartReply.class);
76         final MultipartReplyFlowCase flowCase = mock(MultipartReplyFlowCase.class);
77         final MultipartReplyFlow flow = mock(MultipartReplyFlow.class);
78         final FlowStats flowStat = new FlowStatsBuilder()
79                 .setDurationSec(1L)
80                 .setDurationNsec(1L)
81                 .setTableId(TABLE_NO)
82                 .setByteCount(BigInteger.ONE)
83                 .setPacketCount(BigInteger.ONE)
84                 .setFlags(mock(FlowModFlags.class))
85                 .setMatch(new org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.grouping.MatchBuilder()
86                         .setMatchEntry(Collections.emptyList())
87                         .build())
88                 .build();
89
90         final List<FlowStats> flowStats = Collections.singletonList(flowStat);
91         final List<MultipartReply> input = Collections.singletonList(reply);
92
93         when(flow.getFlowStats()).thenReturn(flowStats);
94         when(flowCase.getMultipartReplyFlow()).thenReturn(flow);
95         when(reply.getMultipartReplyBody()).thenReturn(flowCase);
96
97         final GetFlowStatisticsOutput output = service.buildReply(input, true);
98         assertTrue(output.getFlowAndStatisticsMapList().size() > 0);
99
100         final FlowAndStatisticsMap stats = output.getFlowAndStatisticsMapList().get(0);
101
102         assertEquals(stats.getTableId(), TABLE_NO);
103     }
104
105     @Override
106     public void testStoreStatistics() throws Exception {
107         final FlowAndStatisticsMapList stat = mock(FlowAndStatisticsMapList.class);
108         when(stat.getTableId()).thenReturn(TABLE_NO);
109         when(stat.getMatch()).thenReturn(new MatchBuilder().build());
110
111         final List<FlowAndStatisticsMapList> stats = Arrays.asList(stat);
112         final GetFlowStatisticsOutput output = mock(GetFlowStatisticsOutput.class);
113         when(output.getFlowAndStatisticsMapList()).thenReturn(stats);
114
115         multipartWriterProvider.lookup(MultipartType.OFPMPFLOW).get().write(output, true);
116         verify(deviceContext).writeToTransactionWithParentsSlow(eq(LogicalDatastoreType.OPERATIONAL), any(), any());
117     }
118 }