Convert blueprint extensions to MDSAL APIs
[controller.git] / opendaylight / blueprint / src / main / java / org / opendaylight / controller / blueprint / ext / NotificationListenerBean.java
1 /*
2  * Copyright (c) 2016 Brocade Communications 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.blueprint.ext;
9
10 import org.opendaylight.mdsal.binding.api.NotificationService;
11 import org.opendaylight.yangtools.concepts.ListenerRegistration;
12 import org.opendaylight.yangtools.yang.binding.NotificationListener;
13 import org.osgi.framework.Bundle;
14 import org.slf4j.Logger;
15 import org.slf4j.LoggerFactory;
16
17 /**
18  * Blueprint bean corresponding to the "notification-listener" element that registers a NotificationListener
19  * with the NotificationService.
20  *
21  * @author Thomas Pantelis
22  */
23 public class NotificationListenerBean {
24     private static final Logger LOG = LoggerFactory.getLogger(NotificationListenerBean.class);
25     static final String NOTIFICATION_LISTENER = "notification-listener";
26
27     private Bundle bundle;
28     private NotificationService notificationService;
29     private NotificationListener notificationListener;
30     private ListenerRegistration<?> registration;
31
32     public void setNotificationService(final NotificationService notificationService) {
33         this.notificationService = notificationService;
34     }
35
36     public void setNotificationListener(final NotificationListener notificationListener) {
37         this.notificationListener = notificationListener;
38     }
39
40     public void setBundle(final Bundle bundle) {
41         this.bundle = bundle;
42     }
43
44     public void init() {
45         LOG.debug("{}: init - registering NotificationListener {}", bundle.getSymbolicName(), notificationListener);
46
47         registration = notificationService.registerNotificationListener(notificationListener);
48     }
49
50     public void destroy() {
51         if (registration != null) {
52             LOG.debug("{}: destroy - closing ListenerRegistration {}", bundle.getSymbolicName(), notificationListener);
53             registration.close();
54         } else {
55             LOG.debug("{}: destroy - listener was not registered", bundle.getSymbolicName());
56         }
57     }
58 }