statistics processing writes/removes meter data to/from operational DS
[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         final ListenableFuture<Boolean> flowStatistics = wrapLoggingOnStatisticsRequestCall(MultipartType.OFPMPFLOW);
69
70         final ListenableFuture<Boolean> tableStatistics = wrapLoggingOnStatisticsRequestCall(MultipartType.OFPMPTABLE);
71
72         final ListenableFuture<Boolean> portStatistics = wrapLoggingOnStatisticsRequestCall(MultipartType.OFPMPPORTSTATS);
73
74         final ListenableFuture<Boolean> queueStatistics = wrapLoggingOnStatisticsRequestCall(MultipartType.OFPMPQUEUE);
75
76         final ListenableFuture<Boolean> groupDescStatistics = devState.isGroupAvailable() ? wrapLoggingOnStatisticsRequestCall(MultipartType.OFPMPGROUPDESC) : Futures.<Boolean>immediateFuture(null);
77         final ListenableFuture<Boolean> groupStatistics = devState.isGroupAvailable() ? wrapLoggingOnStatisticsRequestCall(MultipartType.OFPMPGROUP) : Futures.<Boolean>immediateFuture(null);
78
79         final ListenableFuture<Boolean> meterConfigStatistics = devState.isMetersAvailable() ? wrapLoggingOnStatisticsRequestCall(MultipartType.OFPMPMETERCONFIG) : Futures.<Boolean>immediateFuture(null);
80         final ListenableFuture<Boolean> meterStatistics = devState.isMetersAvailable() ? wrapLoggingOnStatisticsRequestCall(MultipartType.OFPMPMETER) : Futures.<Boolean>immediateFuture(null);
81
82
83         final ListenableFuture<List<Boolean>> allFutures = Futures.allAsList(Arrays.asList(flowStatistics, tableStatistics, groupDescStatistics, groupStatistics, meterConfigStatistics, meterStatistics, portStatistics, queueStatistics));
84         final SettableFuture<Void> resultingFuture = SettableFuture.create();
85         Futures.addCallback(allFutures, new FutureCallback<List<Boolean>>() {
86             @Override
87             public void onSuccess(final List<Boolean> booleans) {
88                 resultingFuture.set(null);
89             }
90
91             @Override
92             public void onFailure(final Throwable throwable) {
93                 resultingFuture.setException(throwable);
94             }
95         });
96         return resultingFuture;
97     }
98
99     private ListenableFuture<Boolean> wrapLoggingOnStatisticsRequestCall(final MultipartType type) {
100         final ListenableFuture<Boolean> future = StatisticsGatheringUtils.gatherStatistics(statisticsGatheringService, deviceContext, type);
101         Futures.addCallback(future, new FutureCallback() {
102             @Override
103             public void onSuccess(final Object o) {
104                 LOG.trace("Multipart response for {} was successful.", type);
105             }
106
107             @Override
108             public void onFailure(final Throwable throwable) {
109                 LOG.trace("Multipart response for {} FAILED.", type, throwable);
110             }
111         });
112         return future;
113     }
114
115     @Override
116     public <T> void forgetRequestContext(final RequestContext<T> requestContext) {
117         requestContexts.remove(requestContexts);
118     }
119
120     @Override
121     public <T> SettableFuture<RpcResult<T>> storeOrFail(final RequestContext<T> data) {
122         requestContexts.add(data);
123         return data.getFuture();
124     }
125
126     @Override
127     public <T> RequestContext<T> createRequestContext() {
128         return new RequestContextImpl<>(this);
129     }
130 }