blob: 75b5206693fc0595901659ae8643772a85970e22 [file] [log] [blame]
alshabib79e52872015-12-07 16:01:01 -08001/*
2 * Copyright 2015 Open Networking Laboratory
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 */
16package org.onosproject.incubator.store.mcast.impl;
17
18import com.google.common.collect.ImmutableSet;
19import com.google.common.collect.Sets;
20import org.onosproject.net.ConnectPoint;
21
22import java.util.Set;
23import java.util.concurrent.atomic.AtomicBoolean;
24import java.util.concurrent.atomic.AtomicReference;
25
26import static com.google.common.base.Preconditions.checkNotNull;
27
28/**
29 * Simple entity maintaining a mapping between a source and a collection of sink
30 * connect points.
31 */
32public final class MulticastData {
33
34 private final AtomicReference<ConnectPoint> source =
35 new AtomicReference<>();
36 private final Set<ConnectPoint> sinks;
37 private final AtomicBoolean isEmpty = new AtomicBoolean();
38
39 private MulticastData() {
40 this.sinks = Sets.newConcurrentHashSet();
41 isEmpty.set(true);
42 }
43
44 public MulticastData(ConnectPoint source) {
45 this.source.set(checkNotNull(source, "Multicast source cannot be null."));
46 this.sinks = Sets.newConcurrentHashSet();
47 isEmpty.set(false);
48 }
49
50 public ConnectPoint source() {
51 return source.get();
52 }
53
54 public Set<ConnectPoint> sinks() {
55 return ImmutableSet.copyOf(sinks);
56 }
57
58 public void setSource(ConnectPoint source) {
59 isEmpty.set(false);
60 this.source.set(source);
61 }
62
63 public void appendSink(ConnectPoint sink) {
Jonathan Hart07eb0412016-02-08 16:42:29 -080064 checkNotNull(sink);
alshabib79e52872015-12-07 16:01:01 -080065 sinks.add(sink);
66 }
67
68 public boolean removeSink(ConnectPoint sink) {
Jonathan Hart07eb0412016-02-08 16:42:29 -080069 checkNotNull(sink);
alshabib79e52872015-12-07 16:01:01 -080070 return sinks.remove(sink);
71 }
72
73 public boolean isEmpty() {
74 return isEmpty.get();
75 }
76
77 public static MulticastData empty() {
78 return new MulticastData();
79 }
80
81}