efc8e6fcb2e61e3c84c7c4373d50bd26c9431ab1
[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.RequestInputUtils;
19 import org.opendaylight.openflowplugin.impl.services.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, GetFlowTablesStatisticsOutput, FlowTableStatisticsUpdate> implements
46         OpendaylightFlowTableStatisticsService {
47
48     private final NotificationPublishService notificationPublishService;
49
50     public OpendaylightFlowTableStatisticsServiceImpl(final RequestContextStack requestContextStack,
51                                                       final DeviceContext deviceContext,
52                                                       final AtomicLong compatibilityXidSeed,
53                                                       final NotificationPublishService notificationPublishService) {
54         super(requestContextStack, deviceContext, compatibilityXidSeed);
55         this.notificationPublishService = notificationPublishService;
56     }
57
58     @Override
59     public Future<RpcResult<GetFlowTablesStatisticsOutput>> getFlowTablesStatistics(
60             final GetFlowTablesStatisticsInput input) {
61         return handleAndNotify(input, notificationPublishService);
62     }
63
64     @Override
65     protected OfHeader buildRequest(final Xid xid, final GetFlowTablesStatisticsInput input) throws ServiceException {
66         // Create multipart request body for fetch all the group stats
67         final MultipartRequestTableCaseBuilder multipartRequestTableCaseBuilder = new MultipartRequestTableCaseBuilder();
68         final MultipartRequestTableBuilder multipartRequestTableBuilder = new MultipartRequestTableBuilder();
69         multipartRequestTableBuilder.setEmpty(true);
70         multipartRequestTableCaseBuilder.setMultipartRequestTable(multipartRequestTableBuilder.build());
71
72         // Set request body to main multipart request
73         final MultipartRequestInputBuilder mprInput = RequestInputUtils.createMultipartHeader(
74                 MultipartType.OFPMPTABLE, xid.getValue(), getVersion());
75
76         mprInput.setMultipartRequestBody(multipartRequestTableCaseBuilder.build());
77
78         return mprInput.build();
79     }
80
81     @Override
82     public GetFlowTablesStatisticsOutput buildTxCapableResult(TransactionId emulatedTxId) {
83         return new GetFlowTablesStatisticsOutputBuilder().setTransactionId(emulatedTxId).build();
84     }
85
86     @Override
87     public FlowTableStatisticsUpdate transformToNotification(List<MultipartReply> mpReplyList, TransactionId emulatedTxId) {
88         FlowTableStatisticsUpdateBuilder notification = new FlowTableStatisticsUpdateBuilder();
89         notification.setId(getDeviceInfo().getNodeId());
90         notification.setMoreReplies(Boolean.FALSE);
91         notification.setTransactionId(emulatedTxId);
92
93         final List<FlowTableAndStatisticsMap> salFlowStats = new ArrayList<>();
94         notification.setFlowTableAndStatisticsMap(salFlowStats);
95         for (MultipartReply mpReply : mpReplyList) {
96             MultipartReplyTableCase caseBody = (MultipartReplyTableCase) mpReply.getMultipartReplyBody();
97             MultipartReplyTable replyBody = caseBody.getMultipartReplyTable();
98             List<TableStats> swTablesStats = replyBody.getTableStats();
99
100             for (TableStats swTableStats : swTablesStats) {
101                 FlowTableAndStatisticsMapBuilder statisticsBuilder = new FlowTableAndStatisticsMapBuilder();
102                 statisticsBuilder.setActiveFlows(new Counter32(swTableStats.getActiveCount()));
103                 statisticsBuilder.setPacketsLookedUp(new Counter64(swTableStats.getLookupCount()));
104                 statisticsBuilder.setPacketsMatched(new Counter64(swTableStats.getMatchedCount()));
105                 statisticsBuilder.setTableId(new TableId(swTableStats.getTableId()));
106                 salFlowStats.add(statisticsBuilder.build());
107             }
108         }
109
110         return notification.build();
111     }
112 }