blob: 12f87feeb19caed88bb067f8b4672561a3f0edc0 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
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'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
20/**
21 * Implementation of a load.
22 */
23public 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 Vachuskaf0397b52015-05-29 13:50:17 -070029 private final int interval;
alshabib3d643ec2014-10-22 18:33:00 -070030
31 /**
Thomas Vachuska75aaa672015-04-29 12:24:43 -070032 * Indicates the flow statistics poll interval in seconds.
33 */
34 private static int pollInterval = 10;
35
Thomas Vachuskaf0397b52015-05-29 13:50:17 -070036 /**
alshabib3d643ec2014-10-22 18:33:00 -070037 * 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 Vachuskaf0397b52015-05-29 13:50:17 -070044 this.interval = pollInterval;
alshabib3d643ec2014-10-22 18:33:00 -070045 }
46
47 /**
48 * Creates a load value from the parameters.
Thomas Vachuskaf0397b52015-05-29 13:50:17 -070049 *
50 * @param current the current value
alshabib3d643ec2014-10-22 18:33:00 -070051 * @param previous the previous value
52 */
53 public DefaultLoad(long current, long previous) {
Thomas Vachuskaf0397b52015-05-29 13:50:17 -070054 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) {
alshabib3d643ec2014-10-22 18:33:00 -070065 this.current = current;
66 this.previous = previous;
67 this.time = System.currentTimeMillis();
68 this.isValid = true;
Thomas Vachuskaf0397b52015-05-29 13:50:17 -070069 this.interval = interval;
alshabib3d643ec2014-10-22 18:33:00 -070070 }
71
Thomas Vachuska75aaa672015-04-29 12:24:43 -070072 /**
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
alshabib3d643ec2014-10-22 18:33:00 -070082 @Override
83 public long rate() {
Thomas Vachuskaf0397b52015-05-29 13:50:17 -070084 return (current - previous) / interval;
alshabib3d643ec2014-10-22 18:33:00 -070085 }
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 }
alshabib23a8d7c2014-10-22 21:53:39 -0700101
102 @Override
103 public String toString() {
104 return MoreObjects.toStringHelper("Load").add("rate", rate())
105 .add("latest", latest()).toString();
106
107 }
alshabib3d643ec2014-10-22 18:33:00 -0700108}