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 java.util.ArrayList;
11 import java.util.List;
13 import java.util.concurrent.ExecutorService;
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;
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;
32 public class NotificationServiceImpl implements NotificationService {
33 private final SalRemoteService salRemoteService;
34 private final RestconfClientContext restconfClientContext;
36 private final Multimap<Class<? extends Notification>,NotificationListener<? extends Object>> listeners;
37 private ExecutorService _executor;
39 public NotificationServiceImpl(RestconfClientContext restconfClienetContext){
40 this.restconfClientContext = restconfClienetContext;
41 this.salRemoteService = this.restconfClientContext.getRpcServiceContext(SalRemoteService.class).getRpcService();
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;
48 public ExecutorService getExecutor() {
49 return this._executor;
52 public void setExecutor(final ExecutorService executor) {
53 this._executor = executor;
57 public <T extends Notification> void addNotificationListener(Class<T> notificationType, NotificationListener<T> listener) {
58 this.listeners.put(notificationType, listener);
62 public void addNotificationListener(org.opendaylight.yangtools.yang.binding.NotificationListener listener) {
63 UnsupportedOperationException _unsupportedOperationException = new UnsupportedOperationException("Deprecated method. Use registerNotificationListener instead.");
64 throw _unsupportedOperationException;
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;
75 public <T extends Notification> void removeNotificationListener(Class<T> notificationType, NotificationListener<T> listener) {
76 this.listeners.remove(notificationType, listener);
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);
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);
99 private class SalNotificationRegistration<T extends Notification> implements Registration<NotificationListener<T>>{
100 private final 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();