2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.md.sal.binding.impl;
10 import java.util.ArrayList;
11 import java.util.Collection;
12 import java.util.List;
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;
28 public class ForwardedNotificationService implements NotificationService, AutoCloseable {
30 private final BindingNormalizedNodeSerializer codec;
31 private final DOMNotificationService domNotifService;
32 private final NotificationInvokerFactory notificationInvokerFactory;
34 public ForwardedNotificationService(BindingNormalizedNodeSerializer codec, DOMNotificationService domNotifService, NotificationInvokerFactory notificationInvokerFactory) {
36 this.domNotifService = domNotifService;
37 this.notificationInvokerFactory = notificationInvokerFactory;
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);
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)));
61 public void close() throws Exception {
65 private static class ListenerRegistrationImpl<T extends NotificationListener> extends AbstractListenerRegistration<T> {
66 private final ListenerRegistration<?> listenerRegistration;
68 public ListenerRegistrationImpl(T listener, ListenerRegistration<?> listenerRegistration) {
70 this.listenerRegistration = listenerRegistration;
74 protected void removeRegistration() {
75 listenerRegistration.close();
79 private class NotificationInvokerImpl implements DOMNotificationListener {
80 private final NotificationInvokerFactory.NotificationInvoker invoker;
82 public NotificationInvokerImpl(NotificationInvokerFactory.NotificationInvoker invoker) {
83 this.invoker = invoker;
87 public void onNotification(@Nonnull DOMNotification notification) {
88 final Notification baNotification =
89 codec.fromNormalizedNodeNotification(notification.getType(), notification.getBody());
90 invoker.getInvocationProxy().onNotification(baNotification);