alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.incubator.store.mcast.impl; |
| 17 | |
| 18 | import com.google.common.collect.ImmutableSet; |
| 19 | import com.google.common.collect.Sets; |
| 20 | import org.onosproject.net.ConnectPoint; |
| 21 | |
| 22 | import java.util.Set; |
| 23 | import java.util.concurrent.atomic.AtomicBoolean; |
| 24 | import java.util.concurrent.atomic.AtomicReference; |
| 25 | |
| 26 | import 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 | */ |
| 32 | public 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 Hart | 07eb041 | 2016-02-08 16:42:29 -0800 | [diff] [blame^] | 64 | checkNotNull(sink); |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 65 | sinks.add(sink); |
| 66 | } |
| 67 | |
| 68 | public boolean removeSink(ConnectPoint sink) { |
Jonathan Hart | 07eb041 | 2016-02-08 16:42:29 -0800 | [diff] [blame^] | 69 | checkNotNull(sink); |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 70 | 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 | } |