Merge "LLDP monitor interval update fixes"
[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         naptSwitchHA.handleNaptSwitchDown(dpnId);
62     }
63
64     @Override
65     protected void update(InstanceIdentifier<Node> identifier, Node original, Node update) {
66         LOG.trace("NatNodeEventListener: Node update received");
67     }
68
69     @Override
70     protected void add(InstanceIdentifier<Node> identifier, Node add) {
71         LOG.debug("NatNodeEventListener: Node added received");
72         NodeId nodeId = add.getId();
73         String[] node =  nodeId.getValue().split(":");
74         if(node.length < 2) {
75             LOG.warn("Unexpected nodeId {}", nodeId.getValue());
76             return;
77         }
78         BigInteger dpnId = new BigInteger(node[1]);
79         LOG.debug("NodeId added is {}",dpnId);
80     }
81
82     @Override
83     public void close() throws Exception {
84         if (listenerRegistration != null) {
85             try {
86                 listenerRegistration.close();
87             } catch (final Exception e) {
88                 LOG.error("Error when cleaning up DataChangeListener.", e);
89             }
90             listenerRegistration = null;
91         }
92         LOG.info("NatNodeEventListener Closed");
93     }
94 }