2 * Copyright (c) 2017 Pantheon Technologies s.r.o. 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
9 package org.opendaylight.openflowplugin.impl.statistics.services.direct.multilayer;
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;
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;
45 public class FlowDirectStatisticsServiceTest extends AbstractDirectStatisticsServiceTest {
46 static final Short TABLE_NO = 1;
47 private FlowDirectStatisticsService service;
50 public void setUp() throws Exception {
51 service = new FlowDirectStatisticsService(requestContextStack,
54 multipartWriterProvider);
55 final DeviceFlowRegistry registry = mock(DeviceFlowRegistry.class);
56 when(registry.retrieveDescriptor(any())).thenReturn(FlowDescriptorFactory.create(TABLE_NO, new FlowId("1")));
57 when(deviceContext.getDeviceFlowRegistry()).thenReturn(registry);
61 public void testBuildRequestBody() throws Exception {
62 final GetFlowStatisticsInput input = mock(GetFlowStatisticsInput.class);
64 when(input.getNode()).thenReturn(createNodeRef(NODE_ID));
65 when(input.getTableId()).thenReturn(TABLE_NO);
67 final MultipartRequestFlowCase body = (MultipartRequestFlowCase) ((MultipartRequestInput) service
68 .buildRequest(new Xid(42L), input))
69 .getMultipartRequestBody();
71 final MultipartRequestFlow flow = body.getMultipartRequestFlow();
73 assertEquals(TABLE_NO, flow.getTableId());
77 public void testBuildReply() throws Exception {
78 final MultipartReply reply = mock(MultipartReply.class);
79 final MultipartReplyFlowCase flowCase = mock(MultipartReplyFlowCase.class);
80 final MultipartReplyFlow flow = mock(MultipartReplyFlow.class);
81 final FlowStats flowStat = new FlowStatsBuilder()
85 .setByteCount(BigInteger.ONE)
86 .setPacketCount(BigInteger.ONE)
87 .setFlags(mock(FlowModFlags.class))
88 .setMatch(new org.opendaylight.yang.gen.v1.urn
89 .opendaylight.openflow.oxm.rev150225.match.grouping.MatchBuilder()
90 .setMatchEntry(Collections.emptyList())
94 final List<FlowStats> flowStats = Collections.singletonList(flowStat);
95 final List<MultipartReply> input = Collections.singletonList(reply);
97 when(flow.getFlowStats()).thenReturn(flowStats);
98 when(flowCase.getMultipartReplyFlow()).thenReturn(flow);
99 when(reply.getMultipartReplyBody()).thenReturn(flowCase);
101 final GetFlowStatisticsOutput output = service.buildReply(input, true);
102 assertTrue(output.getFlowAndStatisticsMapList().size() > 0);
104 final FlowAndStatisticsMap stats = output.getFlowAndStatisticsMapList().get(0);
106 assertEquals(stats.getTableId(), TABLE_NO);
110 public void testStoreStatistics() throws Exception {
111 final FlowAndStatisticsMapList stat = mock(FlowAndStatisticsMapList.class);
112 when(stat.getTableId()).thenReturn(TABLE_NO);
113 when(stat.getMatch()).thenReturn(new MatchBuilder().build());
115 final List<FlowAndStatisticsMapList> stats = Arrays.asList(stat);
116 final GetFlowStatisticsOutput output = mock(GetFlowStatisticsOutput.class);
117 when(output.getFlowAndStatisticsMapList()).thenReturn(stats);
119 multipartWriterProvider.lookup(MultipartType.OFPMPFLOW).get().write(output, true);
120 verify(deviceContext).writeToTransactionWithParentsSlow(eq(LogicalDatastoreType.OPERATIONAL), any(), any());