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.statistics.services;
10 import static java.util.Objects.requireNonNull;
12 import com.google.common.util.concurrent.ListenableFuture;
13 import java.util.List;
14 import java.util.concurrent.atomic.AtomicLong;
15 import org.opendaylight.mdsal.binding.api.NotificationPublishService;
16 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
17 import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
18 import org.opendaylight.openflowplugin.api.openflow.device.Xid;
19 import org.opendaylight.openflowplugin.impl.services.util.RequestInputUtils;
20 import org.opendaylight.openflowplugin.impl.statistics.services.compatibility.AbstractCompatibleStatService;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Counter32;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Counter64;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.FlowTableStatisticsUpdate;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.FlowTableStatisticsUpdateBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.GetFlowTablesStatistics;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.GetFlowTablesStatisticsInput;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.GetFlowTablesStatisticsOutput;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.GetFlowTablesStatisticsOutputBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.flow.table.and.statistics.map.FlowTableAndStatisticsMap;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.flow.table.and.statistics.map.FlowTableAndStatisticsMapBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.flow.table.and.statistics.map.FlowTableAndStatisticsMapKey;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.transaction.rev150304.TransactionId;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReply;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyTableCase;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.MultipartRequestTableCaseBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.multipart.request.table._case.MultipartRequestTableBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.TableId;
40 import org.opendaylight.yangtools.yang.binding.util.BindingMap;
41 import org.opendaylight.yangtools.yang.common.Empty;
42 import org.opendaylight.yangtools.yang.common.RpcResult;
45 public final class GetFlowTablesStatisticsImpl
46 extends AbstractCompatibleStatService<GetFlowTablesStatisticsInput, GetFlowTablesStatisticsOutput,
47 FlowTableStatisticsUpdate>
48 implements GetFlowTablesStatistics {
49 private final NotificationPublishService notificationPublishService;
51 public GetFlowTablesStatisticsImpl(final RequestContextStack requestContextStack, final DeviceContext deviceContext,
52 final AtomicLong compatibilityXidSeed, final NotificationPublishService notificationPublishService) {
53 super(requestContextStack, deviceContext, compatibilityXidSeed);
54 this.notificationPublishService = requireNonNull(notificationPublishService);
58 public ListenableFuture<RpcResult<GetFlowTablesStatisticsOutput>> invoke(final GetFlowTablesStatisticsInput input) {
59 return handleAndNotify(input, notificationPublishService);
63 protected OfHeader buildRequest(final Xid xid, final GetFlowTablesStatisticsInput input) {
64 // Set request body to main multipart request
65 return RequestInputUtils.createMultipartHeader(MultipartType.OFPMPTABLE, xid.getValue(), getVersion())
66 // Create multipart request body for fetch all the group stats
67 .setMultipartRequestBody(new MultipartRequestTableCaseBuilder()
68 .setMultipartRequestTable(new MultipartRequestTableBuilder()
69 .setEmpty(Empty.value())
76 public GetFlowTablesStatisticsOutput buildTxCapableResult(final TransactionId emulatedTxId) {
77 return new GetFlowTablesStatisticsOutputBuilder().setTransactionId(emulatedTxId).build();
81 public FlowTableStatisticsUpdate transformToNotification(final List<MultipartReply> mpReplyList,
82 final TransactionId emulatedTxId) {
83 final var notification = new FlowTableStatisticsUpdateBuilder()
84 .setId(getDeviceInfo().getNodeId())
85 .setMoreReplies(Boolean.FALSE)
86 .setTransactionId(emulatedTxId);
88 final var salFlowStats = BindingMap.<FlowTableAndStatisticsMapKey, FlowTableAndStatisticsMap>orderedBuilder();
89 for (var mpReply : mpReplyList) {
90 final var caseBody = (MultipartReplyTableCase) mpReply.getMultipartReplyBody();
92 //TODO: Duplicate code: look at MultiReplyTranslatorUtil method translateTable
93 for (var swTableStats : caseBody.getMultipartReplyTable().nonnullTableStats()) {
94 salFlowStats.add(new FlowTableAndStatisticsMapBuilder()
95 .setActiveFlows(new Counter32(swTableStats.getActiveCount()))
96 .setPacketsLookedUp(new Counter64(swTableStats.getLookupCount()))
97 .setPacketsMatched(new Counter64(swTableStats.getMatchedCount()))
98 .setTableId(new TableId(swTableStats.getTableId()))
103 return notification.setFlowTableAndStatisticsMap(salFlowStats.build()).build();