/* * Copyright (c) 2015 Ericsson India Global Services Pvt Ltd. and others. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html */ package org.opendaylight.vpnservice.dhcpservice; import java.math.BigInteger; import java.util.List; import org.opendaylight.controller.md.sal.binding.api.DataBroker; import org.opendaylight.controller.md.sal.binding.api.DataChangeListener; import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker; import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType; import org.opendaylight.vpnservice.dhcpservice.api.DHCPMConstants; import org.opendaylight.vpnservice.mdsalutil.AbstractDataChangeListener; import org.opendaylight.vpnservice.mdsalutil.MDSALUtil; import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId; import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes; import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node; import org.opendaylight.yangtools.concepts.ListenerRegistration; import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class NodeListener extends AbstractDataChangeListener implements AutoCloseable { private static final Logger LOG = LoggerFactory.getLogger(NodeListener.class); private ListenerRegistration listenerRegistration; private final DataBroker broker; private DhcpManager dhcpManager; private DhcpExternalTunnelManager dhcpExternalTunnelManager; public NodeListener(final DataBroker db, final DhcpManager dhcpMgr, final DhcpExternalTunnelManager dhcpExternalTunnelManager) { super(Node.class); broker = db; dhcpManager = dhcpMgr; this.dhcpExternalTunnelManager = dhcpExternalTunnelManager; registerListener(db); } private void registerListener(final DataBroker db) { try { listenerRegistration = db.registerDataChangeListener(LogicalDatastoreType.OPERATIONAL, getWildCardPath(), NodeListener.this, AsyncDataBroker.DataChangeScope.SUBTREE); } catch (final Exception e) { LOG.error("NodeListener: DataChange listener registration fail!", e); throw new IllegalStateException("NodeListener: registration Listener failed.", e); } } private InstanceIdentifier getWildCardPath() { return InstanceIdentifier.create(Nodes.class).child(Node.class); } @Override protected void remove(InstanceIdentifier identifier, Node del) { NodeId nodeId = del.getId(); BigInteger dpnId = MDSALUtil.getDpnIdFromNodeName(nodeId); List listOfDpns = DhcpServiceUtils.getListOfDpns(broker); dhcpExternalTunnelManager.handleDesignatedDpnDown(dpnId, listOfDpns); } @Override protected void update(InstanceIdentifier identifier, Node original, Node update) { } @Override protected void add(InstanceIdentifier identifier, Node add) { NodeId nodeId = add.getId(); String[] node = nodeId.getValue().split(":"); if(node.length < 2) { LOG.warn("Unexpected nodeId {}", nodeId.getValue()); return; } BigInteger dpId = new BigInteger(node[1]); dhcpManager.setupTableMissForDhcpTable(dpId); dhcpExternalTunnelManager.installDhcpDropActionOnDpn(dpId); List listOfDpns = DhcpServiceUtils.getListOfDpns(broker); dhcpExternalTunnelManager.handleDesignatedDpnDown(DHCPMConstants.INVALID_DPID, listOfDpns); } @Override public void close() throws Exception { if (listenerRegistration != null) { try { listenerRegistration.close(); } catch (final Exception e) { LOG.error("Error when cleaning up NodeListener.", e); } listenerRegistration = null; //ToDo: Should we delete DHCP flows when we are closed? } LOG.debug("Node Listener Closed"); } }