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 | e44592f | 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 | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 27 | * Instance of a member of an action group in a protocol-independent pipeline. |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 28 | */ |
| 29 | @Beta |
Carmelo Cascone | 5bc7e10 | 2018-02-18 18:27:55 -0800 | [diff] [blame] | 30 | public final class PiActionGroupMember implements PiEntity { |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 31 | |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 32 | private final PiActionProfileId actionProfileId; |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 33 | private final PiActionGroupMemberId id; |
| 34 | private final PiAction action; |
Carmelo Cascone | e44592f | 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 | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 40 | private PiActionGroupMember( |
| 41 | PiActionProfileId actionProfileId, PiActionGroupMemberId id, |
| 42 | PiAction action, int weight) { |
| 43 | this.actionProfileId = actionProfileId; |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 44 | 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 Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 59 | * 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 Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 68 | * Returns the action associated to this member. |
| 69 | * |
| 70 | * @return action |
| 71 | */ |
| 72 | public PiAction action() { |
| 73 | return action; |
| 74 | } |
| 75 | |
| 76 | /** |
Andrea Campanella | 8bcd586 | 2017-12-11 11:34:45 +0100 | [diff] [blame] | 77 | * Returns the weight associated to this member. |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 78 | * |
| 79 | * @return weight |
| 80 | */ |
| 81 | public int weight() { |
| 82 | return weight; |
| 83 | } |
| 84 | |
| 85 | @Override |
Carmelo Cascone | 5bc7e10 | 2018-02-18 18:27:55 -0800 | [diff] [blame] | 86 | public PiEntityType piEntityType() { |
| 87 | return PiEntityType.GROUP_MEMBER; |
| 88 | } |
| 89 | |
| 90 | @Override |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 91 | 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 Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 100 | Objects.equal(actionProfileId, that.actionProfileId) && |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 101 | Objects.equal(id, that.id) && |
| 102 | Objects.equal(action, that.action); |
| 103 | } |
| 104 | |
| 105 | @Override |
| 106 | public int hashCode() { |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 107 | return Objects.hashCode(actionProfileId, id, action, weight); |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | @Override |
| 111 | public String toString() { |
| 112 | return MoreObjects.toStringHelper(this) |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 113 | .add("actionProfile", actionProfileId) |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 114 | .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 Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 134 | private PiActionProfileId actionProfileId; |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 135 | private PiActionGroupMemberId id; |
| 136 | private PiAction action; |
| 137 | private int weight; |
| 138 | |
| 139 | private Builder() { |
| 140 | // Hides constructor. |
| 141 | } |
| 142 | |
| 143 | /** |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 144 | * 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 Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 155 | * 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 Campanella | 8bcd586 | 2017-12-11 11:34:45 +0100 | [diff] [blame] | 177 | * Sets the weight of this member. |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 178 | * <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 Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 195 | checkNotNull(actionProfileId); |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 196 | checkNotNull(id); |
| 197 | checkNotNull(action); |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 198 | return new PiActionGroupMember(actionProfileId, id, action, weight); |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 199 | } |
| 200 | } |
| 201 | } |