manually cherry pick this patch https://git.opendaylight.org/gerrit/#/c/34579/
[unimgr.git] / impl / src / main / java / org / opendaylight / unimgr / impl / EvcDataTreeChangeListener.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.EvcAddCommand;
17 import org.opendaylight.unimgr.command.EvcRemoveCommand;
18 import org.opendaylight.unimgr.command.EvcUpdateCommand;
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.Link;
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 EvcDataTreeChangeListener extends UnimgrDataTreeChangeListener<Link> {
29
30     private static final Logger LOG = LoggerFactory.getLogger(EvcDataTreeChangeListener.class);
31     private final  ListenerRegistration<EvcDataTreeChangeListener> listener;
32
33     public EvcDataTreeChangeListener(final DataBroker dataBroker) {
34         super(dataBroker);
35         final DataTreeIdentifier<Link> dataTreeIid = new DataTreeIdentifier<>(LogicalDatastoreType.CONFIGURATION, getEvcTopologyPath());
36         listener = dataBroker.registerDataTreeChangeListener(dataTreeIid, this);
37         LOG.info("EvcDataTreeChangeListener created and registered");
38     }
39
40     @Override
41     public void add(final DataTreeModification<Link> newDataObject) {
42         if(newDataObject.getRootPath() != null && newDataObject.getRootNode() != null) {
43             LOG.info("evc link {} created", newDataObject.getRootNode().getIdentifier());
44             final EvcAddCommand evcAddCmd = new EvcAddCommand(dataBroker, newDataObject);
45             evcAddCmd.execute();
46         }
47     }
48
49     @Override
50     public void close() throws Exception {
51         listener.close();
52     }
53
54     private InstanceIdentifier<Link> getEvcTopologyPath() {
55         final InstanceIdentifier<Link> evcPath = InstanceIdentifier
56                                                    .create(NetworkTopology.class)
57                                                    .child(Topology.class, new TopologyKey(UnimgrConstants.EVC_TOPOLOGY_ID))
58                                                    .child(Link.class);
59         return evcPath;
60     }
61
62     @Override
63     public void remove(final DataTreeModification<Link> removedDataObject) {
64         if(removedDataObject.getRootPath() != null && removedDataObject.getRootNode() != null) {
65             LOG.info("evc link {} deleted", removedDataObject.getRootNode().getIdentifier());
66             final EvcRemoveCommand evcRemovedCmd = new EvcRemoveCommand(dataBroker, removedDataObject);
67             evcRemovedCmd.execute();
68         }
69     }
70
71     @Override
72     public void update(final DataTreeModification<Link> modifiedDataObject) {
73         if(modifiedDataObject.getRootPath() != null && modifiedDataObject.getRootNode() != null) {
74             LOG.info("evc link {} updated", modifiedDataObject.getRootNode().getIdentifier());
75             final EvcUpdateCommand evcUpdateCmd = new EvcUpdateCommand(dataBroker, modifiedDataObject);
76             evcUpdateCmd.execute();
77         }
78     }
79 }