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