2 * Copyright (c) 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
8 package org.opendaylight.vpnservice.natservice.internal;
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;
23 import java.math.BigInteger;
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;
30 public NatNodeEventListener(final DataBroker db,final NaptSwitchHA napt) {
36 private void registerListener(final DataBroker db) {
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);
46 private InstanceIdentifier<Node> getWildCardPath() {
47 return InstanceIdentifier.create(Nodes.class).child(Node.class);
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(":");
56 LOG.warn("Unexpected nodeId {}", nodeId.getValue());
59 BigInteger dpnId = new BigInteger(node[1]);
60 LOG.debug("NodeId removed is {}",dpnId);
64 protected void update(InstanceIdentifier<Node> identifier, Node original, Node update) {
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(":");
73 LOG.warn("Unexpected nodeId {}", nodeId.getValue());
76 BigInteger dpnId = new BigInteger(node[1]);
77 LOG.debug("NodeId added is {}",dpnId);
81 public void close() throws Exception {
82 if (listenerRegistration != null) {
84 listenerRegistration.close();
85 } catch (final Exception e) {
86 LOG.error("Error when cleaning up DataChangeListener.", e);
88 listenerRegistration = null;
90 LOG.info("NatNodeEventListener Closed");