c6203efec0357c53edb9a3a2be78646299872c1a
[openflowplugin.git] / applications / topology-lldp-discovery / src / main / java / org / opendaylight / openflowplugin / applications / topology / lldp / LLDPDiscoveryProvider.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.openflowplugin.applications.topology.lldp;
9
10 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
11 import org.opendaylight.controller.sal.binding.api.data.DataProviderService;
12 import org.opendaylight.openflowplugin.applications.topology.lldp.utils.LLDPDiscoveryUtils;
13 import org.opendaylight.yangtools.concepts.ListenerRegistration;
14 import org.opendaylight.yangtools.yang.binding.NotificationListener;
15 import org.slf4j.Logger;
16 import org.slf4j.LoggerFactory;
17
18 public class LLDPDiscoveryProvider implements AutoCloseable {
19     private static final Logger LOG = LoggerFactory.getLogger(LLDPDiscoveryProvider.class);
20     private DataProviderService dataService;
21     private NotificationProviderService notificationService;
22
23     private ListenerRegistration<NotificationListener> listenerRegistration;
24     private LLDPLinkAger lldpLinkAger;
25
26     public DataProviderService getDataService() {
27         return this.dataService;
28     }
29
30     public void setDataService(final DataProviderService dataService) {
31         this.dataService = dataService;
32     }
33
34     public NotificationProviderService getNotificationService() {
35         return this.notificationService;
36     }
37
38     public void setNotificationService(final NotificationProviderService notificationService) {
39         this.notificationService = notificationService;
40     }
41
42     public void start() {
43         lldpLinkAger = new LLDPLinkAger(LLDPDiscoveryUtils.LLDP_INTERVAL, LLDPDiscoveryUtils.LLDP_EXPIRATION_TIME);
44         lldpLinkAger.setNotificationService(notificationService);
45
46         LLDPDiscoveryListener committer = new LLDPDiscoveryListener(notificationService);
47         committer.setLldpLinkAger(lldpLinkAger);
48
49         ListenerRegistration<NotificationListener> registerNotificationListener =
50                 notificationService.registerNotificationListener(committer);
51         this.listenerRegistration = registerNotificationListener;
52         LOG.info("LLDPDiscoveryListener Started.");
53     }
54
55     public void close() {
56         try {
57             LOG.info("LLDPDiscoveryListener stopped.");
58             if (this.listenerRegistration!=null) {
59                 this.listenerRegistration.close();
60             }
61             lldpLinkAger.close();
62         } catch (Exception e) {
63             throw new Error(e);
64         }
65     }
66 }