DHCP Handling for TOR VM
[vpnservice.git] / dhcpservice / dhcpservice-impl / src / main / java / org / opendaylight / vpnservice / dhcpservice / NodeListener.java
1 /*
2  * Copyright (c) 2015 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.dhcpservice;
9
10 import java.math.BigInteger;
11 import java.util.List;
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;
16 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
17 import org.opendaylight.vpnservice.dhcpservice.api.DHCPMConstants;
18 import org.opendaylight.vpnservice.mdsalutil.AbstractDataChangeListener;
19 import org.opendaylight.vpnservice.mdsalutil.MDSALUtil;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
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 NodeListener extends AbstractDataChangeListener<Node> implements AutoCloseable {
29
30     private static final Logger LOG = LoggerFactory.getLogger(NodeListener.class);
31
32     private ListenerRegistration<DataChangeListener> listenerRegistration;
33     private final DataBroker broker;
34     private DhcpManager dhcpManager;
35     private DhcpExternalTunnelManager dhcpExternalTunnelManager;
36
37     public NodeListener(final DataBroker db, final DhcpManager dhcpMgr, final DhcpExternalTunnelManager dhcpExternalTunnelManager) {
38         super(Node.class);
39         broker = db;
40         dhcpManager = dhcpMgr;
41         this.dhcpExternalTunnelManager = dhcpExternalTunnelManager;
42         registerListener(db);
43     }
44
45     private void registerListener(final DataBroker db) {
46         try {
47             listenerRegistration = db.registerDataChangeListener(LogicalDatastoreType.OPERATIONAL,
48             getWildCardPath(), NodeListener.this, AsyncDataBroker.DataChangeScope.SUBTREE);
49         } catch (final Exception e) {
50             LOG.error("NodeListener: DataChange listener registration fail!", e);
51             throw new IllegalStateException("NodeListener: registration Listener failed.", e);
52         }
53     }
54
55     private InstanceIdentifier<Node> getWildCardPath() {
56         return InstanceIdentifier.create(Nodes.class).child(Node.class);
57     }
58
59     @Override
60     protected void remove(InstanceIdentifier<Node> identifier, Node del) {
61         NodeId nodeId = del.getId();
62         BigInteger dpnId = MDSALUtil.getDpnIdFromNodeName(nodeId);
63         List<BigInteger> listOfDpns = DhcpServiceUtils.getListOfDpns(broker);
64         dhcpExternalTunnelManager.handleDesignatedDpnDown(dpnId, listOfDpns);
65     }
66
67     @Override
68     protected void update(InstanceIdentifier<Node> identifier, Node original, Node update) {
69
70     }
71
72     @Override
73     protected void add(InstanceIdentifier<Node> identifier, Node add) {
74         NodeId nodeId = add.getId();
75         String[] node =  nodeId.getValue().split(":");
76         if(node.length < 2) {
77             LOG.warn("Unexpected nodeId {}", nodeId.getValue());
78             return;
79         }
80         BigInteger dpId = new BigInteger(node[1]);
81         dhcpManager.setupTableMissForDhcpTable(dpId);
82         dhcpExternalTunnelManager.installDhcpDropActionOnDpn(dpId);
83         List<BigInteger> listOfDpns = DhcpServiceUtils.getListOfDpns(broker);
84         dhcpExternalTunnelManager.handleDesignatedDpnDown(DHCPMConstants.INVALID_DPID, listOfDpns);
85     }
86
87     @Override
88     public void close() throws Exception {
89         if (listenerRegistration != null) {
90             try {
91                 listenerRegistration.close();
92             } catch (final Exception e) {
93                 LOG.error("Error when cleaning up NodeListener.", e);
94             }
95             listenerRegistration = null;
96             //ToDo: Should we delete DHCP flows when we are closed?
97         }
98         LOG.debug("Node Listener Closed");
99     }
100 }