Frank Wang | 0e80508 | 2017-07-21 14:37:35 +0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Frank Wang | 0e80508 | 2017-07-21 14:37:35 +0800 | [diff] [blame] | 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 | */ |
| 16 | |
| 17 | package org.onosproject.drivers.bmv2; |
| 18 | |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 19 | import com.google.common.annotations.Beta; |
| 20 | import com.google.common.base.MoreObjects; |
Frank Wang | 0e80508 | 2017-07-21 14:37:35 +0800 | [diff] [blame] | 21 | import com.google.common.base.Objects; |
| 22 | import org.onosproject.net.flow.FlowRule; |
| 23 | |
| 24 | /** |
| 25 | * A wrapper for a ONOS flow rule installed on a BMv2 device. |
| 26 | */ |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 27 | @Beta |
| 28 | final class Bmv2FlowRuleWrapper { |
Frank Wang | 0e80508 | 2017-07-21 14:37:35 +0800 | [diff] [blame] | 29 | |
| 30 | private final FlowRule rule; |
| 31 | private final long installedOnMillis; |
| 32 | |
| 33 | /** |
| 34 | * Creates a new flow rule wrapper. |
| 35 | * |
| 36 | * @param rule a flow rule |
| 37 | * @param installedOnMillis the time (in milliseconds, since January 1, 1970 UTC) when the flow rule was installed |
| 38 | * on the device |
| 39 | */ |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 40 | Bmv2FlowRuleWrapper(FlowRule rule, long installedOnMillis) { |
Frank Wang | 0e80508 | 2017-07-21 14:37:35 +0800 | [diff] [blame] | 41 | this.rule = rule; |
| 42 | this.installedOnMillis = installedOnMillis; |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Returns the flow rule contained by this wrapper. |
| 47 | * |
| 48 | * @return a flow rule |
| 49 | */ |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 50 | FlowRule rule() { |
Frank Wang | 0e80508 | 2017-07-21 14:37:35 +0800 | [diff] [blame] | 51 | return rule; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Return the number of seconds since when this flow rule was installed on the device. |
| 56 | * |
| 57 | * @return an integer value |
| 58 | */ |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 59 | long lifeInSeconds() { |
Frank Wang | 0e80508 | 2017-07-21 14:37:35 +0800 | [diff] [blame] | 60 | return (System.currentTimeMillis() - installedOnMillis) / 1000; |
| 61 | } |
| 62 | |
Frank Wang | 0e80508 | 2017-07-21 14:37:35 +0800 | [diff] [blame] | 63 | @Override |
| 64 | public int hashCode() { |
| 65 | return Objects.hashCode(rule, installedOnMillis); |
| 66 | } |
| 67 | |
| 68 | @Override |
| 69 | public boolean equals(Object obj) { |
| 70 | if (this == obj) { |
| 71 | return true; |
| 72 | } |
| 73 | if (obj == null || getClass() != obj.getClass()) { |
| 74 | return false; |
| 75 | } |
| 76 | final Bmv2FlowRuleWrapper other = (Bmv2FlowRuleWrapper) obj; |
| 77 | return Objects.equal(this.rule, other.rule) |
| 78 | && Objects.equal(this.installedOnMillis, other.installedOnMillis); |
| 79 | } |
| 80 | |
| 81 | @Override |
| 82 | public String toString() { |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 83 | return MoreObjects.toStringHelper(this) |
| 84 | .add("rule", rule) |
| 85 | .add("installedOnMillis", installedOnMillis) |
| 86 | .toString(); |
Frank Wang | 0e80508 | 2017-07-21 14:37:35 +0800 | [diff] [blame] | 87 | } |
| 88 | } |