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 com.google.common.util.concurrent.ListenableFuture;
11 import java.util.List;
12 import java.util.concurrent.atomic.AtomicLong;
13 import org.opendaylight.mdsal.binding.api.NotificationPublishService;
14 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
15 import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
16 import org.opendaylight.openflowplugin.api.openflow.device.Xid;
17 import org.opendaylight.openflowplugin.impl.services.util.RequestInputUtils;
18 import org.opendaylight.openflowplugin.impl.statistics.services.compatibility.AbstractCompatibleStatService;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Counter32;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Counter64;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.FlowTableStatisticsUpdate;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.FlowTableStatisticsUpdateBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.GetFlowTablesStatisticsInput;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.GetFlowTablesStatisticsOutput;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.GetFlowTablesStatisticsOutputBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.OpendaylightFlowTableStatisticsService;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.flow.table.and.statistics.map.FlowTableAndStatisticsMap;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.flow.table.and.statistics.map.FlowTableAndStatisticsMapBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.flow.table.and.statistics.map.FlowTableAndStatisticsMapKey;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.transaction.rev150304.TransactionId;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReply;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartRequestInputBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyTableCase;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.table._case.MultipartReplyTable;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.table._case.multipart.reply.table.TableStats;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.MultipartRequestTableCaseBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.multipart.request.table._case.MultipartRequestTableBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.TableId;
41 import org.opendaylight.yangtools.yang.binding.util.BindingMap;
42 import org.opendaylight.yangtools.yang.common.Empty;
43 import org.opendaylight.yangtools.yang.common.RpcResult;
45 public final class OpendaylightFlowTableStatisticsServiceImpl extends
46 AbstractCompatibleStatService<GetFlowTablesStatisticsInput,
47 GetFlowTablesStatisticsOutput,
48 FlowTableStatisticsUpdate> implements
49 OpendaylightFlowTableStatisticsService {
51 private final NotificationPublishService notificationPublishService;
53 public OpendaylightFlowTableStatisticsServiceImpl(final RequestContextStack requestContextStack,
54 final DeviceContext deviceContext,
55 final AtomicLong compatibilityXidSeed,
56 final NotificationPublishService notificationPublishService) {
57 super(requestContextStack, deviceContext, compatibilityXidSeed);
58 this.notificationPublishService = notificationPublishService;
62 public ListenableFuture<RpcResult<GetFlowTablesStatisticsOutput>> getFlowTablesStatistics(
63 final GetFlowTablesStatisticsInput input) {
64 return handleAndNotify(input, notificationPublishService);
68 protected OfHeader buildRequest(final Xid xid, final GetFlowTablesStatisticsInput input) {
69 // Create multipart request body for fetch all the group stats
70 final MultipartRequestTableCaseBuilder multipartRequestTableCaseBuilder =
71 new MultipartRequestTableCaseBuilder();
72 final MultipartRequestTableBuilder multipartRequestTableBuilder = new MultipartRequestTableBuilder();
73 multipartRequestTableBuilder.setEmpty(Empty.value());
74 multipartRequestTableCaseBuilder.setMultipartRequestTable(multipartRequestTableBuilder.build());
76 // Set request body to main multipart request
77 final MultipartRequestInputBuilder mprInput = RequestInputUtils.createMultipartHeader(
78 MultipartType.OFPMPTABLE, xid.getValue(), getVersion());
80 mprInput.setMultipartRequestBody(multipartRequestTableCaseBuilder.build());
82 return mprInput.build();
86 public GetFlowTablesStatisticsOutput buildTxCapableResult(final TransactionId emulatedTxId) {
87 return new GetFlowTablesStatisticsOutputBuilder().setTransactionId(emulatedTxId).build();
91 public FlowTableStatisticsUpdate transformToNotification(final List<MultipartReply> mpReplyList,
92 final TransactionId emulatedTxId) {
93 FlowTableStatisticsUpdateBuilder notification = new FlowTableStatisticsUpdateBuilder();
94 notification.setId(getDeviceInfo().getNodeId());
95 notification.setMoreReplies(Boolean.FALSE);
96 notification.setTransactionId(emulatedTxId);
98 final var salFlowStats = BindingMap.<FlowTableAndStatisticsMapKey, FlowTableAndStatisticsMap>orderedBuilder();
99 for (MultipartReply mpReply : mpReplyList) {
100 MultipartReplyTableCase caseBody = (MultipartReplyTableCase) mpReply.getMultipartReplyBody();
101 MultipartReplyTable replyBody = caseBody.getMultipartReplyTable();
103 //TODO: Duplicate code: look at MultiReplyTranslatorUtil method translateTable
104 for (TableStats swTableStats : replyBody.nonnullTableStats()) {
105 FlowTableAndStatisticsMapBuilder statisticsBuilder = new FlowTableAndStatisticsMapBuilder();
106 statisticsBuilder.setActiveFlows(new Counter32(swTableStats.getActiveCount()));
107 statisticsBuilder.setPacketsLookedUp(new Counter64(swTableStats.getLookupCount()));
108 statisticsBuilder.setPacketsMatched(new Counter64(swTableStats.getMatchedCount()));
109 statisticsBuilder.setTableId(new TableId(swTableStats.getTableId()));
110 salFlowStats.add(statisticsBuilder.build());
114 return notification.setFlowTableAndStatisticsMap(salFlowStats.build()).build();