blob: 7bb4c2d0f59739897262cfc72d65d5d47a2e2933 [file] [log] [blame]
Carmelo Cascone58136812018-07-19 03:40:16 +02001/*
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
17package org.onosproject.net.pi.runtime;
18
19import com.google.common.annotations.Beta;
20import com.google.common.base.MoreObjects;
21import com.google.common.base.Objects;
22import com.google.common.collect.ImmutableSet;
23
24import java.util.Collection;
25import java.util.Set;
26
27import static com.google.common.base.Preconditions.checkArgument;
28import 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
35public final class PiMulticastGroupEntry implements PiPreEntry {
36
Carmelo Cascone03ae0ac2018-10-11 08:31:59 -070037 private final int groupId;
Carmelo Cascone58136812018-07-19 03:40:16 +020038 private final Set<PiPreReplica> replicas;
39
Carmelo Cascone03ae0ac2018-10-11 08:31:59 -070040 private PiMulticastGroupEntry(int groupId, Set<PiPreReplica> replicas) {
Carmelo Cascone58136812018-07-19 03:40:16 +020041 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 Cascone03ae0ac2018-10-11 08:31:59 -070051 public int groupId() {
Carmelo Cascone58136812018-07-19 03:40:16 +020052 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)
Carmelo Casconeb5324e72018-11-25 02:26:32 -080095 .add("groupId", "0x" + Integer.toHexString(groupId))
Carmelo Cascone58136812018-07-19 03:40:16 +020096 .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
Carmelo Cascone03ae0ac2018-10-11 08:31:59 -0700114 private Integer groupId;
Carmelo Cascone58136812018-07-19 03:40:16 +0200115 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 */
Carmelo Cascone03ae0ac2018-10-11 08:31:59 -0700127 public Builder withGroupId(int groupId) {
Carmelo Cascone58136812018-07-19 03:40:16 +0200128 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}