Vpnmanager and fibmanager changes
[vpnservice.git] / fibmanager / fibmanager-impl / src / main / java / org / opendaylight / vpnservice / fibmanager / FibNodeCapableListener.java
1 /*
2  * Copyright (c) 2015 - 2016 Ericsson India Global Services Pvt Ltd. 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.vpnservice.fibmanager;
10
11 import java.math.BigInteger;
12
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;
27
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;
32
33     public FibNodeCapableListener(final DataBroker dataBroker, FibManager fibManager) {
34         super(FlowCapableNode.class);
35         registerListener(dataBroker);
36         this.fibManager = fibManager;
37     }
38
39     private void registerListener(final DataBroker db) {
40         try {
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);
46         }
47     }
48
49     private InstanceIdentifier<FlowCapableNode> getWildCardPath() {
50         return InstanceIdentifier.create(Nodes.class).child(Node.class).augmentation(FlowCapableNode.class);
51     }
52
53     @Override
54     public void close() throws Exception {
55         if (listenerRegistration != null) {
56             try {
57                 listenerRegistration.close();
58             } catch (final Exception e) {
59                 LOG.error("Error when cleaning up DataChangeListener.", e);
60             }
61             listenerRegistration = null;
62         }
63         LOG.info("FibNodeConnectorListener Closed");
64     }
65
66     @Override
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);
72     }
73
74     @Override
75     protected void remove(InstanceIdentifier<FlowCapableNode> identifier, FlowCapableNode del) {
76     }
77
78     @Override
79     protected void update(InstanceIdentifier<FlowCapableNode> identifier, FlowCapableNode original, FlowCapableNode update) {
80     }
81 }