statistics context implements statistic services calls
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / statistics / StatisticsGatheringUtils.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
9 package org.opendaylight.openflowplugin.impl.statistics;
10
11 import com.google.common.base.Function;
12 import com.google.common.util.concurrent.Futures;
13 import com.google.common.util.concurrent.JdkFutureAdapters;
14 import com.google.common.util.concurrent.ListenableFuture;
15 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAllFlowsStatisticsFromAllFlowTablesInputBuilder;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAllFlowsStatisticsFromAllFlowTablesOutput;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.OpendaylightFlowStatisticsService;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.flow.and.statistics.map.list.FlowAndStatisticsMapList;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.GetFlowTablesStatisticsInputBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.GetFlowTablesStatisticsOutput;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.OpendaylightFlowTableStatisticsService;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GetAllGroupStatisticsInputBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GetAllGroupStatisticsOutput;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.OpendaylightGroupStatisticsService;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.GetAllMeterStatisticsInputBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.GetAllMeterStatisticsOutput;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.OpendaylightMeterStatisticsService;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.GetAllNodeConnectorsStatisticsInputBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.GetAllNodeConnectorsStatisticsOutput;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.OpendaylightPortStatisticsService;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.GetAllQueuesStatisticsFromAllPortsInputBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.GetAllQueuesStatisticsFromAllPortsOutput;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.OpendaylightQueueStatisticsService;
39 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
40 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
41 import org.opendaylight.yangtools.yang.common.RpcResult;
42 import javax.annotation.Nullable;
43 import java.util.List;
44
45 /**
46  * Created by Martin Bobak <mbobak@cisco.com> on 2.4.2015.
47  */
48 public final class StatisticsGatheringUtils {
49
50     private StatisticsGatheringUtils() {
51         throw new IllegalStateException("This class should not be instantiated.");
52     }
53
54     private static NodeRef createNodeRef(DeviceContext deviceContext) {
55         final KeyedInstanceIdentifier<Node, NodeKey> nodeInstanceIdentifier = InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(deviceContext.getPrimaryConnectionContext().getNodeId()));
56         return new NodeRef(nodeInstanceIdentifier);
57     }
58
59     public static ListenableFuture<Boolean> gatherQueueStatistics(OpendaylightQueueStatisticsService queueStatisticsService, DeviceContext deviceContext) {
60
61         final GetAllQueuesStatisticsFromAllPortsInputBuilder builder =
62                 new GetAllQueuesStatisticsFromAllPortsInputBuilder();
63
64         builder.setNode(createNodeRef(deviceContext));
65
66         ListenableFuture<RpcResult<GetAllQueuesStatisticsFromAllPortsOutput>> statisticsDataInFuture =
67                 JdkFutureAdapters.
68                         listenInPoolThread(queueStatisticsService.
69                                 getAllQueuesStatisticsFromAllPorts(builder.build()));
70
71         return Futures.transform(statisticsDataInFuture, new Function<RpcResult<GetAllQueuesStatisticsFromAllPortsOutput>, Boolean>() {
72             @Nullable
73             @Override
74             public Boolean apply(final RpcResult<GetAllQueuesStatisticsFromAllPortsOutput> rpcResult) {
75                 if (rpcResult.isSuccessful()) {
76                     //TODO : implement data read and put them into transaction chain
77                     return Boolean.TRUE;
78                 }
79                 return Boolean.FALSE;
80             }
81         });
82     }
83
84
85     public static ListenableFuture<Boolean> gatherPortStatistics(OpendaylightPortStatisticsService portStatisticsService, DeviceContext deviceContext) {
86
87         final GetAllNodeConnectorsStatisticsInputBuilder builder =
88                 new GetAllNodeConnectorsStatisticsInputBuilder();
89
90         builder.setNode(createNodeRef(deviceContext));
91
92         ListenableFuture<RpcResult<GetAllNodeConnectorsStatisticsOutput>> statisticsDataInFuture =
93                 JdkFutureAdapters.
94                         listenInPoolThread(portStatisticsService.
95                                 getAllNodeConnectorsStatistics(builder.build()));
96
97         return Futures.transform(statisticsDataInFuture, new Function<RpcResult<GetAllNodeConnectorsStatisticsOutput>, Boolean>() {
98             @Nullable
99             @Override
100             public Boolean apply(final RpcResult<GetAllNodeConnectorsStatisticsOutput> rpcResult) {
101                 if (rpcResult.isSuccessful()) {
102                     //TODO : implement data read and put them into transaction chain
103                     return Boolean.TRUE;
104                 }
105                 return Boolean.FALSE;
106             }
107         });
108     }
109
110     public static ListenableFuture<Boolean> gatherMeterStatistics(OpendaylightMeterStatisticsService meterStatisticsService, DeviceContext deviceContext) {
111
112         final GetAllMeterStatisticsInputBuilder builder =
113                 new GetAllMeterStatisticsInputBuilder();
114
115         builder.setNode(createNodeRef(deviceContext));
116
117         ListenableFuture<RpcResult<GetAllMeterStatisticsOutput>> statisticsDataInFuture =
118                 JdkFutureAdapters.
119                         listenInPoolThread(meterStatisticsService.
120                                 getAllMeterStatistics(builder.build()));
121
122         return Futures.transform(statisticsDataInFuture, new Function<RpcResult<GetAllMeterStatisticsOutput>, Boolean>() {
123             @Nullable
124             @Override
125             public Boolean apply(final RpcResult<GetAllMeterStatisticsOutput> rpcResult) {
126                 if (rpcResult.isSuccessful()) {
127                     //TODO : implement data read and put them into transaction chain
128                     return Boolean.TRUE;
129                 }
130                 return Boolean.FALSE;
131             }
132         });
133     }
134
135
136     public static ListenableFuture<Boolean> gatherGroupStatistics(OpendaylightGroupStatisticsService groupStatisticsService, DeviceContext deviceContext) {
137         final GetAllGroupStatisticsInputBuilder builder =
138                 new GetAllGroupStatisticsInputBuilder();
139         builder.setNode(createNodeRef(deviceContext));
140         ListenableFuture<RpcResult<GetAllGroupStatisticsOutput>> allFlowTablesDataInFuture =
141                 JdkFutureAdapters.
142                         listenInPoolThread(groupStatisticsService.
143                                 getAllGroupStatistics(builder.build()));
144
145         return Futures.transform(allFlowTablesDataInFuture, new Function<RpcResult<GetAllGroupStatisticsOutput>, Boolean>() {
146             @Nullable
147             @Override
148             public Boolean apply(final RpcResult<GetAllGroupStatisticsOutput> rpcResult) {
149                 if (rpcResult.isSuccessful()) {
150                     //TODO : implement data read and put them into transaction chain
151                     return Boolean.TRUE;
152                 }
153                 return Boolean.FALSE;
154             }
155         });
156     }
157
158     public static ListenableFuture<Boolean> gatherFlowStatistics(OpendaylightFlowStatisticsService flowStatisticsService, DeviceContext deviceContext) {
159         final GetAllFlowsStatisticsFromAllFlowTablesInputBuilder builder =
160                 new GetAllFlowsStatisticsFromAllFlowTablesInputBuilder();
161         builder.setNode(createNodeRef(deviceContext));
162         ListenableFuture<RpcResult<GetAllFlowsStatisticsFromAllFlowTablesOutput>> allFlowTablesDataInFuture =
163                 JdkFutureAdapters.
164                         listenInPoolThread(flowStatisticsService.
165                                 getAllFlowsStatisticsFromAllFlowTables(builder.build()));
166
167         return Futures.transform(allFlowTablesDataInFuture, new Function<RpcResult<GetAllFlowsStatisticsFromAllFlowTablesOutput>, Boolean>() {
168             @Nullable
169             @Override
170             public Boolean apply(final RpcResult<GetAllFlowsStatisticsFromAllFlowTablesOutput> rpcResult) {
171                 if (rpcResult.isSuccessful()) {
172                     List<FlowAndStatisticsMapList> flowAndStatsList = rpcResult.getResult().getFlowAndStatisticsMapList();
173                     //TODO : implement data read and put them into transaction chain
174                     for (FlowAndStatisticsMapList flowAndStatisticsMap : flowAndStatsList) {
175                     }
176                     return Boolean.TRUE;
177                 }
178                 return Boolean.FALSE;
179             }
180         });
181     }
182
183     public static ListenableFuture<Boolean> gatherTableStatistics(OpendaylightFlowTableStatisticsService flowTableStatisticsService, DeviceContext deviceContext) {
184         GetFlowTablesStatisticsInputBuilder getFlowTablesStatisticsInputBuilder = new GetFlowTablesStatisticsInputBuilder();
185         getFlowTablesStatisticsInputBuilder.setNode(createNodeRef(deviceContext));
186         ListenableFuture<RpcResult<GetFlowTablesStatisticsOutput>> flowTableStaticsDataInFuture = JdkFutureAdapters.listenInPoolThread(flowTableStatisticsService.getFlowTablesStatistics(getFlowTablesStatisticsInputBuilder.build()));
187         return Futures.transform(flowTableStaticsDataInFuture, new Function<RpcResult<GetFlowTablesStatisticsOutput>, Boolean>() {
188             @Nullable
189             @Override
190             public Boolean apply(
191                     final RpcResult<GetFlowTablesStatisticsOutput> rpcResult) {
192                 if (rpcResult.isSuccessful()) {
193                     //TODO : implement data read and put them into transaction chain
194                     return Boolean.TRUE;
195                 }
196                 return Boolean.FALSE;
197             }
198         });
199     }
200 }