manually cherry pick this patch https://git.opendaylight.org/gerrit/#/c/34579/
[unimgr.git] / impl / src / main / java / org / opendaylight / unimgr / api / UnimgrDataTreeChangeListener.java
1 /*
2  * Copyright (c) 2016 CableLabs 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.unimgr.api;
9
10 import java.util.Collection;
11
12 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
13 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
14 import org.opendaylight.controller.md.sal.binding.api.DataTreeChangeListener;
15 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
16 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Link;
17 import org.opendaylight.yangtools.yang.binding.DataObject;
18
19 /**
20  * abstract class for unimgr data tree changes.
21  * @author mohamed el-serngawy
22  * @param <D> extended data object
23  */
24 public abstract class UnimgrDataTreeChangeListener<D extends DataObject> implements DataTreeChangeListener<D>, AutoCloseable {
25
26     protected DataBroker dataBroker;
27
28     public UnimgrDataTreeChangeListener(final DataBroker dataBroker) {
29         this.dataBroker = dataBroker;
30     }
31
32     /**
33      * Basic Implementation of DataTreeChange Listener to execute add, update or remove command
34      * based on the data object modification type.
35      */
36     @Override
37     public void onDataTreeChanged(Collection<DataTreeModification<D>> collection) {
38         for (final DataTreeModification<D> change : collection) {
39             final DataObjectModification<D> root = change.getRootNode();
40             switch (root.getModificationType()) {
41                 case SUBTREE_MODIFIED:
42                     update(change);
43                     break;
44                 case WRITE:
45                     add(change);
46                     break;
47                 case DELETE:
48                     remove(change);
49                     break;
50             }
51         }
52     }
53
54     /**
55      * method should implements the added data object command
56      * @param newDataObject
57      */
58     public abstract void add(DataTreeModification<D> newDataObject);
59
60     /**
61      * method should implements the removed data object command
62      * @param removedDataObject
63      */
64     public abstract void remove(DataTreeModification<D> removedDataObject);
65
66     /**
67      * method should implements the updated data object command
68      * @param modifiedDataObject
69      */
70     public abstract void update(DataTreeModification<D> modifiedDataObject);
71 }