blob: b121483021901dde34665759155e9103341a4f9d [file] [log] [blame]
alshabib79e52872015-12-07 16:01:01 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
alshabib79e52872015-12-07 16:01:01 -08003 *
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 Milkeya9ae0d42017-08-06 22:10:47 -070016package org.onosproject.store.mcast.impl;
alshabib79e52872015-12-07 16:01:01 -080017
18import com.google.common.collect.ImmutableSet;
alshabibe62b5072016-02-14 18:00:54 -080019import com.google.common.collect.Maps;
alshabib79e52872015-12-07 16:01:01 -080020import org.onosproject.net.ConnectPoint;
21
alshabibe62b5072016-02-14 18:00:54 -080022import java.util.Map;
Julia Ferguson75ef1052017-11-19 13:56:17 +000023import java.util.Objects;
alshabib79e52872015-12-07 16:01:01 -080024import java.util.Set;
25import java.util.concurrent.atomic.AtomicBoolean;
26import java.util.concurrent.atomic.AtomicReference;
27
28import static com.google.common.base.Preconditions.checkNotNull;
29
30/**
31 * Simple entity maintaining a mapping between a source and a collection of sink
32 * connect points.
33 */
34public final class MulticastData {
35
36 private final AtomicReference<ConnectPoint> source =
37 new AtomicReference<>();
alshabibe62b5072016-02-14 18:00:54 -080038 private final Map<ConnectPoint, Boolean> sinks;
alshabib79e52872015-12-07 16:01:01 -080039 private final AtomicBoolean isEmpty = new AtomicBoolean();
40
41 private MulticastData() {
alshabibe62b5072016-02-14 18:00:54 -080042 this.sinks = Maps.newConcurrentMap();
alshabib79e52872015-12-07 16:01:01 -080043 isEmpty.set(true);
44 }
45
46 public MulticastData(ConnectPoint source) {
47 this.source.set(checkNotNull(source, "Multicast source cannot be null."));
alshabibe62b5072016-02-14 18:00:54 -080048 this.sinks = Maps.newConcurrentMap();
alshabib79e52872015-12-07 16:01:01 -080049 isEmpty.set(false);
50 }
51
52 public ConnectPoint source() {
53 return source.get();
54 }
55
56 public Set<ConnectPoint> sinks() {
alshabibe62b5072016-02-14 18:00:54 -080057 return ImmutableSet.copyOf(sinks.keySet());
alshabib79e52872015-12-07 16:01:01 -080058 }
59
60 public void setSource(ConnectPoint source) {
Madan Jampani72282af2016-02-23 14:23:52 -080061 // FIXME: violates immutability
alshabib79e52872015-12-07 16:01:01 -080062 isEmpty.set(false);
63 this.source.set(source);
64 }
65
66 public void appendSink(ConnectPoint sink) {
Jonathan Hart07eb0412016-02-08 16:42:29 -080067 checkNotNull(sink);
alshabibe62b5072016-02-14 18:00:54 -080068 sinks.put(sink, true);
alshabib79e52872015-12-07 16:01:01 -080069 }
70
alshabib1aa58142016-02-17 15:37:56 -080071 public void removeSink(ConnectPoint sink) {
Jonathan Hart07eb0412016-02-08 16:42:29 -080072 checkNotNull(sink);
alshabib1aa58142016-02-17 15:37:56 -080073 sinks.remove(sink);
alshabib79e52872015-12-07 16:01:01 -080074 }
75
76 public boolean isEmpty() {
77 return isEmpty.get();
78 }
79
80 public static MulticastData empty() {
81 return new MulticastData();
82 }
83
Julia Ferguson75ef1052017-11-19 13:56:17 +000084 @Override
85 public int hashCode() {
86 return Objects.hash(super.hashCode(), source, sinks, isEmpty);
87 }
88
89 @Override
90 public boolean equals(Object obj) {
91 if (this == obj) {
92 return true;
93 }
94 if (!(obj instanceof MulticastData)) {
95 return false;
96 }
97 final MulticastData other = (MulticastData) obj;
98
99 return super.equals(obj) &&
100 Objects.equals(source(), other.source()) &&
101 Objects.equals(sinks(), other.sinks()) &&
102 Objects.equals(isEmpty, other.isEmpty);
103 }
alshabib79e52872015-12-07 16:01:01 -0800104}