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