2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.openflowplugin.impl.statistics;
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;
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.GroupStatisticsUpdated;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.NodeGroupStatistics;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.group.statistics.GroupStatistics;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.group.statistics.GroupStatisticsBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupId;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.desc.stats.reply.GroupDescStats;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.statistics.reply.GroupStats;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.GroupBuilder;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.GroupKey;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.MeterStatisticsUpdated;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.NodeMeterStatistics;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.nodes.node.meter.MeterStatistics;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.nodes.node.meter.MeterStatisticsBuilder;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.meter.statistics.reply.MeterStats;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReply;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.FlowCapableNodeConnectorStatisticsData;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.NodeConnectorStatisticsUpdate;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.flow.capable.node.connector.statistics.FlowCapableNodeConnectorStatistics;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.flow.capable.node.connector.statistics.FlowCapableNodeConnectorStatisticsBuilder;
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.node.connector.statistics.and.port.number.map.NodeConnectorStatisticsAndPortNumberMap;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.FlowCapableNodeConnectorQueueStatisticsData;
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.FlowCapableNodeConnectorQueueStatisticsDataBuilder;
77 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.QueueStatisticsUpdate;
78 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.flow.capable.node.connector.queue.statistics.FlowCapableNodeConnectorQueueStatistics;
79 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.flow.capable.node.connector.queue.statistics.FlowCapableNodeConnectorQueueStatisticsBuilder;
80 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.queue.id.and.statistics.map.QueueIdAndStatisticsMap;
81 import org.opendaylight.yangtools.yang.binding.DataObject;
82 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
83 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
84 import org.opendaylight.yangtools.yang.common.RpcResult;
85 import org.slf4j.Logger;
86 import org.slf4j.LoggerFactory;
89 * Created by Martin Bobak <mbobak@cisco.com> on 2.4.2015.
91 public final class StatisticsGatheringUtils {
93 private static final Logger LOG = LoggerFactory.getLogger(StatisticsGatheringUtils.class);
94 private static final SinglePurposeMultipartReplyTranslator MULTIPART_REPLY_TRANSLATOR = new SinglePurposeMultipartReplyTranslator();
96 private StatisticsGatheringUtils() {
97 throw new IllegalStateException("This class should not be instantiated.");
100 private static KeyedInstanceIdentifier<Node, NodeKey> getInstanceIdentifier(final DeviceContext deviceContext) {
101 return InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(deviceContext.getPrimaryConnectionContext().getNodeId()));
104 public static ListenableFuture<Boolean> gatherStatistics(final StatisticsGatheringService statisticsGatheringService,
105 final DeviceContext deviceContext,
106 final MultipartType type) {
107 //FIXME : anytype listener must not be send as parameter, it has to be extracted from device context inside service
108 final ListenableFuture<RpcResult<List<MultipartReply>>> statisticsDataInFuture =
109 JdkFutureAdapters.listenInPoolThread(statisticsGatheringService.getStatisticsOfType(type));
110 return transformAndStoreStatisticsData(statisticsDataInFuture, deviceContext);
113 private static ListenableFuture<Boolean> transformAndStoreStatisticsData(final ListenableFuture<RpcResult<List<MultipartReply>>> statisticsDataInFuture,
114 final DeviceContext deviceContext) {
115 return Futures.transform(statisticsDataInFuture, new Function<RpcResult<List<MultipartReply>>, Boolean>() {
118 public Boolean apply(final RpcResult<List<MultipartReply>> rpcResult) {
119 if (rpcResult.isSuccessful()) {
120 for (final MultipartReply singleReply : rpcResult.getResult()) {
121 final List<? extends DataObject> multipartDataList = MULTIPART_REPLY_TRANSLATOR.translate(deviceContext, singleReply);
122 for (final DataObject singleMultipartData : multipartDataList) {
123 if (singleMultipartData instanceof GroupStatisticsUpdated) {
124 processGroupStatistics((GroupStatisticsUpdated) singleMultipartData, deviceContext);
127 if (singleMultipartData instanceof MeterStatisticsUpdated) {
128 processMetersStatistics((MeterStatisticsUpdated) singleMultipartData, deviceContext);
130 if (singleMultipartData instanceof NodeConnectorStatisticsUpdate) {
131 processNodeConnectorStatistics((NodeConnectorStatisticsUpdate) singleMultipartData, deviceContext);
133 if (singleMultipartData instanceof FlowTableStatisticsUpdate) {
134 processFlowTableStatistics((FlowTableStatisticsUpdate) singleMultipartData, deviceContext);
136 if (singleMultipartData instanceof QueueStatisticsUpdate) {
137 processQueueStatistics((QueueStatisticsUpdate) singleMultipartData, deviceContext);
139 if (singleMultipartData instanceof FlowsStatisticsUpdate) {
140 processFlowStatistics((FlowsStatisticsUpdate) singleMultipartData, deviceContext);
142 if (singleMultipartData instanceof GroupDescStatsUpdated) {
143 processGroupDescStats((GroupDescStatsUpdated) singleMultipartData, deviceContext);
146 //TODO : implement experimenter
151 return Boolean.FALSE;
156 private static void processFlowStatistics(final FlowsStatisticsUpdate singleMultipartData, final DeviceContext deviceContext) {
157 final FlowsStatisticsUpdate flowsStatistics = singleMultipartData;
158 final InstanceIdentifier<Node> nodeIdent = InstanceIdentifier.create(Nodes.class)
159 .child(Node.class, new NodeKey(flowsStatistics.getId()));
161 if (deviceContext.getDeviceState().deviceSynchronized()) {
162 for (Map.Entry<FlowHash, FlowDescriptor> registryEntry : deviceContext.getDeviceFlowRegistry().getAllFlowDescriptors().entrySet()) {
163 FlowDescriptor flowDescriptor = registryEntry.getValue();
165 FlowId flowId = flowDescriptor.getFlowId();
166 FlowKey flowKey = new FlowKey(flowId);
167 final InstanceIdentifier<Flow> flowInstanceIdentifier = nodeIdent
168 .augmentation(FlowCapableNode.class)
169 .child(Table.class, flowDescriptor.getTableKey())
170 .child(Flow.class, flowKey);
172 LOG.trace("Deleting flow with id {}", flowInstanceIdentifier);
173 deviceContext.addDeleteToTxChain(LogicalDatastoreType.OPERATIONAL, flowInstanceIdentifier);
176 deviceContext.getDeviceFlowRegistry().removeMarked();
177 for (final FlowAndStatisticsMapList flowStat : flowsStatistics.getFlowAndStatisticsMapList()) {
178 final FlowBuilder flowBuilder = new FlowBuilder(flowStat);
179 FlowId flowId = null;
180 FlowHash flowHash = FlowHashFactory.create(flowBuilder.build());
181 short tableId = flowStat.getTableId();
183 FlowDescriptor flowDescriptor = deviceContext.getDeviceFlowRegistry().retrieveIdForFlow(flowHash);
184 flowId = flowDescriptor.getFlowId();
185 } catch (FlowRegistryException e) {
186 LOG.trace("Flow descriptor for flow hash {} wasn't found.", flowHash.hashCode());
187 flowId = FlowUtil.createAlienFlowId(tableId);
188 FlowDescriptor flowDescriptor = FlowDescriptorFactory.create(tableId, flowId);
189 deviceContext.getDeviceFlowRegistry().store(flowHash, flowDescriptor);
191 FlowKey flowKey = new FlowKey(flowId);
192 flowBuilder.setKey(flowKey);
193 final TableKey tableKey = new TableKey(tableId);
194 final InstanceIdentifier<FlowCapableNode> fNodeIdent = getFlowCapableNodeInstanceIdentifier(singleMultipartData.getId());
195 final InstanceIdentifier<Flow> flowIdent = fNodeIdent.child(Table.class, tableKey).child(Flow.class, flowKey);
196 deviceContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, flowIdent, flowBuilder.build());
200 private static void processQueueStatistics(final QueueStatisticsUpdate singleMultipartData, final DeviceContext deviceContext) {
201 final QueueStatisticsUpdate queueStatisticsUpdate = singleMultipartData;
202 final InstanceIdentifier<Node> nodeIdent = InstanceIdentifier.create(Nodes.class)
203 .child(Node.class, new NodeKey(queueStatisticsUpdate.getId()));
204 for (final QueueIdAndStatisticsMap queueStat : queueStatisticsUpdate.getQueueIdAndStatisticsMap()) {
205 if (queueStat.getQueueId() != null) {
206 final FlowCapableNodeConnectorQueueStatistics statChild =
207 new FlowCapableNodeConnectorQueueStatisticsBuilder(queueStat).build();
208 final FlowCapableNodeConnectorQueueStatisticsDataBuilder statBuild =
209 new FlowCapableNodeConnectorQueueStatisticsDataBuilder();
210 statBuild.setFlowCapableNodeConnectorQueueStatistics(statChild);
211 final QueueKey qKey = new QueueKey(queueStat.getQueueId());
212 final InstanceIdentifier<Queue> queueIdent = nodeIdent
213 .child(NodeConnector.class, new NodeConnectorKey(queueStat.getNodeConnectorId()))
214 .augmentation(FlowCapableNodeConnector.class)
215 .child(Queue.class, qKey);
216 final InstanceIdentifier<FlowCapableNodeConnectorQueueStatisticsData> queueStatIdent = queueIdent.augmentation(FlowCapableNodeConnectorQueueStatisticsData.class);
217 deviceContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, queueStatIdent, statBuild.build());
222 private static void processFlowTableStatistics(final FlowTableStatisticsUpdate singleMultipartData, final DeviceContext deviceContext) {
223 final FlowTableStatisticsUpdate flowTableStatisticsUpdate = singleMultipartData;
224 final InstanceIdentifier<FlowCapableNode> fNodeIdent = getFlowCapableNodeInstanceIdentifier(flowTableStatisticsUpdate.getId());
226 for (final FlowTableAndStatisticsMap tableStat : flowTableStatisticsUpdate.getFlowTableAndStatisticsMap()) {
227 final InstanceIdentifier<FlowTableStatistics> tStatIdent = fNodeIdent.child(Table.class, new TableKey(tableStat.getTableId().getValue()))
228 .augmentation(FlowTableStatisticsData.class).child(FlowTableStatistics.class);
229 final FlowTableStatistics stats = new FlowTableStatisticsBuilder(tableStat).build();
230 deviceContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, tStatIdent, stats);
234 private static void processNodeConnectorStatistics(final NodeConnectorStatisticsUpdate singleMultipartData, final DeviceContext deviceContext) {
235 final NodeConnectorStatisticsUpdate nodeConnectorStatisticsUpdate = singleMultipartData;
236 final InstanceIdentifier<Node> nodeIdent = InstanceIdentifier.create(Nodes.class)
237 .child(Node.class, new NodeKey(nodeConnectorStatisticsUpdate.getId()));
238 for (final NodeConnectorStatisticsAndPortNumberMap nConnectPort : nodeConnectorStatisticsUpdate.getNodeConnectorStatisticsAndPortNumberMap()) {
239 final FlowCapableNodeConnectorStatistics stats = new FlowCapableNodeConnectorStatisticsBuilder(nConnectPort).build();
240 final NodeConnectorKey key = new NodeConnectorKey(nConnectPort.getNodeConnectorId());
241 final InstanceIdentifier<NodeConnector> nodeConnectorIdent = nodeIdent.child(NodeConnector.class, key);
242 final InstanceIdentifier<FlowCapableNodeConnectorStatisticsData> nodeConnStatIdent = nodeConnectorIdent
243 .augmentation(FlowCapableNodeConnectorStatisticsData.class);
244 final InstanceIdentifier<FlowCapableNodeConnectorStatistics> flowCapNodeConnStatIdent =
245 nodeConnStatIdent.child(FlowCapableNodeConnectorStatistics.class);
246 deviceContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, flowCapNodeConnStatIdent, stats);
250 private static void processMetersStatistics(final MeterStatisticsUpdated singleMultipartData, final DeviceContext deviceContext) {
251 final MeterStatisticsUpdated meterStatisticsUpdated = singleMultipartData;
252 final InstanceIdentifier<FlowCapableNode> fNodeIdent = getFlowCapableNodeInstanceIdentifier(meterStatisticsUpdated.getId());
254 for (final MeterStats mStat : meterStatisticsUpdated.getMeterStats()) {
255 final MeterStatistics stats = new MeterStatisticsBuilder(mStat).build();
257 final InstanceIdentifier<Meter> meterIdent = fNodeIdent.child(Meter.class, new MeterKey(mStat.getMeterId()));
258 final InstanceIdentifier<NodeMeterStatistics> nodeMeterStatIdent = meterIdent
259 .augmentation(NodeMeterStatistics.class);
260 final InstanceIdentifier<MeterStatistics> msIdent = nodeMeterStatIdent.child(MeterStatistics.class);
261 deviceContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, msIdent, stats);
265 private static void processGroupDescStats(GroupDescStatsUpdated groupDescStatsUpdated, final DeviceContext deviceContext) {
266 NodeId nodeId = groupDescStatsUpdated.getId();
267 final InstanceIdentifier<FlowCapableNode> fNodeIdent = getFlowCapableNodeInstanceIdentifier(nodeId);
268 for (GroupId groupId : deviceContext.getDeviceGroupRegistry().getAllGroupIds()) {
269 final InstanceIdentifier<Group> groupIdent = fNodeIdent.child(Group.class, new GroupKey(groupId));
270 deviceContext.addDeleteToTxChain(LogicalDatastoreType.OPERATIONAL, groupIdent);
272 deviceContext.getDeviceGroupRegistry().removeMarked();
273 for (GroupDescStats groupDescStats : groupDescStatsUpdated.getGroupDescStats()) {
274 final GroupBuilder groupBuilder = new GroupBuilder(groupDescStats);
275 final InstanceIdentifier<Group> groupIdent = fNodeIdent.child(Group.class, new GroupKey(groupDescStats.getGroupId()));
277 deviceContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, groupIdent, groupBuilder.build());
281 private static void processGroupStatistics(final GroupStatisticsUpdated singleMultipartData, final DeviceContext deviceContext) {
282 final GroupStatisticsUpdated groupStatistics = singleMultipartData;
283 NodeId nodeId = groupStatistics.getId();
284 final InstanceIdentifier<FlowCapableNode> fNodeIdent = getFlowCapableNodeInstanceIdentifier(nodeId);
286 for (final GroupStats groupStats : groupStatistics.getGroupStats()) {
288 final InstanceIdentifier<Group> groupIdent = fNodeIdent.child(Group.class, new GroupKey(groupStats.getGroupId()));
289 final InstanceIdentifier<NodeGroupStatistics> nGroupStatIdent = groupIdent
290 .augmentation(NodeGroupStatistics.class);
292 final InstanceIdentifier<GroupStatistics> gsIdent = nGroupStatIdent.child(GroupStatistics.class);
293 final GroupStatistics stats = new GroupStatisticsBuilder(groupStats).build();
294 deviceContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, gsIdent, stats);
298 private static InstanceIdentifier<FlowCapableNode> getFlowCapableNodeInstanceIdentifier(final NodeId nodeId) {
299 final InstanceIdentifier<Node> nodeIdent = InstanceIdentifier
300 .create(Nodes.class).child(Node.class, new NodeKey(nodeId));
301 return nodeIdent.augmentation(FlowCapableNode.class);