Fixing issue with ovsdb datapath id conversion
[vpnservice.git] / vpnmanager / vpnmanager-impl / src / main / java / org / opendaylight / vpnservice / InterfaceChangeListener.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;
9
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;
24
25 import java.math.BigInteger;
26
27 public class InterfaceChangeListener extends AbstractDataChangeListener<Interface> implements AutoCloseable {
28     private static final Logger LOG = LoggerFactory.getLogger(InterfaceChangeListener.class);
29
30     private ListenerRegistration<DataChangeListener> listenerRegistration;
31     private final DataBroker broker;
32     private VpnInterfaceManager vpnInterfaceManager;
33     private IInterfaceManager interfaceManager;
34
35
36     public InterfaceChangeListener(final DataBroker db, VpnInterfaceManager vpnInterfaceManager) {
37         super(Interface.class);
38         broker = db;
39         this.vpnInterfaceManager = vpnInterfaceManager;
40         registerListener(db);
41     }
42
43     public void setInterfaceManager(IInterfaceManager interfaceManager) {
44       this.interfaceManager = interfaceManager;
45   }
46
47     @Override
48     public void close() throws Exception {
49         if (listenerRegistration != null) {
50             try {
51                 listenerRegistration.close();
52             } catch (final Exception e) {
53                 LOG.error("Error when cleaning up DataChangeListener.", e);
54             }
55             listenerRegistration = null;
56         }
57         LOG.info("Interface listener Closed");
58     }
59
60
61     private void registerListener(final DataBroker db) {
62         try {
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);
68         }
69     }
70
71     @Override
72     protected void add(InstanceIdentifier<Interface> identifier, Interface intrf) {
73         LOG.trace("Adding Interface : key: " + identifier + ", value=" + intrf );
74
75     }
76
77
78     private InstanceIdentifier<Interface> getWildCardPath() {
79         return InstanceIdentifier.create(Interfaces.class).child(Interface.class);
80     }
81
82     @Override
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);
90       }
91         else {
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);
97           }
98           else {
99             LOG.debug("No VPN Interface associated with Interface {}", intrf.getName());
100           }
101         }
102     }
103
104     @Override
105     protected void update(InstanceIdentifier<Interface> identifier,
106             Interface original, Interface update) {
107         // TODO Auto-generated method stub
108
109     }
110
111 }