Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 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; |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 23 | import org.onosproject.net.DeviceId; |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 24 | |
| 25 | import java.util.Collection; |
| 26 | import java.util.Set; |
| 27 | |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 28 | import static com.google.common.base.Preconditions.checkNotNull; |
| 29 | |
| 30 | /** |
| 31 | * Representation of multicast group entry of a protocol-independent packet |
| 32 | * replication engine (PRE). |
| 33 | */ |
| 34 | @Beta |
| 35 | public final class PiMulticastGroupEntry implements PiPreEntry { |
| 36 | |
Carmelo Cascone | 03ae0ac | 2018-10-11 08:31:59 -0700 | [diff] [blame] | 37 | private final int groupId; |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 38 | private final Set<PiPreReplica> replicas; |
| 39 | |
Carmelo Cascone | 03ae0ac | 2018-10-11 08:31:59 -0700 | [diff] [blame] | 40 | private PiMulticastGroupEntry(int groupId, Set<PiPreReplica> replicas) { |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 41 | this.groupId = groupId; |
| 42 | this.replicas = replicas; |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Returns the identifier of this multicast group, unique in the scope of a |
| 47 | * PRE instance. |
| 48 | * |
| 49 | * @return group entry ID |
| 50 | */ |
Carmelo Cascone | 03ae0ac | 2018-10-11 08:31:59 -0700 | [diff] [blame] | 51 | public int groupId() { |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 52 | return groupId; |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Returns the packet replicas provided by this multicast group. |
| 57 | * |
| 58 | * @return packet replicas |
| 59 | */ |
| 60 | public Set<PiPreReplica> replicas() { |
| 61 | return replicas; |
| 62 | } |
| 63 | |
| 64 | @Override |
| 65 | public PiPreEntryType preEntryType() { |
| 66 | return PiPreEntryType.MULTICAST_GROUP; |
| 67 | } |
| 68 | |
| 69 | @Override |
| 70 | public PiEntityType piEntityType() { |
Carmelo Cascone | 9db4d5c | 2019-04-16 17:36:33 -0700 | [diff] [blame] | 71 | return PiEntityType.PRE_ENTRY; |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | @Override |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 75 | public PiMulticastGroupEntryHandle handle(DeviceId deviceId) { |
| 76 | return PiMulticastGroupEntryHandle.of(deviceId, this); |
| 77 | } |
| 78 | |
| 79 | @Override |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 80 | public int hashCode() { |
| 81 | return Objects.hashCode(groupId, replicas); |
| 82 | } |
| 83 | |
| 84 | @Override |
| 85 | public boolean equals(Object obj) { |
| 86 | if (this == obj) { |
| 87 | return true; |
| 88 | } |
| 89 | if (obj == null || getClass() != obj.getClass()) { |
| 90 | return false; |
| 91 | } |
| 92 | final PiMulticastGroupEntry other = (PiMulticastGroupEntry) obj; |
| 93 | return Objects.equal(this.groupId, other.groupId) |
| 94 | && Objects.equal(this.replicas, other.replicas); |
| 95 | } |
| 96 | |
| 97 | @Override |
| 98 | public String toString() { |
| 99 | return MoreObjects.toStringHelper(this) |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 100 | .add("groupId", "0x" + Integer.toHexString(groupId)) |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 101 | .add("replicas", replicas) |
| 102 | .toString(); |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Returns a new builder of multicast group entries. |
| 107 | * |
| 108 | * @return builder |
| 109 | */ |
| 110 | public static Builder builder() { |
| 111 | return new Builder(); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Builder of PI multicast group entries. |
| 116 | */ |
| 117 | public static final class Builder { |
| 118 | |
Carmelo Cascone | 03ae0ac | 2018-10-11 08:31:59 -0700 | [diff] [blame] | 119 | private Integer groupId; |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 120 | private ImmutableSet.Builder<PiPreReplica> replicaSetBuilder = ImmutableSet.builder(); |
| 121 | |
| 122 | private Builder() { |
| 123 | // Hide constructor. |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Sets the identifier of this multicast group. |
| 128 | * |
| 129 | * @param groupId group ID |
| 130 | * @return this |
| 131 | */ |
Carmelo Cascone | 03ae0ac | 2018-10-11 08:31:59 -0700 | [diff] [blame] | 132 | public Builder withGroupId(int groupId) { |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 133 | this.groupId = groupId; |
| 134 | return this; |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Adds the given packet replica to this multicast group. |
| 139 | * |
| 140 | * @param replica packet replica |
| 141 | * @return this |
| 142 | */ |
| 143 | public Builder addReplica(PiPreReplica replica) { |
| 144 | checkNotNull(replica); |
| 145 | replicaSetBuilder.add(replica); |
| 146 | return this; |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Adds the given packet replicas to this multicast group. |
| 151 | * |
| 152 | * @param replicas packet replicas |
| 153 | * @return this |
| 154 | */ |
| 155 | public Builder addReplicas(Collection<PiPreReplica> replicas) { |
| 156 | checkNotNull(replicas); |
| 157 | replicaSetBuilder.addAll(replicas); |
| 158 | return this; |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Returns a new multicast group entry. |
| 163 | * |
| 164 | * @return multicast group entry |
| 165 | */ |
| 166 | public PiMulticastGroupEntry build() { |
| 167 | checkNotNull(groupId, "Multicast group ID must be set"); |
| 168 | final ImmutableSet<PiPreReplica> replicas = replicaSetBuilder.build(); |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 169 | return new PiMulticastGroupEntry(groupId, replicas); |
| 170 | } |
| 171 | } |
| 172 | } |