Connectivity service synchronous write.
[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 =
36                 new DataTreeIdentifier<>(LogicalDatastoreType.CONFIGURATION, getEvcTopologyPath());
37         listener = dataBroker.registerDataTreeChangeListener(dataTreeIid, this);
38         LOG.info("EvcDataTreeChangeListener created and registered");
39     }
40
41     @Override
42     public void add(final DataTreeModification<Link> newDataObject) {
43         if (newDataObject.getRootPath() != null && newDataObject.getRootNode() != null) {
44             LOG.info("evc link {} created", newDataObject.getRootNode().getIdentifier());
45             final EvcAddCommand evcAddCmd = new EvcAddCommand(dataBroker, newDataObject);
46             evcAddCmd.execute();
47         }
48     }
49
50     @Override
51     public void close() throws Exception {
52         listener.close();
53     }
54
55     private InstanceIdentifier<Link> getEvcTopologyPath() {
56         final InstanceIdentifier<Link> evcPath = InstanceIdentifier
57                 .create(NetworkTopology.class)
58                 .child(Topology.class, new TopologyKey(UnimgrConstants.EVC_TOPOLOGY_ID))
59                 .child(Link.class);
60         return evcPath;
61     }
62
63     @Override
64     public void remove(final DataTreeModification<Link> removedDataObject) {
65         if (removedDataObject.getRootPath() != null && removedDataObject.getRootNode() != null) {
66             LOG.info("evc link {} deleted", removedDataObject.getRootNode().getIdentifier());
67             final EvcRemoveCommand evcRemovedCmd = new EvcRemoveCommand(dataBroker, removedDataObject);
68             evcRemovedCmd.execute();
69         }
70     }
71
72     @Override
73     public void update(final DataTreeModification<Link> modifiedDataObject) {
74         if (modifiedDataObject.getRootPath() != null && modifiedDataObject.getRootNode() != null) {
75             LOG.info("evc link {} updated", modifiedDataObject.getRootNode().getIdentifier());
76             final EvcUpdateCommand evcUpdateCmd = new EvcUpdateCommand(dataBroker, modifiedDataObject);
77             evcUpdateCmd.execute();
78         }
79     }
80 }