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