Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Foundation |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.net.statistic; |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 17 | |
alshabib | 23a8d7c | 2014-10-22 21:53:39 -0700 | [diff] [blame] | 18 | import com.google.common.base.MoreObjects; |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 19 | |
Thomas Vachuska | d910a5c | 2015-05-29 17:09:59 -0700 | [diff] [blame] | 20 | import static com.google.common.base.Preconditions.checkArgument; |
| 21 | |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 22 | /** |
| 23 | * Implementation of a load. |
| 24 | */ |
| 25 | public class DefaultLoad implements Load { |
| 26 | |
| 27 | private final boolean isValid; |
| 28 | private final long current; |
| 29 | private final long previous; |
| 30 | private final long time; |
Thomas Vachuska | 204cb6c | 2015-06-04 00:03:06 -0700 | [diff] [blame] | 31 | private final long interval; |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 32 | |
| 33 | /** |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 34 | * Indicates the flow statistics poll interval in seconds. |
| 35 | */ |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 36 | private static long pollInterval = 10; // same as IMMEDIATE and SHORT flow live type |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 37 | |
Thomas Vachuska | f0397b5 | 2015-05-29 13:50:17 -0700 | [diff] [blame] | 38 | /** |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 39 | * Creates an invalid load. |
| 40 | */ |
| 41 | public DefaultLoad() { |
| 42 | this.isValid = false; |
| 43 | this.time = System.currentTimeMillis(); |
| 44 | this.current = -1; |
| 45 | this.previous = -1; |
Thomas Vachuska | f0397b5 | 2015-05-29 13:50:17 -0700 | [diff] [blame] | 46 | this.interval = pollInterval; |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Creates a load value from the parameters. |
Thomas Vachuska | f0397b5 | 2015-05-29 13:50:17 -0700 | [diff] [blame] | 51 | * |
| 52 | * @param current the current value |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 53 | * @param previous the previous value |
| 54 | */ |
| 55 | public DefaultLoad(long current, long previous) { |
Thomas Vachuska | f0397b5 | 2015-05-29 13:50:17 -0700 | [diff] [blame] | 56 | this(current, previous, pollInterval); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Creates a load value from the parameters. |
| 61 | * |
| 62 | * @param current the current value |
| 63 | * @param previous the previous value |
| 64 | * @param interval poll interval for this load |
| 65 | */ |
Thomas Vachuska | 204cb6c | 2015-06-04 00:03:06 -0700 | [diff] [blame] | 66 | public DefaultLoad(long current, long previous, long interval) { |
Thomas Vachuska | d910a5c | 2015-05-29 17:09:59 -0700 | [diff] [blame] | 67 | checkArgument(interval > 0, "Interval must be greater than 0"); |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 68 | this.current = current; |
| 69 | this.previous = previous; |
| 70 | this.time = System.currentTimeMillis(); |
| 71 | this.isValid = true; |
Thomas Vachuska | f0397b5 | 2015-05-29 13:50:17 -0700 | [diff] [blame] | 72 | this.interval = interval; |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 73 | } |
| 74 | |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 75 | /** |
| 76 | * Sets the poll interval in seconds. Used solely for the purpose of |
| 77 | * computing the load. |
| 78 | * |
| 79 | * @param newPollInterval poll interval duration in seconds |
| 80 | */ |
Thomas Vachuska | 204cb6c | 2015-06-04 00:03:06 -0700 | [diff] [blame] | 81 | public static void setPollInterval(long newPollInterval) { |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 82 | pollInterval = newPollInterval; |
| 83 | } |
| 84 | |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 85 | @Override |
| 86 | public long rate() { |
Thomas Vachuska | f0397b5 | 2015-05-29 13:50:17 -0700 | [diff] [blame] | 87 | return (current - previous) / interval; |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | @Override |
| 91 | public long latest() { |
| 92 | return current; |
| 93 | } |
| 94 | |
| 95 | @Override |
| 96 | public boolean isValid() { |
| 97 | return isValid; |
| 98 | } |
| 99 | |
| 100 | @Override |
| 101 | public long time() { |
| 102 | return time; |
| 103 | } |
alshabib | 23a8d7c | 2014-10-22 21:53:39 -0700 | [diff] [blame] | 104 | |
| 105 | @Override |
| 106 | public String toString() { |
| 107 | return MoreObjects.toStringHelper("Load").add("rate", rate()) |
| 108 | .add("latest", latest()).toString(); |
| 109 | |
| 110 | } |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 111 | } |