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> Registration<NotificationListener<T>> registerNotificationListener(Class<T> notificationType, NotificationListener<T> listener) {
58 //TODO implementation using sal-remote
59 List<QName> notifications = new ArrayList<QName>();
60 notifications.add(new QName(notificationType.toString()));
61 String notificationStreamName = RemoteStreamTools.createNotificationStream(salRemoteService, notifications);
62 final Map<String,EventStreamInfo> desiredEventStream = RemoteStreamTools.createEventStream(restconfClientContext, notificationStreamName);
63 RemoteNotificationListener remoteNotificationListener = new RemoteNotificationListener(listener);
64 ListenerRegistration<?> listenerRegistration = restconfClientContext.getEventStreamContext(desiredEventStream.get(desiredEventStream.get(notificationStreamName))).registerNotificationListener(remoteNotificationListener);
65 return new SalNotificationRegistration<T>(listenerRegistration);
69 public Registration<org.opendaylight.yangtools.yang.binding.NotificationListener> registerNotificationListener(org.opendaylight.yangtools.yang.binding.NotificationListener listener) {
70 //TODO implementation using sal-remote
71 String notificationStreamName = RemoteStreamTools.createNotificationStream(salRemoteService, null);
72 final Map<String,EventStreamInfo> desiredEventStream = RemoteStreamTools.createEventStream(restconfClientContext, notificationStreamName);
73 return restconfClientContext.getEventStreamContext(desiredEventStream.get(desiredEventStream.get(notificationStreamName))).registerNotificationListener(listener);
76 private class SalNotificationRegistration<T extends Notification> implements Registration<NotificationListener<T>>{
77 private final Registration<?> registration;
79 public SalNotificationRegistration(ListenerRegistration<?> listenerRegistration){
80 this.registration = listenerRegistration;
84 public NotificationListener<T> getInstance() {
85 return this.getInstance();
89 public void close() throws Exception {
90 this.registration.close();