Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 Open Networking Laboratory |
| 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 | |
| 20 | /** |
| 21 | * Implementation of a load. |
| 22 | */ |
| 23 | public class DefaultLoad implements Load { |
| 24 | |
| 25 | private final boolean isValid; |
| 26 | private final long current; |
| 27 | private final long previous; |
| 28 | private final long time; |
Thomas Vachuska | f0397b5 | 2015-05-29 13:50:17 -0700 | [diff] [blame^] | 29 | private final int interval; |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 30 | |
| 31 | /** |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 32 | * Indicates the flow statistics poll interval in seconds. |
| 33 | */ |
| 34 | private static int pollInterval = 10; |
| 35 | |
Thomas Vachuska | f0397b5 | 2015-05-29 13:50:17 -0700 | [diff] [blame^] | 36 | /** |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 37 | * Creates an invalid load. |
| 38 | */ |
| 39 | public DefaultLoad() { |
| 40 | this.isValid = false; |
| 41 | this.time = System.currentTimeMillis(); |
| 42 | this.current = -1; |
| 43 | this.previous = -1; |
Thomas Vachuska | f0397b5 | 2015-05-29 13:50:17 -0700 | [diff] [blame^] | 44 | this.interval = pollInterval; |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Creates a load value from the parameters. |
Thomas Vachuska | f0397b5 | 2015-05-29 13:50:17 -0700 | [diff] [blame^] | 49 | * |
| 50 | * @param current the current value |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 51 | * @param previous the previous value |
| 52 | */ |
| 53 | public DefaultLoad(long current, long previous) { |
Thomas Vachuska | f0397b5 | 2015-05-29 13:50:17 -0700 | [diff] [blame^] | 54 | this(current, previous, pollInterval); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Creates a load value from the parameters. |
| 59 | * |
| 60 | * @param current the current value |
| 61 | * @param previous the previous value |
| 62 | * @param interval poll interval for this load |
| 63 | */ |
| 64 | public DefaultLoad(long current, long previous, int interval) { |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 65 | this.current = current; |
| 66 | this.previous = previous; |
| 67 | this.time = System.currentTimeMillis(); |
| 68 | this.isValid = true; |
Thomas Vachuska | f0397b5 | 2015-05-29 13:50:17 -0700 | [diff] [blame^] | 69 | this.interval = interval; |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 70 | } |
| 71 | |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 72 | /** |
| 73 | * Sets the poll interval in seconds. Used solely for the purpose of |
| 74 | * computing the load. |
| 75 | * |
| 76 | * @param newPollInterval poll interval duration in seconds |
| 77 | */ |
| 78 | public static void setPollInterval(int newPollInterval) { |
| 79 | pollInterval = newPollInterval; |
| 80 | } |
| 81 | |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 82 | @Override |
| 83 | public long rate() { |
Thomas Vachuska | f0397b5 | 2015-05-29 13:50:17 -0700 | [diff] [blame^] | 84 | return (current - previous) / interval; |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | @Override |
| 88 | public long latest() { |
| 89 | return current; |
| 90 | } |
| 91 | |
| 92 | @Override |
| 93 | public boolean isValid() { |
| 94 | return isValid; |
| 95 | } |
| 96 | |
| 97 | @Override |
| 98 | public long time() { |
| 99 | return time; |
| 100 | } |
alshabib | 23a8d7c | 2014-10-22 21:53:39 -0700 | [diff] [blame] | 101 | |
| 102 | @Override |
| 103 | public String toString() { |
| 104 | return MoreObjects.toStringHelper("Load").add("rate", rate()) |
| 105 | .add("latest", latest()).toString(); |
| 106 | |
| 107 | } |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 108 | } |