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 | |
| 18 | import org.onosproject.event.Event; |
| 19 | import org.onosproject.event.EventDeliveryService; |
| 20 | import org.onosproject.event.EventListener; |
| 21 | import org.onosproject.event.ListenerService; |
| 22 | import org.onosproject.incubator.net.virtual.NetworkId; |
| 23 | |
| 24 | /** |
| 25 | * Basis for virtual event components which need to export listener mechanism. |
| 26 | */ |
| 27 | public abstract class AbstractVirtualListenerManager |
| 28 | <E extends Event, L extends EventListener<E>> |
| 29 | implements ListenerService<E, L> { |
| 30 | |
| 31 | protected final NetworkId networkId; |
| 32 | |
| 33 | protected EventDeliveryService eventDispatcher; |
| 34 | |
| 35 | VirtualListenerRegistryManager listenerManager = |
| 36 | VirtualListenerRegistryManager.getInstance(); |
| 37 | |
| 38 | public AbstractVirtualListenerManager(NetworkId networkId) { |
| 39 | this.networkId = networkId; |
| 40 | } |
| 41 | |
| 42 | @Override |
| 43 | public void addListener(L listener) { |
| 44 | listenerManager.getRegistry(networkId, getEventClass()) |
| 45 | .addListener(listener); |
| 46 | } |
| 47 | |
| 48 | @Override |
| 49 | public void removeListener(L listener) { |
| 50 | listenerManager.getRegistry(networkId, getEventClass()) |
| 51 | .removeListener(listener); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Safely posts the specified event to the local event dispatcher. |
| 56 | * If there is no event dispatcher or if the event is null, this method |
| 57 | * is a noop. |
| 58 | * |
| 59 | * @param event event to be posted; may be null |
| 60 | */ |
| 61 | protected void post(E event) { |
| 62 | if (event != null && eventDispatcher != null) { |
| 63 | VirtualEvent<E> vEvent = |
| 64 | new VirtualEvent<E>(networkId, VirtualEvent.Type.POSTED, event); |
| 65 | eventDispatcher.post(vEvent); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Returns the class type of parameter type. |
| 71 | * More specifically, it returns the class type of event class. |
| 72 | * |
| 73 | * @return the class type of provider service of the service |
| 74 | */ |
| 75 | private Class getEventClass() { |
| 76 | String className = this.getClass().getGenericSuperclass().toString(); |
| 77 | String pramType = className.split("<")[1].split(",")[0]; |
| 78 | |
| 79 | try { |
| 80 | return Class.forName(pramType); |
| 81 | } catch (ClassNotFoundException e) { |
| 82 | e.printStackTrace(); |
| 83 | } |
| 84 | |
| 85 | return null; |
| 86 | } |
| 87 | |
| 88 | } |