blob: 772ddbdf4c3280e37a8de3cd9d4014a2cf7dc711 [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 Katsikas13ccba62020-03-18 12:05:03 +010016
Georgios Katsikas2ebd8a02018-06-27 18:32:50 +020017package org.onosproject.drivers.server.devices.nic;
Georgios Katsikas83600982017-05-28 20:41:45 +020018
19import org.onlab.packet.MacAddress;
20
21import java.util.Objects;
22
23import static com.google.common.base.Preconditions.checkNotNull;
Georgios Katsikas13ccba62020-03-18 12:05:03 +010024import static org.onosproject.drivers.server.Constants.MSG_NIC_FLOW_FILTER_MAC_NULL;
Georgios Katsikas83600982017-05-28 20:41:45 +020025
26/**
27 * A MAC Rx filter value.
28 */
Georgios Katsikas70671b32018-07-02 18:47:27 +020029public final class MacRxFilterValue extends RxFilterValue implements Comparable {
Georgios Katsikas83600982017-05-28 20:41:45 +020030
31 private MacAddress mac;
32
Georgios Katsikas70671b32018-07-02 18:47:27 +020033 /**
34 * Constructs a MAC-based Rx filter.
Georgios Katsikas6dc11c12018-12-20 08:43:29 +010035 * @param cpuId CPU ID of the server this tag will lead to
Georgios Katsikas70671b32018-07-02 18:47:27 +020036 */
Georgios Katsikas6dc11c12018-12-20 08:43:29 +010037 public MacRxFilterValue(int cpuId) {
38 super(cpuId);
Georgios Katsikas83600982017-05-28 20:41:45 +020039 this.mac = null;
40 }
41
Georgios Katsikas70671b32018-07-02 18:47:27 +020042 /**
43 * Constructs a MAC-based Rx filter with specific MAC address.
44 *
45 * @param mac a MAC address to use as a filter
Georgios Katsikas6dc11c12018-12-20 08:43:29 +010046 * @param cpuId CPU ID of the server this tag will lead to
Georgios Katsikas70671b32018-07-02 18:47:27 +020047 */
Georgios Katsikas6dc11c12018-12-20 08:43:29 +010048 public MacRxFilterValue(MacAddress mac, int cpuId) {
49 super(cpuId);
Georgios Katsikas2ebd8a02018-06-27 18:32:50 +020050 setValue(mac);
Georgios Katsikas83600982017-05-28 20:41:45 +020051 }
52
Georgios Katsikas70671b32018-07-02 18:47:27 +020053 /**
54 * Constructs a MAC-based Rx filter out of an existing one.
55 *
56 * @param other a source MacRxFilterValue object
57 */
Georgios Katsikas83600982017-05-28 20:41:45 +020058 public MacRxFilterValue(MacRxFilterValue other) {
Georgios Katsikas6dc11c12018-12-20 08:43:29 +010059 super(other.cpuId);
Georgios Katsikas2ebd8a02018-06-27 18:32:50 +020060 setValue(other.value());
Georgios Katsikas83600982017-05-28 20:41:45 +020061 }
62
63 /**
64 * Returns the value of this Rx filter.
65 *
66 * @return MAC value
67 */
68 public MacAddress value() {
69 return this.mac;
70 }
71
72 /**
73 * Sets the value of this Rx filter.
74 *
75 * @param mac MAC value
76 */
77 public void setValue(MacAddress mac) {
Georgios Katsikas13ccba62020-03-18 12:05:03 +010078 checkNotNull(mac, MSG_NIC_FLOW_FILTER_MAC_NULL);
Georgios Katsikas83600982017-05-28 20:41:45 +020079 this.mac = mac;
80 }
81
82 @Override
83 public int hashCode() {
84 return Objects.hash(this.mac);
85 }
86
87 @Override
88 public boolean equals(Object obj) {
89 if (this == obj) {
90 return true;
91 }
92
93 if ((obj == null) || (!(obj instanceof MacRxFilterValue))) {
94 return false;
95 }
96
97 MacRxFilterValue other = (MacRxFilterValue) obj;
98
99 return this.value().equals(other.value());
100 }
101
102 @Override
103 public int compareTo(Object other) {
104 if (this == other) {
105 return 0;
106 }
107
108 if (other == null) {
109 return -1;
110 }
111
112 if (other instanceof MacRxFilterValue) {
113 MacRxFilterValue otherRxVal = (MacRxFilterValue) other;
114
115 // Extract the digits out of the ID
116 String thisMac = this.toString();
117 String otherMac = otherRxVal.toString();
118
119 return thisMac.compareToIgnoreCase(otherMac);
120 }
121
122 return -1;
123 }
124
125 @Override
126 public String toString() {
Georgios Katsikas13ccba62020-03-18 12:05:03 +0100127 return this.value().toString();
Georgios Katsikas83600982017-05-28 20:41:45 +0200128 }
129
130}