98fb803bd8305b237de3ac49307e4f852fa11537
[controller.git] / opendaylight / protocol_plugins / openflow / src / main / java / org / opendaylight / controller / protocol_plugin / openflow / core / internal / StatisticsCollector.java
1
2 /*
3  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
4  *
5  * This program and the accompanying materials are made available under the
6  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
7  * and is available at http://www.eclipse.org/legal/epl-v10.html
8  */
9
10 package org.opendaylight.controller.protocol_plugin.openflow.core.internal;
11
12 import java.util.List;
13 import java.util.concurrent.Callable;
14 import java.util.concurrent.CopyOnWriteArrayList;
15 import java.util.concurrent.CountDownLatch;
16
17 import org.opendaylight.controller.protocol_plugin.openflow.core.ISwitch;
18 import org.openflow.protocol.OFError;
19 import org.openflow.protocol.OFStatisticsReply;
20 import org.openflow.protocol.OFStatisticsRequest;
21 import org.openflow.protocol.statistics.OFStatistics;
22
23 public class StatisticsCollector implements Callable<Object> {
24
25     private ISwitch sw;
26     private Integer xid;
27     private OFStatisticsRequest request;
28     private CountDownLatch latch;
29     private Object result;
30     private List<OFStatistics> stats;
31
32     public StatisticsCollector(ISwitch sw, int xid, OFStatisticsRequest request) {
33         this.sw = sw;
34         this.xid = xid;
35         this.request = request;
36         latch = new CountDownLatch(1);
37         result = new Object();
38         stats = new CopyOnWriteArrayList<OFStatistics>();
39     }
40
41     /*
42      * accumulate the stats records in result
43      * Returns: true: if this is the last record
44      *                 false: more to come
45      */
46     public boolean collect(OFStatisticsReply reply) {
47         synchronized (result) {
48             stats.addAll(reply.getStatistics());
49             if ((reply.getFlags() & 0x01) == 0) {
50                 // all stats are collected, done
51                 result = stats;
52                 return true;
53             } else {
54                 // still waiting for more to come
55                 return false;
56             }
57         }
58     }
59
60     @Override
61     public Object call() throws Exception {
62         sw.asyncSend(request, xid);
63         // free up the request as it is no longer needed
64         request = null;
65         // wait until all stats replies are received or timeout
66         latch.await();
67         return result;
68     }
69
70     public Integer getXid() {
71         return this.xid;
72     }
73
74     public void wakeup() {
75         this.latch.countDown();
76     }
77
78     public void wakeup(OFError errorMsg) {
79
80     }
81 }