alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
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 | */ |
Ray Milkey | a9ae0d4 | 2017-08-06 22:10:47 -0700 | [diff] [blame] | 16 | package org.onosproject.store.mcast.impl; |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 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; |
Julia Ferguson | 8d31595 | 2017-11-19 13:56:17 +0000 | [diff] [blame] | 23 | import java.util.Objects; |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 24 | import java.util.Set; |
| 25 | import java.util.concurrent.atomic.AtomicBoolean; |
| 26 | import java.util.concurrent.atomic.AtomicReference; |
| 27 | |
Pier Luigi | 57d4179 | 2018-02-26 12:31:38 +0100 | [diff] [blame] | 28 | import static com.google.common.base.MoreObjects.toStringHelper; |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 29 | import static com.google.common.base.Preconditions.checkNotNull; |
| 30 | |
| 31 | /** |
| 32 | * Simple entity maintaining a mapping between a source and a collection of sink |
| 33 | * connect points. |
| 34 | */ |
| 35 | public final class MulticastData { |
| 36 | |
| 37 | private final AtomicReference<ConnectPoint> source = |
| 38 | new AtomicReference<>(); |
alshabib | e62b507 | 2016-02-14 18:00:54 -0800 | [diff] [blame] | 39 | private final Map<ConnectPoint, Boolean> sinks; |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 40 | private final AtomicBoolean isEmpty = new AtomicBoolean(); |
| 41 | |
| 42 | private MulticastData() { |
alshabib | e62b507 | 2016-02-14 18:00:54 -0800 | [diff] [blame] | 43 | this.sinks = Maps.newConcurrentMap(); |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 44 | isEmpty.set(true); |
| 45 | } |
| 46 | |
| 47 | public MulticastData(ConnectPoint source) { |
| 48 | this.source.set(checkNotNull(source, "Multicast source cannot be null.")); |
alshabib | e62b507 | 2016-02-14 18:00:54 -0800 | [diff] [blame] | 49 | this.sinks = Maps.newConcurrentMap(); |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 50 | isEmpty.set(false); |
| 51 | } |
| 52 | |
| 53 | public ConnectPoint source() { |
| 54 | return source.get(); |
| 55 | } |
| 56 | |
| 57 | public Set<ConnectPoint> sinks() { |
alshabib | e62b507 | 2016-02-14 18:00:54 -0800 | [diff] [blame] | 58 | return ImmutableSet.copyOf(sinks.keySet()); |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | public void setSource(ConnectPoint source) { |
Madan Jampani | 72282af | 2016-02-23 14:23:52 -0800 | [diff] [blame] | 62 | // FIXME: violates immutability |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 63 | isEmpty.set(false); |
| 64 | this.source.set(source); |
| 65 | } |
| 66 | |
| 67 | public void appendSink(ConnectPoint sink) { |
Jonathan Hart | 07eb041 | 2016-02-08 16:42:29 -0800 | [diff] [blame] | 68 | checkNotNull(sink); |
alshabib | e62b507 | 2016-02-14 18:00:54 -0800 | [diff] [blame] | 69 | sinks.put(sink, true); |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 70 | } |
| 71 | |
alshabib | 1aa5814 | 2016-02-17 15:37:56 -0800 | [diff] [blame] | 72 | public void removeSink(ConnectPoint sink) { |
Jonathan Hart | 07eb041 | 2016-02-08 16:42:29 -0800 | [diff] [blame] | 73 | checkNotNull(sink); |
alshabib | 1aa5814 | 2016-02-17 15:37:56 -0800 | [diff] [blame] | 74 | sinks.remove(sink); |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | public boolean isEmpty() { |
| 78 | return isEmpty.get(); |
| 79 | } |
| 80 | |
| 81 | public static MulticastData empty() { |
| 82 | return new MulticastData(); |
| 83 | } |
| 84 | |
Julia Ferguson | 8d31595 | 2017-11-19 13:56:17 +0000 | [diff] [blame] | 85 | @Override |
| 86 | public int hashCode() { |
| 87 | return Objects.hash(super.hashCode(), source, sinks, isEmpty); |
| 88 | } |
| 89 | |
| 90 | @Override |
| 91 | public boolean equals(Object obj) { |
| 92 | if (this == obj) { |
| 93 | return true; |
| 94 | } |
| 95 | if (!(obj instanceof MulticastData)) { |
| 96 | return false; |
| 97 | } |
| 98 | final MulticastData other = (MulticastData) obj; |
| 99 | |
| 100 | return super.equals(obj) && |
| 101 | Objects.equals(source(), other.source()) && |
| 102 | Objects.equals(sinks(), other.sinks()) && |
| 103 | Objects.equals(isEmpty, other.isEmpty); |
| 104 | } |
Pier Luigi | 57d4179 | 2018-02-26 12:31:38 +0100 | [diff] [blame] | 105 | |
| 106 | @Override |
| 107 | public String toString() { |
| 108 | return toStringHelper(this) |
| 109 | .add("source", source()) |
| 110 | .add("sinks", sinks()) |
| 111 | .toString(); |
| 112 | } |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 113 | } |