830ff783108d6fce921470c5070d8e7d21165c7a
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / statistics / StatisticsGatheringUtils.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.base.Function;
12 import com.google.common.util.concurrent.Futures;
13 import com.google.common.util.concurrent.JdkFutureAdapters;
14 import com.google.common.util.concurrent.ListenableFuture;
15 import java.util.List;
16 import javax.annotation.Nullable;
17 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
18 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
19 import org.opendaylight.openflowplugin.api.openflow.flow.registry.FlowHash;
20 import org.opendaylight.openflowplugin.api.openflow.flow.registry.FlowRegistryException;
21 import org.opendaylight.openflowplugin.impl.flow.registry.FlowHashFactory;
22 import org.opendaylight.openflowplugin.impl.statistics.services.dedicated.StatisticsGatheringService;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnector;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.Meter;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.MeterKey;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowsStatisticsUpdate;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.flow.and.statistics.map.list.FlowAndStatisticsMapList;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.FlowTableStatisticsData;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.FlowTableStatisticsUpdate;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.flow.table.and.statistics.map.FlowTableAndStatisticsMap;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.flow.table.statistics.FlowTableStatistics;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.flow.table.statistics.FlowTableStatisticsBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.queues.Queue;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.queues.QueueKey;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GroupDescStatsUpdated;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.NodeGroupDescStats;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.NodeGroupDescStatsBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.group.desc.GroupDescBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.desc.stats.reply.GroupDescStats;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.GroupBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.GroupKey;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.MeterStatisticsUpdated;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.NodeMeterStatistics;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.nodes.node.meter.MeterStatistics;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.nodes.node.meter.MeterStatisticsBuilder;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.meter.statistics.reply.MeterStats;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReply;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.FlowCapableNodeConnectorStatisticsData;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.NodeConnectorStatisticsUpdate;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.flow.capable.node.connector.statistics.FlowCapableNodeConnectorStatistics;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.flow.capable.node.connector.statistics.FlowCapableNodeConnectorStatisticsBuilder;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.node.connector.statistics.and.port.number.map.NodeConnectorStatisticsAndPortNumberMap;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.FlowCapableNodeConnectorQueueStatisticsData;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.FlowCapableNodeConnectorQueueStatisticsDataBuilder;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.QueueStatisticsUpdate;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.flow.capable.node.connector.queue.statistics.FlowCapableNodeConnectorQueueStatistics;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.flow.capable.node.connector.queue.statistics.FlowCapableNodeConnectorQueueStatisticsBuilder;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.queue.id.and.statistics.map.QueueIdAndStatisticsMap;
73 import org.opendaylight.yangtools.yang.binding.DataObject;
74 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
75 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
76 import org.opendaylight.yangtools.yang.common.RpcResult;
77 import org.slf4j.Logger;
78 import org.slf4j.LoggerFactory;
79
80 /**
81  * Created by Martin Bobak <mbobak@cisco.com> on 2.4.2015.
82  */
83 public final class StatisticsGatheringUtils {
84
85     private static final Logger LOG = LoggerFactory.getLogger(StatisticsGatheringUtils.class);
86     private static final SinglePurposeMultipartReplyTranslator MULTIPART_REPLY_TRANSLATOR = new SinglePurposeMultipartReplyTranslator();
87
88     private StatisticsGatheringUtils() {
89         throw new IllegalStateException("This class should not be instantiated.");
90     }
91
92     private static KeyedInstanceIdentifier<Node, NodeKey> getInstanceIdentifier(final DeviceContext deviceContext) {
93         return InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(deviceContext.getPrimaryConnectionContext().getNodeId()));
94     }
95
96     public static ListenableFuture<Boolean> gatherStatistics(final StatisticsGatheringService statisticsGatheringService,
97                                                              final DeviceContext deviceContext,
98                                                              MultipartType type) {
99         //FIXME : anytype listener must not be send as parameter, it has to be extracted from device context inside service
100         ListenableFuture<RpcResult<List<MultipartReply>>> statisticsDataInFuture =
101                 JdkFutureAdapters.listenInPoolThread(statisticsGatheringService.getStatisticsOfType(type));
102         return transformAndStoreStatisticsData(statisticsDataInFuture, deviceContext);
103     }
104
105     private static ListenableFuture<Boolean> transformAndStoreStatisticsData(final ListenableFuture<RpcResult<List<MultipartReply>>> statisticsDataInFuture, final DeviceContext deviceContext) {
106         return Futures.transform(statisticsDataInFuture, new Function<RpcResult<List<MultipartReply>>, Boolean>() {
107             @Nullable
108             @Override
109             public Boolean apply(final RpcResult<List<MultipartReply>> rpcResult) {
110                 if (rpcResult.isSuccessful()) {
111                     InstanceIdentifier basicIId = getInstanceIdentifier(deviceContext);
112                     for (MultipartReply singleReply : rpcResult.getResult()) {
113                         List<? extends DataObject> multipartDataList = MULTIPART_REPLY_TRANSLATOR.translate(deviceContext, singleReply);
114                         for (DataObject singleMultipartData : multipartDataList) {
115                             boolean logFirstTime = true;
116                             if (singleMultipartData instanceof GroupDescStatsUpdated) {
117                                 GroupDescStatsUpdated groupDescStatsUpdated = (GroupDescStatsUpdated) singleMultipartData;
118                                 for (GroupDescStats groupDescStats : groupDescStatsUpdated.getGroupDescStats()) {
119                                     final InstanceIdentifier<Node> nodeIdent = InstanceIdentifier
120                                             .create(Nodes.class).child(Node.class, new NodeKey(groupDescStatsUpdated.getId()));
121                                     final InstanceIdentifier<FlowCapableNode> fNodeIdent = nodeIdent.augmentation(FlowCapableNode.class);
122                                     final GroupKey groupKey = new GroupKey(groupDescStats.getGroupId());
123                                     final InstanceIdentifier<Group> groupRef = fNodeIdent.child(Group.class, groupKey);
124                                     final GroupBuilder groupBuilder = new GroupBuilder(groupDescStats);
125                                     final NodeGroupDescStatsBuilder groupDesc = new NodeGroupDescStatsBuilder();
126                                     groupDesc.setGroupDesc(new GroupDescBuilder(groupDescStats).build());
127                                     groupBuilder.addAugmentation(NodeGroupDescStats.class, groupDesc.build());
128                                     if (logFirstTime) {
129                                         logFirstTime = false;
130                                         LOG.info("Writing group statistics to operational DS.");
131                                     }
132                                     deviceContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, groupRef, groupBuilder.build());
133                                 }
134                             }
135
136                             if (singleMultipartData instanceof MeterStatisticsUpdated) {
137                                 MeterStatisticsUpdated meterStatisticsUpdated = (MeterStatisticsUpdated) singleMultipartData;
138                                 final InstanceIdentifier<Node> nodeIdent = InstanceIdentifier
139                                         .create(Nodes.class).child(Node.class, new NodeKey(meterStatisticsUpdated.getId()));
140                                 final InstanceIdentifier<FlowCapableNode> fNodeIdent = nodeIdent.augmentation(FlowCapableNode.class);
141
142                                 for (final MeterStats mStat : meterStatisticsUpdated.getMeterStats()) {
143                                     final MeterStatistics stats = new MeterStatisticsBuilder(mStat).build();
144
145                                     final InstanceIdentifier<Meter> meterIdent = fNodeIdent.child(Meter.class, new MeterKey(mStat.getMeterId()));
146                                     final InstanceIdentifier<NodeMeterStatistics> nodeMeterStatIdent = meterIdent
147                                             .augmentation(NodeMeterStatistics.class);
148                                     final InstanceIdentifier<MeterStatistics> msIdent = nodeMeterStatIdent.child(MeterStatistics.class);
149                                     if (logFirstTime) {
150                                         logFirstTime = false;
151                                         LOG.info("Writing meter statistics to operational DS.");
152                                     }
153                                     deviceContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, msIdent, stats);
154                                 }
155                             }
156                             if (singleMultipartData instanceof NodeConnectorStatisticsUpdate) {
157                                 NodeConnectorStatisticsUpdate nodeConnectorStatisticsUpdate = (NodeConnectorStatisticsUpdate) singleMultipartData;
158                                 final InstanceIdentifier<Node> nodeIdent = InstanceIdentifier.create(Nodes.class)
159                                         .child(Node.class, new NodeKey(nodeConnectorStatisticsUpdate.getId()));
160                                 for (final NodeConnectorStatisticsAndPortNumberMap nConnectPort : nodeConnectorStatisticsUpdate.getNodeConnectorStatisticsAndPortNumberMap()) {
161                                     final FlowCapableNodeConnectorStatistics stats = new FlowCapableNodeConnectorStatisticsBuilder(nConnectPort).build();
162                                     final NodeConnectorKey key = new NodeConnectorKey(nConnectPort.getNodeConnectorId());
163                                     final InstanceIdentifier<NodeConnector> nodeConnectorIdent = nodeIdent.child(NodeConnector.class, key);
164                                     final InstanceIdentifier<FlowCapableNodeConnectorStatisticsData> nodeConnStatIdent = nodeConnectorIdent
165                                             .augmentation(FlowCapableNodeConnectorStatisticsData.class);
166                                     final InstanceIdentifier<FlowCapableNodeConnectorStatistics> flowCapNodeConnStatIdent =
167                                             nodeConnStatIdent.child(FlowCapableNodeConnectorStatistics.class);
168                                     if (logFirstTime) {
169                                         logFirstTime = false;
170                                         LOG.info("Writing port statistics to operational DS.");
171                                     }
172                                     deviceContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, flowCapNodeConnStatIdent, stats);
173                                 }
174                             }
175                             if (singleMultipartData instanceof FlowTableStatisticsUpdate) {
176
177                                 FlowTableStatisticsUpdate flowTableStatisticsUpdate = (FlowTableStatisticsUpdate) singleMultipartData;
178                                 final InstanceIdentifier<Node> nodeIdent = InstanceIdentifier.create(Nodes.class)
179                                         .child(Node.class, new NodeKey(flowTableStatisticsUpdate.getId()));
180                                 final InstanceIdentifier<FlowCapableNode> fNodeIdent = nodeIdent.augmentation(FlowCapableNode.class);
181
182                                 for (final FlowTableAndStatisticsMap tableStat : flowTableStatisticsUpdate.getFlowTableAndStatisticsMap()) {
183                                     final InstanceIdentifier<Table> tableIdent = fNodeIdent
184                                             .child(Table.class, new TableKey(tableStat.getTableId().getValue()));
185                                     final InstanceIdentifier<FlowTableStatisticsData> tableStatIdent = tableIdent.augmentation(FlowTableStatisticsData.class);
186                                     final FlowTableStatistics stats = new FlowTableStatisticsBuilder(tableStat).build();
187                                     final InstanceIdentifier<FlowTableStatistics> tStatIdent = tableStatIdent.child(FlowTableStatistics.class);
188                                     if (logFirstTime) {
189                                         logFirstTime = false;
190                                         LOG.info("Writing table statistics to operational DS.");
191                                     }
192                                     deviceContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, tStatIdent, stats);
193                                 }
194                             }
195                             if (singleMultipartData instanceof QueueStatisticsUpdate) {
196                                 QueueStatisticsUpdate queueStatisticsUpdate = (QueueStatisticsUpdate) singleMultipartData;
197                                 final InstanceIdentifier<Node> nodeIdent = InstanceIdentifier.create(Nodes.class)
198                                         .child(Node.class, new NodeKey(queueStatisticsUpdate.getId()));
199                                 for (final QueueIdAndStatisticsMap queueStat : queueStatisticsUpdate.getQueueIdAndStatisticsMap()) {
200                                     if (queueStat.getQueueId() != null) {
201                                         final FlowCapableNodeConnectorQueueStatistics statChild =
202                                                 new FlowCapableNodeConnectorQueueStatisticsBuilder(queueStat).build();
203                                         final FlowCapableNodeConnectorQueueStatisticsDataBuilder statBuild =
204                                                 new FlowCapableNodeConnectorQueueStatisticsDataBuilder();
205                                         statBuild.setFlowCapableNodeConnectorQueueStatistics(statChild);
206                                         final QueueKey qKey = new QueueKey(queueStat.getQueueId());
207                                         final InstanceIdentifier<Queue> queueIdent = nodeIdent
208                                                 .child(NodeConnector.class, new NodeConnectorKey(queueStat.getNodeConnectorId()))
209                                                 .augmentation(FlowCapableNodeConnector.class)
210                                                 .child(Queue.class, qKey);
211                                         final InstanceIdentifier<FlowCapableNodeConnectorQueueStatisticsData> queueStatIdent = queueIdent.augmentation(FlowCapableNodeConnectorQueueStatisticsData.class);
212                                         if (logFirstTime) {
213                                             logFirstTime = false;
214                                             LOG.info("Writing queue statistics to operational DS.");
215                                         }
216                                         deviceContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, queueStatIdent, statBuild.build());
217                                     }
218                                 }
219                             }
220                             if (singleMultipartData instanceof FlowsStatisticsUpdate) {
221                                 FlowsStatisticsUpdate flowsStatistics = (FlowsStatisticsUpdate) singleMultipartData;
222                                 final InstanceIdentifier<Node> nodeIdent = InstanceIdentifier.create(Nodes.class)
223                                         .child(Node.class, new NodeKey(flowsStatistics.getId()));
224
225                                 for (final FlowAndStatisticsMapList flowStat : flowsStatistics.getFlowAndStatisticsMapList()) {
226                                     final FlowBuilder flowBuilder = new FlowBuilder(flowStat);
227                                     FlowId flowId = null;
228                                     try {
229                                         FlowHash flowHash = FlowHashFactory.create(flowBuilder.build());
230                                         flowId = deviceContext.getFlowRegistry().retrieveIdForFlow(flowHash);
231                                     } catch (FlowRegistryException e) {
232                                         LOG.trace("No flowId found in device's flow registry for flow retrieved by statistics.");
233                                         //TODO : create alien ID for flow
234                                     }
235                                     FlowKey flowKey = new FlowKey(flowId);
236                                     flowBuilder.setKey(flowKey);
237                                     TableKey tableKey = new TableKey(flowStat.getTableId());
238                                     final InstanceIdentifier<FlowCapableNode> fNodeIdent = nodeIdent.augmentation(FlowCapableNode.class);
239                                     final InstanceIdentifier<Flow> flowIdent = fNodeIdent.child(Table.class, tableKey).child(Flow.class, flowKey);
240                                     if (logFirstTime) {
241                                         logFirstTime = false;
242                                         LOG.info("Writing flow statistics to operational DS.");
243                                     }
244                                     deviceContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, flowIdent, flowBuilder.build());
245                                 }
246                             }
247
248                             //TODO : implement experimenter
249                         }
250                     }
251                     return Boolean.TRUE;
252                 }
253                 return Boolean.FALSE;
254             }
255         });
256     }
257 }