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