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.ArrayList;
12 import java.util.List;
13 import java.util.concurrent.atomic.AtomicLong;
14 import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
15 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
16 import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
17 import org.opendaylight.openflowplugin.api.openflow.device.Xid;
18 import org.opendaylight.openflowplugin.impl.services.util.RequestInputUtils;
19 import org.opendaylight.openflowplugin.impl.services.util.ServiceException;
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.GetFlowTablesStatisticsInput;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.GetFlowTablesStatisticsOutput;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.GetFlowTablesStatisticsOutputBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.OpendaylightFlowTableStatisticsService;
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.transaction.rev150304.TransactionId;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReply;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartRequestInputBuilder;
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.reply.multipart.reply.body.multipart.reply.table._case.MultipartReplyTable;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.table._case.multipart.reply.table.TableStats;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.MultipartRequestTableCaseBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.multipart.request.table._case.MultipartRequestTableBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.TableId;
42 import org.opendaylight.yangtools.yang.common.RpcResult;
44 public final class OpendaylightFlowTableStatisticsServiceImpl extends
45 AbstractCompatibleStatService<GetFlowTablesStatisticsInput,
46 GetFlowTablesStatisticsOutput,
47 FlowTableStatisticsUpdate> implements
48 OpendaylightFlowTableStatisticsService {
50 private final NotificationPublishService notificationPublishService;
52 public OpendaylightFlowTableStatisticsServiceImpl(final RequestContextStack requestContextStack,
53 final DeviceContext deviceContext,
54 final AtomicLong compatibilityXidSeed,
55 final NotificationPublishService notificationPublishService) {
56 super(requestContextStack, deviceContext, compatibilityXidSeed);
57 this.notificationPublishService = notificationPublishService;
61 public ListenableFuture<RpcResult<GetFlowTablesStatisticsOutput>> getFlowTablesStatistics(
62 final GetFlowTablesStatisticsInput input) {
63 return handleAndNotify(input, notificationPublishService);
67 protected OfHeader buildRequest(final Xid xid, final GetFlowTablesStatisticsInput input) throws ServiceException {
68 // Create multipart request body for fetch all the group stats
69 final MultipartRequestTableCaseBuilder multipartRequestTableCaseBuilder =
70 new MultipartRequestTableCaseBuilder();
71 final MultipartRequestTableBuilder multipartRequestTableBuilder = new MultipartRequestTableBuilder();
72 multipartRequestTableBuilder.setEmpty(true);
73 multipartRequestTableCaseBuilder.setMultipartRequestTable(multipartRequestTableBuilder.build());
75 // Set request body to main multipart request
76 final MultipartRequestInputBuilder mprInput = RequestInputUtils.createMultipartHeader(
77 MultipartType.OFPMPTABLE, xid.getValue(), getVersion());
79 mprInput.setMultipartRequestBody(multipartRequestTableCaseBuilder.build());
81 return mprInput.build();
85 public GetFlowTablesStatisticsOutput buildTxCapableResult(TransactionId emulatedTxId) {
86 return new GetFlowTablesStatisticsOutputBuilder().setTransactionId(emulatedTxId).build();
90 public FlowTableStatisticsUpdate transformToNotification(List<MultipartReply> mpReplyList,
91 TransactionId emulatedTxId) {
92 FlowTableStatisticsUpdateBuilder notification = new FlowTableStatisticsUpdateBuilder();
93 notification.setId(getDeviceInfo().getNodeId());
94 notification.setMoreReplies(Boolean.FALSE);
95 notification.setTransactionId(emulatedTxId);
97 final List<FlowTableAndStatisticsMap> salFlowStats = new ArrayList<>();
98 notification.setFlowTableAndStatisticsMap(salFlowStats);
99 for (MultipartReply mpReply : mpReplyList) {
100 MultipartReplyTableCase caseBody = (MultipartReplyTableCase) mpReply.getMultipartReplyBody();
101 MultipartReplyTable replyBody = caseBody.getMultipartReplyTable();
102 List<TableStats> swTablesStats = replyBody.getTableStats();
104 //TODO: Duplicate code: look at MultiReplyTranslatorUtil method translateTable
105 for (TableStats swTableStats : swTablesStats) {
106 FlowTableAndStatisticsMapBuilder statisticsBuilder = new FlowTableAndStatisticsMapBuilder();
107 statisticsBuilder.setActiveFlows(new Counter32(swTableStats.getActiveCount()));
108 statisticsBuilder.setPacketsLookedUp(new Counter64(swTableStats.getLookupCount()));
109 statisticsBuilder.setPacketsMatched(new Counter64(swTableStats.getMatchedCount()));
110 statisticsBuilder.setTableId(new TableId(swTableStats.getTableId()));
111 salFlowStats.add(statisticsBuilder.build());
115 return notification.build();