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; |
| 23 | |
| 24 | import java.util.Collection; |
| 25 | import java.util.Set; |
| 26 | |
| 27 | import static com.google.common.base.Preconditions.checkArgument; |
| 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 | |
| 37 | private final long groupId; |
| 38 | private final Set<PiPreReplica> replicas; |
| 39 | |
| 40 | private PiMulticastGroupEntry(long groupId, Set<PiPreReplica> replicas) { |
| 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 | */ |
| 51 | public long groupId() { |
| 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() { |
| 71 | return PiEntityType.PRE_MULTICAST_GROUP_ENTRY; |
| 72 | } |
| 73 | |
| 74 | @Override |
| 75 | public int hashCode() { |
| 76 | return Objects.hashCode(groupId, replicas); |
| 77 | } |
| 78 | |
| 79 | @Override |
| 80 | public boolean equals(Object obj) { |
| 81 | if (this == obj) { |
| 82 | return true; |
| 83 | } |
| 84 | if (obj == null || getClass() != obj.getClass()) { |
| 85 | return false; |
| 86 | } |
| 87 | final PiMulticastGroupEntry other = (PiMulticastGroupEntry) obj; |
| 88 | return Objects.equal(this.groupId, other.groupId) |
| 89 | && Objects.equal(this.replicas, other.replicas); |
| 90 | } |
| 91 | |
| 92 | @Override |
| 93 | public String toString() { |
| 94 | return MoreObjects.toStringHelper(this) |
| 95 | .add("groupId", groupId) |
| 96 | .add("replicas", replicas) |
| 97 | .toString(); |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Returns a new builder of multicast group entries. |
| 102 | * |
| 103 | * @return builder |
| 104 | */ |
| 105 | public static Builder builder() { |
| 106 | return new Builder(); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Builder of PI multicast group entries. |
| 111 | */ |
| 112 | public static final class Builder { |
| 113 | |
| 114 | private Long groupId; |
| 115 | private ImmutableSet.Builder<PiPreReplica> replicaSetBuilder = ImmutableSet.builder(); |
| 116 | |
| 117 | private Builder() { |
| 118 | // Hide constructor. |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Sets the identifier of this multicast group. |
| 123 | * |
| 124 | * @param groupId group ID |
| 125 | * @return this |
| 126 | */ |
| 127 | public Builder withGroupId(long groupId) { |
| 128 | this.groupId = groupId; |
| 129 | return this; |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Adds the given packet replica to this multicast group. |
| 134 | * |
| 135 | * @param replica packet replica |
| 136 | * @return this |
| 137 | */ |
| 138 | public Builder addReplica(PiPreReplica replica) { |
| 139 | checkNotNull(replica); |
| 140 | replicaSetBuilder.add(replica); |
| 141 | return this; |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Adds the given packet replicas to this multicast group. |
| 146 | * |
| 147 | * @param replicas packet replicas |
| 148 | * @return this |
| 149 | */ |
| 150 | public Builder addReplicas(Collection<PiPreReplica> replicas) { |
| 151 | checkNotNull(replicas); |
| 152 | replicaSetBuilder.addAll(replicas); |
| 153 | return this; |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Returns a new multicast group entry. |
| 158 | * |
| 159 | * @return multicast group entry |
| 160 | */ |
| 161 | public PiMulticastGroupEntry build() { |
| 162 | checkNotNull(groupId, "Multicast group ID must be set"); |
| 163 | final ImmutableSet<PiPreReplica> replicas = replicaSetBuilder.build(); |
| 164 | checkArgument(!replicas.isEmpty(), "At least one replica must be defined"); |
| 165 | return new PiMulticastGroupEntry(groupId, replicas); |
| 166 | } |
| 167 | } |
| 168 | } |