9249109c63e090cdd676a2856872348b86eddfaf
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / statistics / services / OpendaylightFlowTableStatisticsServiceImpl.java
1 /**
2  * Copyright (c) 2015 Cisco Systems, Inc. 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 package org.opendaylight.openflowplugin.impl.statistics.services;
9
10 import java.util.ArrayList;
11 import java.util.List;
12 import java.util.concurrent.Future;
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;
43
44 public final class OpendaylightFlowTableStatisticsServiceImpl extends
45         AbstractCompatibleStatService<GetFlowTablesStatisticsInput,
46         GetFlowTablesStatisticsOutput,
47         FlowTableStatisticsUpdate> implements
48         OpendaylightFlowTableStatisticsService {
49
50     private final NotificationPublishService notificationPublishService;
51
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;
58     }
59
60     @Override
61     public Future<RpcResult<GetFlowTablesStatisticsOutput>> getFlowTablesStatistics(
62             final GetFlowTablesStatisticsInput input) {
63         return handleAndNotify(input, notificationPublishService);
64     }
65
66     @Override
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());
74
75         // Set request body to main multipart request
76         final MultipartRequestInputBuilder mprInput = RequestInputUtils.createMultipartHeader(
77                 MultipartType.OFPMPTABLE, xid.getValue(), getVersion());
78
79         mprInput.setMultipartRequestBody(multipartRequestTableCaseBuilder.build());
80
81         return mprInput.build();
82     }
83
84     @Override
85     public GetFlowTablesStatisticsOutput buildTxCapableResult(TransactionId emulatedTxId) {
86         return new GetFlowTablesStatisticsOutputBuilder().setTransactionId(emulatedTxId).build();
87     }
88
89     @Override
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);
96
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();
103
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());
112             }
113         }
114
115         return notification.build();
116     }
117 }