2 * Copyright (c) 2015 - 2016 Ericsson India Global Services Pvt Ltd. 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.vpnservice.fibmanager;
11 import java.math.BigInteger;
13 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
14 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
15 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
16 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
17 import org.opendaylight.vpnservice.mdsalutil.AbstractDataChangeListener;
18 import org.opendaylight.vpnservice.mdsalutil.MDSALUtil;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
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;
28 public class FibNodeCapableListener extends AbstractDataChangeListener<FlowCapableNode> implements AutoCloseable{
29 private static final Logger LOG = LoggerFactory.getLogger(FibNodeCapableListener.class);
30 private ListenerRegistration<DataChangeListener> listenerRegistration;
31 private FibManager fibManager;
33 public FibNodeCapableListener(final DataBroker dataBroker, FibManager fibManager) {
34 super(FlowCapableNode.class);
35 registerListener(dataBroker);
36 this.fibManager = fibManager;
39 private void registerListener(final DataBroker db) {
41 listenerRegistration = db.registerDataChangeListener(LogicalDatastoreType.OPERATIONAL,
42 getWildCardPath(), FibNodeCapableListener.this, DataChangeScope.ONE);
43 } catch (final Exception e) {
44 LOG.error("FibNodeConnectorListener: DataChange listener registration fail!", e);
45 throw new IllegalStateException("FibNodeConnectorListener: registration Listener failed.", e);
49 private InstanceIdentifier<FlowCapableNode> getWildCardPath() {
50 return InstanceIdentifier.create(Nodes.class).child(Node.class).augmentation(FlowCapableNode.class);
54 public void close() throws Exception {
55 if (listenerRegistration != null) {
57 listenerRegistration.close();
58 } catch (final Exception e) {
59 LOG.error("Error when cleaning up DataChangeListener.", e);
61 listenerRegistration = null;
63 LOG.info("FibNodeConnectorListener Closed");
67 protected void add(InstanceIdentifier<FlowCapableNode> identifier, FlowCapableNode node) {
68 LOG.trace("FlowCapableNode Added: key: " + identifier + ", value=" + node );
69 NodeKey nodeKey = identifier.firstKeyOf(Node.class, NodeKey.class);
70 BigInteger dpnId = MDSALUtil.getDpnIdFromNodeName(nodeKey.getId());
71 fibManager.processNodeAdd(dpnId);
75 protected void remove(InstanceIdentifier<FlowCapableNode> identifier, FlowCapableNode del) {
79 protected void update(InstanceIdentifier<FlowCapableNode> identifier, FlowCapableNode original, FlowCapableNode update) {