blob: 41b93f60a7b5592086d677989332835d8e0fce95 [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
Carmelo Cascone58136812018-07-19 03:40:16 +020027import static com.google.common.base.Preconditions.checkNotNull;
28
29/**
30 * Representation of multicast group entry of a protocol-independent packet
31 * replication engine (PRE).
32 */
33@Beta
34public final class PiMulticastGroupEntry implements PiPreEntry {
35
Carmelo Cascone03ae0ac2018-10-11 08:31:59 -070036 private final int groupId;
Carmelo Cascone58136812018-07-19 03:40:16 +020037 private final Set<PiPreReplica> replicas;
38
Carmelo Cascone03ae0ac2018-10-11 08:31:59 -070039 private PiMulticastGroupEntry(int groupId, Set<PiPreReplica> replicas) {
Carmelo Cascone58136812018-07-19 03:40:16 +020040 this.groupId = groupId;
41 this.replicas = replicas;
42 }
43
44 /**
45 * Returns the identifier of this multicast group, unique in the scope of a
46 * PRE instance.
47 *
48 * @return group entry ID
49 */
Carmelo Cascone03ae0ac2018-10-11 08:31:59 -070050 public int groupId() {
Carmelo Cascone58136812018-07-19 03:40:16 +020051 return groupId;
52 }
53
54 /**
55 * Returns the packet replicas provided by this multicast group.
56 *
57 * @return packet replicas
58 */
59 public Set<PiPreReplica> replicas() {
60 return replicas;
61 }
62
63 @Override
64 public PiPreEntryType preEntryType() {
65 return PiPreEntryType.MULTICAST_GROUP;
66 }
67
68 @Override
69 public PiEntityType piEntityType() {
70 return PiEntityType.PRE_MULTICAST_GROUP_ENTRY;
71 }
72
73 @Override
74 public int hashCode() {
75 return Objects.hashCode(groupId, replicas);
76 }
77
78 @Override
79 public boolean equals(Object obj) {
80 if (this == obj) {
81 return true;
82 }
83 if (obj == null || getClass() != obj.getClass()) {
84 return false;
85 }
86 final PiMulticastGroupEntry other = (PiMulticastGroupEntry) obj;
87 return Objects.equal(this.groupId, other.groupId)
88 && Objects.equal(this.replicas, other.replicas);
89 }
90
91 @Override
92 public String toString() {
93 return MoreObjects.toStringHelper(this)
Carmelo Casconeb5324e72018-11-25 02:26:32 -080094 .add("groupId", "0x" + Integer.toHexString(groupId))
Carmelo Cascone58136812018-07-19 03:40:16 +020095 .add("replicas", replicas)
96 .toString();
97 }
98
99 /**
100 * Returns a new builder of multicast group entries.
101 *
102 * @return builder
103 */
104 public static Builder builder() {
105 return new Builder();
106 }
107
108 /**
109 * Builder of PI multicast group entries.
110 */
111 public static final class Builder {
112
Carmelo Cascone03ae0ac2018-10-11 08:31:59 -0700113 private Integer groupId;
Carmelo Cascone58136812018-07-19 03:40:16 +0200114 private ImmutableSet.Builder<PiPreReplica> replicaSetBuilder = ImmutableSet.builder();
115
116 private Builder() {
117 // Hide constructor.
118 }
119
120 /**
121 * Sets the identifier of this multicast group.
122 *
123 * @param groupId group ID
124 * @return this
125 */
Carmelo Cascone03ae0ac2018-10-11 08:31:59 -0700126 public Builder withGroupId(int groupId) {
Carmelo Cascone58136812018-07-19 03:40:16 +0200127 this.groupId = groupId;
128 return this;
129 }
130
131 /**
132 * Adds the given packet replica to this multicast group.
133 *
134 * @param replica packet replica
135 * @return this
136 */
137 public Builder addReplica(PiPreReplica replica) {
138 checkNotNull(replica);
139 replicaSetBuilder.add(replica);
140 return this;
141 }
142
143 /**
144 * Adds the given packet replicas to this multicast group.
145 *
146 * @param replicas packet replicas
147 * @return this
148 */
149 public Builder addReplicas(Collection<PiPreReplica> replicas) {
150 checkNotNull(replicas);
151 replicaSetBuilder.addAll(replicas);
152 return this;
153 }
154
155 /**
156 * Returns a new multicast group entry.
157 *
158 * @return multicast group entry
159 */
160 public PiMulticastGroupEntry build() {
161 checkNotNull(groupId, "Multicast group ID must be set");
162 final ImmutableSet<PiPreReplica> replicas = replicaSetBuilder.build();
Carmelo Cascone58136812018-07-19 03:40:16 +0200163 return new PiMulticastGroupEntry(groupId, replicas);
164 }
165 }
166}