blob: 690d1186d2f86d536c2453e5835f6c413b2b3676 [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;
Carmelo Casconee44592f2018-09-12 02:24:47 -070022import org.onosproject.net.pi.model.PiActionProfileId;
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -040023
24import static com.google.common.base.Preconditions.checkNotNull;
25
26/**
Carmelo Cascone87892e22017-11-13 16:01:29 -080027 * Instance of a member of an action group in a protocol-independent pipeline.
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -040028 */
29@Beta
Carmelo Cascone5bc7e102018-02-18 18:27:55 -080030public final class PiActionGroupMember implements PiEntity {
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -040031
Carmelo Casconee44592f2018-09-12 02:24:47 -070032 private final PiActionProfileId actionProfileId;
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -040033 private final PiActionGroupMemberId id;
34 private final PiAction action;
Carmelo Casconee44592f2018-09-12 02:24:47 -070035 // FIXME: in P4Runtime weight is an attribute of the member reference in a
36 // group. Either remove it from this class or define the containing group
37 // ID.
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -040038 private final int weight;
39
Carmelo Casconee44592f2018-09-12 02:24:47 -070040 private PiActionGroupMember(
41 PiActionProfileId actionProfileId, PiActionGroupMemberId id,
42 PiAction action, int weight) {
43 this.actionProfileId = actionProfileId;
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -040044 this.id = id;
45 this.action = action;
46 this.weight = weight;
47 }
48
49 /**
50 * Returns the identifier of this member.
51 *
52 * @return member identifier
53 */
54 public PiActionGroupMemberId id() {
55 return id;
56 }
57
58 /**
Carmelo Casconee44592f2018-09-12 02:24:47 -070059 * Returns the identifier of the action profile.
60 *
61 * @return action profile identifier
62 */
63 public PiActionProfileId actionProfile() {
64 return actionProfileId;
65 }
66
67 /**
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -040068 * Returns the action associated to this member.
69 *
70 * @return action
71 */
72 public PiAction action() {
73 return action;
74 }
75
76 /**
Andrea Campanella8bcd5862017-12-11 11:34:45 +010077 * Returns the weight associated to this member.
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -040078 *
79 * @return weight
80 */
81 public int weight() {
82 return weight;
83 }
84
85 @Override
Carmelo Cascone5bc7e102018-02-18 18:27:55 -080086 public PiEntityType piEntityType() {
87 return PiEntityType.GROUP_MEMBER;
88 }
89
90 @Override
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -040091 public boolean equals(Object o) {
92 if (this == o) {
93 return true;
94 }
95 if (!(o instanceof PiActionGroupMember)) {
96 return false;
97 }
98 PiActionGroupMember that = (PiActionGroupMember) o;
99 return weight == that.weight &&
Carmelo Casconee44592f2018-09-12 02:24:47 -0700100 Objects.equal(actionProfileId, that.actionProfileId) &&
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -0400101 Objects.equal(id, that.id) &&
102 Objects.equal(action, that.action);
103 }
104
105 @Override
106 public int hashCode() {
Carmelo Casconee44592f2018-09-12 02:24:47 -0700107 return Objects.hashCode(actionProfileId, id, action, weight);
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -0400108 }
109
110 @Override
111 public String toString() {
112 return MoreObjects.toStringHelper(this)
Carmelo Casconee44592f2018-09-12 02:24:47 -0700113 .add("actionProfile", actionProfileId)
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -0400114 .add("id", id)
115 .add("action", action)
116 .add("weight", weight)
117 .toString();
118 }
119
120 /**
121 * Returns a new builder of action group members.
122 *
123 * @return member builder
124 */
125 public static Builder builder() {
126 return new Builder();
127 }
128
129 /**
130 * Builder of action group members.
131 */
132 public static final class Builder {
133
Carmelo Casconee44592f2018-09-12 02:24:47 -0700134 private PiActionProfileId actionProfileId;
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -0400135 private PiActionGroupMemberId id;
136 private PiAction action;
137 private int weight;
138
139 private Builder() {
140 // Hides constructor.
141 }
142
143 /**
Carmelo Casconee44592f2018-09-12 02:24:47 -0700144 * Sets the action profile identifier of this member.
145 *
146 * @param actionProfileId action profile identifier
147 * @return this
148 */
149 public Builder forActionProfile(PiActionProfileId actionProfileId) {
150 this.actionProfileId = actionProfileId;
151 return this;
152 }
153
154 /**
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -0400155 * Sets the identifier of this member.
156 *
157 * @param id member identifier
158 * @return this
159 */
160 public Builder withId(PiActionGroupMemberId id) {
161 this.id = id;
162 return this;
163 }
164
165 /**
166 * Sets the action of this member.
167 *
168 * @param action action
169 * @return this
170 */
171 public Builder withAction(PiAction action) {
172 this.action = action;
173 return this;
174 }
175
176 /**
Andrea Campanella8bcd5862017-12-11 11:34:45 +0100177 * Sets the weight of this member.
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -0400178 * <p>
179 * Default value is 0.
180 *
181 * @param weight weight
182 * @return this
183 */
184 public Builder withWeight(int weight) {
185 this.weight = weight;
186 return this;
187 }
188
189 /**
190 * Creates a new action group member.
191 *
192 * @return action group member
193 */
194 public PiActionGroupMember build() {
Carmelo Casconee44592f2018-09-12 02:24:47 -0700195 checkNotNull(actionProfileId);
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -0400196 checkNotNull(id);
197 checkNotNull(action);
Carmelo Casconee44592f2018-09-12 02:24:47 -0700198 return new PiActionGroupMember(actionProfileId, id, action, weight);
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -0400199 }
200 }
201}