blob: b2771ac0d1124642382dfc76f4757c5741ca0bae [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 */
alshabib3d643ec2014-10-22 18:33:00 -070016package org.onlab.onos.net.statistic;
17
alshabib23a8d7c2014-10-22 21:53:39 -070018import com.google.common.base.MoreObjects;
alshabib3d643ec2014-10-22 18:33:00 -070019import org.onlab.onos.net.flow.FlowRuleProvider;
20
21/**
22 * Implementation of a load.
23 */
24public class DefaultLoad implements Load {
25
26 private final boolean isValid;
27 private final long current;
28 private final long previous;
29 private final long time;
30
31 /**
32 * Creates an invalid load.
33 */
34 public DefaultLoad() {
35 this.isValid = false;
36 this.time = System.currentTimeMillis();
37 this.current = -1;
38 this.previous = -1;
39 }
40
41 /**
42 * Creates a load value from the parameters.
43 * @param current the current value
44 * @param previous the previous value
45 */
46 public DefaultLoad(long current, long previous) {
47 this.current = current;
48 this.previous = previous;
49 this.time = System.currentTimeMillis();
50 this.isValid = true;
51 }
52
53 @Override
54 public long rate() {
55 return (current - previous) / FlowRuleProvider.POLL_INTERVAL;
56 }
57
58 @Override
59 public long latest() {
60 return current;
61 }
62
63 @Override
64 public boolean isValid() {
65 return isValid;
66 }
67
68 @Override
69 public long time() {
70 return time;
71 }
alshabib23a8d7c2014-10-22 21:53:39 -070072
73 @Override
74 public String toString() {
75 return MoreObjects.toStringHelper("Load").add("rate", rate())
76 .add("latest", latest()).toString();
77
78 }
alshabib3d643ec2014-10-22 18:33:00 -070079}