yoonseon | fe72197 | 2017-01-10 17:18:49 -0800 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.incubator.net.virtual.event; |
| 17 | |
yoonseon | c6a6927 | 2017-01-12 18:22:20 -0800 | [diff] [blame] | 18 | import org.onlab.osgi.ServiceDirectory; |
yoonseon | fe72197 | 2017-01-10 17:18:49 -0800 | [diff] [blame] | 19 | import org.onosproject.event.Event; |
| 20 | import org.onosproject.event.EventDeliveryService; |
| 21 | import org.onosproject.event.EventListener; |
| 22 | import org.onosproject.event.ListenerService; |
| 23 | import org.onosproject.incubator.net.virtual.NetworkId; |
yoonseon | c6a6927 | 2017-01-12 18:22:20 -0800 | [diff] [blame] | 24 | import org.onosproject.incubator.net.virtual.VirtualNetworkService; |
| 25 | import org.onosproject.incubator.net.virtual.VnetService; |
yoonseon | fe72197 | 2017-01-10 17:18:49 -0800 | [diff] [blame] | 26 | |
| 27 | /** |
| 28 | * Basis for virtual event components which need to export listener mechanism. |
| 29 | */ |
| 30 | public abstract class AbstractVirtualListenerManager |
| 31 | <E extends Event, L extends EventListener<E>> |
yoonseon | c6a6927 | 2017-01-12 18:22:20 -0800 | [diff] [blame] | 32 | implements ListenerService<E, L>, VnetService { |
| 33 | |
| 34 | private static final String NETWORK_NULL = "Network ID cannot be null"; |
yoonseon | fe72197 | 2017-01-10 17:18:49 -0800 | [diff] [blame] | 35 | |
| 36 | protected final NetworkId networkId; |
yoonseon | c6a6927 | 2017-01-12 18:22:20 -0800 | [diff] [blame] | 37 | protected final VirtualNetworkService manager; |
| 38 | protected final ServiceDirectory serviceDirectory; |
yoonseon | fe72197 | 2017-01-10 17:18:49 -0800 | [diff] [blame] | 39 | |
| 40 | protected EventDeliveryService eventDispatcher; |
| 41 | |
| 42 | VirtualListenerRegistryManager listenerManager = |
| 43 | VirtualListenerRegistryManager.getInstance(); |
| 44 | |
yoonseon | c6a6927 | 2017-01-12 18:22:20 -0800 | [diff] [blame] | 45 | public AbstractVirtualListenerManager(VirtualNetworkService manager, |
| 46 | NetworkId networkId) { |
| 47 | this.manager = manager; |
yoonseon | fe72197 | 2017-01-10 17:18:49 -0800 | [diff] [blame] | 48 | this.networkId = networkId; |
yoonseon | c6a6927 | 2017-01-12 18:22:20 -0800 | [diff] [blame] | 49 | this.serviceDirectory = manager.getServiceDirectory(); |
| 50 | |
| 51 | //Set default event delivery service by default |
| 52 | this.eventDispatcher = serviceDirectory.get(EventDeliveryService.class); |
yoonseon | fe72197 | 2017-01-10 17:18:49 -0800 | [diff] [blame] | 53 | } |
| 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 | |
yoonseon | c6a6927 | 2017-01-12 18:22:20 -0800 | [diff] [blame] | 82 | @Override |
| 83 | public NetworkId networkId() { |
| 84 | return this.networkId; |
| 85 | } |
| 86 | |
yoonseon | fe72197 | 2017-01-10 17:18:49 -0800 | [diff] [blame] | 87 | /** |
| 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 | */ |
yoonseon | c6a6927 | 2017-01-12 18:22:20 -0800 | [diff] [blame] | 93 | public Class getEventClass() { |
yoonseon | fe72197 | 2017-01-10 17:18:49 -0800 | [diff] [blame] | 94 | 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 | } |
yoonseon | fe72197 | 2017-01-10 17:18:49 -0800 | [diff] [blame] | 105 | } |