Bug 2364: Migrated Binding MD-SAL to not use composites nodes
[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 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;
15 import java.util.Set;
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;
32
33 public class ForwardedNotificationService implements NotificationService, AutoCloseable {
34
35     public static final Factory<NotificationService> BUILDER_FACTORY = new Factory<NotificationService>() {
36
37         @Override
38         public BindingDOMAdapterBuilder<NotificationService> newBuilder() {
39             return new Builder();
40         }
41
42     };
43     private final BindingNormalizedNodeSerializer codec;
44     private final DOMNotificationService domNotifService;
45     private final NotificationInvokerFactory notificationInvokerFactory;
46
47     public ForwardedNotificationService(BindingNormalizedNodeSerializer codec, DOMNotificationService domNotifService, NotificationInvokerFactory notificationInvokerFactory) {
48         this.codec = codec;
49         this.domNotifService = domNotifService;
50         this.notificationInvokerFactory = notificationInvokerFactory;
51     }
52
53     @Override
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);
61     }
62
63
64
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)));
69         }
70         return schemaPaths;
71     }
72
73     @Override
74     public void close() throws Exception {
75
76     }
77
78     private static class ListenerRegistrationImpl<T extends NotificationListener> extends AbstractListenerRegistration<T> {
79         private final ListenerRegistration<?> listenerRegistration;
80
81         public ListenerRegistrationImpl(T listener, ListenerRegistration<?> listenerRegistration) {
82             super(listener);
83             this.listenerRegistration = listenerRegistration;
84         }
85
86         @Override
87         protected void removeRegistration() {
88             listenerRegistration.close();
89         }
90     }
91
92     private class NotificationInvokerImpl implements DOMNotificationListener {
93         private final NotificationInvokerFactory.NotificationInvoker invoker;
94
95         public NotificationInvokerImpl(NotificationInvokerFactory.NotificationInvoker invoker) {
96             this.invoker = invoker;
97         }
98
99         @Override
100         public void onNotification(@Nonnull DOMNotification notification) {
101             final Notification baNotification =
102                     codec.fromNormalizedNodeNotification(notification.getType(), notification.getBody());
103             invoker.getInvocationProxy().onNotification(baNotification);
104
105         }
106     }
107
108     private static class Builder extends BindingDOMAdapterBuilder<NotificationService> {
109
110
111         @Override
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 ForwardedNotificationService(codec.getCodecFactory(), domNotification, invokerFactory);
117         }
118
119         @Override
120         public Set<? extends Class<? extends DOMService>> getRequiredDelegates() {
121             return ImmutableSet.of(DOMNotificationService.class);
122         }
123
124
125
126     }
127 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.