blob: 210a1e8fb7494e72518520c743a0a1f536b4ad67 [file] [log] [blame]
Georgios Katsikas70671b32018-07-02 18:47:27 +02001/*
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 Katsikas13ccba62020-03-18 12:05:03 +010016
Georgios Katsikas70671b32018-07-02 18:47:27 +020017package org.onosproject.drivers.server.devices.nic;
18
19import java.util.Objects;
20
21import static com.google.common.base.Preconditions.checkArgument;
22import static com.google.common.base.Preconditions.checkNotNull;
Georgios Katsikas13ccba62020-03-18 12:05:03 +010023import static org.onosproject.drivers.server.Constants.MSG_NIC_FLOW_FILTER_NEGATIVE;
24import static org.onosproject.drivers.server.Constants.MSG_NIC_FLOW_RULE_NULL;
Georgios Katsikas70671b32018-07-02 18:47:27 +020025
26/**
27 * A flow rule-based Rx filter value.
28 */
29public final class FlowRxFilterValue extends RxFilterValue
30 implements Comparable {
31
Georgios Katsikas6dc11c12018-12-20 08:43:29 +010032 private long value;
Georgios Katsikas70671b32018-07-02 18:47:27 +020033 private String flowRule;
34
35 /**
36 * Constructs a flow-based Rx filter.
Georgios Katsikas6dc11c12018-12-20 08:43:29 +010037 *
38 * @param cpuId CPU ID of the server this tag will lead to
Georgios Katsikas70671b32018-07-02 18:47:27 +020039 */
Georgios Katsikas6dc11c12018-12-20 08:43:29 +010040 public FlowRxFilterValue(int cpuId) {
41 super(cpuId);
Georgios Katsikas70671b32018-07-02 18:47:27 +020042 setValue(0);
43 setRule("");
44 }
45
46 /**
Georgios Katsikas6dc11c12018-12-20 08:43:29 +010047 * Constructs a flow-based Rx filter with physical CPU core ID.
Georgios Katsikas70671b32018-07-02 18:47:27 +020048 *
Georgios Katsikas6dc11c12018-12-20 08:43:29 +010049 * @param value Flow tag
50 * @param cpuId CPU ID of the server this tag will lead to
Georgios Katsikas70671b32018-07-02 18:47:27 +020051 */
Georgios Katsikas6dc11c12018-12-20 08:43:29 +010052 public FlowRxFilterValue(long value, int cpuId) {
53 super(cpuId);
54 setValue(value);
Georgios Katsikas70671b32018-07-02 18:47:27 +020055 setRule("");
56 }
57
58 /**
59 * Constructs a flow-based Rx filter with CPU core ID
60 * and an associated rule.
61 *
Georgios Katsikas6dc11c12018-12-20 08:43:29 +010062 * @param value Flow tag
63 * @param cpuId CPU ID of the server this tag will lead to
Georgios Katsikas70671b32018-07-02 18:47:27 +020064 * @param flowRule a flow rule as a string
65 */
Georgios Katsikas6dc11c12018-12-20 08:43:29 +010066 public FlowRxFilterValue(long value, int cpuId, String flowRule) {
67 super(cpuId);
68 setValue(value);
Georgios Katsikas70671b32018-07-02 18:47:27 +020069 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 Katsikas6dc11c12018-12-20 08:43:29 +010078 super(other.cpuId);
Georgios Katsikas70671b32018-07-02 18:47:27 +020079 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 Katsikas6dc11c12018-12-20 08:43:29 +010089 return this.value;
Georgios Katsikas70671b32018-07-02 18:47:27 +020090 }
91
92 /**
93 * Sets the value of this Rx filter.
94 *
Georgios Katsikas6dc11c12018-12-20 08:43:29 +010095 * @param value a CPU core ID for this Rx filter
Georgios Katsikas70671b32018-07-02 18:47:27 +020096 */
Georgios Katsikas6dc11c12018-12-20 08:43:29 +010097 private void setValue(long value) {
Georgios Katsikas13ccba62020-03-18 12:05:03 +010098 checkArgument(value >= 0, MSG_NIC_FLOW_FILTER_NEGATIVE);
Georgios Katsikas6dc11c12018-12-20 08:43:29 +010099 this.value = value;
Georgios Katsikas70671b32018-07-02 18:47:27 +0200100 }
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 Katsikas13ccba62020-03-18 12:05:03 +0100117 checkNotNull(flowRule, MSG_NIC_FLOW_RULE_NULL);
Georgios Katsikas70671b32018-07-02 18:47:27 +0200118 this.flowRule = flowRule;
119 }
120
121 @Override
122 public int hashCode() {
Georgios Katsikas6dc11c12018-12-20 08:43:29 +0100123 return Objects.hash(this.value, this.flowRule, this.cpuId);
Georgios Katsikas70671b32018-07-02 18:47:27 +0200124 }
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 Katsikas6dc11c12018-12-20 08:43:29 +0100138 return this.value() == other.value() &&
139 this.rule().equals(other.rule()) && ((RxFilterValue) this).equals(other);
Georgios Katsikas70671b32018-07-02 18:47:27 +0200140 }
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 Katsikas6dc11c12018-12-20 08:43:29 +0100155 long thisCpuId = this.value();
156 long otherCpuId = otherRxVal.value();
Georgios Katsikas70671b32018-07-02 18:47:27 +0200157
Georgios Katsikas6dc11c12018-12-20 08:43:29 +0100158 if (thisCpuId > otherCpuId) {
Georgios Katsikas70671b32018-07-02 18:47:27 +0200159 return 1;
Georgios Katsikas6dc11c12018-12-20 08:43:29 +0100160 } else if (thisCpuId < otherCpuId) {
Georgios Katsikas70671b32018-07-02 18:47:27 +0200161 return -1;
162 } else {
Georgios Katsikas6dc11c12018-12-20 08:43:29 +0100163 return this.cpuId - otherRxVal.cpuId;
Georgios Katsikas70671b32018-07-02 18:47:27 +0200164 }
165 }
166
167 return -1;
168 }
169
170 @Override
171 public String toString() {
172 return Long.toString(this.value());
173 }
174
175}