Bug 5268 - Handle l3vpn delete and recreate scenario
[vpnservice.git] / vpnmanager / vpnmanager-impl / src / main / java / org / opendaylight / vpnservice / InterfaceStateChangeListener.java
1 /*
2  * Copyright (c) 2015 - 2016 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.utilities.InterfaceUtils;
15 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana._if.type.rev140508.Tunnel;
16 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfacesState;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rpcs.rev151003.OdlInterfaceRpcService;
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 InterfaceStateChangeListener extends AbstractDataChangeListener<Interface> implements AutoCloseable {
28     private static final Logger LOG = LoggerFactory.getLogger(InterfaceStateChangeListener.class);
29
30     private ListenerRegistration<DataChangeListener> listenerRegistration;
31     private final DataBroker broker;
32     private VpnInterfaceManager vpnInterfaceManager;
33     private OdlInterfaceRpcService interfaceManager;
34
35
36     public InterfaceStateChangeListener(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(OdlInterfaceRpcService 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.OPERATIONAL,
64                                                                  getWildCardPath(), InterfaceStateChangeListener.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("Received interface {} up event", intrf);
74       try {
75         String interfaceName = intrf.getName();
76         LOG.info("Received port UP event for interface {} ", interfaceName);
77         org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface
78             configInterface = InterfaceUtils.getInterface(broker, interfaceName);
79         BigInteger dpnId = InterfaceUtils.getDpIdFromInterface(intrf);
80         if (configInterface != null && configInterface.getType().equals(Tunnel.class)) {
81           if(intrf.getOperStatus().equals(Interface.OperStatus.Up)) {
82             //advertise all prefixes in all vpns for this dpn to bgp
83             // FIXME: Blocked until tunnel event[vxlan/gre] support is available
84             // vpnInterfaceManager.updatePrefixesForDPN(dpnId, VpnInterfaceManager.UpdateRouteAction.ADVERTISE_ROUTE);
85           }
86         } else {
87           vpnInterfaceManager.processVpnInterfaceUp(dpnId, interfaceName, intrf.getIfIndex());
88         }
89       } catch (Exception e) {
90         LOG.error("Exception caught in Interface Operational State Up event", e);
91       }
92     }
93
94
95     private InstanceIdentifier<Interface> getWildCardPath() {
96         return InstanceIdentifier.create(InterfacesState.class).child(Interface.class);
97     }
98
99     @Override
100     protected void remove(InstanceIdentifier<Interface> identifier, Interface intrf) {
101       LOG.trace("Received interface {} down event", intrf);
102       try {
103         String interfaceName = intrf.getName();
104         LOG.info("Received port DOWN event for interface {} ", interfaceName);
105         BigInteger dpId = InterfaceUtils.getDpIdFromInterface(intrf);
106         if (intrf != null && intrf.getType() != null && intrf.getType().equals(Tunnel.class)) {
107           //withdraw all prefixes in all vpns for this dpn from bgp
108           // FIXME: Blocked until tunnel event[vxlan/gre] support is available
109           // vpnInterfaceManager.updatePrefixesForDPN(dpId, VpnInterfaceManager.UpdateRouteAction.WITHDRAW_ROUTE);
110         } else {
111           if (VpnUtil.isVpnInterfaceConfigured(broker, interfaceName)) {
112             vpnInterfaceManager.processVpnInterfaceDown(dpId, interfaceName, intrf.getIfIndex(), true);
113           }
114         }
115       } catch (Exception e) {
116         LOG.error("Exception caught in onVlanInterfaceOperationalStateDown", e);
117       }
118     }
119
120     @Override
121     protected void update(InstanceIdentifier<Interface> identifier,
122             Interface original, Interface update) {
123       LOG.trace("Operation Interface update event - Old: {}, New: {}", original, update);
124       String interfaceName = update.getName();
125       org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface
126           intf = InterfaceUtils.getInterface(broker, interfaceName);
127       if (intf != null && intf.getType().equals(Tunnel.class)) {
128         /*
129         // FIXME: Blocked until tunnel event[vxlan/gre] support is available
130         BigInteger dpnId = InterfaceUtils.getDpIdFromInterface(update);
131         if(update.getOperStatus().equals(Interface.OperStatus.Up)) {
132           //advertise all prefixes in all vpns for this dpn to bgp
133           // vpnInterfaceManager.updatePrefixesForDPN(dpnId, VpnInterfaceManager.UpdateRouteAction.ADVERTISE_ROUTE);
134         } else if(update.getOperStatus().equals(Interface.OperStatus.Down)) {
135           //withdraw all prefixes in all vpns for this dpn from bgp
136           // vpnInterfaceManager.updatePrefixesForDPN(dpnId, VpnInterfaceManager.UpdateRouteAction.WITHDRAW_ROUTE);
137         }*/
138       }
139
140     }
141
142 }