alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 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; |
alshabib | e62b507 | 2016-02-14 18:00:54 -0800 | [diff] [blame] | 19 | import com.google.common.collect.Maps; |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 20 | import org.onosproject.net.ConnectPoint; |
| 21 | |
alshabib | e62b507 | 2016-02-14 18:00:54 -0800 | [diff] [blame] | 22 | import java.util.Map; |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 23 | import java.util.Set; |
| 24 | import java.util.concurrent.atomic.AtomicBoolean; |
| 25 | import java.util.concurrent.atomic.AtomicReference; |
| 26 | |
| 27 | import static com.google.common.base.Preconditions.checkNotNull; |
| 28 | |
| 29 | /** |
| 30 | * Simple entity maintaining a mapping between a source and a collection of sink |
| 31 | * connect points. |
| 32 | */ |
| 33 | public final class MulticastData { |
| 34 | |
| 35 | private final AtomicReference<ConnectPoint> source = |
| 36 | new AtomicReference<>(); |
alshabib | e62b507 | 2016-02-14 18:00:54 -0800 | [diff] [blame] | 37 | private final Map<ConnectPoint, Boolean> sinks; |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 38 | private final AtomicBoolean isEmpty = new AtomicBoolean(); |
| 39 | |
| 40 | private MulticastData() { |
alshabib | e62b507 | 2016-02-14 18:00:54 -0800 | [diff] [blame] | 41 | this.sinks = Maps.newConcurrentMap(); |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 42 | isEmpty.set(true); |
| 43 | } |
| 44 | |
| 45 | public MulticastData(ConnectPoint source) { |
| 46 | this.source.set(checkNotNull(source, "Multicast source cannot be null.")); |
alshabib | e62b507 | 2016-02-14 18:00:54 -0800 | [diff] [blame] | 47 | this.sinks = Maps.newConcurrentMap(); |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 48 | isEmpty.set(false); |
| 49 | } |
| 50 | |
| 51 | public ConnectPoint source() { |
| 52 | return source.get(); |
| 53 | } |
| 54 | |
| 55 | public Set<ConnectPoint> sinks() { |
alshabib | e62b507 | 2016-02-14 18:00:54 -0800 | [diff] [blame] | 56 | return ImmutableSet.copyOf(sinks.keySet()); |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | public void setSource(ConnectPoint source) { |
Madan Jampani | 72282af | 2016-02-23 14:23:52 -0800 | [diff] [blame] | 60 | // FIXME: violates immutability |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 61 | isEmpty.set(false); |
| 62 | this.source.set(source); |
| 63 | } |
| 64 | |
| 65 | public void appendSink(ConnectPoint sink) { |
Jonathan Hart | 07eb041 | 2016-02-08 16:42:29 -0800 | [diff] [blame] | 66 | checkNotNull(sink); |
alshabib | e62b507 | 2016-02-14 18:00:54 -0800 | [diff] [blame] | 67 | sinks.put(sink, true); |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 68 | } |
| 69 | |
alshabib | 1aa5814 | 2016-02-17 15:37:56 -0800 | [diff] [blame] | 70 | public void removeSink(ConnectPoint sink) { |
Jonathan Hart | 07eb041 | 2016-02-08 16:42:29 -0800 | [diff] [blame] | 71 | checkNotNull(sink); |
alshabib | 1aa5814 | 2016-02-17 15:37:56 -0800 | [diff] [blame] | 72 | sinks.remove(sink); |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | public boolean isEmpty() { |
| 76 | return isEmpty.get(); |
| 77 | } |
| 78 | |
| 79 | public static MulticastData empty() { |
| 80 | return new MulticastData(); |
| 81 | } |
| 82 | |
| 83 | } |