bd0f614ac645e966ca0e1684834c3d2d1d750a9c
[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.FlowCapableNodeConnector;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.Meter;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.MeterKey;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowsStatisticsUpdate;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.FlowTableStatisticsData;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.flow.table.and.statistics.map.FlowTableAndStatisticsMap;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.flow.table.statistics.FlowTableStatistics;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.flow.table.statistics.FlowTableStatisticsBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.queues.Queue;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.queues.QueueKey;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.NodeGroupStatistics;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.group.statistics.GroupStatistics;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.group.statistics.GroupStatisticsBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.statistics.reply.GroupStats;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.GroupKey;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.NodeMeterStatistics;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.nodes.node.meter.MeterStatistics;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.nodes.node.meter.MeterStatisticsBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.meter.statistics.reply.MeterStats;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReply;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReplyMessage;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.FlowCapableNodeConnectorStatisticsData;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.flow.capable.node.connector.statistics.FlowCapableNodeConnectorStatistics;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.flow.capable.node.connector.statistics.FlowCapableNodeConnectorStatisticsBuilder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.node.connector.statistics.and.port.number.map.NodeConnectorStatisticsAndPortNumberMap;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.FlowCapableNodeConnectorQueueStatisticsData;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.FlowCapableNodeConnectorQueueStatisticsDataBuilder;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.flow.capable.node.connector.queue.statistics.FlowCapableNodeConnectorQueueStatistics;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.flow.capable.node.connector.queue.statistics.FlowCapableNodeConnectorQueueStatisticsBuilder;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.queue.id.and.statistics.map.QueueIdAndStatisticsMap;
59 import org.opendaylight.yangtools.yang.binding.DataObject;
60 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
61 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
62 import org.opendaylight.yangtools.yang.common.RpcResult;
63
64 /**
65  * Created by Martin Bobak <mbobak@cisco.com> on 2.4.2015.
66  */
67 public final class StatisticsGatheringUtils {
68
69     private static final SinglePurposeMultipartReplyTranslator MULTIPART_REPLY_TRANSLATOR = new SinglePurposeMultipartReplyTranslator();
70
71     private StatisticsGatheringUtils() {
72         throw new IllegalStateException("This class should not be instantiated.");
73     }
74
75     private static KeyedInstanceIdentifier<Node, NodeKey> getInstanceIdentifier(final DeviceContext deviceContext) {
76         return InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(deviceContext.getPrimaryConnectionContext().getNodeId()));
77     }
78
79     public static ListenableFuture<Boolean> gatherStatistics(final StatisticsGatheringService statisticsGatheringService,
80                                                              final DeviceContext deviceContext,
81                                                              MultipartType type) {
82         ListenableFuture<RpcResult<List<MultipartReply>>> statisticsDataInFuture =
83                 JdkFutureAdapters.listenInPoolThread(statisticsGatheringService.getStatisticsOfType(type, deviceContext.getAnyMessageTypeListener()));
84         return transformAndStoreStatisticsData(statisticsDataInFuture, deviceContext);
85     }
86
87     private static ListenableFuture<Boolean> transformAndStoreStatisticsData(final ListenableFuture<RpcResult<List<MultipartReply>>> statisticsDataInFuture, final DeviceContext deviceContext) {
88         return Futures.transform(statisticsDataInFuture, new Function<RpcResult<List<MultipartReply>>, Boolean>() {
89             @Nullable
90             @Override
91             public Boolean apply(final RpcResult<List<MultipartReply>> rpcResult) {
92                 if (rpcResult.isSuccessful()) {
93                     InstanceIdentifier basicIId = getInstanceIdentifier(deviceContext);
94                     for (MultipartReply singleReply : rpcResult.getResult()) {
95                         List<? extends DataObject> multipartDataList = MULTIPART_REPLY_TRANSLATOR.translate(deviceContext, singleReply);
96                         for (DataObject singleMultipartData : multipartDataList) {
97                             MultipartReplyMessage mpReply = (MultipartReplyMessage) singleMultipartData;
98                             switch (mpReply.getType()) {
99                                 case OFPMPGROUPDESC:
100                                     final GroupStatistics groupStatistics = new GroupStatisticsBuilder((GroupStats) singleMultipartData).build();
101                                     final InstanceIdentifier<Group> groupIdent = basicIId.child(Group.class, new GroupKey(groupStatistics.getGroupId()));
102                                     final InstanceIdentifier<NodeGroupStatistics> nGroupStatIdent = groupIdent
103                                             .augmentation(NodeGroupStatistics.class);
104                                     final InstanceIdentifier<GroupStatistics> gsIdent = nGroupStatIdent.child(GroupStatistics.class);
105                                     deviceContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, gsIdent, groupStatistics);
106                                     break;
107                                 case OFPMPMETERCONFIG:
108                                     final MeterStatistics stats = new MeterStatisticsBuilder((MeterStats) singleMultipartData).build();
109                                     final InstanceIdentifier<Meter> meterIdent = basicIId.child(Meter.class, new MeterKey(stats.getMeterId()));
110                                     final InstanceIdentifier<NodeMeterStatistics> nodeMeterStatIdent = meterIdent
111                                             .augmentation(NodeMeterStatistics.class);
112                                     final InstanceIdentifier<MeterStatistics> msIdent = nodeMeterStatIdent.child(MeterStatistics.class);
113                                     deviceContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, msIdent, stats);
114                                     break;
115                                 case OFPMPPORTDESC:
116                                     NodeConnectorStatisticsAndPortNumberMap nodeConnectorStatisticsAndPortNumberMap = (NodeConnectorStatisticsAndPortNumberMap) singleMultipartData;
117                                     final FlowCapableNodeConnectorStatistics nodeConnectorStatistics = new FlowCapableNodeConnectorStatisticsBuilder(nodeConnectorStatisticsAndPortNumberMap).build();
118                                     final NodeConnectorKey key = new NodeConnectorKey(nodeConnectorStatisticsAndPortNumberMap.getNodeConnectorId());
119                                     final InstanceIdentifier<NodeConnector> nodeConnectorIdent = basicIId.child(NodeConnector.class, key);
120                                     final InstanceIdentifier<FlowCapableNodeConnectorStatisticsData> nodeConnStatIdent = nodeConnectorIdent
121                                             .augmentation(FlowCapableNodeConnectorStatisticsData.class);
122                                     final InstanceIdentifier<FlowCapableNodeConnectorStatistics> flowCapNodeConnStatIdent =
123                                             nodeConnStatIdent.child(FlowCapableNodeConnectorStatistics.class);
124                                     deviceContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, flowCapNodeConnStatIdent, nodeConnectorStatistics);
125                                     break;
126                                 case OFPMPTABLE:
127                                     FlowTableAndStatisticsMap flowTableAndStatisticsMap = (FlowTableAndStatisticsMap) singleMultipartData;
128                                     final InstanceIdentifier<Table> tableIdent = basicIId
129                                             .child(Table.class, new TableKey(flowTableAndStatisticsMap.getTableId().getValue()));
130                                     final FlowTableStatistics flowTableStatistics = new FlowTableStatisticsBuilder(flowTableAndStatisticsMap).build();
131                                     final InstanceIdentifier<FlowTableStatisticsData> tableStatIdent = tableIdent
132                                             .augmentation(FlowTableStatisticsData.class);
133
134                                     final InstanceIdentifier<FlowTableStatistics> tStatIdent = tableStatIdent.child(FlowTableStatistics.class);
135                                     deviceContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, tStatIdent, flowTableStatistics);
136                                     break;
137                                 case OFPMPQUEUE:
138                                     QueueIdAndStatisticsMap queueIdAndStatisticsMap = (QueueIdAndStatisticsMap) singleMultipartData;
139                                     final FlowCapableNodeConnectorQueueStatistics statChild =
140                                             new FlowCapableNodeConnectorQueueStatisticsBuilder(queueIdAndStatisticsMap).build();
141                                     final FlowCapableNodeConnectorQueueStatisticsDataBuilder statBuild =
142                                             new FlowCapableNodeConnectorQueueStatisticsDataBuilder();
143                                     statBuild.setFlowCapableNodeConnectorQueueStatistics(statChild);
144                                     final QueueKey qKey = new QueueKey(queueIdAndStatisticsMap.getQueueId());
145                                     final InstanceIdentifier<Queue> queueIdent = basicIId
146                                             .child(NodeConnector.class, new NodeConnectorKey(queueIdAndStatisticsMap.getNodeConnectorId()))
147                                             .augmentation(FlowCapableNodeConnector.class)
148                                             .child(Queue.class, qKey);
149                                     final InstanceIdentifier<FlowCapableNodeConnectorQueueStatisticsData> queueStatIdent = queueIdent.augmentation(FlowCapableNodeConnectorQueueStatisticsData.class);
150                                     deviceContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, queueStatIdent, statBuild.build());
151                                     break;
152                                 case OFPMPFLOW:
153                                     FlowsStatisticsUpdate flowsStatisticsUpdate = (FlowsStatisticsUpdate) singleMultipartData;
154                                     deviceContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, basicIId, flowsStatisticsUpdate);
155                                     break;
156                                 case OFPMPEXPERIMENTER:
157                                     //TODO : implement this
158                                     break;
159
160                             }
161                         }
162                     }
163                     return Boolean.TRUE;
164                 }
165                 return Boolean.FALSE;
166             }
167         });
168     }
169 }