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