blob: 92c35627cf7fb24b5afbabaf3343421dee7f7f67 [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 Casconecb4327a2018-09-11 15:17:23 -070027 * Instance of a member of an action profile in a protocol-independent pipeline.
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -040028 */
29@Beta
Carmelo Casconecb4327a2018-09-11 15:17:23 -070030public final class PiActionProfileMember implements PiEntity {
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -040031
Carmelo Casconee44592f2018-09-12 02:24:47 -070032 private final PiActionProfileId actionProfileId;
Carmelo Casconecb4327a2018-09-11 15:17:23 -070033 private final PiActionProfileMemberId memberId;
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -040034 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 Casconecb4327a2018-09-11 15:17:23 -070040 private PiActionProfileMember(PiActionProfileId actionProfileId,
41 PiActionProfileMemberId memberId,
42 PiAction action,
43 int weight) {
Carmelo Casconee44592f2018-09-12 02:24:47 -070044 this.actionProfileId = actionProfileId;
Carmelo Casconecb4327a2018-09-11 15:17:23 -070045 this.memberId = memberId;
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -040046 this.action = action;
47 this.weight = weight;
48 }
49
50 /**
51 * Returns the identifier of this member.
52 *
53 * @return member identifier
54 */
Carmelo Casconecb4327a2018-09-11 15:17:23 -070055 public PiActionProfileMemberId id() {
56 return memberId;
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -040057 }
58
59 /**
Carmelo Casconee44592f2018-09-12 02:24:47 -070060 * Returns the identifier of the action profile.
61 *
62 * @return action profile identifier
63 */
64 public PiActionProfileId actionProfile() {
65 return actionProfileId;
66 }
67
68 /**
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -040069 * Returns the action associated to this member.
70 *
71 * @return action
72 */
73 public PiAction action() {
74 return action;
75 }
76
77 /**
Andrea Campanella8bcd5862017-12-11 11:34:45 +010078 * Returns the weight associated to this member.
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -040079 *
80 * @return weight
81 */
82 public int weight() {
83 return weight;
84 }
85
86 @Override
Carmelo Cascone5bc7e102018-02-18 18:27:55 -080087 public PiEntityType piEntityType() {
Carmelo Casconecb4327a2018-09-11 15:17:23 -070088 return PiEntityType.ACTION_PROFILE_MEMBER;
Carmelo Cascone5bc7e102018-02-18 18:27:55 -080089 }
90
91 @Override
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -040092 public boolean equals(Object o) {
93 if (this == o) {
94 return true;
95 }
Carmelo Casconecb4327a2018-09-11 15:17:23 -070096 if (!(o instanceof PiActionProfileMember)) {
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -040097 return false;
98 }
Carmelo Casconecb4327a2018-09-11 15:17:23 -070099 PiActionProfileMember that = (PiActionProfileMember) o;
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -0400100 return weight == that.weight &&
Carmelo Casconee44592f2018-09-12 02:24:47 -0700101 Objects.equal(actionProfileId, that.actionProfileId) &&
Carmelo Casconecb4327a2018-09-11 15:17:23 -0700102 Objects.equal(memberId, that.memberId) &&
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -0400103 Objects.equal(action, that.action);
104 }
105
106 @Override
107 public int hashCode() {
Carmelo Casconecb4327a2018-09-11 15:17:23 -0700108 return Objects.hashCode(actionProfileId, memberId, action, weight);
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -0400109 }
110
111 @Override
112 public String toString() {
113 return MoreObjects.toStringHelper(this)
Carmelo Casconee44592f2018-09-12 02:24:47 -0700114 .add("actionProfile", actionProfileId)
Carmelo Casconecb4327a2018-09-11 15:17:23 -0700115 .add("id", memberId)
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -0400116 .add("action", action)
117 .add("weight", weight)
118 .toString();
119 }
120
121 /**
Carmelo Casconecb4327a2018-09-11 15:17:23 -0700122 * Returns a new builder of action profile members.
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -0400123 *
124 * @return member builder
125 */
126 public static Builder builder() {
127 return new Builder();
128 }
129
130 /**
Carmelo Casconecb4327a2018-09-11 15:17:23 -0700131 * Builder of action profile members.
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -0400132 */
133 public static final class Builder {
134
Carmelo Casconee44592f2018-09-12 02:24:47 -0700135 private PiActionProfileId actionProfileId;
Carmelo Casconecb4327a2018-09-11 15:17:23 -0700136 private PiActionProfileMemberId id;
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -0400137 private PiAction action;
138 private int weight;
139
140 private Builder() {
141 // Hides constructor.
142 }
143
144 /**
Carmelo Casconee44592f2018-09-12 02:24:47 -0700145 * Sets the action profile identifier of this member.
146 *
147 * @param actionProfileId action profile identifier
148 * @return this
149 */
150 public Builder forActionProfile(PiActionProfileId actionProfileId) {
151 this.actionProfileId = actionProfileId;
152 return this;
153 }
154
155 /**
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -0400156 * Sets the identifier of this member.
157 *
158 * @param id member identifier
159 * @return this
160 */
Carmelo Casconecb4327a2018-09-11 15:17:23 -0700161 public Builder withId(PiActionProfileMemberId id) {
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -0400162 this.id = id;
163 return this;
164 }
165
166 /**
167 * Sets the action of this member.
168 *
169 * @param action action
170 * @return this
171 */
172 public Builder withAction(PiAction action) {
173 this.action = action;
174 return this;
175 }
176
177 /**
Andrea Campanella8bcd5862017-12-11 11:34:45 +0100178 * Sets the weight of this member.
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -0400179 * <p>
180 * Default value is 0.
181 *
182 * @param weight weight
183 * @return this
184 */
185 public Builder withWeight(int weight) {
186 this.weight = weight;
187 return this;
188 }
189
190 /**
Carmelo Casconecb4327a2018-09-11 15:17:23 -0700191 * Creates a new action profile member.
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -0400192 *
Carmelo Casconecb4327a2018-09-11 15:17:23 -0700193 * @return action profile member
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -0400194 */
Carmelo Casconecb4327a2018-09-11 15:17:23 -0700195 public PiActionProfileMember build() {
Carmelo Casconee44592f2018-09-12 02:24:47 -0700196 checkNotNull(actionProfileId);
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -0400197 checkNotNull(id);
198 checkNotNull(action);
Carmelo Casconecb4327a2018-09-11 15:17:23 -0700199 return new PiActionProfileMember(actionProfileId, id, action, weight);
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -0400200 }
201 }
202}