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

©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.