2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.groupbasedpolicy.renderer.vpp.listener;
11 import java.util.Collection;
13 import javax.annotation.Nonnull;
15 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
16 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
17 import org.opendaylight.controller.md.sal.binding.api.DataTreeChangeListener;
18 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
19 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
20 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
21 import org.opendaylight.groupbasedpolicy.renderer.vpp.event.NodeOperEvent;
22 import org.opendaylight.groupbasedpolicy.renderer.vpp.manager.VppNodeManager;
23 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
24 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
25 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
26 import org.opendaylight.yangtools.concepts.ListenerRegistration;
27 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
31 import com.google.common.base.Preconditions;
32 import com.google.common.eventbus.EventBus;
34 public class VppNodeListener implements DataTreeChangeListener<Node>, AutoCloseable {
36 private static final Logger LOG = LoggerFactory.getLogger(VppNodeListener.class);
38 private final ListenerRegistration<VppNodeListener> listenerRegistration;
39 private final VppNodeManager nodeManager;
40 private final EventBus eventBus;
42 public VppNodeListener(DataBroker dataBroker, VppNodeManager nodeManager, EventBus eventBus) {
43 this.nodeManager = Preconditions.checkNotNull(nodeManager);
44 this.eventBus = Preconditions.checkNotNull(eventBus);
46 final DataTreeIdentifier<Node> networkTopologyPath = new DataTreeIdentifier<>(LogicalDatastoreType.OPERATIONAL,
47 InstanceIdentifier.builder(NetworkTopology.class).child(Topology.class).child(Node.class).build());
48 listenerRegistration =
49 Preconditions.checkNotNull(dataBroker.registerDataTreeChangeListener(networkTopologyPath, this));
50 LOG.info("Network-Topology VppNodelistener registered");
54 public void onDataTreeChanged(@Nonnull Collection<DataTreeModification<Node>> changes) {
55 LOG.debug("Topology Node changed. Changes {}", changes);
57 for (DataTreeModification<Node> modification : changes) {
58 DataObjectModification<Node> rootNode = modification.getRootNode();
59 Node dataAfter = rootNode.getDataAfter();
60 Node dataBefore = rootNode.getDataBefore();
62 new NodeOperEvent(modification.getRootPath().getRootIdentifier(), dataBefore, dataAfter);
64 nodeManager.syncNodes(dataAfter, dataBefore);
68 public ListenerRegistration<VppNodeListener> getRegistrationObject() {
69 return listenerRegistration;
73 public void close() throws Exception {
74 listenerRegistration.close();