blob: e87876c60f5a440ac244a838aa993565d37cbf12 [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>>
yoonseonc6a69272017-01-12 18:22:20 -080032 implements ListenerService<E, L>, VnetService {
33
34 private static final String NETWORK_NULL = "Network ID cannot be null";
yoonseonfe721972017-01-10 17:18:49 -080035
36 protected final NetworkId networkId;
yoonseonc6a69272017-01-12 18:22:20 -080037 protected final VirtualNetworkService manager;
38 protected final ServiceDirectory serviceDirectory;
yoonseonfe721972017-01-10 17:18:49 -080039
40 protected EventDeliveryService eventDispatcher;
41
42 VirtualListenerRegistryManager listenerManager =
43 VirtualListenerRegistryManager.getInstance();
44
yoonseonc6a69272017-01-12 18:22:20 -080045 public AbstractVirtualListenerManager(VirtualNetworkService manager,
46 NetworkId networkId) {
47 this.manager = manager;
yoonseonfe721972017-01-10 17:18:49 -080048 this.networkId = networkId;
yoonseonc6a69272017-01-12 18:22:20 -080049 this.serviceDirectory = manager.getServiceDirectory();
50
51 //Set default event delivery service by default
52 this.eventDispatcher = serviceDirectory.get(EventDeliveryService.class);
yoonseonfe721972017-01-10 17:18:49 -080053 }
54
55 @Override
56 public void addListener(L listener) {
57 listenerManager.getRegistry(networkId, getEventClass())
58 .addListener(listener);
59 }
60
61 @Override
62 public void removeListener(L listener) {
63 listenerManager.getRegistry(networkId, getEventClass())
64 .removeListener(listener);
65 }
66
67 /**
68 * Safely posts the specified event to the local event dispatcher.
69 * If there is no event dispatcher or if the event is null, this method
70 * is a noop.
71 *
72 * @param event event to be posted; may be null
73 */
74 protected void post(E event) {
75 if (event != null && eventDispatcher != null) {
76 VirtualEvent<E> vEvent =
77 new VirtualEvent<E>(networkId, VirtualEvent.Type.POSTED, event);
78 eventDispatcher.post(vEvent);
79 }
80 }
81
yoonseonc6a69272017-01-12 18:22:20 -080082 @Override
83 public NetworkId networkId() {
84 return this.networkId;
85 }
86
yoonseonfe721972017-01-10 17:18:49 -080087 /**
88 * Returns the class type of parameter type.
89 * More specifically, it returns the class type of event class.
90 *
91 * @return the class type of provider service of the service
92 */
yoonseonc6a69272017-01-12 18:22:20 -080093 public Class getEventClass() {
yoonseonfe721972017-01-10 17:18:49 -080094 String className = this.getClass().getGenericSuperclass().toString();
95 String pramType = className.split("<")[1].split(",")[0];
96
97 try {
98 return Class.forName(pramType);
99 } catch (ClassNotFoundException e) {
100 e.printStackTrace();
101 }
102
103 return null;
104 }
yoonseonfe721972017-01-10 17:18:49 -0800105}