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
9 package org.opendaylight.vpnservice.dhcpservice;
11 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
12 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
13 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
14 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
15 import org.opendaylight.vpnservice.mdsalutil.AbstractDataChangeListener;
16 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.Interfaces;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.IfTunnel;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.ParentRefs;
21 import org.opendaylight.yangtools.concepts.ListenerRegistration;
22 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
26 public class DhcpInterfaceConfigListener extends AbstractDataChangeListener<Interface> implements AutoCloseable {
28 private static final Logger logger = LoggerFactory.getLogger(DhcpInterfaceConfigListener.class);
29 private ListenerRegistration<DataChangeListener> listenerRegistration;
30 private DataBroker dataBroker;
31 private DhcpExternalTunnelManager dhcpExternalTunnelManager;
33 public DhcpInterfaceConfigListener(DataBroker dataBroker, DhcpExternalTunnelManager dhcpExternalTunnelManager) {
34 super(Interface.class);
35 this.dataBroker = dataBroker;
36 this.dhcpExternalTunnelManager = dhcpExternalTunnelManager;
40 private void registerListener() {
42 listenerRegistration = dataBroker.registerDataChangeListener(LogicalDatastoreType.CONFIGURATION,
43 getWildCardPath(), DhcpInterfaceConfigListener.this, DataChangeScope.SUBTREE);
44 } catch (final Exception e) {
45 logger.error("DhcpInterfaceEventListener DataChange listener registration fail!", e);
46 throw new IllegalStateException("DhcpInterfaceEventListener registration Listener failed.", e);
50 private InstanceIdentifier<Interface> getWildCardPath() {
51 return InstanceIdentifier.create(Interfaces.class).child(Interface.class);
55 public void close() throws Exception {
56 if (listenerRegistration != null) {
58 listenerRegistration.close();
59 } catch (final Exception e) {
60 logger.error("Error when cleaning up DataChangeListener.", e);
62 listenerRegistration = null;
64 logger.info("DhcpInterfaceConfigListener Closed");
67 protected void remove(InstanceIdentifier<Interface> identifier, Interface del) {
68 IfTunnel tunnelInterface = del.getAugmentation(IfTunnel.class);
69 if (tunnelInterface != null && !tunnelInterface.isInternal()) {
70 IpAddress tunnelIp = tunnelInterface.getTunnelDestination();
71 ParentRefs interfce = del.getAugmentation(ParentRefs.class);
72 if (interfce != null) {
73 dhcpExternalTunnelManager.handleTunnelStateDown(tunnelIp, interfce.getDatapathNodeIdentifier());
79 protected void update(InstanceIdentifier<Interface> identifier, Interface original, Interface update) {
80 // Handled in update () DhcpInterfaceEventListener
84 protected void add(InstanceIdentifier<Interface> identifier, Interface add) {
85 // Handled in add() DhcpInterfaceEventListener