Configurable update-strategy for clusteredAppConfig
[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.controller.md.sal.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(NotificationService notificationService) {
33         this.notificationService = notificationService;
34     }
35     public void setNotificationListener(NotificationListener notificationListener) {
36         this.notificationListener = notificationListener;
37     }
38
39     public void setBundle(Bundle bundle) {
40         this.bundle = bundle;
41     }
42
43     public void init() {
44         LOG.debug("{}: init - registering NotificationListener {}", bundle.getSymbolicName(), notificationListener);
45
46         registration = notificationService.registerNotificationListener(notificationListener);
47     }
48
49     public void destroy() {
50         if(registration != null) {
51             LOG.debug("{}: destroy - closing ListenerRegistration {}", bundle.getSymbolicName(), notificationListener);
52             registration.close();
53         } else {
54             LOG.debug("{}: destroy - listener was not registered", bundle.getSymbolicName());
55         }
56     }
57 }