alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 1 | package org.onlab.onos.net.statistic; |
| 2 | |
alshabib | 23a8d7c | 2014-10-22 21:53:39 -0700 | [diff] [blame^] | 3 | import com.google.common.base.MoreObjects; |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 4 | import org.onlab.onos.net.flow.FlowRuleProvider; |
| 5 | |
| 6 | /** |
| 7 | * Implementation of a load. |
| 8 | */ |
| 9 | public class DefaultLoad implements Load { |
| 10 | |
| 11 | private final boolean isValid; |
| 12 | private final long current; |
| 13 | private final long previous; |
| 14 | private final long time; |
| 15 | |
| 16 | /** |
| 17 | * Creates an invalid load. |
| 18 | */ |
| 19 | public DefaultLoad() { |
| 20 | this.isValid = false; |
| 21 | this.time = System.currentTimeMillis(); |
| 22 | this.current = -1; |
| 23 | this.previous = -1; |
| 24 | } |
| 25 | |
| 26 | /** |
| 27 | * Creates a load value from the parameters. |
| 28 | * @param current the current value |
| 29 | * @param previous the previous value |
| 30 | */ |
| 31 | public DefaultLoad(long current, long previous) { |
| 32 | this.current = current; |
| 33 | this.previous = previous; |
| 34 | this.time = System.currentTimeMillis(); |
| 35 | this.isValid = true; |
| 36 | } |
| 37 | |
| 38 | @Override |
| 39 | public long rate() { |
| 40 | return (current - previous) / FlowRuleProvider.POLL_INTERVAL; |
| 41 | } |
| 42 | |
| 43 | @Override |
| 44 | public long latest() { |
| 45 | return current; |
| 46 | } |
| 47 | |
| 48 | @Override |
| 49 | public boolean isValid() { |
| 50 | return isValid; |
| 51 | } |
| 52 | |
| 53 | @Override |
| 54 | public long time() { |
| 55 | return time; |
| 56 | } |
alshabib | 23a8d7c | 2014-10-22 21:53:39 -0700 | [diff] [blame^] | 57 | |
| 58 | @Override |
| 59 | public String toString() { |
| 60 | return MoreObjects.toStringHelper("Load").add("rate", rate()) |
| 61 | .add("latest", latest()).toString(); |
| 62 | |
| 63 | } |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 64 | } |