Merge "BUG-832 Fix exception from sal-netconf-connector."
[controller.git] / opendaylight / md-sal / inventory-manager / src / main / java / org / opendaylight / controller / md / inventory / manager / FlowCapableInventoryProvider.java
1 /**
2  * Copyright (c) 2014 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.md.inventory.manager;
9
10 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
11 import org.opendaylight.controller.sal.binding.api.data.DataModificationTransaction;
12 import org.opendaylight.controller.sal.binding.api.data.DataProviderService;
13 import org.opendaylight.yangtools.concepts.Registration;
14 import org.opendaylight.yangtools.yang.binding.NotificationListener;
15 import org.slf4j.Logger;
16 import org.slf4j.LoggerFactory;
17
18 public class FlowCapableInventoryProvider implements AutoCloseable {
19
20     private final static Logger LOG = LoggerFactory.getLogger(FlowCapableInventoryProvider.class);
21
22     private DataProviderService dataService;
23     private NotificationProviderService notificationService;
24     private Registration<NotificationListener> listenerRegistration;
25     private final NodeChangeCommiter changeCommiter = new NodeChangeCommiter(FlowCapableInventoryProvider.this);
26
27     public void start() {
28         this.listenerRegistration = this.notificationService.registerNotificationListener(this.changeCommiter);
29         LOG.info("Flow Capable Inventory Provider started.");
30     }
31
32     protected DataModificationTransaction startChange() {
33         DataProviderService _dataService = this.dataService;
34         return _dataService.beginTransaction();
35     }
36
37     @Override
38     public void close() {
39         try {
40             LOG.info("Flow Capable Inventory Provider stopped.");
41             if (this.listenerRegistration != null) {
42                 this.listenerRegistration.close();
43             }
44         } catch (Exception e) {
45             String errMsg = "Error by stop Flow Capable Inventory Provider.";
46             LOG.error(errMsg, e);
47             throw new RuntimeException(errMsg, e);
48         }
49     }
50
51     public DataProviderService getDataService() {
52         return this.dataService;
53     }
54
55     public void setDataService(final DataProviderService dataService) {
56         this.dataService = dataService;
57     }
58
59     public NotificationProviderService getNotificationService() {
60         return this.notificationService;
61     }
62
63     public void setNotificationService(
64             final NotificationProviderService notificationService) {
65         this.notificationService = notificationService;
66     }
67 }