blob: 73898f5d6c2dabd664b7b94933a96346bf06198e [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;
Carmelo Cascone4c289b72019-01-22 15:30:45 -080023import org.onosproject.net.DeviceId;
Carmelo Cascone58136812018-07-19 03:40:16 +020024
25import java.util.Collection;
26import java.util.Set;
27
Carmelo Cascone58136812018-07-19 03:40:16 +020028import 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() {
Carmelo Cascone9db4d5c2019-04-16 17:36:33 -070071 return PiEntityType.PRE_ENTRY;
Carmelo Cascone58136812018-07-19 03:40:16 +020072 }
73
74 @Override
Carmelo Cascone4c289b72019-01-22 15:30:45 -080075 public PiMulticastGroupEntryHandle handle(DeviceId deviceId) {
76 return PiMulticastGroupEntryHandle.of(deviceId, this);
77 }
78
79 @Override
Carmelo Cascone58136812018-07-19 03:40:16 +020080 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 Casconeb5324e72018-11-25 02:26:32 -0800100 .add("groupId", "0x" + Integer.toHexString(groupId))
Carmelo Cascone58136812018-07-19 03:40:16 +0200101 .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 Cascone03ae0ac2018-10-11 08:31:59 -0700119 private Integer groupId;
Carmelo Cascone58136812018-07-19 03:40:16 +0200120 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 Cascone03ae0ac2018-10-11 08:31:59 -0700132 public Builder withGroupId(int groupId) {
Carmelo Cascone58136812018-07-19 03:40:16 +0200133 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 Cascone58136812018-07-19 03:40:16 +0200169 return new PiMulticastGroupEntry(groupId, replicas);
170 }
171 }
172}