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