2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.openflowplugin.impl.statistics;
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;
37 * Created by Martin Bobak <mbobak@cisco.com> on 1.4.2015.
39 public class StatisticsContextImpl implements StatisticsContext {
41 private static final Logger LOG = LoggerFactory.getLogger(StatisticsContextImpl.class);
42 private final List<RequestContext> requestContexts = new ArrayList();
43 private final DeviceContext deviceContext;
46 private final StatisticsGatheringService statisticsGatheringService;
48 public StatisticsContextImpl(final DeviceContext deviceContext) {
49 this.deviceContext = deviceContext;
50 statisticsGatheringService = new StatisticsGatheringService(this, deviceContext);
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
64 public ListenableFuture<Void> gatherDynamicData() {
66 final DeviceState devState = deviceContext.getDeviceState();
68 ListenableFuture<Boolean> emptyFuture = Futures.<Boolean>immediateFuture(null);
69 final ListenableFuture<Boolean> flowStatistics = devState.isFlowStatisticsAvailable() ? wrapLoggingOnStatisticsRequestCall(MultipartType.OFPMPFLOW) : emptyFuture;
71 final ListenableFuture<Boolean> tableStatistics = devState.isTableStatisticsAvailable() ? wrapLoggingOnStatisticsRequestCall(MultipartType.OFPMPTABLE) : emptyFuture;
73 final ListenableFuture<Boolean> portStatistics = devState.isPortStatisticsAvailable() ? wrapLoggingOnStatisticsRequestCall(MultipartType.OFPMPPORTSTATS) : emptyFuture;
75 final ListenableFuture<Boolean> queueStatistics = devState.isQueueStatisticsAvailable() ? wrapLoggingOnStatisticsRequestCall(MultipartType.OFPMPQUEUE) : emptyFuture;
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);
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);
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>>() {
88 public void onSuccess(final List<Boolean> booleans) {
89 resultingFuture.set(null);
93 public void onFailure(final Throwable throwable) {
94 resultingFuture.setException(throwable);
97 return resultingFuture;
100 private ListenableFuture<Boolean> wrapLoggingOnStatisticsRequestCall(final MultipartType type) {
101 final ListenableFuture<Boolean> future = StatisticsGatheringUtils.gatherStatistics(statisticsGatheringService, deviceContext, type);
102 Futures.addCallback(future, new FutureCallback() {
104 public void onSuccess(final Object o) {
105 LOG.trace("Multipart response for {} was successful.", type);
109 public void onFailure(final Throwable throwable) {
110 LOG.trace("Multipart response for {} FAILED.", type, throwable);
117 public <T> void forgetRequestContext(final RequestContext<T> requestContext) {
118 requestContexts.remove(requestContexts);
122 public <T> SettableFuture<RpcResult<T>> storeOrFail(final RequestContext<T> data) {
123 requestContexts.add(data);
124 return data.getFuture();
128 public <T> RequestContext<T> createRequestContext() {
129 return new RequestContextImpl<>(this);