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.GroupStatisticsUpdated;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.NodeGroupStatistics;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.group.statistics.GroupStatistics;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.group.statistics.GroupStatisticsBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.statistics.reply.GroupStats;
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.GroupKey;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.MeterStatisticsUpdated;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.NodeMeterStatistics;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.nodes.node.meter.MeterStatistics;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.nodes.node.meter.MeterStatisticsBuilder;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.meter.statistics.reply.MeterStats;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReply;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.FlowCapableNodeConnectorStatisticsData;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.NodeConnectorStatisticsUpdate;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.flow.capable.node.connector.statistics.FlowCapableNodeConnectorStatistics;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.flow.capable.node.connector.statistics.FlowCapableNodeConnectorStatisticsBuilder;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.node.connector.statistics.and.port.number.map.NodeConnectorStatisticsAndPortNumberMap;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.FlowCapableNodeConnectorQueueStatisticsData;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.FlowCapableNodeConnectorQueueStatisticsDataBuilder;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.QueueStatisticsUpdate;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.flow.capable.node.connector.queue.statistics.FlowCapableNodeConnectorQueueStatistics;
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.flow.capable.node.connector.queue.statistics.FlowCapableNodeConnectorQueueStatisticsBuilder;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.queue.id.and.statistics.map.QueueIdAndStatisticsMap;
76 import org.opendaylight.yangtools.yang.binding.DataObject;
77 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
78 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
79 import org.opendaylight.yangtools.yang.common.RpcResult;
80 import org.slf4j.Logger;
81 import org.slf4j.LoggerFactory;
84 * Created by Martin Bobak <mbobak@cisco.com> on 2.4.2015.
86 public final class StatisticsGatheringUtils {
88 private static final Logger LOG = LoggerFactory.getLogger(StatisticsGatheringUtils.class);
89 private static final SinglePurposeMultipartReplyTranslator MULTIPART_REPLY_TRANSLATOR = new SinglePurposeMultipartReplyTranslator();
91 private StatisticsGatheringUtils() {
92 throw new IllegalStateException("This class should not be instantiated.");
95 private static KeyedInstanceIdentifier<Node, NodeKey> getInstanceIdentifier(final DeviceContext deviceContext) {
96 return InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(deviceContext.getPrimaryConnectionContext().getNodeId()));
99 public static ListenableFuture<Boolean> gatherStatistics(final StatisticsGatheringService statisticsGatheringService,
100 final DeviceContext deviceContext,
101 final MultipartType type) {
102 //FIXME : anytype listener must not be send as parameter, it has to be extracted from device context inside service
103 final ListenableFuture<RpcResult<List<MultipartReply>>> statisticsDataInFuture =
104 JdkFutureAdapters.listenInPoolThread(statisticsGatheringService.getStatisticsOfType(type));
105 return transformAndStoreStatisticsData(statisticsDataInFuture, deviceContext);
108 private static ListenableFuture<Boolean> transformAndStoreStatisticsData(final ListenableFuture<RpcResult<List<MultipartReply>>> statisticsDataInFuture,
109 final DeviceContext deviceContext) {
110 return Futures.transform(statisticsDataInFuture, new Function<RpcResult<List<MultipartReply>>, Boolean>() {
113 public Boolean apply(final RpcResult<List<MultipartReply>> rpcResult) {
114 if (rpcResult.isSuccessful()) {
115 final InstanceIdentifier basicIId = getInstanceIdentifier(deviceContext);
116 for (final MultipartReply singleReply : rpcResult.getResult()) {
117 final List<? extends DataObject> multipartDataList = MULTIPART_REPLY_TRANSLATOR.translate(deviceContext, singleReply);
118 for (final DataObject singleMultipartData : multipartDataList) {
119 boolean logFirstTime = true;
120 if (singleMultipartData instanceof GroupStatisticsUpdated) {
121 final GroupStatisticsUpdated groupStatistics = (GroupStatisticsUpdated) singleMultipartData;
122 final InstanceIdentifier<Node> nodeIdent = InstanceIdentifier
123 .create(Nodes.class).child(Node.class, new NodeKey(groupStatistics.getId()));
124 final InstanceIdentifier<FlowCapableNode> fNodeIdent = nodeIdent.augmentation(FlowCapableNode.class);
126 for (final GroupStats groupStats : groupStatistics.getGroupStats()) {
127 final InstanceIdentifier<Group> groupIdent = fNodeIdent.child(Group.class, new GroupKey(groupStats.getGroupId()));
128 final InstanceIdentifier<NodeGroupStatistics> nGroupStatIdent = groupIdent
129 .augmentation(NodeGroupStatistics.class);
130 final InstanceIdentifier<GroupStatistics> gsIdent = nGroupStatIdent.child(GroupStatistics.class);
131 final GroupStatistics stats = new GroupStatisticsBuilder(groupStats).build();
132 deviceContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, gsIdent, stats);
136 if (singleMultipartData instanceof MeterStatisticsUpdated) {
137 final 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);
142 for (final MeterStats mStat : meterStatisticsUpdated.getMeterStats()) {
143 final MeterStatistics stats = new MeterStatisticsBuilder(mStat).build();
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 deviceContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, msIdent, stats);
152 if (singleMultipartData instanceof NodeConnectorStatisticsUpdate) {
153 final NodeConnectorStatisticsUpdate nodeConnectorStatisticsUpdate = (NodeConnectorStatisticsUpdate) singleMultipartData;
154 final InstanceIdentifier<Node> nodeIdent = InstanceIdentifier.create(Nodes.class)
155 .child(Node.class, new NodeKey(nodeConnectorStatisticsUpdate.getId()));
156 for (final NodeConnectorStatisticsAndPortNumberMap nConnectPort : nodeConnectorStatisticsUpdate.getNodeConnectorStatisticsAndPortNumberMap()) {
157 final FlowCapableNodeConnectorStatistics stats = new FlowCapableNodeConnectorStatisticsBuilder(nConnectPort).build();
158 final NodeConnectorKey key = new NodeConnectorKey(nConnectPort.getNodeConnectorId());
159 final InstanceIdentifier<NodeConnector> nodeConnectorIdent = nodeIdent.child(NodeConnector.class, key);
160 final InstanceIdentifier<FlowCapableNodeConnectorStatisticsData> nodeConnStatIdent = nodeConnectorIdent
161 .augmentation(FlowCapableNodeConnectorStatisticsData.class);
162 final InstanceIdentifier<FlowCapableNodeConnectorStatistics> flowCapNodeConnStatIdent =
163 nodeConnStatIdent.child(FlowCapableNodeConnectorStatistics.class);
164 deviceContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, flowCapNodeConnStatIdent, stats);
167 if (singleMultipartData instanceof FlowTableStatisticsUpdate) {
169 final FlowTableStatisticsUpdate flowTableStatisticsUpdate = (FlowTableStatisticsUpdate) singleMultipartData;
170 final InstanceIdentifier<FlowCapableNode> fNodeIdent = InstanceIdentifier.create(Nodes.class)
171 .child(Node.class, new NodeKey(flowTableStatisticsUpdate.getId())).augmentation(FlowCapableNode.class);
173 for (final FlowTableAndStatisticsMap tableStat : flowTableStatisticsUpdate.getFlowTableAndStatisticsMap()) {
174 final InstanceIdentifier<FlowTableStatistics> tStatIdent = fNodeIdent.child(Table.class, new TableKey(tableStat.getTableId().getValue()))
175 .augmentation(FlowTableStatisticsData.class).child(FlowTableStatistics.class);
176 final FlowTableStatistics stats = new FlowTableStatisticsBuilder(tableStat).build();
177 deviceContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, tStatIdent, stats);
180 if (singleMultipartData instanceof QueueStatisticsUpdate) {
181 final QueueStatisticsUpdate queueStatisticsUpdate = (QueueStatisticsUpdate) singleMultipartData;
182 final InstanceIdentifier<Node> nodeIdent = InstanceIdentifier.create(Nodes.class)
183 .child(Node.class, new NodeKey(queueStatisticsUpdate.getId()));
184 for (final QueueIdAndStatisticsMap queueStat : queueStatisticsUpdate.getQueueIdAndStatisticsMap()) {
185 if (queueStat.getQueueId() != null) {
186 final FlowCapableNodeConnectorQueueStatistics statChild =
187 new FlowCapableNodeConnectorQueueStatisticsBuilder(queueStat).build();
188 final FlowCapableNodeConnectorQueueStatisticsDataBuilder statBuild =
189 new FlowCapableNodeConnectorQueueStatisticsDataBuilder();
190 statBuild.setFlowCapableNodeConnectorQueueStatistics(statChild);
191 final QueueKey qKey = new QueueKey(queueStat.getQueueId());
192 final InstanceIdentifier<Queue> queueIdent = nodeIdent
193 .child(NodeConnector.class, new NodeConnectorKey(queueStat.getNodeConnectorId()))
194 .augmentation(FlowCapableNodeConnector.class)
195 .child(Queue.class, qKey);
196 final InstanceIdentifier<FlowCapableNodeConnectorQueueStatisticsData> queueStatIdent = queueIdent.augmentation(FlowCapableNodeConnectorQueueStatisticsData.class);
197 deviceContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, queueStatIdent, statBuild.build());
201 if (singleMultipartData instanceof FlowsStatisticsUpdate) {
202 final FlowsStatisticsUpdate flowsStatistics = (FlowsStatisticsUpdate) singleMultipartData;
203 final InstanceIdentifier<Node> nodeIdent = InstanceIdentifier.create(Nodes.class)
204 .child(Node.class, new NodeKey(flowsStatistics.getId()));
206 if (deviceContext.getDeviceState().deviceSynchronized()) {
207 for (Map.Entry<FlowHash, FlowDescriptor> registryEntry : deviceContext.getDeviceFlowRegistry().getAllFlowDescriptors().entrySet()) {
208 FlowDescriptor flowDescriptor = registryEntry.getValue();
210 FlowId flowId = flowDescriptor.getFlowId();
211 FlowKey flowKey = new FlowKey(flowId);
212 final InstanceIdentifier<Flow> flowInstanceIdentifier = nodeIdent
213 .augmentation(FlowCapableNode.class)
214 .child(Table.class, flowDescriptor.getTableKey())
215 .child(Flow.class, flowKey);
217 LOG.trace("Deleting flow with id {}", flowInstanceIdentifier);
218 deviceContext.addDeleteToTxChain(LogicalDatastoreType.OPERATIONAL, flowInstanceIdentifier);
221 deviceContext.getDeviceFlowRegistry().removeMarked();
222 for (final FlowAndStatisticsMapList flowStat : flowsStatistics.getFlowAndStatisticsMapList()) {
223 final FlowBuilder flowBuilder = new FlowBuilder(flowStat);
224 FlowId flowId = null;
225 FlowHash flowHash = FlowHashFactory.create(flowBuilder.build());
226 short tableId = flowStat.getTableId();
228 FlowDescriptor flowDescriptor = deviceContext.getDeviceFlowRegistry().retrieveIdForFlow(flowHash);
229 flowId = flowDescriptor.getFlowId();
230 } catch (FlowRegistryException e) {
231 LOG.trace("Flow descriptor for flow hash {} wasn't found.", flowHash.hashCode());
232 flowId = FlowUtil.createAlienFlowId(tableId);
233 FlowDescriptor flowDescriptor = FlowDescriptorFactory.create(tableId, flowId);
234 deviceContext.getDeviceFlowRegistry().store(flowHash, flowDescriptor);
236 FlowKey flowKey = new FlowKey(flowId);
237 flowBuilder.setKey(flowKey);
238 final TableKey tableKey = new TableKey(tableId);
239 final InstanceIdentifier<FlowCapableNode> fNodeIdent = nodeIdent.augmentation(FlowCapableNode.class);
240 final InstanceIdentifier<Flow> flowIdent = fNodeIdent.child(Table.class, tableKey).child(Flow.class, flowKey);
241 deviceContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, flowIdent, flowBuilder.build());
245 //TODO : implement experimenter
250 return Boolean.FALSE;