blob: 565a81d45fb0dbcca53332993c01c689dfdb8a36 [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 Ferguson8d315952017-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
Pier Luigi57d41792018-02-26 12:31:38 +010028import static com.google.common.base.MoreObjects.toStringHelper;
alshabib79e52872015-12-07 16:01:01 -080029import 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 */
35public final class MulticastData {
36
37 private final AtomicReference<ConnectPoint> source =
38 new AtomicReference<>();
alshabibe62b5072016-02-14 18:00:54 -080039 private final Map<ConnectPoint, Boolean> sinks;
alshabib79e52872015-12-07 16:01:01 -080040 private final AtomicBoolean isEmpty = new AtomicBoolean();
41
42 private MulticastData() {
alshabibe62b5072016-02-14 18:00:54 -080043 this.sinks = Maps.newConcurrentMap();
alshabib79e52872015-12-07 16:01:01 -080044 isEmpty.set(true);
45 }
46
47 public MulticastData(ConnectPoint source) {
48 this.source.set(checkNotNull(source, "Multicast source cannot be null."));
alshabibe62b5072016-02-14 18:00:54 -080049 this.sinks = Maps.newConcurrentMap();
alshabib79e52872015-12-07 16:01:01 -080050 isEmpty.set(false);
51 }
52
53 public ConnectPoint source() {
54 return source.get();
55 }
56
57 public Set<ConnectPoint> sinks() {
alshabibe62b5072016-02-14 18:00:54 -080058 return ImmutableSet.copyOf(sinks.keySet());
alshabib79e52872015-12-07 16:01:01 -080059 }
60
61 public void setSource(ConnectPoint source) {
Madan Jampani72282af2016-02-23 14:23:52 -080062 // FIXME: violates immutability
alshabib79e52872015-12-07 16:01:01 -080063 isEmpty.set(false);
64 this.source.set(source);
65 }
66
67 public void appendSink(ConnectPoint sink) {
Jonathan Hart07eb0412016-02-08 16:42:29 -080068 checkNotNull(sink);
alshabibe62b5072016-02-14 18:00:54 -080069 sinks.put(sink, true);
alshabib79e52872015-12-07 16:01:01 -080070 }
71
alshabib1aa58142016-02-17 15:37:56 -080072 public void removeSink(ConnectPoint sink) {
Jonathan Hart07eb0412016-02-08 16:42:29 -080073 checkNotNull(sink);
alshabib1aa58142016-02-17 15:37:56 -080074 sinks.remove(sink);
alshabib79e52872015-12-07 16:01:01 -080075 }
76
77 public boolean isEmpty() {
78 return isEmpty.get();
79 }
80
81 public static MulticastData empty() {
82 return new MulticastData();
83 }
84
Julia Ferguson8d315952017-11-19 13:56:17 +000085 @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 Luigi57d41792018-02-26 12:31:38 +0100105
106 @Override
107 public String toString() {
108 return toStringHelper(this)
109 .add("source", source())
110 .add("sinks", sinks())
111 .toString();
112 }
alshabib79e52872015-12-07 16:01:01 -0800113}