blob: 1755136001a34c2808b8e01bb786d7db2a420faa [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2014-present Open Networking Laboratory
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07003 *
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'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.net.statistic;
alshabib3d643ec2014-10-22 18:33:00 -070017
alshabib23a8d7c2014-10-22 21:53:39 -070018import com.google.common.base.MoreObjects;
alshabib3d643ec2014-10-22 18:33:00 -070019
Thomas Vachuskad910a5c2015-05-29 17:09:59 -070020import static com.google.common.base.Preconditions.checkArgument;
21
alshabib3d643ec2014-10-22 18:33:00 -070022/**
23 * Implementation of a load.
24 */
25public 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 Vachuska204cb6c2015-06-04 00:03:06 -070031 private final long interval;
alshabib3d643ec2014-10-22 18:33:00 -070032
33 /**
Thomas Vachuska75aaa672015-04-29 12:24:43 -070034 * Indicates the flow statistics poll interval in seconds.
35 */
Thomas Vachuska204cb6c2015-06-04 00:03:06 -070036 private static long pollInterval = 10;
Thomas Vachuska75aaa672015-04-29 12:24:43 -070037
Thomas Vachuskaf0397b52015-05-29 13:50:17 -070038 /**
alshabib3d643ec2014-10-22 18:33:00 -070039 * 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 Vachuskaf0397b52015-05-29 13:50:17 -070046 this.interval = pollInterval;
alshabib3d643ec2014-10-22 18:33:00 -070047 }
48
49 /**
50 * Creates a load value from the parameters.
Thomas Vachuskaf0397b52015-05-29 13:50:17 -070051 *
52 * @param current the current value
alshabib3d643ec2014-10-22 18:33:00 -070053 * @param previous the previous value
54 */
55 public DefaultLoad(long current, long previous) {
Thomas Vachuskaf0397b52015-05-29 13:50:17 -070056 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 Vachuska204cb6c2015-06-04 00:03:06 -070066 public DefaultLoad(long current, long previous, long interval) {
Thomas Vachuskad910a5c2015-05-29 17:09:59 -070067 checkArgument(interval > 0, "Interval must be greater than 0");
alshabib3d643ec2014-10-22 18:33:00 -070068 this.current = current;
69 this.previous = previous;
70 this.time = System.currentTimeMillis();
71 this.isValid = true;
Thomas Vachuskaf0397b52015-05-29 13:50:17 -070072 this.interval = interval;
alshabib3d643ec2014-10-22 18:33:00 -070073 }
74
Thomas Vachuska75aaa672015-04-29 12:24:43 -070075 /**
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 Vachuska204cb6c2015-06-04 00:03:06 -070081 public static void setPollInterval(long newPollInterval) {
Thomas Vachuska75aaa672015-04-29 12:24:43 -070082 pollInterval = newPollInterval;
83 }
84
alshabib3d643ec2014-10-22 18:33:00 -070085 @Override
86 public long rate() {
Thomas Vachuskaf0397b52015-05-29 13:50:17 -070087 return (current - previous) / interval;
alshabib3d643ec2014-10-22 18:33:00 -070088 }
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 }
alshabib23a8d7c2014-10-22 21:53:39 -0700104
105 @Override
106 public String toString() {
107 return MoreObjects.toStringHelper("Load").add("rate", rate())
108 .add("latest", latest()).toString();
109
110 }
alshabib3d643ec2014-10-22 18:33:00 -0700111}