blob: 57dfe0c6c7cd1b64048a585ca91ef08efb4e0327 [file] [log] [blame]
yoonseonfe721972017-01-10 17:18:49 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
yoonseonfe721972017-01-10 17:18:49 -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 */
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;
Harold Huang4a652112017-03-29 00:06:27 +080022import org.onosproject.event.ListenerRegistry;
yoonseonfe721972017-01-10 17:18:49 -080023import org.onosproject.event.ListenerService;
24import org.onosproject.incubator.net.virtual.NetworkId;
yoonseonc6a69272017-01-12 18:22:20 -080025import org.onosproject.incubator.net.virtual.VirtualNetworkService;
26import org.onosproject.incubator.net.virtual.VnetService;
yoonseonfe721972017-01-10 17:18:49 -080027
28/**
29 * Basis for virtual event components which need to export listener mechanism.
30 */
31public abstract class AbstractVirtualListenerManager
32 <E extends Event, L extends EventListener<E>>
Yoonseon Hanb14461b2017-03-07 14:08:01 +090033 implements ListenerService<E, L>, VnetService {
yoonseonfe721972017-01-10 17:18:49 -080034
35 protected final NetworkId networkId;
yoonseonc6a69272017-01-12 18:22:20 -080036 protected final VirtualNetworkService manager;
37 protected final ServiceDirectory serviceDirectory;
yoonseonfe721972017-01-10 17:18:49 -080038
39 protected EventDeliveryService eventDispatcher;
40
Harold Huang4a652112017-03-29 00:06:27 +080041 private ListenerRegistry<E, L> listenerRegistry;
42
Yoonseon Hanb14461b2017-03-07 14:08:01 +090043 private VirtualListenerRegistryManager listenerManager =
yoonseonfe721972017-01-10 17:18:49 -080044 VirtualListenerRegistryManager.getInstance();
45
yoonseonc6a69272017-01-12 18:22:20 -080046 public AbstractVirtualListenerManager(VirtualNetworkService manager,
Yoonseon Hanb14461b2017-03-07 14:08:01 +090047 NetworkId networkId,
48 Class<? extends Event> eventClass) {
yoonseonc6a69272017-01-12 18:22:20 -080049 this.manager = manager;
yoonseonfe721972017-01-10 17:18:49 -080050 this.networkId = networkId;
yoonseonc6a69272017-01-12 18:22:20 -080051 this.serviceDirectory = manager.getServiceDirectory();
52
53 //Set default event delivery service by default
54 this.eventDispatcher = serviceDirectory.get(EventDeliveryService.class);
Harold Huang4a652112017-03-29 00:06:27 +080055
56 //Initialize and reference to the listener registry
57 this.listenerRegistry = listenerManager.getRegistry(networkId, eventClass);
yoonseonfe721972017-01-10 17:18:49 -080058 }
59
60 @Override
Yoonseon Hanb14461b2017-03-07 14:08:01 +090061 @SuppressWarnings("unchecked")
yoonseonfe721972017-01-10 17:18:49 -080062 public void addListener(L listener) {
Harold Huang4a652112017-03-29 00:06:27 +080063 listenerRegistry.addListener(listener);
yoonseonfe721972017-01-10 17:18:49 -080064 }
65
66 @Override
Yoonseon Hanb14461b2017-03-07 14:08:01 +090067 @SuppressWarnings("unchecked")
yoonseonfe721972017-01-10 17:18:49 -080068 public void removeListener(L listener) {
Harold Huang4a652112017-03-29 00:06:27 +080069 listenerRegistry.removeListener(listener);
yoonseonfe721972017-01-10 17:18:49 -080070 }
71
72 /**
73 * Safely posts the specified event to the local event dispatcher.
74 * If there is no event dispatcher or if the event is null, this method
75 * is a noop.
76 *
77 * @param event event to be posted; may be null
78 */
79 protected void post(E event) {
80 if (event != null && eventDispatcher != null) {
81 VirtualEvent<E> vEvent =
82 new VirtualEvent<E>(networkId, VirtualEvent.Type.POSTED, event);
83 eventDispatcher.post(vEvent);
84 }
85 }
86
yoonseonc6a69272017-01-12 18:22:20 -080087 @Override
88 public NetworkId networkId() {
89 return this.networkId;
90 }
yoonseonfe721972017-01-10 17:18:49 -080091}