blob: 9d01a0037815320932d674c7f3928b72ef256f9b [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 */
16
17package org.onosproject.incubator.net.virtual.event;
18
19import com.google.common.collect.Maps;
20import org.onosproject.event.Event;
21import org.onosproject.event.EventSink;
22import org.onosproject.event.ListenerRegistry;
23import org.onosproject.incubator.net.virtual.NetworkId;
24
25import 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 */
31public 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());
75 listenerRegistry.process(originalEvent);
76 lastStart = listenerRegistry;
77 }
78
79 @Override
80 public void onProcessLimit() {
81 lastStart.onProcessLimit();
82 }
83
84 /**
85 * Prevents object instantiation from external.
86 */
87 private static final class SingletonHelper {
88 private static final String ILLEGAL_ACCESS_MSG =
89 "Should not instantiate this class.";
90 private static final VirtualListenerRegistryManager INSTANCE =
91 new VirtualListenerRegistryManager();
92
93 private SingletonHelper() {
94 throw new IllegalAccessError(ILLEGAL_ACCESS_MSG);
95 }
96 }
97}