blob: 866e845af447f998c91fd263798131cd42d74a79 [file] [log] [blame]
yoonseonfe721972017-01-10 17:18:49 -08001/*
2 * Copyright 2017-present Open Networking Laboratory
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.incubator.net.virtual.event;
17
yoonseonc6a69272017-01-12 18:22:20 -080018import org.onlab.osgi.ServiceDirectory;
yoonseonfe721972017-01-10 17:18:49 -080019import org.onosproject.event.Event;
20import org.onosproject.event.EventDeliveryService;
21import org.onosproject.event.EventListener;
22import org.onosproject.event.ListenerService;
23import org.onosproject.incubator.net.virtual.NetworkId;
yoonseonc6a69272017-01-12 18:22:20 -080024import org.onosproject.incubator.net.virtual.VirtualNetworkService;
25import org.onosproject.incubator.net.virtual.VnetService;
yoonseonfe721972017-01-10 17:18:49 -080026
27/**
28 * Basis for virtual event components which need to export listener mechanism.
29 */
30public abstract class AbstractVirtualListenerManager
31 <E extends Event, L extends EventListener<E>>
Yoonseon Hanb14461b2017-03-07 14:08:01 +090032 implements ListenerService<E, L>, VnetService {
yoonseonfe721972017-01-10 17:18:49 -080033
34 protected final NetworkId networkId;
yoonseonc6a69272017-01-12 18:22:20 -080035 protected final VirtualNetworkService manager;
36 protected final ServiceDirectory serviceDirectory;
yoonseonfe721972017-01-10 17:18:49 -080037
38 protected EventDeliveryService eventDispatcher;
39
Yoonseon Hanb14461b2017-03-07 14:08:01 +090040 private VirtualListenerRegistryManager listenerManager =
yoonseonfe721972017-01-10 17:18:49 -080041 VirtualListenerRegistryManager.getInstance();
42
Yoonseon Hanb14461b2017-03-07 14:08:01 +090043 private Class<? extends Event> eventClass;
44
yoonseonc6a69272017-01-12 18:22:20 -080045 public AbstractVirtualListenerManager(VirtualNetworkService manager,
Yoonseon Hanb14461b2017-03-07 14:08:01 +090046 NetworkId networkId,
47 Class<? extends Event> eventClass) {
yoonseonc6a69272017-01-12 18:22:20 -080048 this.manager = manager;
yoonseonfe721972017-01-10 17:18:49 -080049 this.networkId = networkId;
yoonseonc6a69272017-01-12 18:22:20 -080050 this.serviceDirectory = manager.getServiceDirectory();
51
Yoonseon Hanb14461b2017-03-07 14:08:01 +090052 this.eventClass = eventClass;
53
yoonseonc6a69272017-01-12 18:22:20 -080054 //Set default event delivery service by default
55 this.eventDispatcher = serviceDirectory.get(EventDeliveryService.class);
yoonseonfe721972017-01-10 17:18:49 -080056 }
57
58 @Override
Yoonseon Hanb14461b2017-03-07 14:08:01 +090059 @SuppressWarnings("unchecked")
yoonseonfe721972017-01-10 17:18:49 -080060 public void addListener(L listener) {
Yoonseon Hanb14461b2017-03-07 14:08:01 +090061 listenerManager.getRegistry(networkId, eventClass).addListener(listener);
yoonseonfe721972017-01-10 17:18:49 -080062 }
63
64 @Override
Yoonseon Hanb14461b2017-03-07 14:08:01 +090065 @SuppressWarnings("unchecked")
yoonseonfe721972017-01-10 17:18:49 -080066 public void removeListener(L listener) {
Yoonseon Hanb14461b2017-03-07 14:08:01 +090067 listenerManager.getRegistry(networkId, eventClass).removeListener(listener);
yoonseonfe721972017-01-10 17:18:49 -080068 }
69
70 /**
71 * Safely posts the specified event to the local event dispatcher.
72 * If there is no event dispatcher or if the event is null, this method
73 * is a noop.
74 *
75 * @param event event to be posted; may be null
76 */
77 protected void post(E event) {
78 if (event != null && eventDispatcher != null) {
79 VirtualEvent<E> vEvent =
80 new VirtualEvent<E>(networkId, VirtualEvent.Type.POSTED, event);
81 eventDispatcher.post(vEvent);
82 }
83 }
84
yoonseonc6a69272017-01-12 18:22:20 -080085 @Override
86 public NetworkId networkId() {
87 return this.networkId;
88 }
yoonseonfe721972017-01-10 17:18:49 -080089}