2 * Copyright (c) 2015 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;
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.DataChangeScope;
13 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
14 import org.opendaylight.vpnservice.interfacemgr.interfaces.IInterfaceManager;
15 import org.opendaylight.vpnservice.mdsalutil.NwConstants;
16 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.interfaces.VpnInterface;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana._if.type.rev140508.Tunnel;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.Interfaces;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
20 import org.opendaylight.yangtools.concepts.ListenerRegistration;
21 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
25 import java.math.BigInteger;
27 public class InterfaceChangeListener extends AbstractDataChangeListener<Interface> implements AutoCloseable {
28 private static final Logger LOG = LoggerFactory.getLogger(InterfaceChangeListener.class);
30 private ListenerRegistration<DataChangeListener> listenerRegistration;
31 private final DataBroker broker;
32 private VpnInterfaceManager vpnInterfaceManager;
33 private IInterfaceManager interfaceManager;
36 public InterfaceChangeListener(final DataBroker db, VpnInterfaceManager vpnInterfaceManager) {
37 super(Interface.class);
39 this.vpnInterfaceManager = vpnInterfaceManager;
43 public void setInterfaceManager(IInterfaceManager interfaceManager) {
44 this.interfaceManager = interfaceManager;
48 public void close() throws Exception {
49 if (listenerRegistration != null) {
51 listenerRegistration.close();
52 } catch (final Exception e) {
53 LOG.error("Error when cleaning up DataChangeListener.", e);
55 listenerRegistration = null;
57 LOG.info("Interface listener Closed");
61 private void registerListener(final DataBroker db) {
63 listenerRegistration = db.registerDataChangeListener(LogicalDatastoreType.CONFIGURATION,
64 getWildCardPath(), InterfaceChangeListener.this, DataChangeScope.SUBTREE);
65 } catch (final Exception e) {
66 LOG.error("Interface DataChange listener registration failed", e);
67 throw new IllegalStateException("Nexthop Manager registration Listener failed.", e);
72 protected void add(InstanceIdentifier<Interface> identifier, Interface intrf) {
73 LOG.trace("Adding Interface : key: " + identifier + ", value=" + intrf );
78 private InstanceIdentifier<Interface> getWildCardPath() {
79 return InstanceIdentifier.create(Interfaces.class).child(Interface.class);
83 protected void remove(InstanceIdentifier<Interface> identifier, Interface intrf) {
84 LOG.trace("Remove interface event - key: {}, value: {}", identifier, intrf );
85 if (intrf.getType().equals(Tunnel.class)) {
86 BigInteger dpnId = interfaceManager.getDpnForInterface(intrf);
87 String ifName = intrf.getName();
88 LOG.debug("Removing tunnel interface associated with Interface {}", intrf.getName());
89 vpnInterfaceManager.makeTunnelIngressFlow(dpnId, ifName, NwConstants.DEL_FLOW);
92 VpnInterface vpnInterface = vpnInterfaceManager.getVpnInterface(intrf.getName());
93 if (vpnInterface !=null) {
94 InstanceIdentifier<VpnInterface> id = VpnUtil.getVpnInterfaceIdentifier(intrf.getName());
95 LOG.debug("Removing VPN Interface associated with Interface {}", intrf.getName());
96 vpnInterfaceManager.remove(id, vpnInterface);
99 LOG.debug("No VPN Interface associated with Interface {}", intrf.getName());
105 protected void update(InstanceIdentifier<Interface> identifier,
106 Interface original, Interface update) {
107 // TODO Auto-generated method stub