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