blob: 879783bfc7d0bafe79f78b799b355770d0aed923 [file] [log] [blame]
Frank Wang0e805082017-07-21 14:37:35 +08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Frank Wang0e805082017-07-21 14:37:35 +08003 *
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
17package org.onosproject.drivers.bmv2;
18
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +020019import com.google.common.annotations.Beta;
20import com.google.common.base.MoreObjects;
Frank Wang0e805082017-07-21 14:37:35 +080021import com.google.common.base.Objects;
22import org.onosproject.net.flow.FlowRule;
23
24/**
25 * A wrapper for a ONOS flow rule installed on a BMv2 device.
26 */
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +020027@Beta
28final class Bmv2FlowRuleWrapper {
Frank Wang0e805082017-07-21 14:37:35 +080029
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 Cascone0b22d8f2017-07-31 07:22:27 +020040 Bmv2FlowRuleWrapper(FlowRule rule, long installedOnMillis) {
Frank Wang0e805082017-07-21 14:37:35 +080041 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 Cascone0b22d8f2017-07-31 07:22:27 +020050 FlowRule rule() {
Frank Wang0e805082017-07-21 14:37:35 +080051 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 Cascone0b22d8f2017-07-31 07:22:27 +020059 long lifeInSeconds() {
Frank Wang0e805082017-07-21 14:37:35 +080060 return (System.currentTimeMillis() - installedOnMillis) / 1000;
61 }
62
Frank Wang0e805082017-07-21 14:37:35 +080063 @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 Cascone0b22d8f2017-07-31 07:22:27 +020083 return MoreObjects.toStringHelper(this)
84 .add("rule", rule)
85 .add("installedOnMillis", installedOnMillis)
86 .toString();
Frank Wang0e805082017-07-21 14:37:35 +080087 }
88}