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