manually cherry pick this patch https://git.opendaylight.org/gerrit/#/c/34579/
[unimgr.git] / impl / src / main / java / org / opendaylight / unimgr / impl / UniDataTreeChangeListener.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
9 package org.opendaylight.unimgr.impl;
10
11 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
12 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
13 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
14 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
15 import org.opendaylight.unimgr.api.UnimgrDataTreeChangeListener;
16 import org.opendaylight.unimgr.command.UniAddCommand;
17 import org.opendaylight.unimgr.command.UniRemoveCommand;
18 import org.opendaylight.unimgr.command.UniUpdateCommand;
19 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
20 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
21 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
22 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
23 import org.opendaylight.yangtools.concepts.ListenerRegistration;
24 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 public class UniDataTreeChangeListener extends UnimgrDataTreeChangeListener<Node> {
29
30     private static final Logger LOG = LoggerFactory.getLogger(UniDataTreeChangeListener.class);
31     private final ListenerRegistration<UniDataTreeChangeListener> listener;
32
33
34     public UniDataTreeChangeListener(final DataBroker dataBroker) {
35         super(dataBroker);
36         final InstanceIdentifier<Node> uniPath = getUniTopologyPath();
37         final DataTreeIdentifier<Node> dataTreeIid = new DataTreeIdentifier<>(LogicalDatastoreType.CONFIGURATION, uniPath);
38         listener = dataBroker.registerDataTreeChangeListener(dataTreeIid, this);
39         LOG.info("UniDataTreeChangeListener created and registered");
40     }
41
42     @Override
43     public void add(final DataTreeModification<Node> newDataObject) {
44         if (newDataObject.getRootPath() != null && newDataObject.getRootNode() != null) {
45             LOG.info("uni node {} created", newDataObject.getRootNode().getIdentifier());
46             final UniAddCommand uniAddCmd = new UniAddCommand(dataBroker, newDataObject);
47             uniAddCmd.execute();
48         }
49     }
50
51     @Override
52     public void close() throws Exception {
53          listener.close();
54     }
55
56     private InstanceIdentifier<Node> getUniTopologyPath() {
57         final InstanceIdentifier<Node> nodePath = InstanceIdentifier
58                                                 .create(NetworkTopology.class)
59                                                 .child(Topology.class,
60                                                         new TopologyKey(UnimgrConstants.UNI_TOPOLOGY_ID))
61                                                 .child(Node.class);
62         return nodePath;
63     }
64
65     @Override
66     public void remove(final DataTreeModification<Node> removedDataObject) {
67         if (removedDataObject.getRootPath() != null && removedDataObject.getRootNode() != null) {
68             LOG.info("uni node {} deleted", removedDataObject.getRootNode().getIdentifier());
69             final UniRemoveCommand uniRemoveCmd = new UniRemoveCommand(dataBroker, removedDataObject);
70             uniRemoveCmd.execute();
71         }
72     }
73
74     @Override
75     public void update(final DataTreeModification<Node> modifiedDataObject) {
76         if (modifiedDataObject.getRootPath() != null && modifiedDataObject.getRootNode() != null) {
77             LOG.info("uni node {} created", modifiedDataObject.getRootNode().getIdentifier());
78             final UniUpdateCommand uniUpdateCmd = new UniUpdateCommand(dataBroker, modifiedDataObject);
79             uniUpdateCmd.execute();
80         }
81     }
82
83 }