statistics context gather only relevant statistics
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / statistics / StatisticsContextImpl.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.util.concurrent.FutureCallback;
12 import com.google.common.util.concurrent.Futures;
13 import com.google.common.util.concurrent.ListenableFuture;
14 import com.google.common.util.concurrent.SettableFuture;
15 import java.util.ArrayList;
16 import java.util.Arrays;
17 import java.util.List;
18 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
19 import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
20 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
21 import org.opendaylight.openflowplugin.api.openflow.statistics.StatisticsContext;
22 import org.opendaylight.openflowplugin.impl.rpc.RequestContextImpl;
23 import org.opendaylight.openflowplugin.impl.statistics.services.dedicated.StatisticsGatheringService;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAllFlowsStatisticsFromAllFlowTablesInputBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;
30 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
31 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
32 import org.opendaylight.yangtools.yang.common.RpcResult;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 /**
37  * Created by Martin Bobak <mbobak@cisco.com> on 1.4.2015.
38  */
39 public class StatisticsContextImpl implements StatisticsContext {
40
41     private static final Logger LOG = LoggerFactory.getLogger(StatisticsContextImpl.class);
42     private final List<RequestContext> requestContexts = new ArrayList();
43     private final DeviceContext deviceContext;
44
45
46     private final StatisticsGatheringService statisticsGatheringService;
47
48     public StatisticsContextImpl(final DeviceContext deviceContext) {
49         this.deviceContext = deviceContext;
50         statisticsGatheringService = new StatisticsGatheringService(this, deviceContext);
51
52     }
53
54     private void pollFlowStatistics() {
55         final KeyedInstanceIdentifier<Node, NodeKey> nodeII = InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(deviceContext.getPrimaryConnectionContext().getNodeId()));
56         final NodeRef nodeRef = new NodeRef(nodeII);
57         final GetAllFlowsStatisticsFromAllFlowTablesInputBuilder builder =
58                 new GetAllFlowsStatisticsFromAllFlowTablesInputBuilder();
59         builder.setNode(nodeRef);
60         //TODO : process data from result
61     }
62
63     @Override
64     public ListenableFuture<Void> gatherDynamicData() {
65
66         final DeviceState devState = deviceContext.getDeviceState();
67
68         ListenableFuture<Boolean> emptyFuture = Futures.<Boolean>immediateFuture(null);
69         final ListenableFuture<Boolean> flowStatistics = devState.isFlowStatisticsAvailable() ? wrapLoggingOnStatisticsRequestCall(MultipartType.OFPMPFLOW) : emptyFuture;
70
71         final ListenableFuture<Boolean> tableStatistics = devState.isTableStatisticsAvailable() ? wrapLoggingOnStatisticsRequestCall(MultipartType.OFPMPTABLE) : emptyFuture;
72
73         final ListenableFuture<Boolean> portStatistics = devState.isPortStatisticsAvailable() ? wrapLoggingOnStatisticsRequestCall(MultipartType.OFPMPPORTSTATS) : emptyFuture;
74
75         final ListenableFuture<Boolean> queueStatistics = devState.isQueueStatisticsAvailable() ? wrapLoggingOnStatisticsRequestCall(MultipartType.OFPMPQUEUE) : emptyFuture;
76
77         final ListenableFuture<Boolean> groupDescStatistics = devState.isGroupAvailable() ? wrapLoggingOnStatisticsRequestCall(MultipartType.OFPMPGROUPDESC) : Futures.<Boolean>immediateFuture(null);
78         final ListenableFuture<Boolean> groupStatistics = devState.isGroupAvailable() ? wrapLoggingOnStatisticsRequestCall(MultipartType.OFPMPGROUP) : Futures.<Boolean>immediateFuture(null);
79
80         final ListenableFuture<Boolean> meterConfigStatistics = devState.isMetersAvailable() ? wrapLoggingOnStatisticsRequestCall(MultipartType.OFPMPMETERCONFIG) : Futures.<Boolean>immediateFuture(null);
81         final ListenableFuture<Boolean> meterStatistics = devState.isMetersAvailable() ? wrapLoggingOnStatisticsRequestCall(MultipartType.OFPMPMETER) : Futures.<Boolean>immediateFuture(null);
82
83
84         final ListenableFuture<List<Boolean>> allFutures = Futures.allAsList(Arrays.asList(flowStatistics, tableStatistics, groupDescStatistics, groupStatistics, meterConfigStatistics, meterStatistics, portStatistics, queueStatistics));
85         final SettableFuture<Void> resultingFuture = SettableFuture.create();
86         Futures.addCallback(allFutures, new FutureCallback<List<Boolean>>() {
87             @Override
88             public void onSuccess(final List<Boolean> booleans) {
89                 resultingFuture.set(null);
90             }
91
92             @Override
93             public void onFailure(final Throwable throwable) {
94                 resultingFuture.setException(throwable);
95             }
96         });
97         return resultingFuture;
98     }
99
100     private ListenableFuture<Boolean> wrapLoggingOnStatisticsRequestCall(final MultipartType type) {
101         final ListenableFuture<Boolean> future = StatisticsGatheringUtils.gatherStatistics(statisticsGatheringService, deviceContext, type);
102         Futures.addCallback(future, new FutureCallback() {
103             @Override
104             public void onSuccess(final Object o) {
105                 LOG.trace("Multipart response for {} was successful.", type);
106             }
107
108             @Override
109             public void onFailure(final Throwable throwable) {
110                 LOG.trace("Multipart response for {} FAILED.", type, throwable);
111             }
112         });
113         return future;
114     }
115
116     @Override
117     public <T> void forgetRequestContext(final RequestContext<T> requestContext) {
118         requestContexts.remove(requestContexts);
119     }
120
121     @Override
122     public <T> SettableFuture<RpcResult<T>> storeOrFail(final RequestContext<T> data) {
123         requestContexts.add(data);
124         return data.getFuture();
125     }
126
127     @Override
128     public <T> RequestContext<T> createRequestContext() {
129         return new RequestContextImpl<>(this);
130     }
131 }