Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 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 | */ |
| 16 | |
| 17 | package org.onosproject.net.pi.runtime; |
| 18 | |
| 19 | import com.google.common.annotations.Beta; |
| 20 | import com.google.common.base.MoreObjects; |
| 21 | import com.google.common.base.Objects; |
Carmelo Cascone | e3ba922 | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 22 | import org.onosproject.net.pi.model.PiActionProfileId; |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 23 | |
| 24 | import static com.google.common.base.Preconditions.checkNotNull; |
| 25 | |
| 26 | /** |
Carmelo Cascone | f2b7bfe | 2018-09-11 15:17:23 -0700 | [diff] [blame] | 27 | * Instance of a member of an action profile in a protocol-independent pipeline. |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 28 | */ |
| 29 | @Beta |
Carmelo Cascone | f2b7bfe | 2018-09-11 15:17:23 -0700 | [diff] [blame] | 30 | public final class PiActionProfileMember implements PiEntity { |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 31 | |
Carmelo Cascone | e3ba922 | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 32 | private final PiActionProfileId actionProfileId; |
Carmelo Cascone | f2b7bfe | 2018-09-11 15:17:23 -0700 | [diff] [blame] | 33 | private final PiActionProfileMemberId memberId; |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 34 | private final PiAction action; |
Carmelo Cascone | e3ba922 | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 35 | // 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 Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 38 | private final int weight; |
| 39 | |
Carmelo Cascone | f2b7bfe | 2018-09-11 15:17:23 -0700 | [diff] [blame] | 40 | private PiActionProfileMember(PiActionProfileId actionProfileId, |
| 41 | PiActionProfileMemberId memberId, |
| 42 | PiAction action, |
| 43 | int weight) { |
Carmelo Cascone | e3ba922 | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 44 | this.actionProfileId = actionProfileId; |
Carmelo Cascone | f2b7bfe | 2018-09-11 15:17:23 -0700 | [diff] [blame] | 45 | this.memberId = memberId; |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 46 | this.action = action; |
| 47 | this.weight = weight; |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Returns the identifier of this member. |
| 52 | * |
| 53 | * @return member identifier |
| 54 | */ |
Carmelo Cascone | f2b7bfe | 2018-09-11 15:17:23 -0700 | [diff] [blame] | 55 | public PiActionProfileMemberId id() { |
| 56 | return memberId; |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | /** |
Carmelo Cascone | e3ba922 | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 60 | * 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 Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 69 | * Returns the action associated to this member. |
| 70 | * |
| 71 | * @return action |
| 72 | */ |
| 73 | public PiAction action() { |
| 74 | return action; |
| 75 | } |
| 76 | |
| 77 | /** |
Andrea Campanella | 8bcd586 | 2017-12-11 11:34:45 +0100 | [diff] [blame] | 78 | * Returns the weight associated to this member. |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 79 | * |
| 80 | * @return weight |
| 81 | */ |
| 82 | public int weight() { |
| 83 | return weight; |
| 84 | } |
| 85 | |
| 86 | @Override |
Carmelo Cascone | 5bc7e10 | 2018-02-18 18:27:55 -0800 | [diff] [blame] | 87 | public PiEntityType piEntityType() { |
Carmelo Cascone | f2b7bfe | 2018-09-11 15:17:23 -0700 | [diff] [blame] | 88 | return PiEntityType.ACTION_PROFILE_MEMBER; |
Carmelo Cascone | 5bc7e10 | 2018-02-18 18:27:55 -0800 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | @Override |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 92 | public boolean equals(Object o) { |
| 93 | if (this == o) { |
| 94 | return true; |
| 95 | } |
Carmelo Cascone | f2b7bfe | 2018-09-11 15:17:23 -0700 | [diff] [blame] | 96 | if (!(o instanceof PiActionProfileMember)) { |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 97 | return false; |
| 98 | } |
Carmelo Cascone | f2b7bfe | 2018-09-11 15:17:23 -0700 | [diff] [blame] | 99 | PiActionProfileMember that = (PiActionProfileMember) o; |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 100 | return weight == that.weight && |
Carmelo Cascone | e3ba922 | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 101 | Objects.equal(actionProfileId, that.actionProfileId) && |
Carmelo Cascone | f2b7bfe | 2018-09-11 15:17:23 -0700 | [diff] [blame] | 102 | Objects.equal(memberId, that.memberId) && |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 103 | Objects.equal(action, that.action); |
| 104 | } |
| 105 | |
| 106 | @Override |
| 107 | public int hashCode() { |
Carmelo Cascone | f2b7bfe | 2018-09-11 15:17:23 -0700 | [diff] [blame] | 108 | return Objects.hashCode(actionProfileId, memberId, action, weight); |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | @Override |
| 112 | public String toString() { |
| 113 | return MoreObjects.toStringHelper(this) |
Carmelo Cascone | e3ba922 | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 114 | .add("actionProfile", actionProfileId) |
Carmelo Cascone | f2b7bfe | 2018-09-11 15:17:23 -0700 | [diff] [blame] | 115 | .add("id", memberId) |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 116 | .add("action", action) |
| 117 | .add("weight", weight) |
| 118 | .toString(); |
| 119 | } |
| 120 | |
| 121 | /** |
Carmelo Cascone | f2b7bfe | 2018-09-11 15:17:23 -0700 | [diff] [blame] | 122 | * Returns a new builder of action profile members. |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 123 | * |
| 124 | * @return member builder |
| 125 | */ |
| 126 | public static Builder builder() { |
| 127 | return new Builder(); |
| 128 | } |
| 129 | |
| 130 | /** |
Carmelo Cascone | f2b7bfe | 2018-09-11 15:17:23 -0700 | [diff] [blame] | 131 | * Builder of action profile members. |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 132 | */ |
| 133 | public static final class Builder { |
| 134 | |
Carmelo Cascone | e3ba922 | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 135 | private PiActionProfileId actionProfileId; |
Carmelo Cascone | f2b7bfe | 2018-09-11 15:17:23 -0700 | [diff] [blame] | 136 | private PiActionProfileMemberId id; |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 137 | private PiAction action; |
| 138 | private int weight; |
| 139 | |
| 140 | private Builder() { |
| 141 | // Hides constructor. |
| 142 | } |
| 143 | |
| 144 | /** |
Carmelo Cascone | e3ba922 | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 145 | * 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 Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 156 | * Sets the identifier of this member. |
| 157 | * |
| 158 | * @param id member identifier |
| 159 | * @return this |
| 160 | */ |
Carmelo Cascone | f2b7bfe | 2018-09-11 15:17:23 -0700 | [diff] [blame] | 161 | public Builder withId(PiActionProfileMemberId id) { |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 162 | 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 Campanella | 8bcd586 | 2017-12-11 11:34:45 +0100 | [diff] [blame] | 178 | * Sets the weight of this member. |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 179 | * <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 Cascone | f2b7bfe | 2018-09-11 15:17:23 -0700 | [diff] [blame] | 191 | * Creates a new action profile member. |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 192 | * |
Carmelo Cascone | f2b7bfe | 2018-09-11 15:17:23 -0700 | [diff] [blame] | 193 | * @return action profile member |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 194 | */ |
Carmelo Cascone | f2b7bfe | 2018-09-11 15:17:23 -0700 | [diff] [blame] | 195 | public PiActionProfileMember build() { |
Carmelo Cascone | e3ba922 | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 196 | checkNotNull(actionProfileId); |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 197 | checkNotNull(id); |
| 198 | checkNotNull(action); |
Carmelo Cascone | f2b7bfe | 2018-09-11 15:17:23 -0700 | [diff] [blame] | 199 | return new PiActionProfileMember(actionProfileId, id, action, weight); |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 200 | } |
| 201 | } |
| 202 | } |