Changed NetconfDeviceDatastoreAdapter and NetconfDeviceTopologyAdapter to use Transac...
[controller.git] / opendaylight / md-sal / sal-binding-broker / src / main / java / org / opendaylight / controller / md / sal / binding / impl / ForwardedNotificationService.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 package org.opendaylight.controller.md.sal.binding.impl;
9
10 import java.util.ArrayList;
11 import java.util.Collection;
12 import java.util.List;
13 import java.util.Set;
14 import javax.annotation.Nonnull;
15 import org.opendaylight.controller.md.sal.binding.api.NotificationService;
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.controller.sal.binding.spi.NotificationInvokerFactory;
20 import org.opendaylight.yangtools.binding.data.codec.api.BindingNormalizedNodeSerializer;
21 import org.opendaylight.yangtools.concepts.AbstractListenerRegistration;
22 import org.opendaylight.yangtools.concepts.ListenerRegistration;
23 import org.opendaylight.yangtools.yang.binding.Notification;
24 import org.opendaylight.yangtools.yang.binding.NotificationListener;
25 import org.opendaylight.yangtools.yang.binding.util.BindingReflections;
26 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
27
28 public class ForwardedNotificationService implements NotificationService, AutoCloseable {
29
30     private final BindingNormalizedNodeSerializer codec;
31     private final DOMNotificationService domNotifService;
32     private final NotificationInvokerFactory notificationInvokerFactory;
33
34     public ForwardedNotificationService(BindingNormalizedNodeSerializer codec, DOMNotificationService domNotifService, NotificationInvokerFactory notificationInvokerFactory) {
35         this.codec = codec;
36         this.domNotifService = domNotifService;
37         this.notificationInvokerFactory = notificationInvokerFactory;
38     }
39
40     @Override
41     public <T extends NotificationListener> ListenerRegistration<T> registerNotificationListener(T listener) {
42         final NotificationInvokerFactory.NotificationInvoker invoker = notificationInvokerFactory.invokerFor(listener);
43         final DOMNotificationListener domListener = new NotificationInvokerImpl(invoker);
44         final Collection<SchemaPath> schemaPaths = convertNotifTypesToSchemaPath(invoker.getSupportedNotifications());
45         final ListenerRegistration<DOMNotificationListener> domRegistration =
46                 domNotifService.registerNotificationListener(domListener, schemaPaths);
47         return new ListenerRegistrationImpl<>(listener, domRegistration);
48     }
49
50
51
52     private Collection<SchemaPath> convertNotifTypesToSchemaPath(Set<Class<? extends Notification>> notificationTypes) {
53         final List<SchemaPath> schemaPaths = new ArrayList<>();
54         for (Class<? extends Notification> notificationType : notificationTypes) {
55             schemaPaths.add(SchemaPath.create(true, BindingReflections.findQName(notificationType)));
56         }
57         return schemaPaths;
58     }
59
60     @Override
61     public void close() throws Exception {
62
63     }
64
65     private static class ListenerRegistrationImpl<T extends NotificationListener> extends AbstractListenerRegistration<T> {
66         private final ListenerRegistration<?> listenerRegistration;
67
68         public ListenerRegistrationImpl(T listener, ListenerRegistration<?> listenerRegistration) {
69             super(listener);
70             this.listenerRegistration = listenerRegistration;
71         }
72
73         @Override
74         protected void removeRegistration() {
75             listenerRegistration.close();
76         }
77     }
78
79     private class NotificationInvokerImpl implements DOMNotificationListener {
80         private final NotificationInvokerFactory.NotificationInvoker invoker;
81
82         public NotificationInvokerImpl(NotificationInvokerFactory.NotificationInvoker invoker) {
83             this.invoker = invoker;
84         }
85
86         @Override
87         public void onNotification(@Nonnull DOMNotification notification) {
88             final Notification baNotification =
89                     codec.fromNormalizedNodeNotification(notification.getType(), notification.getBody());
90             invoker.getInvocationProxy().onNotification(baNotification);
91
92         }
93     }
94
95 }