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 com.google.common.collect.ClassToInstanceMap;
11 import com.google.common.collect.ImmutableSet;
12 import java.util.ArrayList;
13 import java.util.Collection;
14 import java.util.List;
16 import javax.annotation.Nonnull;
17 import org.opendaylight.controller.md.sal.binding.api.NotificationService;
18 import org.opendaylight.controller.md.sal.binding.impl.BindingDOMAdapterBuilder.Factory;
19 import org.opendaylight.controller.md.sal.dom.api.DOMNotification;
20 import org.opendaylight.controller.md.sal.dom.api.DOMNotificationListener;
21 import org.opendaylight.controller.md.sal.dom.api.DOMNotificationService;
22 import org.opendaylight.controller.md.sal.dom.api.DOMService;
23 import org.opendaylight.controller.sal.binding.codegen.impl.SingletonHolder;
24 import org.opendaylight.controller.sal.binding.spi.NotificationInvokerFactory;
25 import org.opendaylight.yangtools.binding.data.codec.api.BindingNormalizedNodeSerializer;
26 import org.opendaylight.yangtools.concepts.AbstractListenerRegistration;
27 import org.opendaylight.yangtools.concepts.ListenerRegistration;
28 import org.opendaylight.yangtools.yang.binding.Notification;
29 import org.opendaylight.yangtools.yang.binding.NotificationListener;
30 import org.opendaylight.yangtools.yang.binding.util.BindingReflections;
31 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
33 public class BindingDOMNotificationServiceAdapter implements NotificationService, AutoCloseable {
35 public static final Factory<NotificationService> BUILDER_FACTORY = new Factory<NotificationService>() {
38 public BindingDOMAdapterBuilder<NotificationService> newBuilder() {
43 private final BindingNormalizedNodeSerializer codec;
44 private final DOMNotificationService domNotifService;
45 private final NotificationInvokerFactory notificationInvokerFactory;
47 public BindingDOMNotificationServiceAdapter(BindingNormalizedNodeSerializer codec, DOMNotificationService domNotifService, NotificationInvokerFactory notificationInvokerFactory) {
49 this.domNotifService = domNotifService;
50 this.notificationInvokerFactory = notificationInvokerFactory;
54 public <T extends NotificationListener> ListenerRegistration<T> registerNotificationListener(T listener) {
55 final NotificationInvokerFactory.NotificationInvoker invoker = notificationInvokerFactory.invokerFor(listener);
56 final DOMNotificationListener domListener = new NotificationInvokerImpl(invoker);
57 final Collection<SchemaPath> schemaPaths = convertNotifTypesToSchemaPath(invoker.getSupportedNotifications());
58 final ListenerRegistration<DOMNotificationListener> domRegistration =
59 domNotifService.registerNotificationListener(domListener, schemaPaths);
60 return new ListenerRegistrationImpl<>(listener, domRegistration);
65 private Collection<SchemaPath> convertNotifTypesToSchemaPath(Set<Class<? extends Notification>> notificationTypes) {
66 final List<SchemaPath> schemaPaths = new ArrayList<>();
67 for (Class<? extends Notification> notificationType : notificationTypes) {
68 schemaPaths.add(SchemaPath.create(true, BindingReflections.findQName(notificationType)));
74 public void close() throws Exception {
78 private static class ListenerRegistrationImpl<T extends NotificationListener> extends AbstractListenerRegistration<T> {
79 private final ListenerRegistration<?> listenerRegistration;
81 public ListenerRegistrationImpl(T listener, ListenerRegistration<?> listenerRegistration) {
83 this.listenerRegistration = listenerRegistration;
87 protected void removeRegistration() {
88 listenerRegistration.close();
92 private class NotificationInvokerImpl implements DOMNotificationListener {
93 private final NotificationInvokerFactory.NotificationInvoker invoker;
95 public NotificationInvokerImpl(NotificationInvokerFactory.NotificationInvoker invoker) {
96 this.invoker = invoker;
100 public void onNotification(@Nonnull DOMNotification notification) {
101 final Notification baNotification =
102 codec.fromNormalizedNodeNotification(notification.getType(), notification.getBody());
103 invoker.getInvocationProxy().onNotification(baNotification);
108 private static class Builder extends BindingDOMAdapterBuilder<NotificationService> {
112 protected NotificationService createInstance(BindingToNormalizedNodeCodec codec,
113 ClassToInstanceMap<DOMService> delegates) {
114 DOMNotificationService domNotification = delegates.getInstance(DOMNotificationService.class);
115 NotificationInvokerFactory invokerFactory = SingletonHolder.INVOKER_FACTORY;
116 return new BindingDOMNotificationServiceAdapter(codec.getCodecRegistry(), domNotification, invokerFactory);
120 public Set<? extends Class<? extends DOMService>> getRequiredDelegates() {
121 return ImmutableSet.of(DOMNotificationService.class);