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; |
| 22 | |
| 23 | import static com.google.common.base.Preconditions.checkNotNull; |
| 24 | |
| 25 | /** |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 26 | * 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] | 27 | */ |
| 28 | @Beta |
Carmelo Cascone | 5bc7e10 | 2018-02-18 18:27:55 -0800 | [diff] [blame] | 29 | public final class PiActionGroupMember implements PiEntity { |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 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 | /** |
Andrea Campanella | 8bcd586 | 2017-12-11 11:34:45 +0100 | [diff] [blame] | 60 | * Returns the weight associated to this member. |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 61 | * |
| 62 | * @return weight |
| 63 | */ |
| 64 | public int weight() { |
| 65 | return weight; |
| 66 | } |
| 67 | |
| 68 | @Override |
Carmelo Cascone | 5bc7e10 | 2018-02-18 18:27:55 -0800 | [diff] [blame] | 69 | public PiEntityType piEntityType() { |
| 70 | return PiEntityType.GROUP_MEMBER; |
| 71 | } |
| 72 | |
| 73 | @Override |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 74 | public boolean equals(Object o) { |
| 75 | if (this == o) { |
| 76 | return true; |
| 77 | } |
| 78 | if (!(o instanceof PiActionGroupMember)) { |
| 79 | return false; |
| 80 | } |
| 81 | PiActionGroupMember that = (PiActionGroupMember) o; |
| 82 | return weight == that.weight && |
| 83 | Objects.equal(id, that.id) && |
| 84 | Objects.equal(action, that.action); |
| 85 | } |
| 86 | |
| 87 | @Override |
| 88 | public int hashCode() { |
| 89 | return Objects.hashCode(id, action, weight); |
| 90 | } |
| 91 | |
| 92 | @Override |
| 93 | public String toString() { |
| 94 | return MoreObjects.toStringHelper(this) |
| 95 | .add("id", id) |
| 96 | .add("action", action) |
| 97 | .add("weight", weight) |
| 98 | .toString(); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Returns a new builder of action group members. |
| 103 | * |
| 104 | * @return member builder |
| 105 | */ |
| 106 | public static Builder builder() { |
| 107 | return new Builder(); |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Builder of action group members. |
| 112 | */ |
| 113 | public static final class Builder { |
| 114 | |
| 115 | private PiActionGroupMemberId id; |
| 116 | private PiAction action; |
| 117 | private int weight; |
| 118 | |
| 119 | private Builder() { |
| 120 | // Hides constructor. |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Sets the identifier of this member. |
| 125 | * |
| 126 | * @param id member identifier |
| 127 | * @return this |
| 128 | */ |
| 129 | public Builder withId(PiActionGroupMemberId id) { |
| 130 | this.id = id; |
| 131 | return this; |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Sets the action of this member. |
| 136 | * |
| 137 | * @param action action |
| 138 | * @return this |
| 139 | */ |
| 140 | public Builder withAction(PiAction action) { |
| 141 | this.action = action; |
| 142 | return this; |
| 143 | } |
| 144 | |
| 145 | /** |
Andrea Campanella | 8bcd586 | 2017-12-11 11:34:45 +0100 | [diff] [blame] | 146 | * Sets the weight of this member. |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 147 | * <p> |
| 148 | * Default value is 0. |
| 149 | * |
| 150 | * @param weight weight |
| 151 | * @return this |
| 152 | */ |
| 153 | public Builder withWeight(int weight) { |
| 154 | this.weight = weight; |
| 155 | return this; |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Creates a new action group member. |
| 160 | * |
| 161 | * @return action group member |
| 162 | */ |
| 163 | public PiActionGroupMember build() { |
| 164 | checkNotNull(id); |
| 165 | checkNotNull(action); |
| 166 | return new PiActionGroupMember(id, action, weight); |
| 167 | } |
| 168 | } |
| 169 | } |