blob: 25b28bb84e9a8fa52753d37cb2a5298f2b6e6ace [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.
Georgios Katsikas6dc11c12018-12-20 08:43:29 +010033 * @param cpuId CPU ID of the server this tag will lead to
Georgios Katsikas70671b32018-07-02 18:47:27 +020034 */
Georgios Katsikas6dc11c12018-12-20 08:43:29 +010035 public MacRxFilterValue(int cpuId) {
36 super(cpuId);
Georgios Katsikas83600982017-05-28 20:41:45 +020037 this.mac = null;
38 }
39
Georgios Katsikas70671b32018-07-02 18:47:27 +020040 /**
41 * Constructs a MAC-based Rx filter with specific MAC address.
42 *
43 * @param mac a MAC address to use as a filter
Georgios Katsikas6dc11c12018-12-20 08:43:29 +010044 * @param cpuId CPU ID of the server this tag will lead to
Georgios Katsikas70671b32018-07-02 18:47:27 +020045 */
Georgios Katsikas6dc11c12018-12-20 08:43:29 +010046 public MacRxFilterValue(MacAddress mac, int cpuId) {
47 super(cpuId);
Georgios Katsikas2ebd8a02018-06-27 18:32:50 +020048 setValue(mac);
Georgios Katsikas83600982017-05-28 20:41:45 +020049 }
50
Georgios Katsikas70671b32018-07-02 18:47:27 +020051 /**
52 * Constructs a MAC-based Rx filter out of an existing one.
53 *
54 * @param other a source MacRxFilterValue object
55 */
Georgios Katsikas83600982017-05-28 20:41:45 +020056 public MacRxFilterValue(MacRxFilterValue other) {
Georgios Katsikas6dc11c12018-12-20 08:43:29 +010057 super(other.cpuId);
Georgios Katsikas2ebd8a02018-06-27 18:32:50 +020058 setValue(other.value());
Georgios Katsikas83600982017-05-28 20:41:45 +020059 }
60
61 /**
62 * Returns the value of this Rx filter.
63 *
64 * @return MAC value
65 */
66 public MacAddress value() {
67 return this.mac;
68 }
69
70 /**
71 * Sets the value of this Rx filter.
72 *
73 * @param mac MAC value
74 */
75 public void setValue(MacAddress mac) {
76 checkNotNull(mac, "MAC address of Rx filter is NULL");
Georgios Katsikas83600982017-05-28 20:41:45 +020077 this.mac = mac;
78 }
79
80 @Override
81 public int hashCode() {
82 return Objects.hash(this.mac);
83 }
84
85 @Override
86 public boolean equals(Object obj) {
87 if (this == obj) {
88 return true;
89 }
90
91 if ((obj == null) || (!(obj instanceof MacRxFilterValue))) {
92 return false;
93 }
94
95 MacRxFilterValue other = (MacRxFilterValue) obj;
96
97 return this.value().equals(other.value());
98 }
99
100 @Override
101 public int compareTo(Object other) {
102 if (this == other) {
103 return 0;
104 }
105
106 if (other == null) {
107 return -1;
108 }
109
110 if (other instanceof MacRxFilterValue) {
111 MacRxFilterValue otherRxVal = (MacRxFilterValue) other;
112
113 // Extract the digits out of the ID
114 String thisMac = this.toString();
115 String otherMac = otherRxVal.toString();
116
117 return thisMac.compareToIgnoreCase(otherMac);
118 }
119
120 return -1;
121 }
122
123 @Override
124 public String toString() {
125 return this.value().toString();
126 }
127
128}