blob: b5bd5427459d146bd755ed5e33369626a53d6d92 [file] [log] [blame]
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -04001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -04003 *
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 */
16
17package org.onosproject.net.pi.runtime;
18
19import com.google.common.annotations.Beta;
20import com.google.common.base.MoreObjects;
21import com.google.common.base.Objects;
22
23import static com.google.common.base.Preconditions.checkNotNull;
24
25/**
26 * Member of an action group in a protocol-independent pipeline.
27 */
28@Beta
29public final class PiActionGroupMember {
30
31 private final PiActionGroupMemberId id;
32 private final PiAction action;
33 private final int weight;
34
35 private PiActionGroupMember(PiActionGroupMemberId id, PiAction action, int weight) {
36 this.id = id;
37 this.action = action;
38 this.weight = weight;
39 }
40
41 /**
42 * Returns the identifier of this member.
43 *
44 * @return member identifier
45 */
46 public PiActionGroupMemberId id() {
47 return id;
48 }
49
50 /**
51 * Returns the action associated to this member.
52 *
53 * @return action
54 */
55 public PiAction action() {
56 return action;
57 }
58
59 /**
60 * Returns the weight associated to this member. Valid if the action group is of type {@link
61 * PiActionGroup.Type#SELECT}.
62 *
63 * @return weight
64 */
65 public int weight() {
66 return weight;
67 }
68
69 @Override
70 public boolean equals(Object o) {
71 if (this == o) {
72 return true;
73 }
74 if (!(o instanceof PiActionGroupMember)) {
75 return false;
76 }
77 PiActionGroupMember that = (PiActionGroupMember) o;
78 return weight == that.weight &&
79 Objects.equal(id, that.id) &&
80 Objects.equal(action, that.action);
81 }
82
83 @Override
84 public int hashCode() {
85 return Objects.hashCode(id, action, weight);
86 }
87
88 @Override
89 public String toString() {
90 return MoreObjects.toStringHelper(this)
91 .add("id", id)
92 .add("action", action)
93 .add("weight", weight)
94 .toString();
95 }
96
97 /**
98 * Returns a new builder of action group members.
99 *
100 * @return member builder
101 */
102 public static Builder builder() {
103 return new Builder();
104 }
105
106 /**
107 * Builder of action group members.
108 */
109 public static final class Builder {
110
111 private PiActionGroupMemberId id;
112 private PiAction action;
113 private int weight;
114
115 private Builder() {
116 // Hides constructor.
117 }
118
119 /**
120 * Sets the identifier of this member.
121 *
122 * @param id member identifier
123 * @return this
124 */
125 public Builder withId(PiActionGroupMemberId id) {
126 this.id = id;
127 return this;
128 }
129
130 /**
131 * Sets the action of this member.
132 *
133 * @param action action
134 * @return this
135 */
136 public Builder withAction(PiAction action) {
137 this.action = action;
138 return this;
139 }
140
141 /**
142 * Sets the weight of this member. Valid if the action group is of type {@link PiActionGroup.Type#SELECT}.
143 * <p>
144 * Default value is 0.
145 *
146 * @param weight weight
147 * @return this
148 */
149 public Builder withWeight(int weight) {
150 this.weight = weight;
151 return this;
152 }
153
154 /**
155 * Creates a new action group member.
156 *
157 * @return action group member
158 */
159 public PiActionGroupMember build() {
160 checkNotNull(id);
161 checkNotNull(action);
162 return new PiActionGroupMember(id, action, weight);
163 }
164 }
165}