Fix star import and enable checkstyle rule to prevent it.
[controller.git] / opendaylight / md-sal / sal-compability / src / main / java / org / opendaylight / controller / sal / compability / adsal / FlowStatisticsAdapter.java
1 package org.opendaylight.controller.sal.compability.adsal;
2
3 import java.math.BigInteger;
4 import java.util.ArrayList;
5 import java.util.List;
6 import java.util.concurrent.Future;
7
8 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
9 import org.opendaylight.controller.sal.common.util.Futures;
10 import org.opendaylight.controller.sal.common.util.Rpcs;
11 import org.opendaylight.controller.sal.compability.NodeMapping;
12 import org.opendaylight.controller.sal.compability.ToSalConversionsUtils;
13 import org.opendaylight.controller.sal.core.ConstructionException;
14 import org.opendaylight.controller.sal.core.Node;
15 import org.opendaylight.controller.sal.flowprogrammer.Flow;
16 import org.opendaylight.controller.sal.reader.FlowOnNode;
17 import org.opendaylight.controller.sal.reader.IReadService;
18 import org.opendaylight.controller.sal.reader.IReadServiceListener;
19 import org.opendaylight.controller.sal.reader.NodeConnectorStatistics;
20 import org.opendaylight.controller.sal.reader.NodeDescription;
21 import org.opendaylight.controller.sal.reader.NodeTableStatistics;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.Counter64;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowStatisticsUpdatedBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAllFlowStatisticsInput;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAllFlowStatisticsOutput;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAllFlowStatisticsOutputBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAllNodeConnectorStatisticsInput;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAllNodeConnectorStatisticsOutput;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAllNodeConnectorStatisticsOutputBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetFlowStatisticsInput;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetFlowStatisticsOutput;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetFlowStatisticsOutputBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetFlowTableStatisticsInput;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetFlowTableStatisticsOutput;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetFlowTableStatisticsOutputBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetNodeConnectorStatisticsInput;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetNodeConnectorStatisticsOutput;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetNodeConnectorStatisticsOutputBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.NodeConnectorStatisticsUpdatedBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.OpendaylightFlowStatisticsService;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.get.all.flow.statistics.output.FlowStatistics;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.get.all.flow.statistics.output.FlowStatisticsBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.get.all.node.connector.statistics.output.NodeConnectorStatisticsBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.statistics.Duration;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.statistics.DurationBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRef;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.statistics.types.rev130925.node.connector.statistics.Bytes;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.statistics.types.rev130925.node.connector.statistics.BytesBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.statistics.types.rev130925.node.connector.statistics.Packets;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.statistics.types.rev130925.node.connector.statistics.PacketsBuilder;
51 import org.opendaylight.yangtools.yang.common.RpcResult;
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
54
55 public class FlowStatisticsAdapter implements OpendaylightFlowStatisticsService, IReadServiceListener {
56
57     private static final Logger LOG = LoggerFactory.getLogger(FlowStatisticsAdapter.class);
58     private IReadService readDelegate;
59     private NotificationProviderService notifier;
60
61     @Override
62     public Future<RpcResult<GetAllFlowStatisticsOutput>> getAllFlowStatistics(GetAllFlowStatisticsInput input) {
63         GetAllFlowStatisticsOutput rpcResultType = null;
64         boolean rpcResultBool = false;
65
66         try {
67             Node adNode = NodeMapping.toADNode(input.getNode());
68             List<FlowOnNode> flowsOnNode = readDelegate.readAllFlows(adNode);
69             List<FlowStatistics> flowsStatistics = toOdFlowsStatistics(flowsOnNode);
70             GetAllFlowStatisticsOutputBuilder builder = new GetAllFlowStatisticsOutputBuilder();
71             rpcResultType = builder.setFlowStatistics(flowsStatistics).build();
72             rpcResultBool = true;
73         } catch (ConstructionException e) {
74             LOG.error(e.getMessage());
75         }
76
77         return Futures.immediateFuture(Rpcs.getRpcResult(rpcResultBool, rpcResultType, null));
78     }
79
80     @Override
81     public Future<RpcResult<GetAllNodeConnectorStatisticsOutput>> getAllNodeConnectorStatistics(
82             GetAllNodeConnectorStatisticsInput input) {
83         GetAllNodeConnectorStatisticsOutput rpcResultType = null;
84         boolean rpcResultBool = false;
85
86         try {
87             Node adNode = NodeMapping.toADNode(input.getNode());
88             List<NodeConnectorStatistics> nodesConnectorStatistics = readDelegate.readNodeConnectors(adNode);
89             List<org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.get.all.node.connector.statistics.output.NodeConnectorStatistics> odNodesConnectorStatistics;
90             odNodesConnectorStatistics = toOdNodesConnectorStatistics(nodesConnectorStatistics);
91             GetAllNodeConnectorStatisticsOutputBuilder builder = new GetAllNodeConnectorStatisticsOutputBuilder();
92             rpcResultType = builder.setNodeConnectorStatistics(odNodesConnectorStatistics).build();
93             rpcResultBool = true;
94         } catch (ConstructionException e) {
95             LOG.error(e.getMessage());
96         }
97
98         return Futures.immediateFuture(Rpcs.getRpcResult(rpcResultBool, rpcResultType, null));
99     }
100
101     @Override
102     public Future<RpcResult<GetFlowStatisticsOutput>> getFlowStatistics(GetFlowStatisticsInput input) {
103         GetFlowStatisticsOutput rpcResultType = null;
104         boolean rpcResultBool = false;
105
106         try {
107             Node node = NodeMapping.toADNode(input.getNode());
108             Flow flow = ToSalConversionsUtils.toFlow(input);
109             FlowOnNode readFlow = readDelegate.readFlow(node, flow);
110             FlowStatistics flowOnNodeToFlowStatistics = toOdFlowStatistics(readFlow);
111             rpcResultType = new GetFlowStatisticsOutputBuilder(flowOnNodeToFlowStatistics).build();
112             rpcResultBool = true;
113         } catch (ConstructionException e) {
114             LOG.error(e.getMessage());
115         }
116
117         return Futures.immediateFuture(Rpcs.getRpcResult(rpcResultBool, rpcResultType, null));
118     }
119
120     @Override
121     public Future<RpcResult<GetFlowTableStatisticsOutput>> getFlowTableStatistics(GetFlowTableStatisticsInput input) {
122         GetFlowTableStatisticsOutput rpcResultType = null;
123         boolean rpcResultBool = false;
124
125         try {
126             Node node = NodeMapping.toADNode(input.getNode());
127             List<NodeTableStatistics> nodesTable = readDelegate.readNodeTable(node);
128             NodeTableStatistics nodeTable = null;
129             if (!nodesTable.isEmpty()) {
130                 nodeTable = nodesTable.get(0);
131                 rpcResultType = toOdTableStatistics(nodeTable);
132                 rpcResultBool = true;
133             }
134         } catch (ConstructionException e) {
135             LOG.error(e.getMessage());
136         }
137
138         return Futures.immediateFuture(Rpcs.getRpcResult(rpcResultBool, rpcResultType, null));
139     }
140
141     @Override
142     public Future<RpcResult<GetNodeConnectorStatisticsOutput>> getNodeConnectorStatistics(
143             GetNodeConnectorStatisticsInput input) {
144         GetNodeConnectorStatisticsOutput rpcResultType = null;
145         boolean rpcResultBool = false;
146
147         NodeConnectorRef nodeConnector = input.getNodeConnector();
148         try {
149             NodeConnectorStatistics nodeConnectorStats = readDelegate.readNodeConnector(NodeMapping
150                     .toADNodeConnector(nodeConnector));
151             org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.get.all.node.connector.statistics.output.NodeConnectorStatistics odNodeConnectorStatistics = toOdNodeConnectorStatistics(nodeConnectorStats);
152             rpcResultType = new GetNodeConnectorStatisticsOutputBuilder(odNodeConnectorStatistics).build();
153             rpcResultBool = true;
154         } catch (ConstructionException e) {
155             LOG.error(e.getMessage());
156         }
157
158         return Futures.immediateFuture(Rpcs.getRpcResult(rpcResultBool, rpcResultType, null));
159     }
160
161     @Override
162     public void descriptionStatisticsUpdated(Node node, NodeDescription nodeDescription) {
163
164         // TODO which *StatisticsUpdated interface should be used?
165
166     }
167
168     @Override
169     public void nodeConnectorStatisticsUpdated(Node node, List<NodeConnectorStatistics> ncStatsList) {
170         for (NodeConnectorStatistics ndConStats : ncStatsList) {
171             org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.get.all.node.connector.statistics.output.NodeConnectorStatistics odNodeConnectorStatistics;
172             odNodeConnectorStatistics = toOdNodeConnectorStatistics(ndConStats);
173             NodeConnectorStatisticsUpdatedBuilder statisticsBuilder = new NodeConnectorStatisticsUpdatedBuilder(
174                     odNodeConnectorStatistics);
175             notifier.publish(statisticsBuilder.build());
176         }
177     }
178
179     @Override
180     public void nodeFlowStatisticsUpdated(Node node, List<FlowOnNode> flowStatsList) {
181         for (FlowOnNode flowOnNode : flowStatsList) {
182             FlowStatistics flowStatistics = toOdFlowStatistics(flowOnNode);
183             FlowStatisticsUpdatedBuilder statisticsBuilder = new FlowStatisticsUpdatedBuilder(flowStatistics);
184             notifier.publish(statisticsBuilder.build());
185         }
186     }
187
188     @Override
189     public void nodeTableStatisticsUpdated(Node node, List<NodeTableStatistics> tableStatsList) {
190         // TODO : Not implemented by AD-SAL.
191     }
192
193     private List<FlowStatistics> toOdFlowsStatistics(List<FlowOnNode> flowsOnNode) {
194         List<FlowStatistics> flowsStatistics = new ArrayList<>();
195         for (FlowOnNode flowOnNode : flowsOnNode) {
196             flowsStatistics.add(toOdFlowStatistics(flowOnNode));
197         }
198         return flowsStatistics;
199     }
200
201     private FlowStatistics toOdFlowStatistics(FlowOnNode flowOnNode) {
202         FlowStatisticsBuilder builder = new FlowStatisticsBuilder();
203
204         builder.setByteCount(toCounter64(flowOnNode.getByteCount()));
205         builder.setPacketCount(toCounter64(flowOnNode.getPacketCount()));
206         builder.setDuration(extractDuration(flowOnNode));
207
208         return builder.build();
209     }
210
211     private Duration extractDuration(FlowOnNode flowOnNode) {
212         DurationBuilder builder = new DurationBuilder();
213         builder.setNanosecond(toCounter64(flowOnNode.getDurationNanoseconds()));
214         builder.setSecond(toCounter64(flowOnNode.getDurationSeconds()));
215         return builder.build();
216     }
217
218     private Counter64 toCounter64(long num) {
219         String byteCountStr = String.valueOf(num);
220         BigInteger byteCountBigInt = new BigInteger(byteCountStr);
221         return new Counter64(byteCountBigInt);
222     }
223
224     private Counter64 toCounter64(int num) {
225         String byteCountStr = String.valueOf(num);
226         BigInteger byteCountBigInt = new BigInteger(byteCountStr);
227         return new Counter64(byteCountBigInt);
228     }
229
230     private List<org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.get.all.node.connector.statistics.output.NodeConnectorStatistics> toOdNodesConnectorStatistics(
231             List<NodeConnectorStatistics> nodesConnectorStatistics) {
232         List<org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.get.all.node.connector.statistics.output.NodeConnectorStatistics> odNodesConnectorStatistics = new ArrayList<>();
233         for (NodeConnectorStatistics nodeConnectorStatistics : nodesConnectorStatistics) {
234             odNodesConnectorStatistics.add(toOdNodeConnectorStatistics(nodeConnectorStatistics));
235         }
236         return odNodesConnectorStatistics;
237     }
238
239     private org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.get.all.node.connector.statistics.output.NodeConnectorStatistics toOdNodeConnectorStatistics(
240             NodeConnectorStatistics ndConStats) {
241         NodeConnectorStatisticsBuilder builder = new NodeConnectorStatisticsBuilder();
242
243         builder.setBytes(extractBytes(ndConStats));
244         builder.setCollisionCount(toBI(ndConStats.getCollisionCount()));
245         builder.setDuration(null);
246         builder.setPackets(extractPackets(ndConStats));
247         builder.setReceiveCrcError(toBI(ndConStats.getReceiveCRCErrorCount()));
248         builder.setReceiveDrops(toBI(ndConStats.getReceiveDropCount()));
249         builder.setReceiveErrors(toBI(ndConStats.getReceiveErrorCount()));
250         builder.setReceiveFrameError(toBI(ndConStats.getReceiveFrameErrorCount()));
251         builder.setReceiveOverRunError(toBI(ndConStats.getReceiveOverRunErrorCount()));
252         builder.setTransmitDrops(toBI(ndConStats.getTransmitDropCount()));
253         builder.setTransmitErrors(toBI(ndConStats.getTransmitErrorCount()));
254
255         return builder.build();
256     }
257
258     private BigInteger toBI(long num) {
259         String numStr = String.valueOf(num);
260         return new BigInteger(numStr);
261     }
262
263     private Packets extractPackets(NodeConnectorStatistics nodeConnectorStatistics) {
264         long receivePacketCount = nodeConnectorStatistics.getReceivePacketCount();
265         long transmitPacketCount = nodeConnectorStatistics.getTransmitPacketCount();
266
267         PacketsBuilder builder = new PacketsBuilder();
268         builder.setReceived(toBI(receivePacketCount));
269         builder.setTransmitted(toBI(transmitPacketCount));
270
271         return builder.build();
272     }
273
274     private Bytes extractBytes(NodeConnectorStatistics nodeConnectorStatistics) {
275         long transmitByteCount = nodeConnectorStatistics.getTransmitByteCount();
276         long receiveByteCount = nodeConnectorStatistics.getReceiveByteCount();
277
278         BytesBuilder builder = new BytesBuilder();
279         builder.setReceived(toBI(receiveByteCount));
280         builder.setTransmitted(toBI(transmitByteCount));
281
282         return builder.build();
283     }
284
285     private GetFlowTableStatisticsOutput toOdTableStatistics(NodeTableStatistics nodeTable) {
286         GetFlowTableStatisticsOutputBuilder builder = new GetFlowTableStatisticsOutputBuilder();
287
288         builder.setActive(toCounter64(nodeTable.getActiveCount()));
289         builder.setLookup(toCounter64(nodeTable.getLookupCount()));
290         builder.setMatched(toCounter64(nodeTable.getMatchedCount()));
291
292         return builder.build();
293     }
294
295 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.