blob: a9582abd75b78ea378f89ebc6743a3bce51d44a7 [file] [log] [blame]
wei wei89ddc322015-03-22 16:29:04 -05001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
wei wei89ddc322015-03-22 16:29:04 -05003 *
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 */
Thomas Vachuskabf916ea2015-05-20 18:24:34 -070016package org.onosproject.incubator.net.tunnel.impl;
wei wei89ddc322015-03-22 16:29:04 -050017
wei wei89ddc322015-03-22 16:29:04 -050018import org.apache.felix.scr.annotations.Activate;
19import org.apache.felix.scr.annotations.Component;
20import org.apache.felix.scr.annotations.Deactivate;
21import org.apache.felix.scr.annotations.Reference;
22import org.apache.felix.scr.annotations.ReferenceCardinality;
23import org.apache.felix.scr.annotations.Service;
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070024import org.onosproject.net.provider.AbstractListenerProviderRegistry;
jcc4a20a5f2015-04-30 15:43:39 +080025import org.onosproject.core.ApplicationId;
Thomas Vachuskabf916ea2015-05-20 18:24:34 -070026import org.onosproject.incubator.net.tunnel.DefaultTunnel;
samuel7a5691a2015-05-23 00:36:32 +080027import org.onosproject.incubator.net.tunnel.Tunnel;
Avantika-Huawei1e0a5862016-04-04 14:22:22 +053028import org.onosproject.incubator.net.tunnel.Tunnel.State;
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070029import org.onosproject.incubator.net.tunnel.Tunnel.Type;
Thomas Vachuskabf916ea2015-05-20 18:24:34 -070030import org.onosproject.incubator.net.tunnel.TunnelAdminService;
31import org.onosproject.incubator.net.tunnel.TunnelDescription;
32import org.onosproject.incubator.net.tunnel.TunnelEndPoint;
33import org.onosproject.incubator.net.tunnel.TunnelEvent;
34import org.onosproject.incubator.net.tunnel.TunnelId;
35import org.onosproject.incubator.net.tunnel.TunnelListener;
36import org.onosproject.incubator.net.tunnel.TunnelName;
37import org.onosproject.incubator.net.tunnel.TunnelProvider;
38import org.onosproject.incubator.net.tunnel.TunnelProviderRegistry;
39import org.onosproject.incubator.net.tunnel.TunnelProviderService;
40import org.onosproject.incubator.net.tunnel.TunnelService;
41import org.onosproject.incubator.net.tunnel.TunnelStore;
42import org.onosproject.incubator.net.tunnel.TunnelStoreDelegate;
43import org.onosproject.incubator.net.tunnel.TunnelSubscription;
samuel7a5691a2015-05-23 00:36:32 +080044import org.onosproject.net.Annotations;
cheng fan35dc0f22015-06-10 06:02:47 +080045import org.onosproject.net.DeviceId;
Avantika-Huaweie10a6e92016-04-06 21:06:41 +053046import org.onosproject.net.ElementId;
samuel7a5691a2015-05-23 00:36:32 +080047import org.onosproject.net.Path;
samuel7a5691a2015-05-23 00:36:32 +080048import org.onosproject.net.provider.AbstractProviderService;
49import org.onosproject.net.provider.ProviderId;
wei wei89ddc322015-03-22 16:29:04 -050050import org.slf4j.Logger;
51
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070052import java.util.Collection;
53import java.util.Collections;
54import java.util.Set;
55
56import static com.google.common.base.Preconditions.checkNotNull;
57import static org.slf4j.LoggerFactory.getLogger;
58
wei wei89ddc322015-03-22 16:29:04 -050059/**
60 * Provides implementation of the tunnel NB/SB APIs.
61 */
62@Component(immediate = true, enabled = true)
63@Service
jcc4a20a5f2015-04-30 15:43:39 +080064public class TunnelManager
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070065 extends AbstractListenerProviderRegistry<TunnelEvent, TunnelListener,
66 TunnelProvider, TunnelProviderService>
wei wei89ddc322015-03-22 16:29:04 -050067 implements TunnelService, TunnelAdminService, TunnelProviderRegistry {
68
69 private static final String TUNNNEL_ID_NULL = "Tunnel ID cannot be null";
Satish Kd70e9572016-04-28 13:58:51 +053070 private static final String TUNNNEL_NULL = "Tunnel cannot be null";
wei wei89ddc322015-03-22 16:29:04 -050071
72 private final Logger log = getLogger(getClass());
73
wei wei89ddc322015-03-22 16:29:04 -050074 private final TunnelStoreDelegate delegate = new InternalStoreDelegate();
jccd8697232015-05-05 14:42:23 +080075
wei wei89ddc322015-03-22 16:29:04 -050076 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
77 protected TunnelStore store;
78
wei wei89ddc322015-03-22 16:29:04 -050079
wei wei89ddc322015-03-22 16:29:04 -050080 @Activate
81 public void activate() {
jccd8697232015-05-05 14:42:23 +080082 store.setDelegate(delegate);
83 eventDispatcher.addSink(TunnelEvent.class, listenerRegistry);
wei wei89ddc322015-03-22 16:29:04 -050084 log.info("Started");
85 }
86
87 @Deactivate
88 public void deactivate() {
jccd8697232015-05-05 14:42:23 +080089 store.unsetDelegate(delegate);
90 eventDispatcher.removeSink(TunnelEvent.class);
wei wei89ddc322015-03-22 16:29:04 -050091 log.info("Stopped");
92 }
93
94 @Override
jccd8697232015-05-05 14:42:23 +080095 public void removeTunnel(TunnelId tunnelId) {
96 checkNotNull(tunnelId, TUNNNEL_ID_NULL);
jccd8697232015-05-05 14:42:23 +080097 Tunnel tunnel = store.queryTunnel(tunnelId);
Satish Kd8adcef2016-04-26 16:31:14 +053098 if (tunnel != null) {
99 store.deleteTunnel(tunnelId);
100 if (tunnel.providerId() != null) {
101 TunnelProvider provider = getProvider(tunnel.providerId());
102 if (provider != null) {
103 provider.releaseTunnel(tunnel);
104 }
105 } else {
106 Set<ProviderId> ids = getProviders();
107 for (ProviderId providerId : ids) {
108 TunnelProvider provider = getProvider(providerId);
109 provider.releaseTunnel(tunnel);
110 }
jccd8697232015-05-05 14:42:23 +0800111 }
112 }
113 }
114
115 @Override
116 public void updateTunnel(Tunnel tunnel, Path path) {
117 store.createOrUpdateTunnel(tunnel);
118 if (tunnel.providerId() != null) {
119 TunnelProvider provider = getProvider(tunnel.providerId());
120 if (provider != null) {
121 provider.updateTunnel(tunnel, path);
122 }
123 } else {
124 Set<ProviderId> ids = getProviders();
125 for (ProviderId providerId : ids) {
126 TunnelProvider provider = getProvider(providerId);
127 provider.updateTunnel(tunnel, path);
128 }
129 }
130 }
131
132 @Override
tony-liu1abfb632016-11-09 15:22:51 +0800133 public void updateTunnelState(Tunnel tunnel, State state) {
134 Tunnel storedTunnel = store.queryTunnel(tunnel.tunnelId());
135 store.createOrUpdateTunnel(storedTunnel, state);
136 }
137
138 @Override
jccd8697232015-05-05 14:42:23 +0800139 public void removeTunnels(TunnelEndPoint src, TunnelEndPoint dst,
140 ProviderId producerName) {
jccd8697232015-05-05 14:42:23 +0800141 Collection<Tunnel> setTunnels = store.queryTunnel(src, dst);
Satish Kd8adcef2016-04-26 16:31:14 +0530142 if (!setTunnels.isEmpty()) {
143 store.deleteTunnel(src, dst, producerName);
144 for (Tunnel tunnel : setTunnels) {
145 if (producerName != null
146 && !tunnel.providerId().equals(producerName)) {
147 continue;
jccd8697232015-05-05 14:42:23 +0800148 }
Satish Kd8adcef2016-04-26 16:31:14 +0530149 if (tunnel.providerId() != null) {
150 TunnelProvider provider = getProvider(tunnel.providerId());
151 if (provider != null) {
152 provider.releaseTunnel(tunnel);
153 }
154 } else {
155 Set<ProviderId> ids = getProviders();
156 for (ProviderId providerId : ids) {
157 TunnelProvider provider = getProvider(providerId);
158 provider.releaseTunnel(tunnel);
159 }
jccd8697232015-05-05 14:42:23 +0800160 }
161 }
162 }
163 }
164
165 @Override
166 public void removeTunnels(TunnelEndPoint src, TunnelEndPoint dst, Type type,
167 ProviderId producerName) {
jccd8697232015-05-05 14:42:23 +0800168 Collection<Tunnel> setTunnels = store.queryTunnel(src, dst);
Satish Kd8adcef2016-04-26 16:31:14 +0530169 if (!setTunnels.isEmpty()) {
170 store.deleteTunnel(src, dst, type, producerName);
171 for (Tunnel tunnel : setTunnels) {
172 if (producerName != null
173 && !tunnel.providerId().equals(producerName)
174 || !type.equals(tunnel.type())) {
175 continue;
jccd8697232015-05-05 14:42:23 +0800176 }
Satish Kd8adcef2016-04-26 16:31:14 +0530177 if (tunnel.providerId() != null) {
178 TunnelProvider provider = getProvider(tunnel.providerId());
179 if (provider != null) {
180 provider.releaseTunnel(tunnel);
181 }
182 } else {
183 Set<ProviderId> ids = getProviders();
184 for (ProviderId providerId : ids) {
185 TunnelProvider provider = getProvider(providerId);
186 provider.releaseTunnel(tunnel);
187 }
jccd8697232015-05-05 14:42:23 +0800188 }
189 }
190 }
191 }
192
193 @Override
194 public Tunnel borrowTunnel(ApplicationId consumerId, TunnelId tunnelId,
195 Annotations... annotations) {
196 return store.borrowTunnel(consumerId, tunnelId, annotations);
197 }
198
199 @Override
200 public Collection<Tunnel> borrowTunnel(ApplicationId consumerId,
201 TunnelName tunnelName,
202 Annotations... annotations) {
203 return store.borrowTunnel(consumerId, tunnelName, annotations);
204 }
205
206 @Override
207 public Collection<Tunnel> borrowTunnel(ApplicationId consumerId,
208 TunnelEndPoint src, TunnelEndPoint dst,
209 Annotations... annotations) {
210 Collection<Tunnel> tunnels = store.borrowTunnel(consumerId, src,
211 dst, annotations);
212 if (tunnels == null || tunnels.size() == 0) {
213 Tunnel tunnel = new DefaultTunnel(null, src, dst, null, null, null,
214 null, null, annotations);
215 Set<ProviderId> ids = getProviders();
216 for (ProviderId providerId : ids) {
217 TunnelProvider provider = getProvider(providerId);
218 provider.setupTunnel(tunnel, null);
219 }
220 }
221 return tunnels;
222 }
223
224 @Override
225 public Collection<Tunnel> borrowTunnel(ApplicationId consumerId,
226 TunnelEndPoint src, TunnelEndPoint dst,
227 Type type, Annotations... annotations) {
228 Collection<Tunnel> tunnels = store.borrowTunnel(consumerId, src,
229 dst, type,
230 annotations);
231 if (tunnels == null || tunnels.size() == 0) {
232 Tunnel tunnel = new DefaultTunnel(null, src, dst, type, null, null,
233 null, null, annotations);
234 Set<ProviderId> ids = getProviders();
235 for (ProviderId providerId : ids) {
236 TunnelProvider provider = getProvider(providerId);
237 provider.setupTunnel(tunnel, null);
238 }
239 }
240 return tunnels;
241 }
242
243 @Override
Avantika-Huaweie10a6e92016-04-06 21:06:41 +0530244 public TunnelId setupTunnel(ApplicationId producerId, ElementId srcElementId, Tunnel tunnel, Path path) {
Satish Kd70e9572016-04-28 13:58:51 +0530245 // TODO: producerId to check if really required to consider while setup the tunnel.
246 checkNotNull(tunnel, TUNNNEL_NULL);
247 TunnelId tunnelId = store.createOrUpdateTunnel(tunnel, State.INIT);
248 if (tunnelId != null) {
249 Set<ProviderId> ids = getProviders();
chengfan42782b52016-12-30 15:03:55 +0800250 Tunnel newT = queryTunnel(tunnelId);
Satish Kd70e9572016-04-28 13:58:51 +0530251 for (ProviderId providerId : ids) {
252 TunnelProvider provider = getProvider(providerId);
chengfan42782b52016-12-30 15:03:55 +0800253 provider.setupTunnel(srcElementId, newT, path);
Satish Kd70e9572016-04-28 13:58:51 +0530254 }
255 }
256 return tunnelId;
Avantika-Huaweie10a6e92016-04-06 21:06:41 +0530257 }
258
259 @Override
260 public boolean downTunnel(ApplicationId producerId, TunnelId tunnelId) {
Satish Kd70e9572016-04-28 13:58:51 +0530261 // TODO: producerId to check if really required to consider while deleting the tunnel.
262 checkNotNull(tunnelId, TUNNNEL_ID_NULL);
263 Tunnel tunnel = store.queryTunnel(tunnelId);
264 if (tunnel != null) {
265 TunnelId updtTunnelId = store.createOrUpdateTunnel(tunnel, State.INACTIVE);
266 if (updtTunnelId != null) {
267 Set<ProviderId> ids = getProviders();
268 for (ProviderId providerId : ids) {
269 TunnelProvider provider = getProvider(providerId);
270 provider.releaseTunnel(tunnel);
271 }
272 }
273 return true;
274 }
Avantika-Huaweie10a6e92016-04-06 21:06:41 +0530275 return false;
276 }
277
278 @Override
jccd8697232015-05-05 14:42:23 +0800279 public boolean returnTunnel(ApplicationId consumerId,
280 TunnelId tunnelId, Annotations... annotations) {
281 return store.returnTunnel(consumerId, tunnelId, annotations);
282 }
283
284 @Override
285 public boolean returnTunnel(ApplicationId consumerId,
286 TunnelName tunnelName,
287 Annotations... annotations) {
288 return store.returnTunnel(consumerId, tunnelName, annotations);
289 }
290
291 @Override
292 public boolean returnTunnel(ApplicationId consumerId, TunnelEndPoint src,
293 TunnelEndPoint dst, Type type,
294 Annotations... annotations) {
295 return store.returnTunnel(consumerId, src, dst, type, annotations);
296 }
297
298 @Override
299 public boolean returnTunnel(ApplicationId consumerId, TunnelEndPoint src,
300 TunnelEndPoint dst, Annotations... annotations) {
301 return store.returnTunnel(consumerId, src, dst, annotations);
302 }
303
304 @Override
305 public Tunnel queryTunnel(TunnelId tunnelId) {
306 return store.queryTunnel(tunnelId);
307 }
308
309 @Override
310 public Collection<TunnelSubscription> queryTunnelSubscription(ApplicationId consumerId) {
311 return store.queryTunnelSubscription(consumerId);
312 }
313
314 @Override
315 public Collection<Tunnel> queryTunnel(Type type) {
316 return store.queryTunnel(type);
317 }
318
319 @Override
320 public Collection<Tunnel> queryTunnel(TunnelEndPoint src, TunnelEndPoint dst) {
321 return store.queryTunnel(src, dst);
322 }
323
samuel7a5691a2015-05-23 00:36:32 +0800324
325 @Override
326 public Collection<Tunnel> queryAllTunnels() {
327 return store.queryAllTunnels();
328 }
329
jccd8697232015-05-05 14:42:23 +0800330 @Override
331 public int tunnelCount() {
332 return store.tunnelCount();
333 }
334
335 @Override
wei wei89ddc322015-03-22 16:29:04 -0500336 protected TunnelProviderService createProviderService(TunnelProvider provider) {
wei wei89ddc322015-03-22 16:29:04 -0500337 return new InternalTunnelProviderService(provider);
338 }
339
wei wei89ddc322015-03-22 16:29:04 -0500340 private class InternalTunnelProviderService
341 extends AbstractProviderService<TunnelProvider>
342 implements TunnelProviderService {
343 protected InternalTunnelProviderService(TunnelProvider provider) {
344 super(provider);
wei wei89ddc322015-03-22 16:29:04 -0500345 }
346
jccd8697232015-05-05 14:42:23 +0800347
wei wei89ddc322015-03-22 16:29:04 -0500348 @Override
jcc4a20a5f2015-04-30 15:43:39 +0800349 public TunnelId tunnelAdded(TunnelDescription tunnel) {
jccd8697232015-05-05 14:42:23 +0800350 Tunnel storedTunnel = new DefaultTunnel(provider().id(),
351 tunnel.src(), tunnel.dst(),
352 tunnel.type(),
353 tunnel.groupId(),
354 tunnel.id(),
355 tunnel.tunnelName(),
samuel7a5691a2015-05-23 00:36:32 +0800356 tunnel.path(),
Satish Kd70e9572016-04-28 13:58:51 +0530357 tunnel.resource(),
jccd8697232015-05-05 14:42:23 +0800358 tunnel.annotations());
359 return store.createOrUpdateTunnel(storedTunnel);
wei wei89ddc322015-03-22 16:29:04 -0500360 }
361
362 @Override
Avantika-Huawei1e0a5862016-04-04 14:22:22 +0530363 public TunnelId tunnelAdded(TunnelDescription tunnel, State state) {
364 Tunnel storedTunnel = new DefaultTunnel(provider().id(),
365 tunnel.src(), tunnel.dst(),
366 tunnel.type(),
367 state,
368 tunnel.groupId(),
369 tunnel.id(),
370 tunnel.tunnelName(),
371 tunnel.path(),
Satish Kd70e9572016-04-28 13:58:51 +0530372 tunnel.resource(),
Avantika-Huawei1e0a5862016-04-04 14:22:22 +0530373 tunnel.annotations());
374 return store.createOrUpdateTunnel(storedTunnel);
375 }
376
377 @Override
wei wei89ddc322015-03-22 16:29:04 -0500378 public void tunnelUpdated(TunnelDescription tunnel) {
jccd8697232015-05-05 14:42:23 +0800379 Tunnel storedTunnel = new DefaultTunnel(provider().id(),
380 tunnel.src(), tunnel.dst(),
381 tunnel.type(),
382 tunnel.groupId(),
383 tunnel.id(),
384 tunnel.tunnelName(),
samuel7a5691a2015-05-23 00:36:32 +0800385 tunnel.path(),
Satish Kd70e9572016-04-28 13:58:51 +0530386 tunnel.resource(),
jccd8697232015-05-05 14:42:23 +0800387 tunnel.annotations());
388 store.createOrUpdateTunnel(storedTunnel);
wei wei89ddc322015-03-22 16:29:04 -0500389 }
390
391 @Override
Avantika-Huawei4c3c9972016-04-05 14:51:46 +0530392 public void tunnelUpdated(TunnelDescription tunnel, State state) {
393 Tunnel storedTunnel = new DefaultTunnel(provider().id(),
394 tunnel.src(), tunnel.dst(),
395 tunnel.type(),
396 state,
397 tunnel.groupId(),
398 tunnel.id(),
399 tunnel.tunnelName(),
400 tunnel.path(),
Satish Kd70e9572016-04-28 13:58:51 +0530401 tunnel.resource(),
Avantika-Huawei4c3c9972016-04-05 14:51:46 +0530402 tunnel.annotations());
403 store.createOrUpdateTunnel(storedTunnel, state);
404 }
405
406 @Override
wei wei89ddc322015-03-22 16:29:04 -0500407 public void tunnelRemoved(TunnelDescription tunnel) {
jccd8697232015-05-05 14:42:23 +0800408 if (tunnel.id() != null) {
409 store.deleteTunnel(tunnel.id());
samuel7a5691a2015-05-23 00:36:32 +0800410 return;
jccd8697232015-05-05 14:42:23 +0800411 }
412 if (tunnel.src() != null && tunnel.dst() != null
413 && tunnel.type() != null) {
414 store.deleteTunnel(tunnel.src(), tunnel.dst(), tunnel.type(),
415 provider().id());
samuel7a5691a2015-05-23 00:36:32 +0800416 return;
jccd8697232015-05-05 14:42:23 +0800417 }
418 if (tunnel.src() != null && tunnel.dst() != null
419 && tunnel.type() == null) {
420 store.deleteTunnel(tunnel.src(), tunnel.dst(), provider().id());
samuel7a5691a2015-05-23 00:36:32 +0800421 return;
jccd8697232015-05-05 14:42:23 +0800422 }
wei wei89ddc322015-03-22 16:29:04 -0500423 }
424
samuel7a5691a2015-05-23 00:36:32 +0800425
426 @Override
427 public Tunnel tunnelQueryById(TunnelId tunnelId) {
428 return store.queryTunnel(tunnelId);
429 }
430
431
wei wei89ddc322015-03-22 16:29:04 -0500432 }
433
434 private class InternalStoreDelegate implements TunnelStoreDelegate {
435 @Override
436 public void notify(TunnelEvent event) {
wei wei89ddc322015-03-22 16:29:04 -0500437 if (event != null) {
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700438 post(event);
wei wei89ddc322015-03-22 16:29:04 -0500439 }
440 }
441 }
samuel7a5691a2015-05-23 00:36:32 +0800442
cheng fan35dc0f22015-06-10 06:02:47 +0800443 @Override
444 public Iterable<Tunnel> getTunnels(DeviceId deviceId) {
445 return Collections.emptyList();
446 }
447
wei wei89ddc322015-03-22 16:29:04 -0500448}