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 |
| 37 | public final class PiActionGroup { |
| 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 | |
| 128 | /** |
| 129 | * Builder of action groups. |
| 130 | */ |
| 131 | public static final class Builder { |
| 132 | |
| 133 | private PiActionGroupId id; |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame^] | 134 | private PiActionGroupType type; |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 135 | private Map<PiActionGroupMemberId, PiActionGroupMember> members = Maps.newHashMap(); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 136 | private PiActionProfileId piActionProfileId; |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 137 | |
| 138 | private Builder() { |
| 139 | // hides constructor. |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Sets the identifier of this action group. |
| 144 | * |
| 145 | * @param id action group identifier |
| 146 | * @return this |
| 147 | */ |
| 148 | public Builder withId(PiActionGroupId id) { |
| 149 | this.id = id; |
| 150 | return this; |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Sets the type of this action group. |
| 155 | * |
| 156 | * @param type action group type |
| 157 | * @return this |
| 158 | */ |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame^] | 159 | public Builder withType(PiActionGroupType type) { |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 160 | this.type = type; |
| 161 | return this; |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Adds one member to this action group. |
| 166 | * |
| 167 | * @param member action group member |
| 168 | * @return this |
| 169 | */ |
| 170 | public Builder addMember(PiActionGroupMember member) { |
| 171 | members.put(member.id(), member); |
| 172 | return this; |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Adds many members to this action group. |
| 177 | * |
| 178 | * @param members action group members |
| 179 | * @return this |
| 180 | */ |
| 181 | public Builder addMembers(Collection<PiActionGroupMember> members) { |
| 182 | members.forEach(this::addMember); |
| 183 | return this; |
| 184 | } |
| 185 | |
| 186 | /** |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 187 | * Sets the identifier of the action profile. |
| 188 | * |
| 189 | * @param piActionProfileId the identifier of the action profile |
| 190 | * @return this |
| 191 | */ |
| 192 | public Builder withActionProfileId(PiActionProfileId piActionProfileId) { |
| 193 | this.piActionProfileId = piActionProfileId; |
| 194 | return this; |
| 195 | } |
| 196 | |
| 197 | /** |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 198 | * Creates a new action group. |
| 199 | * |
| 200 | * @return action group |
| 201 | */ |
| 202 | public PiActionGroup build() { |
| 203 | checkNotNull(id); |
| 204 | checkNotNull(type); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 205 | checkArgument(!members.isEmpty(), "Members cannot be empty"); |
| 206 | checkNotNull(piActionProfileId); |
| 207 | return new PiActionGroup(id, type, |
| 208 | ImmutableSet.copyOf(members.values()), |
| 209 | piActionProfileId); |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 210 | } |
| 211 | } |
| 212 | } |