BUG-2314 Migrate netconf-connector to NormalizedNode
[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 static org.opendaylight.controller.sal.connect.netconf.util.NetconfMessageTransformUtil.toPath;
12
13 import com.google.common.collect.HashMultimap;
14 import com.google.common.collect.Lists;
15 import com.google.common.collect.Multimap;
16 import java.util.Collection;
17 import javax.annotation.Nonnull;
18 import org.opendaylight.controller.md.sal.dom.api.DOMNotification;
19 import org.opendaylight.controller.md.sal.dom.api.DOMNotificationListener;
20 import org.opendaylight.controller.md.sal.dom.api.DOMNotificationService;
21 import org.opendaylight.yangtools.concepts.ListenerRegistration;
22 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
23 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
24
25 class NetconfDeviceNotificationService implements DOMNotificationService {
26
27     private final Multimap<SchemaPath, DOMNotificationListener> listeners = HashMultimap.create();
28
29     // Notification publish is very simple and hijacks the thread of the caller
30     // TODO shouldnt we reuse the implementation for notification router from sal-broker-impl ?
31     public synchronized void publishNotification(final ContainerNode notification) {
32         final SchemaPath schemaPath = toPath(notification.getNodeType());
33         for (final DOMNotificationListener domNotificationListener : listeners.get(schemaPath)) {
34             domNotificationListener.onNotification(new DOMNotification() {
35                 @Nonnull
36                 @Override
37                 public SchemaPath getType() {
38                     return schemaPath;
39                 }
40
41                 @Nonnull
42                 @Override
43                 public ContainerNode getBody() {
44                     return notification;
45                 }
46             });
47         }
48     }
49
50     @Override
51     public synchronized <T extends DOMNotificationListener> ListenerRegistration<T> registerNotificationListener(@Nonnull final T listener, @Nonnull final Collection<SchemaPath> types) {
52         for (final SchemaPath type : types) {
53             listeners.put(type, listener);
54         }
55
56         // FIXME this should invoke create-subscription rpc on the remote device for a given notification
57
58         return new ListenerRegistration<T>() {
59             @Override
60             public void close() {
61                 for (final SchemaPath type : types) {
62                     listeners.remove(type, listener);
63                 }
64             }
65
66             @Override
67             public T getInstance() {
68                 return listener;
69             }
70         };
71     }
72
73     @Override
74     public synchronized <T extends DOMNotificationListener> ListenerRegistration<T> registerNotificationListener(@Nonnull final T listener, final SchemaPath... types) {
75         return registerNotificationListener(listener, Lists.newArrayList(types));
76     }
77 }