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; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 19 | import org.onosproject.net.flow.FlowRuleProvider; |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 20 | |
| 21 | /** |
| 22 | * Implementation of a load. |
| 23 | */ |
| 24 | public 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 | } |
alshabib | 23a8d7c | 2014-10-22 21:53:39 -0700 | [diff] [blame] | 72 | |
| 73 | @Override |
| 74 | public String toString() { |
| 75 | return MoreObjects.toStringHelper("Load").add("rate", rate()) |
| 76 | .add("latest", latest()).toString(); |
| 77 | |
| 78 | } |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 79 | } |