blob: 9965818b7b9cd62817495edc523c4e8d2c4921eb [file] [log] [blame]
Andrea Campanella545edb42018-03-20 16:37:29 -07001/*
2 * Copyright 2018-present Open Networking Foundation
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 */
16package org.onosproject.mcast.api;
17
18import com.google.common.annotations.Beta;
19import com.google.common.collect.ImmutableMap;
20import com.google.common.collect.ImmutableSet;
21import org.onosproject.net.ConnectPoint;
22import org.onosproject.net.HostId;
23
24import java.util.Collection;
25import java.util.Map;
26import java.util.Objects;
27import java.util.Set;
28import java.util.concurrent.ConcurrentHashMap;
29import java.util.stream.Collectors;
30
31import static com.google.common.base.MoreObjects.toStringHelper;
32import static com.google.common.base.Preconditions.checkArgument;
33import static com.google.common.base.Preconditions.checkNotNull;
34
35/**
36 * Data regarding a multicast route, comprised of a type, multiple sources and multiple sinks.
37 */
38@Beta
39public final class McastRouteData {
40
41 private final ConcurrentHashMap<ConnectPoint, Boolean> sources = new ConcurrentHashMap<>();
42 private final ConcurrentHashMap<HostId, Set<ConnectPoint>> sinks = new ConcurrentHashMap<>();
43
44 private McastRouteData() {
45 }
46
47 /**
48 * Sources contained in the associated route.
49 *
50 * @return set of sources
51 */
52 public Set<ConnectPoint> sources() {
53 return ImmutableSet.copyOf(sources.keySet());
54 }
55
56 /**
57 * Sources contained in the associated route.
58 *
59 * @return map of hostIds and associated sinks
60 */
61 public Map<HostId, Set<ConnectPoint>> sinks() {
62 return ImmutableMap.copyOf(sinks);
63 }
64
65 /**
66 * Sources contained in the associated route.
67 *
68 * @return map of hostIds and associated sinks
69 */
70 public Set<ConnectPoint> allSinks() {
71 return sinks.values().stream().flatMap(Collection::stream).collect(Collectors.toSet());
72 }
73
74 /**
75 * Sinks contained in the associated route for the given host.
76 *
77 * @param hostId the host
78 * @return set of sinks
79 */
80 public Set<ConnectPoint> sinks(HostId hostId) {
81 return sinks.get(hostId);
82 }
83
84 /**
85 * Sinks contained in the associated route that are not bound to any host.
86 *
87 * @return set of sinks
88 */
89 public Set<ConnectPoint> nonHostSinks() {
90 return sinks.get(HostId.NONE);
91 }
92
93 /**
94 * Add the sources for the associated route.
95 *
96 * @param sources set of sources
97 */
98 public void addSources(Set<ConnectPoint> sources) {
99 checkArgument(!sources.contains(null));
100 sources.forEach(source -> {
101 this.sources.put(source, true);
102 });
103 }
104
105 /**
106 * Removes all the sources contained in the associated route.
107 */
108 public void removeSources() {
109 sources.clear();
110 }
111
112 /**
113 * Removes the given sources contained in the associated route.
114 *
115 * @param sources the sources to remove
116 */
117 public void removeSources(Set<ConnectPoint> sources) {
118 checkArgument(!sources.contains(null));
119 sources.forEach(this.sources::remove);
120 }
121
122 /**
123 * Adds sinks for a given host Id. If the Host Id is {@link HostId#NONE} the sinks are intended to be
124 * used at all times independently of the attached host.
125 *
126 * @param hostId the host
127 * @param sinks the sinks
128 */
129 public void addSinks(HostId hostId, Set<ConnectPoint> sinks) {
130 checkNotNull(hostId);
131 checkArgument(!sinks.contains(null));
132 this.sinks.put(hostId, sinks);
133 }
134
135 /**
136 * Adds sink for this route that are not associated directly with a given host.
137 *
138 * @param sinks the sinks
139 */
140 public void addNonHostSinks(Set<ConnectPoint> sinks) {
141 checkArgument(!sinks.contains(null));
142 this.sinks.put(HostId.NONE, sinks);
143 }
144
145 /**
146 * Removes all the sinks for this route.
147 */
148 public void removeSinks() {
149 sinks.clear();
150 }
151
152 /**
153 * Removes all the sinks for the given host for this route.
154 *
155 * @param hostId the host
156 */
157 public void removeSinks(HostId hostId) {
158 checkNotNull(hostId);
159 this.sinks.remove(hostId);
160 }
161
162 /**
163 * Removes all the given sinks for the given host for this route.
164 *
165 * @param hostId the host
166 * @param sinks the sinks to remove
167 */
168 public void removeSinks(HostId hostId, Set<ConnectPoint> sinks) {
169 checkNotNull(hostId);
170 checkArgument(!sinks.contains(null));
171 this.sinks.get(hostId).removeAll(sinks);
172 }
173
174 /**
175 * Returns if the route has no sinks.
176 *
177 * @return true if no sinks
178 */
179 public boolean isEmpty() {
180 return sinks.isEmpty();
181 }
182
183 /**
184 * Creates an empty route object.
185 *
186 * @return an empty muticast rout data object.
187 */
188 public static McastRouteData empty() {
189 return new McastRouteData();
190 }
191
192 @Override
193 public int hashCode() {
194 return Objects.hash(super.hashCode(), sources, sinks);
195 }
196
197 @Override
198 public boolean equals(Object obj) {
199 if (this == obj) {
200 return true;
201 }
202 if (!(obj instanceof McastRouteData)) {
203 return false;
204 }
205 final McastRouteData other = (McastRouteData) obj;
206
207 return super.equals(obj) &&
208 Objects.equals(sources(), other.sources()) &&
209 Objects.equals(sinks(), other.sinks());
210 }
211
212 @Override
213 public String toString() {
214 return toStringHelper(this)
215 .add("sources", sources())
216 .add("sinks", sinks())
217 .toString();
218 }
219}