BUG-4118: Li:backward compatibility - rpcs - stats services
[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.statistics.services.compatibility.AbstractCompatibleStatService;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.Counter32;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.Counter64;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.FlowTableStatisticsUpdate;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.FlowTableStatisticsUpdateBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.GetFlowTablesStatisticsInput;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.GetFlowTablesStatisticsOutput;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.GetFlowTablesStatisticsOutputBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.OpendaylightFlowTableStatisticsService;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.flow.table.and.statistics.map.FlowTableAndStatisticsMap;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.flow.table.and.statistics.map.FlowTableAndStatisticsMapBuilder;
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.common.RpcResult;
42
43 public final class OpendaylightFlowTableStatisticsServiceImpl extends
44         AbstractCompatibleStatService<GetFlowTablesStatisticsInput, GetFlowTablesStatisticsOutput, FlowTableStatisticsUpdate> implements
45         OpendaylightFlowTableStatisticsService {
46
47     private final NotificationPublishService notificationPublishService;
48
49     public OpendaylightFlowTableStatisticsServiceImpl(final RequestContextStack requestContextStack,
50                                                       final DeviceContext deviceContext,
51                                                       final AtomicLong compatibilityXidSeed,
52                                                       final NotificationPublishService notificationPublishService) {
53         super(requestContextStack, deviceContext, compatibilityXidSeed);
54         this.notificationPublishService = notificationPublishService;
55     }
56
57     @Override
58     public Future<RpcResult<GetFlowTablesStatisticsOutput>> getFlowTablesStatistics(
59             final GetFlowTablesStatisticsInput input) {
60         return handleAndNotify(input, notificationPublishService);
61     }
62
63     @Override
64     protected OfHeader buildRequest(final Xid xid, final GetFlowTablesStatisticsInput input) {
65         // Create multipart request body for fetch all the group stats
66         final MultipartRequestTableCaseBuilder multipartRequestTableCaseBuilder = new MultipartRequestTableCaseBuilder();
67         final MultipartRequestTableBuilder multipartRequestTableBuilder = new MultipartRequestTableBuilder();
68         multipartRequestTableBuilder.setEmpty(true);
69         multipartRequestTableCaseBuilder.setMultipartRequestTable(multipartRequestTableBuilder.build());
70
71         // Set request body to main multipart request
72         final MultipartRequestInputBuilder mprInput = RequestInputUtils.createMultipartHeader(
73                 MultipartType.OFPMPTABLE, xid.getValue(), getVersion());
74
75         mprInput.setMultipartRequestBody(multipartRequestTableCaseBuilder.build());
76
77         return mprInput.build();
78     }
79
80     @Override
81     public GetFlowTablesStatisticsOutput buildTxCapableResult(TransactionId emulatedTxId) {
82         return new GetFlowTablesStatisticsOutputBuilder().setTransactionId(emulatedTxId).build();
83     }
84
85     @Override
86     public FlowTableStatisticsUpdate transformToNotification(List<MultipartReply> mpReplyList, TransactionId emulatedTxId) {
87         FlowTableStatisticsUpdateBuilder notification = new FlowTableStatisticsUpdateBuilder();
88         notification.setId(getDeviceContext().getDeviceState().getNodeId());
89         notification.setMoreReplies(Boolean.FALSE);
90         notification.setTransactionId(emulatedTxId);
91
92         final List<FlowTableAndStatisticsMap> salFlowStats = new ArrayList<>();
93         notification.setFlowTableAndStatisticsMap(salFlowStats);
94         for (MultipartReply mpReply : mpReplyList) {
95             MultipartReplyTableCase caseBody = (MultipartReplyTableCase) mpReply.getMultipartReplyBody();
96             MultipartReplyTable replyBody = caseBody.getMultipartReplyTable();
97             List<TableStats> swTablesStats = replyBody.getTableStats();
98
99             for (TableStats swTableStats : swTablesStats) {
100                 FlowTableAndStatisticsMapBuilder statisticsBuilder = new FlowTableAndStatisticsMapBuilder();
101                 statisticsBuilder.setActiveFlows(new Counter32(swTableStats.getActiveCount()));
102                 statisticsBuilder.setPacketsLookedUp(new Counter64(swTableStats.getLookupCount()));
103                 statisticsBuilder.setPacketsMatched(new Counter64(swTableStats.getMatchedCount()));
104                 statisticsBuilder.setTableId(new TableId(swTableStats.getTableId()));
105                 salFlowStats.add(statisticsBuilder.build());
106             }
107         }
108
109         return notification.build();
110     }
111 }