Merge "update nagasena dependencies for IT"
[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.annotations.VisibleForTesting;
12 import com.google.common.base.Optional;
13 import com.google.common.base.Preconditions;
14 import com.google.common.collect.ImmutableList;
15 import com.google.common.util.concurrent.FutureCallback;
16 import com.google.common.util.concurrent.Futures;
17 import com.google.common.util.concurrent.ListenableFuture;
18 import com.google.common.util.concurrent.SettableFuture;
19 import io.netty.util.Timeout;
20 import java.util.ArrayList;
21 import java.util.Collection;
22 import java.util.HashSet;
23 import java.util.Iterator;
24 import java.util.List;
25 import javax.annotation.CheckForNull;
26 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
27 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
28 import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
29 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
30 import org.opendaylight.openflowplugin.api.openflow.rpc.listener.ItemLifecycleListener;
31 import org.opendaylight.openflowplugin.api.openflow.statistics.StatisticsContext;
32 import org.opendaylight.openflowplugin.impl.rpc.AbstractRequestContext;
33 import org.opendaylight.openflowplugin.impl.rpc.listener.ItemLifecycleListenerImpl;
34 import org.opendaylight.openflowplugin.impl.services.RequestContextUtil;
35 import org.opendaylight.openflowplugin.impl.statistics.services.dedicated.StatisticsGatheringOnTheFlyService;
36 import org.opendaylight.openflowplugin.impl.statistics.services.dedicated.StatisticsGatheringService;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40
41 /**
42  * Created by Martin Bobak <mbobak@cisco.com> on 1.4.2015.
43  */
44 public class StatisticsContextImpl implements StatisticsContext {
45
46     private static final Logger LOG = LoggerFactory.getLogger(StatisticsContextImpl.class);
47     private static final String CONNECTION_CLOSED = "Connection closed.";
48
49     private final ItemLifecycleListener itemLifeCycleListener;
50     private final Collection<RequestContext<?>> requestContexts = new HashSet<>();
51     private final DeviceContext deviceContext;
52     private final DeviceState devState;
53     private final ListenableFuture<Boolean> emptyFuture;
54     private final List<MultipartType> collectingStatType;
55
56     private StatisticsGatheringService statisticsGatheringService;
57     private StatisticsGatheringOnTheFlyService statisticsGatheringOnTheFlyService;
58     private Timeout pollTimeout;
59
60     public StatisticsContextImpl(@CheckForNull final DeviceContext deviceContext) {
61         this.deviceContext = Preconditions.checkNotNull(deviceContext);
62         devState = Preconditions.checkNotNull(deviceContext.getDeviceState());
63         emptyFuture = Futures.immediateFuture(new Boolean(false));
64         statisticsGatheringService = new StatisticsGatheringService(this, deviceContext);
65         statisticsGatheringOnTheFlyService = new StatisticsGatheringOnTheFlyService(this, deviceContext);
66
67         final List<MultipartType> statListForCollecting = new ArrayList<>();
68         if (devState.isTableStatisticsAvailable()) {
69             statListForCollecting.add(MultipartType.OFPMPTABLE);
70         }
71         if (devState.isFlowStatisticsAvailable()) {
72             statListForCollecting.add(MultipartType.OFPMPFLOW);
73         }
74         if (devState.isGroupAvailable()) {
75             statListForCollecting.add(MultipartType.OFPMPGROUPDESC);
76             statListForCollecting.add(MultipartType.OFPMPGROUP);
77         }
78         if (devState.isMetersAvailable()) {
79             statListForCollecting.add(MultipartType.OFPMPMETERCONFIG);
80             statListForCollecting.add(MultipartType.OFPMPMETER);
81         }
82         if (devState.isPortStatisticsAvailable()) {
83             statListForCollecting.add(MultipartType.OFPMPPORTSTATS);
84         }
85         if (devState.isQueueStatisticsAvailable()) {
86             statListForCollecting.add(MultipartType.OFPMPQUEUE);
87         }
88         collectingStatType = ImmutableList.<MultipartType>copyOf(statListForCollecting);
89         itemLifeCycleListener = new ItemLifecycleListenerImpl(deviceContext);
90     }
91
92     @Override
93     public ListenableFuture<Boolean> gatherDynamicData() {
94         final ListenableFuture<Boolean> errorResultFuture = deviceConnectionCheck();
95         if (errorResultFuture != null) {
96             return errorResultFuture;
97         }
98         final Iterator<MultipartType> statIterator = collectingStatType.iterator();
99         final SettableFuture<Boolean> settableStatResultFuture = SettableFuture.create();
100         statChainFuture(statIterator, settableStatResultFuture);
101         return settableStatResultFuture;
102     }
103
104     private ListenableFuture<Boolean> chooseStat(final MultipartType multipartType) {
105         switch (multipartType) {
106             case OFPMPFLOW:
107                 return collectFlowStatistics(multipartType);
108             case OFPMPTABLE:
109                 return collectTableStatistics(multipartType);
110             case OFPMPPORTSTATS:
111                 return collectPortStatistics(multipartType);
112             case OFPMPQUEUE:
113                 return collectQueueStatistics(multipartType);
114             case OFPMPGROUPDESC:
115                 return collectGroupDescStatistics(multipartType);
116             case OFPMPGROUP:
117                 return collectGroupStatistics(multipartType);
118             case OFPMPMETERCONFIG:
119                 return collectMeterConfigStatistics(multipartType);
120             case OFPMPMETER:
121                 return collectMeterStatistics(multipartType);
122             default:
123                 LOG.warn("Unsuported Statistics type {}", multipartType);
124                 return Futures.immediateCheckedFuture(Boolean.TRUE);
125         }
126     }
127
128     @Override
129     public <T> RequestContext<T> createRequestContext() {
130         final AbstractRequestContext<T> ret = new AbstractRequestContext<T>(deviceContext.getReservedXid()) {
131             @Override
132             public void close() {
133                 requestContexts.remove(this);
134             }
135         };
136         requestContexts.add(ret);
137         return ret;
138     }
139
140     @Override
141     public void close() {
142         for (final RequestContext<?> requestContext : requestContexts) {
143             RequestContextUtil.closeRequestContextWithRpcError(requestContext, CONNECTION_CLOSED);
144         }
145         if (null != pollTimeout && !pollTimeout.isExpired()) {
146             pollTimeout.cancel();
147         }
148     }
149
150     @Override
151     public void setPollTimeout(Timeout pollTimeout) {
152         this.pollTimeout = pollTimeout;
153     }
154
155     @Override
156     public Optional<Timeout> getPollTimeout() {
157         return Optional.fromNullable(pollTimeout);
158     }
159
160     void statChainFuture(final Iterator<MultipartType> iterator, final SettableFuture<Boolean> resultFuture) {
161         if ( ! iterator.hasNext()) {
162             resultFuture.set(Boolean.TRUE);
163             return;
164         }
165         final ListenableFuture<Boolean> deviceStatisticsCollectionFuture = chooseStat(iterator.next());
166         Futures.addCallback(deviceStatisticsCollectionFuture, new FutureCallback<Boolean>() {
167             @Override
168             public void onSuccess(final Boolean result) {
169                 statChainFuture(iterator, resultFuture);
170             }
171             @Override
172             public void onFailure(final Throwable t) {
173                 resultFuture.setException(t);
174             }
175         });
176     }
177
178     /**
179      * Method checks a device state. It returns null for be able continue. Otherwise it returns immediateFuture
180      * which has to be returned from caller too
181      *
182      * @return
183      */
184     @VisibleForTesting
185     ListenableFuture<Boolean> deviceConnectionCheck() {
186         if (!ConnectionContext.CONNECTION_STATE.WORKING.equals(deviceContext.getPrimaryConnectionContext().getConnectionState())) {
187             ListenableFuture<Boolean> resultingFuture = SettableFuture.create();
188             switch (deviceContext.getPrimaryConnectionContext().getConnectionState()) {
189                 case RIP:
190                     final String errMsg = String.format("Device connection doesn't exist anymore. Primary connection status : %s",
191                             deviceContext.getPrimaryConnectionContext().getConnectionState());
192                     resultingFuture = Futures.immediateFailedFuture(new Throwable(errMsg));
193                     break;
194                 default:
195                     resultingFuture = Futures.immediateCheckedFuture(Boolean.TRUE);
196                     break;
197             }
198             return resultingFuture;
199         }
200         return null;
201     }
202
203     private ListenableFuture<Boolean> collectFlowStatistics(final MultipartType multipartType) {
204         return devState.isFlowStatisticsAvailable() ? StatisticsGatheringUtils.gatherStatistics(
205                 statisticsGatheringOnTheFlyService, deviceContext, /*MultipartType.OFPMPFLOW*/ multipartType) : emptyFuture;
206     }
207
208     private ListenableFuture<Boolean> collectTableStatistics(final MultipartType multipartType) {
209         return devState.isTableStatisticsAvailable() ? StatisticsGatheringUtils.gatherStatistics(
210                 statisticsGatheringService, deviceContext, /*MultipartType.OFPMPTABLE*/ multipartType) : emptyFuture;
211     }
212
213     private ListenableFuture<Boolean> collectPortStatistics(final MultipartType multipartType) {
214         return devState.isPortStatisticsAvailable() ? StatisticsGatheringUtils.gatherStatistics(
215                 statisticsGatheringService, deviceContext, /*MultipartType.OFPMPPORTSTATS*/ multipartType) : emptyFuture;
216     }
217
218     private ListenableFuture<Boolean> collectQueueStatistics(final MultipartType multipartType) {
219         return devState.isQueueStatisticsAvailable() ? StatisticsGatheringUtils.gatherStatistics(
220                 statisticsGatheringService, deviceContext, /*MultipartType.OFPMPQUEUE*/ multipartType) : emptyFuture;
221     }
222
223     private ListenableFuture<Boolean> collectGroupDescStatistics(final MultipartType multipartType) {
224         return devState.isGroupAvailable() ? StatisticsGatheringUtils.gatherStatistics(
225                 statisticsGatheringService, deviceContext, /*MultipartType.OFPMPGROUPDESC*/ multipartType) : emptyFuture;
226     }
227
228     private ListenableFuture<Boolean> collectGroupStatistics(final MultipartType multipartType) {
229         return devState.isGroupAvailable() ? StatisticsGatheringUtils.gatherStatistics(
230                 statisticsGatheringService, deviceContext, /*MultipartType.OFPMPGROUP*/ multipartType) : emptyFuture;
231     }
232
233     private ListenableFuture<Boolean> collectMeterConfigStatistics(final MultipartType multipartType) {
234         return devState.isMetersAvailable() ? StatisticsGatheringUtils.gatherStatistics(
235                 statisticsGatheringService, deviceContext, /*MultipartType.OFPMPMETERCONFIG*/ multipartType) : emptyFuture;
236     }
237
238     private ListenableFuture<Boolean> collectMeterStatistics(final MultipartType multipartType) {
239         return devState.isMetersAvailable() ? StatisticsGatheringUtils.gatherStatistics(
240                 statisticsGatheringService, deviceContext, /*MultipartType.OFPMPMETER*/ multipartType) : emptyFuture;
241     }
242
243     @VisibleForTesting
244     protected void setStatisticsGatheringService(StatisticsGatheringService statisticsGatheringService) {
245         this.statisticsGatheringService = statisticsGatheringService;
246     }
247
248     @VisibleForTesting
249     protected void setStatisticsGatheringOnTheFlyService(StatisticsGatheringOnTheFlyService
250                                                              statisticsGatheringOnTheFlyService) {
251         this.statisticsGatheringOnTheFlyService = statisticsGatheringOnTheFlyService;
252     }
253
254     @Override
255     public ItemLifecycleListener getItemLifeCycleListener() {
256         return itemLifeCycleListener;
257     }
258 }