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 | import com.google.common.collect.ImmutableSet; |
| 23 | import com.google.common.collect.Maps; |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 24 | import org.onosproject.net.pi.model.PiActionGroupType; |
| 25 | import org.onosproject.net.pi.model.PiActionProfileId; |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 26 | |
| 27 | import java.util.Collection; |
| 28 | import java.util.Map; |
| 29 | |
| 30 | import static com.google.common.base.Preconditions.checkArgument; |
| 31 | import static com.google.common.base.Preconditions.checkNotNull; |
| 32 | |
| 33 | /** |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 34 | * Instance of an action group of a protocol-independent pipeline. |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 35 | */ |
| 36 | @Beta |
Carmelo Cascone | 94a659f | 2017-11-21 00:06:11 -0800 | [diff] [blame^] | 37 | public final class PiActionGroup implements PiEntity { |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 38 | |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 39 | private final PiActionGroupId id; |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 40 | private final PiActionGroupType type; |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 41 | private final ImmutableSet<PiActionGroupMember> members; |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 42 | private final PiActionProfileId piActionProfileId; |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 43 | |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 44 | private PiActionGroup(PiActionGroupId id, PiActionGroupType type, |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 45 | ImmutableSet<PiActionGroupMember> members, |
| 46 | PiActionProfileId piActionProfileId) { |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 47 | this.id = id; |
| 48 | this.type = type; |
| 49 | this.members = members; |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 50 | this.piActionProfileId = piActionProfileId; |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Returns the identifier of this action group. |
| 55 | * |
| 56 | * @return action group identifier |
| 57 | */ |
| 58 | public PiActionGroupId id() { |
| 59 | return id; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Returns the type of this action group. |
| 64 | * |
| 65 | * @return action group type |
| 66 | */ |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 67 | public PiActionGroupType type() { |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 68 | return type; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Returns the members of this action group. |
| 73 | * |
| 74 | * @return collection of action members. |
| 75 | */ |
| 76 | public Collection<PiActionGroupMember> members() { |
| 77 | return members; |
| 78 | } |
| 79 | |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 80 | /** |
| 81 | * Gets identifier of the action profile. |
| 82 | * |
| 83 | * @return action profile id |
| 84 | */ |
| 85 | public PiActionProfileId actionProfileId() { |
| 86 | return piActionProfileId; |
| 87 | } |
| 88 | |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 89 | @Override |
| 90 | public boolean equals(Object o) { |
| 91 | if (this == o) { |
| 92 | return true; |
| 93 | } |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 94 | if (o == null || !(o instanceof PiActionGroup)) { |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 95 | return false; |
| 96 | } |
| 97 | PiActionGroup that = (PiActionGroup) o; |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 98 | return Objects.equal(id, that.id) && |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 99 | Objects.equal(type, that.type) && |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 100 | Objects.equal(members, that.members) && |
| 101 | Objects.equal(piActionProfileId, that.piActionProfileId); |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | @Override |
| 105 | public int hashCode() { |
| 106 | return Objects.hashCode(id, type, members); |
| 107 | } |
| 108 | |
| 109 | @Override |
| 110 | public String toString() { |
| 111 | return MoreObjects.toStringHelper(this) |
| 112 | .add("groupId", id) |
| 113 | .add("type", type) |
| 114 | .add("members", members) |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 115 | .add("piActionProfileId", piActionProfileId) |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 116 | .toString(); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Returns a new builder of action groups. |
| 121 | * |
| 122 | * @return action group builder |
| 123 | */ |
| 124 | public static Builder builder() { |
| 125 | return new Builder(); |
| 126 | } |
| 127 | |
Carmelo Cascone | 94a659f | 2017-11-21 00:06:11 -0800 | [diff] [blame^] | 128 | @Override |
| 129 | public PiEntityType piEntityType() { |
| 130 | return PiEntityType.GROUP; |
| 131 | } |
| 132 | |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 133 | /** |
| 134 | * Builder of action groups. |
| 135 | */ |
| 136 | public static final class Builder { |
| 137 | |
| 138 | private PiActionGroupId id; |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 139 | private PiActionGroupType type; |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 140 | private Map<PiActionGroupMemberId, PiActionGroupMember> members = Maps.newHashMap(); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 141 | private PiActionProfileId piActionProfileId; |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 142 | |
| 143 | private Builder() { |
| 144 | // hides constructor. |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Sets the identifier of this action group. |
| 149 | * |
| 150 | * @param id action group identifier |
| 151 | * @return this |
| 152 | */ |
| 153 | public Builder withId(PiActionGroupId id) { |
| 154 | this.id = id; |
| 155 | return this; |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Sets the type of this action group. |
| 160 | * |
| 161 | * @param type action group type |
| 162 | * @return this |
| 163 | */ |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 164 | public Builder withType(PiActionGroupType type) { |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 165 | this.type = type; |
| 166 | return this; |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Adds one member to this action group. |
| 171 | * |
| 172 | * @param member action group member |
| 173 | * @return this |
| 174 | */ |
| 175 | public Builder addMember(PiActionGroupMember member) { |
| 176 | members.put(member.id(), member); |
| 177 | return this; |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Adds many members to this action group. |
| 182 | * |
| 183 | * @param members action group members |
| 184 | * @return this |
| 185 | */ |
| 186 | public Builder addMembers(Collection<PiActionGroupMember> members) { |
| 187 | members.forEach(this::addMember); |
| 188 | return this; |
| 189 | } |
| 190 | |
| 191 | /** |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 192 | * Sets the identifier of the action profile. |
| 193 | * |
| 194 | * @param piActionProfileId the identifier of the action profile |
| 195 | * @return this |
| 196 | */ |
| 197 | public Builder withActionProfileId(PiActionProfileId piActionProfileId) { |
| 198 | this.piActionProfileId = piActionProfileId; |
| 199 | return this; |
| 200 | } |
| 201 | |
| 202 | /** |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 203 | * Creates a new action group. |
| 204 | * |
| 205 | * @return action group |
| 206 | */ |
| 207 | public PiActionGroup build() { |
| 208 | checkNotNull(id); |
| 209 | checkNotNull(type); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 210 | checkArgument(!members.isEmpty(), "Members cannot be empty"); |
| 211 | checkNotNull(piActionProfileId); |
| 212 | return new PiActionGroup(id, type, |
| 213 | ImmutableSet.copyOf(members.values()), |
| 214 | piActionProfileId); |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 215 | } |
| 216 | } |
| 217 | } |