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