blob: 97d3fe1eaa4c3ac59ad10950737366cc8e7595d4 [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;
29
30 /**
Thomas Vachuska75aaa672015-04-29 12:24:43 -070031 * Indicates the flow statistics poll interval in seconds.
32 */
33 private static int pollInterval = 10;
34
35 /**
alshabib3d643ec2014-10-22 18:33:00 -070036 * 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 Vachuska75aaa672015-04-29 12:24:43 -070057 /**
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
alshabib3d643ec2014-10-22 18:33:00 -070067 @Override
68 public long rate() {
Thomas Vachuska75aaa672015-04-29 12:24:43 -070069 return (current - previous) / pollInterval;
alshabib3d643ec2014-10-22 18:33:00 -070070 }
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 }
alshabib23a8d7c2014-10-22 21:53:39 -070086
87 @Override
88 public String toString() {
89 return MoreObjects.toStringHelper("Load").add("rate", rate())
90 .add("latest", latest()).toString();
91
92 }
alshabib3d643ec2014-10-22 18:33:00 -070093}