blob: 2676f65741fc138db61c432b1104a2d98611d85f [file] [log] [blame]
Georgios Katsikas83600982017-05-28 20:41:45 +02001/*
2 * Copyright 2017-present Open Networking Foundation
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 */
Georgios Katsikas2ebd8a02018-06-27 18:32:50 +020016package org.onosproject.drivers.server.devices.nic;
Georgios Katsikas83600982017-05-28 20:41:45 +020017
18import org.onlab.packet.MacAddress;
19
20import java.util.Objects;
21
22import static com.google.common.base.Preconditions.checkNotNull;
23
24/**
25 * A MAC Rx filter value.
26 */
Georgios Katsikas70671b32018-07-02 18:47:27 +020027public final class MacRxFilterValue extends RxFilterValue implements Comparable {
Georgios Katsikas83600982017-05-28 20:41:45 +020028
29 private MacAddress mac;
30
Georgios Katsikas70671b32018-07-02 18:47:27 +020031 /**
32 * Constructs a MAC-based Rx filter.
33 */
Georgios Katsikas83600982017-05-28 20:41:45 +020034 public MacRxFilterValue() {
35 super();
Georgios Katsikas83600982017-05-28 20:41:45 +020036 this.mac = null;
37 }
38
Georgios Katsikas70671b32018-07-02 18:47:27 +020039 /**
40 * Constructs a MAC-based Rx filter with specific MAC address.
41 *
42 * @param mac a MAC address to use as a filter
43 */
Georgios Katsikas83600982017-05-28 20:41:45 +020044 public MacRxFilterValue(MacAddress mac) {
45 super();
Georgios Katsikas2ebd8a02018-06-27 18:32:50 +020046 setValue(mac);
Georgios Katsikas83600982017-05-28 20:41:45 +020047 }
48
Georgios Katsikas70671b32018-07-02 18:47:27 +020049 /**
50 * Constructs a MAC-based Rx filter out of an existing one.
51 *
52 * @param other a source MacRxFilterValue object
53 */
Georgios Katsikas83600982017-05-28 20:41:45 +020054 public MacRxFilterValue(MacRxFilterValue other) {
55 super();
Georgios Katsikas2ebd8a02018-06-27 18:32:50 +020056 setValue(other.value());
Georgios Katsikas83600982017-05-28 20:41:45 +020057 }
58
59 /**
60 * Returns the value of this Rx filter.
61 *
62 * @return MAC value
63 */
64 public MacAddress value() {
65 return this.mac;
66 }
67
68 /**
69 * Sets the value of this Rx filter.
70 *
71 * @param mac MAC value
72 */
73 public void setValue(MacAddress mac) {
74 checkNotNull(mac, "MAC address of Rx filter is NULL");
Georgios Katsikas83600982017-05-28 20:41:45 +020075 this.mac = mac;
76 }
77
78 @Override
79 public int hashCode() {
80 return Objects.hash(this.mac);
81 }
82
83 @Override
84 public boolean equals(Object obj) {
85 if (this == obj) {
86 return true;
87 }
88
89 if ((obj == null) || (!(obj instanceof MacRxFilterValue))) {
90 return false;
91 }
92
93 MacRxFilterValue other = (MacRxFilterValue) obj;
94
95 return this.value().equals(other.value());
96 }
97
98 @Override
99 public int compareTo(Object other) {
100 if (this == other) {
101 return 0;
102 }
103
104 if (other == null) {
105 return -1;
106 }
107
108 if (other instanceof MacRxFilterValue) {
109 MacRxFilterValue otherRxVal = (MacRxFilterValue) other;
110
111 // Extract the digits out of the ID
112 String thisMac = this.toString();
113 String otherMac = otherRxVal.toString();
114
115 return thisMac.compareToIgnoreCase(otherMac);
116 }
117
118 return -1;
119 }
120
121 @Override
122 public String toString() {
123 return this.value().toString();
124 }
125
126}