3275f2979692aa8ba9c472c6a7a8cf6f03ca3277
[netconf.git] / netconf / mdsal-netconf-notification / src / main / java / org / opendaylight / controller / config / yang / netconf / mdsal / notification / OperationalDatastoreListener.java
1 /*
2  * Copyright (c) 2016 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.config.yang.netconf.mdsal.notification;
9
10 import org.opendaylight.mdsal.binding.api.DataBroker;
11 import org.opendaylight.mdsal.binding.api.DataTreeChangeListener;
12 import org.opendaylight.mdsal.binding.api.DataTreeIdentifier;
13 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
14 import org.opendaylight.yangtools.concepts.ListenerRegistration;
15 import org.opendaylight.yangtools.yang.binding.DataObject;
16 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
17
18 /**
19  * Abstract base class for subclasses, which want to listen for changes on specified subtree in operational datastore.
20  *
21  * @param <T> data object class
22  */
23 abstract class OperationalDatastoreListener<T extends DataObject> implements DataTreeChangeListener<T> {
24
25     private final InstanceIdentifier<T> instanceIdentifier;
26
27     /**
28      * Constructor.
29      *
30      * @param instanceIdentifier instance identifier of subtree, on which this instance should listen on changes.
31      */
32     OperationalDatastoreListener(final InstanceIdentifier<T> instanceIdentifier) {
33         this.instanceIdentifier = instanceIdentifier;
34     }
35
36     /**
37      * Registers this instance as OPERATIONAL datastore listener via provided dataBroker.
38      *
39      * @param dataBroker data broker
40      * @return listener registration
41      */
42     ListenerRegistration<OperationalDatastoreListener<T>> registerOnChanges(final DataBroker dataBroker) {
43         DataTreeIdentifier<T> id = DataTreeIdentifier.create(LogicalDatastoreType.OPERATIONAL, instanceIdentifier);
44         return dataBroker.registerDataTreeChangeListener(id, this);
45     }
46
47 }