Merge "Fixed bug when Binding-Aware Data Change Listeners we're not triggered."
[controller.git] / opendaylight / md-sal / sal-restconf-broker / src / main / java / org / opendaylight / controller / sal / restconf / broker / impl / NotificationServiceImpl.java
1 /*
2  * Copyright (c) 2013 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.sal.restconf.broker.impl;
9
10 import com.google.common.collect.HashMultimap;
11 import com.google.common.collect.Multimap;
12 import com.google.common.collect.Multimaps;
13 import com.google.common.collect.SetMultimap;
14 import java.util.ArrayList;
15 import java.util.List;
16 import java.util.Map;
17 import java.util.concurrent.ExecutorService;
18 import org.opendaylight.controller.sal.binding.api.NotificationListener;
19 import org.opendaylight.controller.sal.binding.api.NotificationService;
20 import org.opendaylight.controller.sal.restconf.broker.listeners.RemoteNotificationListener;
21 import org.opendaylight.controller.sal.restconf.broker.tools.RemoteStreamTools;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.remote.rev140114.QName;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.remote.rev140114.SalRemoteService;
24 import org.opendaylight.yangtools.concepts.ListenerRegistration;
25 import org.opendaylight.yangtools.concepts.Registration;
26 import org.opendaylight.yangtools.restconf.client.api.RestconfClientContext;
27 import org.opendaylight.yangtools.restconf.client.api.event.EventStreamInfo;
28 import org.opendaylight.yangtools.yang.binding.Notification;
29
30 public class NotificationServiceImpl implements NotificationService {
31     private SalRemoteService salRemoteService;
32     private RestconfClientContext restconfClientContext;
33
34     private final Multimap<Class<? extends Notification>,NotificationListener<? extends Object>> listeners;
35     private ExecutorService _executor;
36
37     public NotificationServiceImpl(RestconfClientContext restconfClienetContext){
38         this.restconfClientContext = restconfClienetContext;
39         this.salRemoteService = this.restconfClientContext.getRpcServiceContext(SalRemoteService.class).getRpcService();
40
41         HashMultimap<Class<? extends Notification>,NotificationListener<? extends Object>> _create = HashMultimap.<Class<? extends Notification>, NotificationListener<? extends Object>>create();
42         SetMultimap<Class<? extends Notification>,NotificationListener<? extends Object>> _synchronizedSetMultimap = Multimaps.<Class<? extends Notification>, NotificationListener<? extends Object>>synchronizedSetMultimap(_create);
43         this.listeners = _synchronizedSetMultimap;
44
45     }
46     public ExecutorService getExecutor() {
47         return this._executor;
48     }
49
50     public void setExecutor(final ExecutorService executor) {
51         this._executor = executor;
52     }
53
54     @Override
55     public <T extends Notification> void addNotificationListener(Class<T> notificationType, NotificationListener<T> listener) {
56         this.listeners.put(notificationType, listener);
57     }
58
59     @Override
60     public void addNotificationListener(org.opendaylight.yangtools.yang.binding.NotificationListener listener) {
61         UnsupportedOperationException _unsupportedOperationException = new UnsupportedOperationException("Deprecated method. Use registerNotificationListener instead.");
62         throw _unsupportedOperationException;
63     }
64
65     @Override
66     public void removeNotificationListener(org.opendaylight.yangtools.yang.binding.NotificationListener listener) {
67         UnsupportedOperationException _unsupportedOperationException = new UnsupportedOperationException(
68                 "Deprecated method. Use RegisterNotificationListener returned value to close registration.");
69         throw _unsupportedOperationException;
70     }
71
72     @Override
73     public <T extends Notification> void removeNotificationListener(Class<T> notificationType, NotificationListener<T> listener) {
74         this.listeners.remove(notificationType, listener);
75     }
76
77     @Override
78     public <T extends Notification> Registration<NotificationListener<T>> registerNotificationListener(Class<T> notificationType, NotificationListener<T> listener) {
79         //TODO implementation using sal-remote
80         List<QName> notifications = new ArrayList<QName>();
81         notifications.add(new QName(notificationType.toString()));
82         String notificationStreamName = RemoteStreamTools.createNotificationStream(salRemoteService, notifications);
83         final Map<String,EventStreamInfo> desiredEventStream = RemoteStreamTools.createEventStream(restconfClientContext, notificationStreamName);
84         RemoteNotificationListener remoteNotificationListener = new RemoteNotificationListener(listener);
85         ListenerRegistration listenerRegistration = restconfClientContext.getEventStreamContext(desiredEventStream.get(desiredEventStream.get(notificationStreamName))).registerNotificationListener(remoteNotificationListener);
86         SalNotificationRegistration salNotificationRegistration = new SalNotificationRegistration(listenerRegistration);
87         return salNotificationRegistration;
88     }
89
90     @Override
91     public Registration<org.opendaylight.yangtools.yang.binding.NotificationListener> registerNotificationListener(org.opendaylight.yangtools.yang.binding.NotificationListener listener) {
92         //TODO implementation using sal-remote
93         String notificationStreamName = RemoteStreamTools.createNotificationStream(salRemoteService, null);
94         final Map<String,EventStreamInfo> desiredEventStream = RemoteStreamTools.createEventStream(restconfClientContext, notificationStreamName);
95         ListenerRegistration listenerRegistration = restconfClientContext.getEventStreamContext(desiredEventStream.get(desiredEventStream.get(notificationStreamName))).registerNotificationListener(listener);
96         return listenerRegistration;
97     }
98
99     private class SalNotificationRegistration<T extends Notification> implements Registration<NotificationListener<T>>{
100         private Registration registration;
101
102         public SalNotificationRegistration(ListenerRegistration listenerRegistration){
103             this.registration = listenerRegistration;
104         }
105
106         @Override
107         public NotificationListener<T> getInstance() {
108             return this.getInstance();
109         }
110
111         @Override
112         public void close() throws Exception {
113             this.registration.close();
114         }
115     }
116
117
118 }