884ef7a8e4c47affb47632a0a88e210e370ee7f7
[netvirt.git] / vpnservice / vpnmanager / vpnmanager-impl / src / main / java / org / opendaylight / netvirt / vpnmanager / SubnetRouteInterfaceStateChangeListener.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.netvirt.vpnmanager;
9
10 import java.math.BigInteger;
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;
14 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
15 import org.opendaylight.netvirt.vpnmanager.utilities.InterfaceUtils;
16 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana._if.type.rev140508.Tunnel;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfacesState;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
19 import org.opendaylight.yangtools.concepts.ListenerRegistration;
20 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 public class SubnetRouteInterfaceStateChangeListener extends AbstractDataChangeListener<Interface>
25         implements AutoCloseable {
26     private static final Logger LOG = LoggerFactory.getLogger(InterfaceStateChangeListener.class);
27     private ListenerRegistration<DataChangeListener> listenerRegistration;
28     private final DataBroker dataBroker;
29     private final VpnInterfaceManager vpnInterfaceManager;
30     private final VpnSubnetRouteHandler vpnSubnetRouteHandler;
31
32     public SubnetRouteInterfaceStateChangeListener(final DataBroker dataBroker,
33                                                    final VpnInterfaceManager vpnInterfaceManager,
34                                                    final VpnSubnetRouteHandler vpnSubnetRouteHandler) {
35         super(Interface.class);
36         this.dataBroker = dataBroker;
37         this.vpnInterfaceManager = vpnInterfaceManager;
38         this.vpnSubnetRouteHandler = vpnSubnetRouteHandler;
39     }
40
41     public void start() {
42         LOG.info("{} start", getClass().getSimpleName());
43         listenerRegistration = dataBroker.registerDataChangeListener(LogicalDatastoreType.OPERATIONAL,
44                 getWildCardPath(), this, AsyncDataBroker.DataChangeScope.SUBTREE);
45     }
46
47     private InstanceIdentifier<Interface> getWildCardPath() {
48         return InstanceIdentifier.create(InterfacesState.class).child(Interface.class);
49     }
50
51     @Override
52     public void close() throws Exception {
53         if (listenerRegistration != null) {
54             listenerRegistration.close();
55             listenerRegistration = null;
56         }
57         LOG.info("{} close", getClass().getSimpleName());
58     }
59
60     @Override
61     protected void add(InstanceIdentifier<Interface> identifier, Interface intrf) {
62         LOG.trace("Received interface {} up event", intrf);
63         try {
64             String interfaceName = intrf.getName();
65             LOG.info("Received port UP event for interface {} ", interfaceName);
66             org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface
67                     configInterface = InterfaceUtils.getInterface(dataBroker, interfaceName);
68             if (configInterface != null) {
69                 if (!configInterface.getType().equals(Tunnel.class)) {
70                     BigInteger dpnId = InterfaceUtils.getDpIdFromInterface(intrf);
71                     vpnSubnetRouteHandler.onInterfaceUp(dpnId, intrf.getName());
72                 }
73             }
74         } catch (Exception e) {
75             LOG.error("Exception observed in handling addition for VPN Interface {}. ", intrf.getName(), e);
76         }
77     }
78
79     @Override
80     protected void remove(InstanceIdentifier<Interface> identifier, Interface intrf) {
81         LOG.trace("Received interface {} down event", intrf);
82         try {
83             String interfaceName = intrf.getName();
84             BigInteger dpnId = BigInteger.ZERO;
85             LOG.info("Received port DOWN event for interface {} ", interfaceName);
86             if (intrf != null && intrf.getType() != null && intrf.getType().equals(Tunnel.class)) {
87                 //withdraw all prefixes in all vpns for this dpn from bgp
88                 // FIXME: Blocked until tunnel event[vxlan/gre] support is available
89                 // vpnInterfaceManager.updatePrefixesForDPN(dpId, VpnInterfaceManager.UpdateRouteAction.WITHDRAW_ROUTE);
90             } else {
91                 try {
92                     dpnId = InterfaceUtils.getDpIdFromInterface(intrf);
93                 } catch (Exception e){
94                     LOG.error("Unable to retrieve dpnId from interface operational data store for interface {}. Fetching from vpn interface op data store. ", intrf.getName(), e);
95                 }
96                 vpnSubnetRouteHandler.onInterfaceDown(dpnId, intrf.getName());
97             }
98         } catch (Exception e) {
99             LOG.error("Exception observed in handling deletion of VPN Interface {}. ", intrf.getName(), e);
100         }
101     }
102
103     @Override
104     protected void update(InstanceIdentifier<Interface> identifier,
105                           Interface original, Interface update) {
106         LOG.trace("Operation Interface update event - Old: {}, New: {}", original, update);
107         String interfaceName = update.getName();
108         BigInteger dpId = InterfaceUtils.getDpIdFromInterface(update);
109         if (update != null) {
110             if (!update.getType().equals(Tunnel.class)) {
111                 if (update.getOperStatus().equals(Interface.OperStatus.Up)) {
112                     vpnSubnetRouteHandler.onInterfaceUp(dpId, update.getName());
113                 } else if (update.getOperStatus().equals(Interface.OperStatus.Down)) {
114                     if (VpnUtil.isVpnInterfaceConfigured(dataBroker, interfaceName)) {
115                         vpnSubnetRouteHandler.onInterfaceDown(dpId, update.getName());
116                     }
117                 }
118             }
119         }
120     }
121 }