blob: 554be28bc635bfa78f78e19b36d96231ea97d47c [file] [log] [blame]
Charles Chanb87d9f12018-10-31 21:00:06 -07001/*
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 */
16package org.onosproject.net.flowobjective;
17
18import org.onosproject.net.flow.TrafficTreatment;
19
20import java.util.Objects;
21
22import static com.google.common.base.MoreObjects.toStringHelper;
23
24/**
Niloofar Toorchi8bbe9ca2021-07-30 14:08:04 -070025 * Represents a next action specified by traffic treatment and weight.
Charles Chanb87d9f12018-10-31 21:00:06 -070026 */
27public final class DefaultNextTreatment implements NextTreatment {
28 private final TrafficTreatment treatment;
Niloofar Toorchi8bbe9ca2021-07-30 14:08:04 -070029 private final int weight;
Charles Chanb87d9f12018-10-31 21:00:06 -070030
Niloofar Toorchi8bbe9ca2021-07-30 14:08:04 -070031 private DefaultNextTreatment(TrafficTreatment treatment, int weight) {
Charles Chanb87d9f12018-10-31 21:00:06 -070032 this.treatment = treatment;
Niloofar Toorchi8bbe9ca2021-07-30 14:08:04 -070033 this.weight = weight;
Charles Chanb87d9f12018-10-31 21:00:06 -070034 }
35
36 /**
37 * Returns traffic treatment.
38 *
39 * @return traffic treatment.
40 */
41 public TrafficTreatment treatment() {
42 return treatment;
43 }
44
45 /**
46 * Returns an instance of DefaultNextTreatment with given traffic treatment.
47 *
48 * @param treatment traffic treatment
49 * @return an instance of DefaultNextTreatment
50 */
51 public static DefaultNextTreatment of(TrafficTreatment treatment) {
Niloofar Toorchi8bbe9ca2021-07-30 14:08:04 -070052 return new DefaultNextTreatment(treatment, DEFAULT_WEIGHT);
53 }
54
55 /**
56 * Returns an instance of DefaultNextTreatment with given traffic treatment and weight.
57 *
58 * @param treatment traffic treatment
59 * @param weight the weight of next treatment
60 * @return an instance of DefaultNextTreatment
61 */
62 public static DefaultNextTreatment of(TrafficTreatment treatment, int weight) {
63 return new DefaultNextTreatment(treatment, weight);
64 }
65
66 @Override
67 public int weight() {
68 return weight;
Charles Chanb87d9f12018-10-31 21:00:06 -070069 }
70
71 @Override
72 public Type type() {
73 return Type.TREATMENT;
74 }
75
76 @Override
77 public int hashCode() {
Niloofar Toorchi8bbe9ca2021-07-30 14:08:04 -070078 return Objects.hash(treatment, weight);
Charles Chanb87d9f12018-10-31 21:00:06 -070079 }
80
81 @Override
82 public boolean equals(Object obj) {
83 if (this == obj) {
84 return true;
85 }
86 if (obj instanceof DefaultNextTreatment) {
87 final DefaultNextTreatment other = (DefaultNextTreatment) obj;
Niloofar Toorchi8bbe9ca2021-07-30 14:08:04 -070088 return Objects.equals(this.treatment, other.treatment) && Objects.equals(this.weight, other.weight);
Charles Chanb87d9f12018-10-31 21:00:06 -070089 }
90 return false;
91 }
92
93 @Override
94 public String toString() {
95 return toStringHelper(this)
96 .add("treatment", treatment)
Niloofar Toorchi8bbe9ca2021-07-30 14:08:04 -070097 .add("weight", weight)
Charles Chanb87d9f12018-10-31 21:00:06 -070098 .toString();
99 }
100}