Georgios Katsikas | 70671b3 | 2018-07-02 18:47:27 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-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 Katsikas | 13ccba6 | 2020-03-18 12:05:03 +0100 | [diff] [blame] | 16 | |
Georgios Katsikas | 70671b3 | 2018-07-02 18:47:27 +0200 | [diff] [blame] | 17 | package org.onosproject.drivers.server.devices.nic; |
| 18 | |
| 19 | import java.util.Objects; |
| 20 | |
| 21 | import static com.google.common.base.Preconditions.checkArgument; |
| 22 | import static com.google.common.base.Preconditions.checkNotNull; |
Georgios Katsikas | 13ccba6 | 2020-03-18 12:05:03 +0100 | [diff] [blame] | 23 | import static org.onosproject.drivers.server.Constants.MSG_NIC_FLOW_FILTER_NEGATIVE; |
| 24 | import static org.onosproject.drivers.server.Constants.MSG_NIC_FLOW_RULE_NULL; |
Georgios Katsikas | 70671b3 | 2018-07-02 18:47:27 +0200 | [diff] [blame] | 25 | |
| 26 | /** |
| 27 | * A flow rule-based Rx filter value. |
| 28 | */ |
| 29 | public final class FlowRxFilterValue extends RxFilterValue |
| 30 | implements Comparable { |
| 31 | |
Georgios Katsikas | 6dc11c1 | 2018-12-20 08:43:29 +0100 | [diff] [blame] | 32 | private long value; |
Georgios Katsikas | 70671b3 | 2018-07-02 18:47:27 +0200 | [diff] [blame] | 33 | private String flowRule; |
| 34 | |
| 35 | /** |
| 36 | * Constructs a flow-based Rx filter. |
Georgios Katsikas | 6dc11c1 | 2018-12-20 08:43:29 +0100 | [diff] [blame] | 37 | * |
| 38 | * @param cpuId CPU ID of the server this tag will lead to |
Georgios Katsikas | 70671b3 | 2018-07-02 18:47:27 +0200 | [diff] [blame] | 39 | */ |
Georgios Katsikas | 6dc11c1 | 2018-12-20 08:43:29 +0100 | [diff] [blame] | 40 | public FlowRxFilterValue(int cpuId) { |
| 41 | super(cpuId); |
Georgios Katsikas | 70671b3 | 2018-07-02 18:47:27 +0200 | [diff] [blame] | 42 | setValue(0); |
| 43 | setRule(""); |
| 44 | } |
| 45 | |
| 46 | /** |
Georgios Katsikas | 6dc11c1 | 2018-12-20 08:43:29 +0100 | [diff] [blame] | 47 | * Constructs a flow-based Rx filter with physical CPU core ID. |
Georgios Katsikas | 70671b3 | 2018-07-02 18:47:27 +0200 | [diff] [blame] | 48 | * |
Georgios Katsikas | 6dc11c1 | 2018-12-20 08:43:29 +0100 | [diff] [blame] | 49 | * @param value Flow tag |
| 50 | * @param cpuId CPU ID of the server this tag will lead to |
Georgios Katsikas | 70671b3 | 2018-07-02 18:47:27 +0200 | [diff] [blame] | 51 | */ |
Georgios Katsikas | 6dc11c1 | 2018-12-20 08:43:29 +0100 | [diff] [blame] | 52 | public FlowRxFilterValue(long value, int cpuId) { |
| 53 | super(cpuId); |
| 54 | setValue(value); |
Georgios Katsikas | 70671b3 | 2018-07-02 18:47:27 +0200 | [diff] [blame] | 55 | setRule(""); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Constructs a flow-based Rx filter with CPU core ID |
| 60 | * and an associated rule. |
| 61 | * |
Georgios Katsikas | 6dc11c1 | 2018-12-20 08:43:29 +0100 | [diff] [blame] | 62 | * @param value Flow tag |
| 63 | * @param cpuId CPU ID of the server this tag will lead to |
Georgios Katsikas | 70671b3 | 2018-07-02 18:47:27 +0200 | [diff] [blame] | 64 | * @param flowRule a flow rule as a string |
| 65 | */ |
Georgios Katsikas | 6dc11c1 | 2018-12-20 08:43:29 +0100 | [diff] [blame] | 66 | public FlowRxFilterValue(long value, int cpuId, String flowRule) { |
| 67 | super(cpuId); |
| 68 | setValue(value); |
Georgios Katsikas | 70671b3 | 2018-07-02 18:47:27 +0200 | [diff] [blame] | 69 | setRule(flowRule); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Constructs a flow-based Rx filter out of an existing one. |
| 74 | * |
| 75 | * @param other a source FlowRxFilterValue object |
| 76 | */ |
| 77 | public FlowRxFilterValue(FlowRxFilterValue other) { |
Georgios Katsikas | 6dc11c1 | 2018-12-20 08:43:29 +0100 | [diff] [blame] | 78 | super(other.cpuId); |
Georgios Katsikas | 70671b3 | 2018-07-02 18:47:27 +0200 | [diff] [blame] | 79 | setValue(other.value()); |
| 80 | setRule(other.rule()); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Returns the value of this Rx filter. |
| 85 | * |
| 86 | * @return Flow Rx filter value |
| 87 | */ |
| 88 | public long value() { |
Georgios Katsikas | 6dc11c1 | 2018-12-20 08:43:29 +0100 | [diff] [blame] | 89 | return this.value; |
Georgios Katsikas | 70671b3 | 2018-07-02 18:47:27 +0200 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Sets the value of this Rx filter. |
| 94 | * |
Georgios Katsikas | 6dc11c1 | 2018-12-20 08:43:29 +0100 | [diff] [blame] | 95 | * @param value a CPU core ID for this Rx filter |
Georgios Katsikas | 70671b3 | 2018-07-02 18:47:27 +0200 | [diff] [blame] | 96 | */ |
Georgios Katsikas | 6dc11c1 | 2018-12-20 08:43:29 +0100 | [diff] [blame] | 97 | private void setValue(long value) { |
Georgios Katsikas | 13ccba6 | 2020-03-18 12:05:03 +0100 | [diff] [blame] | 98 | checkArgument(value >= 0, MSG_NIC_FLOW_FILTER_NEGATIVE); |
Georgios Katsikas | 6dc11c1 | 2018-12-20 08:43:29 +0100 | [diff] [blame] | 99 | this.value = value; |
Georgios Katsikas | 70671b3 | 2018-07-02 18:47:27 +0200 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Returns the rule of this Rx filter. |
| 104 | * |
| 105 | * @return Flow Rx filter rule |
| 106 | */ |
| 107 | public String rule() { |
| 108 | return this.flowRule; |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Sets the rule of this Rx filter. |
| 113 | * |
| 114 | * @param flowRule Flow Rx filter rule as a string |
| 115 | */ |
| 116 | public void setRule(String flowRule) { |
Georgios Katsikas | 13ccba6 | 2020-03-18 12:05:03 +0100 | [diff] [blame] | 117 | checkNotNull(flowRule, MSG_NIC_FLOW_RULE_NULL); |
Georgios Katsikas | 70671b3 | 2018-07-02 18:47:27 +0200 | [diff] [blame] | 118 | this.flowRule = flowRule; |
| 119 | } |
| 120 | |
| 121 | @Override |
| 122 | public int hashCode() { |
Georgios Katsikas | 6dc11c1 | 2018-12-20 08:43:29 +0100 | [diff] [blame] | 123 | return Objects.hash(this.value, this.flowRule, this.cpuId); |
Georgios Katsikas | 70671b3 | 2018-07-02 18:47:27 +0200 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | @Override |
| 127 | public boolean equals(Object obj) { |
| 128 | if (this == obj) { |
| 129 | return true; |
| 130 | } |
| 131 | |
| 132 | if ((obj == null) || (!(obj instanceof FlowRxFilterValue))) { |
| 133 | return false; |
| 134 | } |
| 135 | |
| 136 | FlowRxFilterValue other = (FlowRxFilterValue) obj; |
| 137 | |
Georgios Katsikas | 6dc11c1 | 2018-12-20 08:43:29 +0100 | [diff] [blame] | 138 | return this.value() == other.value() && |
| 139 | this.rule().equals(other.rule()) && ((RxFilterValue) this).equals(other); |
Georgios Katsikas | 70671b3 | 2018-07-02 18:47:27 +0200 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | @Override |
| 143 | public int compareTo(Object other) { |
| 144 | if (this == other) { |
| 145 | return 0; |
| 146 | } |
| 147 | |
| 148 | if (other == null) { |
| 149 | return -1; |
| 150 | } |
| 151 | |
| 152 | if (other instanceof FlowRxFilterValue) { |
| 153 | FlowRxFilterValue otherRxVal = (FlowRxFilterValue) other; |
| 154 | |
Georgios Katsikas | 6dc11c1 | 2018-12-20 08:43:29 +0100 | [diff] [blame] | 155 | long thisCpuId = this.value(); |
| 156 | long otherCpuId = otherRxVal.value(); |
Georgios Katsikas | 70671b3 | 2018-07-02 18:47:27 +0200 | [diff] [blame] | 157 | |
Georgios Katsikas | 6dc11c1 | 2018-12-20 08:43:29 +0100 | [diff] [blame] | 158 | if (thisCpuId > otherCpuId) { |
Georgios Katsikas | 70671b3 | 2018-07-02 18:47:27 +0200 | [diff] [blame] | 159 | return 1; |
Georgios Katsikas | 6dc11c1 | 2018-12-20 08:43:29 +0100 | [diff] [blame] | 160 | } else if (thisCpuId < otherCpuId) { |
Georgios Katsikas | 70671b3 | 2018-07-02 18:47:27 +0200 | [diff] [blame] | 161 | return -1; |
| 162 | } else { |
Georgios Katsikas | 6dc11c1 | 2018-12-20 08:43:29 +0100 | [diff] [blame] | 163 | return this.cpuId - otherRxVal.cpuId; |
Georgios Katsikas | 70671b3 | 2018-07-02 18:47:27 +0200 | [diff] [blame] | 164 | } |
| 165 | } |
| 166 | |
| 167 | return -1; |
| 168 | } |
| 169 | |
| 170 | @Override |
| 171 | public String toString() { |
| 172 | return Long.toString(this.value()); |
| 173 | } |
| 174 | |
| 175 | } |