yoonseon | fe72197 | 2017-01-10 17:18:49 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
yoonseon | fe72197 | 2017-01-10 17:18:49 -0800 | [diff] [blame] | 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 | |
| 17 | package org.onosproject.incubator.net.virtual.event; |
| 18 | |
| 19 | import com.google.common.collect.Maps; |
| 20 | import org.onosproject.event.Event; |
| 21 | import org.onosproject.event.EventSink; |
| 22 | import org.onosproject.event.ListenerRegistry; |
| 23 | import org.onosproject.incubator.net.virtual.NetworkId; |
| 24 | |
| 25 | import java.util.Map; |
| 26 | |
| 27 | /** |
| 28 | * Base implementation of an virtual event sink and a registry capable of tracking |
| 29 | * listeners and dispatching events to them as part of event sink processing. |
| 30 | */ |
| 31 | public final class VirtualListenerRegistryManager |
| 32 | implements EventSink<VirtualEvent> { |
| 33 | |
| 34 | private Map<NetworkId, Map<Class<? extends Event>, ListenerRegistry>> |
| 35 | listenerMapByNetwork = Maps.newConcurrentMap(); |
| 36 | |
| 37 | ListenerRegistry lastStart; |
| 38 | |
| 39 | // non-instantiable (except for our Singleton) |
| 40 | private VirtualListenerRegistryManager() { |
| 41 | |
| 42 | } |
| 43 | |
| 44 | public static VirtualListenerRegistryManager getInstance() { |
| 45 | return SingletonHelper.INSTANCE; |
| 46 | } |
| 47 | |
| 48 | public ListenerRegistry getRegistry(NetworkId networkId, |
| 49 | Class<? extends Event> eventClass) { |
| 50 | Map<Class<? extends Event>, ListenerRegistry> listenerMapByEvent = |
| 51 | listenerMapByNetwork.get(networkId); |
| 52 | |
| 53 | if (listenerMapByEvent == null) { |
| 54 | listenerMapByEvent = Maps.newConcurrentMap(); |
| 55 | listenerMapByNetwork.putIfAbsent(networkId, listenerMapByEvent); |
| 56 | } |
| 57 | |
| 58 | ListenerRegistry listenerRegistry = listenerMapByEvent.get(eventClass); |
| 59 | |
| 60 | if (listenerRegistry == null) { |
| 61 | listenerRegistry = new ListenerRegistry(); |
| 62 | listenerMapByEvent.putIfAbsent(eventClass, listenerRegistry); |
| 63 | } |
| 64 | |
| 65 | return listenerRegistry; |
| 66 | } |
| 67 | |
| 68 | @Override |
| 69 | public void process(VirtualEvent event) { |
| 70 | NetworkId networkId = event.networkId(); |
| 71 | Event originalEvent = (Event) event.subject(); |
| 72 | |
| 73 | ListenerRegistry listenerRegistry = |
| 74 | listenerMapByNetwork.get(networkId).get(originalEvent.getClass()); |
Yoonseon Han | b14461b | 2017-03-07 14:08:01 +0900 | [diff] [blame] | 75 | if (listenerRegistry != null) { |
| 76 | listenerRegistry.process(originalEvent); |
| 77 | lastStart = listenerRegistry; |
| 78 | } |
yoonseon | fe72197 | 2017-01-10 17:18:49 -0800 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | @Override |
| 82 | public void onProcessLimit() { |
| 83 | lastStart.onProcessLimit(); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Prevents object instantiation from external. |
| 88 | */ |
| 89 | private static final class SingletonHelper { |
| 90 | private static final String ILLEGAL_ACCESS_MSG = |
| 91 | "Should not instantiate this class."; |
| 92 | private static final VirtualListenerRegistryManager INSTANCE = |
| 93 | new VirtualListenerRegistryManager(); |
| 94 | |
| 95 | private SingletonHelper() { |
| 96 | throw new IllegalAccessError(ILLEGAL_ACCESS_MSG); |
| 97 | } |
| 98 | } |
| 99 | } |