Fix raw references to Promise
[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
14 import org.opendaylight.controller.sal.binding.api.NotificationListener;
15 import org.opendaylight.controller.sal.binding.api.NotificationService;
16 import org.opendaylight.controller.sal.restconf.broker.listeners.RemoteNotificationListener;
17 import org.opendaylight.controller.sal.restconf.broker.tools.RemoteStreamTools;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.remote.rev140114.QName;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.remote.rev140114.SalRemoteService;
20 import org.opendaylight.yangtools.concepts.AbstractListenerRegistration;
21 import org.opendaylight.yangtools.concepts.ListenerRegistration;
22 import org.opendaylight.yangtools.restconf.client.api.RestconfClientContext;
23 import org.opendaylight.yangtools.restconf.client.api.event.EventStreamInfo;
24 import org.opendaylight.yangtools.yang.binding.Notification;
25
26 public class NotificationServiceImpl implements NotificationService {
27     private final SalRemoteService salRemoteService;
28     private final RestconfClientContext restconfClientContext;
29
30     public NotificationServiceImpl(RestconfClientContext restconfClienetContext){
31         this.restconfClientContext = restconfClienetContext;
32         this.salRemoteService = this.restconfClientContext.getRpcServiceContext(SalRemoteService.class).getRpcService();
33     }
34
35     @Override
36     public <T extends Notification> ListenerRegistration<NotificationListener<T>> registerNotificationListener(Class<T> notificationType, NotificationListener<T> listener) {
37         //TODO implementation using sal-remote
38         List<QName> notifications = new ArrayList<QName>();
39         notifications.add(new QName(notificationType.toString()));
40         String notificationStreamName = RemoteStreamTools.createNotificationStream(salRemoteService, notifications);
41         final Map<String,EventStreamInfo> desiredEventStream = RemoteStreamTools.createEventStream(restconfClientContext, notificationStreamName);
42         RemoteNotificationListener remoteNotificationListener = new RemoteNotificationListener(listener);
43
44         final ListenerRegistration<?> listenerRegistration = restconfClientContext.getEventStreamContext(desiredEventStream.get(desiredEventStream.get(notificationStreamName)))
45                 .registerNotificationListener(remoteNotificationListener);
46
47         return new AbstractListenerRegistration<NotificationListener<T>>(listener) {
48             @Override
49             protected void removeRegistration() {
50                 listenerRegistration.close();
51             }
52         };
53     }
54
55     @Override
56     public ListenerRegistration<org.opendaylight.yangtools.yang.binding.NotificationListener> registerNotificationListener(org.opendaylight.yangtools.yang.binding.NotificationListener listener) {
57         //TODO implementation using sal-remote
58         String notificationStreamName = RemoteStreamTools.createNotificationStream(salRemoteService, null);
59         final Map<String,EventStreamInfo> desiredEventStream = RemoteStreamTools.createEventStream(restconfClientContext, notificationStreamName);
60         return restconfClientContext.getEventStreamContext(desiredEventStream.get(desiredEventStream.get(notificationStreamName))).registerNotificationListener(listener);
61     }
62 }