BUG 5044: Neutron vpn should create vpn interface name with tap port name.
[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             vpnInterfaceManager.updatePrefixesForDPN(dpnId, VpnInterfaceManager.UpdateRouteAction.ADVERTISE_ROUTE);
84           }
85         } else {
86           vpnInterfaceManager.processVpnInterfaceUp(dpnId, interfaceName, intrf.getIfIndex());
87         }
88       } catch (Exception e) {
89         LOG.error("Exception caught in Interface Operational State Up event", e);
90       }
91     }
92
93
94     private InstanceIdentifier<Interface> getWildCardPath() {
95         return InstanceIdentifier.create(InterfacesState.class).child(Interface.class);
96     }
97
98     @Override
99     protected void remove(InstanceIdentifier<Interface> identifier, Interface intrf) {
100       LOG.trace("Received interface {} down event", intrf);
101       try {
102         String interfaceName = intrf.getName();
103         LOG.info("Received port DOWN event for interface {} ", interfaceName);
104         org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface
105             intf = InterfaceUtils.getInterface(broker, interfaceName);
106         BigInteger dpId = InterfaceUtils.getDpIdFromInterface(intrf);
107         if (intf != null && intf.getType().equals(Tunnel.class)) {
108           if(intrf.getOperStatus().equals(Interface.OperStatus.Down)) {
109             //withdraw all prefixes in all vpns for this dpn from bgp
110             vpnInterfaceManager.updatePrefixesForDPN(dpId, VpnInterfaceManager.UpdateRouteAction.WITHDRAW_ROUTE);
111           }
112         } else {
113           if (VpnUtil.isVpnInterfaceConfigured(broker, interfaceName)) {
114             vpnInterfaceManager.processVpnInterfaceDown(dpId, interfaceName, intrf.getIfIndex(), true);
115           }
116         }
117       } catch (Exception e) {
118         LOG.error("Exception caught in onVlanInterfaceOperationalStateDown", e);
119       }
120     }
121
122     @Override
123     protected void update(InstanceIdentifier<Interface> identifier,
124             Interface original, Interface update) {
125       LOG.trace("Operation Interface update event - Old: {}, New: {}", original, update);
126       String interfaceName = update.getName();
127       org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface
128           intf = InterfaceUtils.getInterface(broker, interfaceName);
129       if (intf != null && intf.getType().equals(Tunnel.class)) {
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 }