Bump versions by 0.1.0 for next dev cycle
[vpnservice.git] / natservice / natservice-impl / src / main / java / org / opendaylight / vpnservice / natservice / internal / NatNodeEventListener.java
1 /*
2  * Copyright (c) 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 package org.opendaylight.vpnservice.natservice.internal;
9
10 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
11 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
12 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker;
13 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
14 import org.opendaylight.vpnservice.mdsalutil.AbstractDataChangeListener;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
18 import org.opendaylight.yangtools.concepts.ListenerRegistration;
19 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 import java.math.BigInteger;
24
25 public class NatNodeEventListener extends AbstractDataChangeListener<Node> implements AutoCloseable {
26     private static final Logger LOG = LoggerFactory.getLogger(NatNodeEventListener.class);
27     private ListenerRegistration<DataChangeListener> listenerRegistration;
28     private NaptSwitchHA naptSwitchHA;
29
30     public NatNodeEventListener(final DataBroker db,final  NaptSwitchHA napt) {
31         super(Node.class);
32         naptSwitchHA = napt;
33         registerListener(db);
34     }
35
36     private void registerListener(final DataBroker db) {
37         try {
38             listenerRegistration = db.registerDataChangeListener(LogicalDatastoreType.OPERATIONAL,
39                     getWildCardPath(), NatNodeEventListener.this, AsyncDataBroker.DataChangeScope.SUBTREE);
40         } catch (final Exception e) {
41             LOG.error("NatNodeEventListener: DataChange listener registration fail!", e);
42             throw new IllegalStateException("NatNodeEventListener: registration Listener failed.", e);
43         }
44     }
45
46     private InstanceIdentifier<Node> getWildCardPath() {
47         return InstanceIdentifier.create(Nodes.class).child(Node.class);
48     }
49
50     @Override
51     protected void remove(InstanceIdentifier<Node> identifier, Node del) {
52         LOG.debug("NatNodeEventListener: Node removed received");
53         NodeId nodeId = del.getId();
54         String[] node =  nodeId.getValue().split(":");
55         if(node.length < 2) {
56             LOG.warn("Unexpected nodeId {}", nodeId.getValue());
57             return;
58         }
59         BigInteger dpnId = new BigInteger(node[1]);
60         LOG.debug("NodeId removed is {}",dpnId);
61     }
62
63     @Override
64     protected void update(InstanceIdentifier<Node> identifier, Node original, Node update) {
65     }
66
67     @Override
68     protected void add(InstanceIdentifier<Node> identifier, Node add) {
69         LOG.debug("NatNodeEventListener: Node added received");
70         NodeId nodeId = add.getId();
71         String[] node =  nodeId.getValue().split(":");
72         if(node.length < 2) {
73             LOG.warn("Unexpected nodeId {}", nodeId.getValue());
74             return;
75         }
76         BigInteger dpnId = new BigInteger(node[1]);
77         LOG.debug("NodeId added is {}",dpnId);
78     }
79
80     @Override
81     public void close() throws Exception {
82         if (listenerRegistration != null) {
83             try {
84                 listenerRegistration.close();
85             } catch (final Exception e) {
86                 LOG.error("Error when cleaning up DataChangeListener.", e);
87             }
88             listenerRegistration = null;
89         }
90         LOG.info("NatNodeEventListener Closed");
91     }
92 }