CDS: Add stress test RPC to the cars model
[controller.git] / opendaylight / md-sal / sal-netconf-connector / src / main / java / org / opendaylight / controller / sal / connect / netconf / sal / NetconfDeviceNotificationService.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.controller.sal.connect.netconf.sal;
10
11 import com.google.common.collect.HashMultimap;
12 import com.google.common.collect.Lists;
13 import com.google.common.collect.Multimap;
14 import java.util.Collection;
15 import javax.annotation.Nonnull;
16 import org.opendaylight.controller.md.sal.dom.api.DOMNotification;
17 import org.opendaylight.controller.md.sal.dom.api.DOMNotificationListener;
18 import org.opendaylight.controller.md.sal.dom.api.DOMNotificationService;
19 import org.opendaylight.yangtools.concepts.ListenerRegistration;
20 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
21
22 class NetconfDeviceNotificationService implements DOMNotificationService {
23
24     private final Multimap<SchemaPath, DOMNotificationListener> listeners = HashMultimap.create();
25
26     // Notification publish is very simple and hijacks the thread of the caller
27     // TODO shouldnt we reuse the implementation for notification router from sal-broker-impl ?
28     public synchronized void publishNotification(final DOMNotification notification) {
29         for (final DOMNotificationListener domNotificationListener : listeners.get(notification.getType())) {
30             domNotificationListener.onNotification(notification);
31         }
32     }
33
34     @Override
35     public synchronized <T extends DOMNotificationListener> ListenerRegistration<T> registerNotificationListener(@Nonnull final T listener, @Nonnull final Collection<SchemaPath> types) {
36         for (final SchemaPath type : types) {
37             listeners.put(type, listener);
38         }
39
40         // FIXME this should invoke create-subscription rpc on the remote device for a given notification
41
42         return new ListenerRegistration<T>() {
43             @Override
44             public void close() {
45                 for (final SchemaPath type : types) {
46                     listeners.remove(type, listener);
47                 }
48             }
49
50             @Override
51             public T getInstance() {
52                 return listener;
53             }
54         };
55     }
56
57     @Override
58     public synchronized <T extends DOMNotificationListener> ListenerRegistration<T> registerNotificationListener(@Nonnull final T listener, final SchemaPath... types) {
59         return registerNotificationListener(listener, Lists.newArrayList(types));
60     }
61 }