2 * Copyright (c) 2013 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.sal.restconf.broker.impl;
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;
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;
30 public class NotificationServiceImpl implements NotificationService {
31 private SalRemoteService salRemoteService;
32 private RestconfClientContext restconfClientContext;
34 private final Multimap<Class<? extends Notification>,NotificationListener<? extends Object>> listeners;
35 private ExecutorService _executor;
37 public NotificationServiceImpl(RestconfClientContext restconfClienetContext){
38 this.restconfClientContext = restconfClienetContext;
39 this.salRemoteService = this.restconfClientContext.getRpcServiceContext(SalRemoteService.class).getRpcService();
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;
46 public ExecutorService getExecutor() {
47 return this._executor;
50 public void setExecutor(final ExecutorService executor) {
51 this._executor = executor;
55 public <T extends Notification> void addNotificationListener(Class<T> notificationType, NotificationListener<T> listener) {
56 this.listeners.put(notificationType, listener);
60 public void addNotificationListener(org.opendaylight.yangtools.yang.binding.NotificationListener listener) {
61 UnsupportedOperationException _unsupportedOperationException = new UnsupportedOperationException("Deprecated method. Use registerNotificationListener instead.");
62 throw _unsupportedOperationException;
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;
73 public <T extends Notification> void removeNotificationListener(Class<T> notificationType, NotificationListener<T> listener) {
74 this.listeners.remove(notificationType, listener);
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;
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;
99 private class SalNotificationRegistration<T extends Notification> implements Registration<NotificationListener<T>>{
100 private Registration registration;
102 public SalNotificationRegistration(ListenerRegistration listenerRegistration){
103 this.registration = listenerRegistration;
107 public NotificationListener<T> getInstance() {
108 return this.getInstance();
112 public void close() throws Exception {
113 this.registration.close();