Imported vpnservice as a subtree
[netvirt.git] / vpnservice / 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 com.google.common.base.Optional;
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.DataChangeScope;
14 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
15 import org.opendaylight.vpnservice.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.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.router.interfaces.RouterInterface;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rpcs.rev151003.OdlInterfaceRpcService;
22 import org.opendaylight.yangtools.concepts.ListenerRegistration;
23 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 import java.math.BigInteger;
28
29 public class InterfaceStateChangeListener extends AbstractDataChangeListener<Interface> implements AutoCloseable {
30     private static final Logger LOG = LoggerFactory.getLogger(InterfaceStateChangeListener.class);
31
32     private ListenerRegistration<DataChangeListener> listenerRegistration;
33     private final DataBroker broker;
34     private VpnInterfaceManager vpnInterfaceManager;
35     private OdlInterfaceRpcService interfaceManager;
36
37
38     public InterfaceStateChangeListener(final DataBroker db, VpnInterfaceManager vpnInterfaceManager) {
39         super(Interface.class);
40         broker = db;
41         this.vpnInterfaceManager = vpnInterfaceManager;
42         registerListener(db);
43     }
44
45     public void setInterfaceManager(OdlInterfaceRpcService interfaceManager) {
46       this.interfaceManager = interfaceManager;
47   }
48
49     @Override
50     public void close() throws Exception {
51         if (listenerRegistration != null) {
52             try {
53                 listenerRegistration.close();
54             } catch (final Exception e) {
55                 LOG.error("Error when cleaning up DataChangeListener.", e);
56             }
57             listenerRegistration = null;
58         }
59         LOG.info("Interface listener Closed");
60     }
61
62
63     private void registerListener(final DataBroker db) {
64         try {
65             listenerRegistration = db.registerDataChangeListener(LogicalDatastoreType.OPERATIONAL,
66                                                                  getWildCardPath(), InterfaceStateChangeListener.this, DataChangeScope.SUBTREE);
67         } catch (final Exception e) {
68             LOG.error("Interface DataChange listener registration failed", e);
69             throw new IllegalStateException("Nexthop Manager registration Listener failed.", e);
70         }
71     }
72
73     @Override
74     protected void add(InstanceIdentifier<Interface> identifier, Interface intrf) {
75       LOG.trace("Received interface {} up event", intrf);
76       try {
77         String interfaceName = intrf.getName();
78         LOG.info("Received port UP event for interface {} ", interfaceName);
79         org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface
80             configInterface = InterfaceUtils.getInterface(broker, interfaceName);
81         BigInteger dpnId = InterfaceUtils.getDpIdFromInterface(intrf);
82         if (configInterface != null && configInterface.getType().equals(Tunnel.class)) {
83           if(intrf.getOperStatus().equals(Interface.OperStatus.Up)) {
84             //advertise all prefixes in all vpns for this dpn to bgp
85             // FIXME: Blocked until tunnel event[vxlan/gre] support is available
86             // vpnInterfaceManager.updatePrefixesForDPN(dpnId, VpnInterfaceManager.UpdateRouteAction.ADVERTISE_ROUTE);
87           }
88         } else {
89           vpnInterfaceManager.processVpnInterfaceUp(dpnId, interfaceName, intrf.getIfIndex());
90           vpnInterfaceManager.getVpnSubnetRouteHandler().onInterfaceUp(intrf);
91           handleRouterInterfacesUpEvent(interfaceName);
92         }
93       } catch (Exception e) {
94         LOG.error("Exception caught in Interface Operational State Up event", e);
95       }
96     }
97
98
99     private InstanceIdentifier<Interface> getWildCardPath() {
100         return InstanceIdentifier.create(InterfacesState.class).child(Interface.class);
101     }
102
103     @Override
104     protected void remove(InstanceIdentifier<Interface> identifier, Interface intrf) {
105       LOG.trace("Received interface {} down event", intrf);
106       try {
107         String interfaceName = intrf.getName();
108         LOG.info("Received port DOWN event for interface {} ", interfaceName);
109         BigInteger dpId = InterfaceUtils.getDpIdFromInterface(intrf);
110         if (intrf != null && intrf.getType() != null && intrf.getType().equals(Tunnel.class)) {
111           //withdraw all prefixes in all vpns for this dpn from bgp
112           // FIXME: Blocked until tunnel event[vxlan/gre] support is available
113           // vpnInterfaceManager.updatePrefixesForDPN(dpId, VpnInterfaceManager.UpdateRouteAction.WITHDRAW_ROUTE);
114         } else {
115           if (VpnUtil.isVpnInterfaceConfigured(broker, interfaceName)) {
116             vpnInterfaceManager.processVpnInterfaceDown(dpId, interfaceName, intrf.getIfIndex(), true);
117             vpnInterfaceManager.getVpnSubnetRouteHandler().onInterfaceDown(intrf);
118             handleRouterInterfacesDownEvent(interfaceName,dpId);
119           }
120         }
121       } catch (Exception e) {
122         LOG.error("Exception caught in onVlanInterfaceOperationalStateDown", e);
123       }
124     }
125
126     @Override
127     protected void update(InstanceIdentifier<Interface> identifier,
128             Interface original, Interface update) {
129       LOG.trace("Operation Interface update event - Old: {}, New: {}", original, update);
130       String interfaceName = update.getName();
131       org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface
132           intf = InterfaceUtils.getInterface(broker, interfaceName);
133       if (intf != null && intf.getType().equals(Tunnel.class)) {
134         /*
135         // FIXME: Blocked until tunnel event[vxlan/gre] support is available
136         BigInteger dpnId = InterfaceUtils.getDpIdFromInterface(update);
137         if(update.getOperStatus().equals(Interface.OperStatus.Up)) {
138           //advertise all prefixes in all vpns for this dpn to bgp
139           // vpnInterfaceManager.updatePrefixesForDPN(dpnId, VpnInterfaceManager.UpdateRouteAction.ADVERTISE_ROUTE);
140           vpnInterfaceManager.getVpnSubnetRouteHandler().onInterfaceUp(update);
141         } else if(update.getOperStatus().equals(Interface.OperStatus.Down)) {
142           //withdraw all prefixes in all vpns for this dpn from bgp
143           // vpnInterfaceManager.updatePrefixesForDPN(dpnId, VpnInterfaceManager.UpdateRouteAction.WITHDRAW_ROUTE);
144           vpnInterfaceManager.getVpnSubnetRouteHandler().onInterfaceDown(update);
145         }*/
146       }
147
148     }
149
150     void handleRouterInterfacesUpEvent(String interfaceName) {
151         Optional<RouterInterface> optRouterInterface = VpnUtil.read(broker, LogicalDatastoreType.CONFIGURATION, VpnUtil.getRouterInterfaceId(interfaceName));
152         if(optRouterInterface.isPresent()) {
153             RouterInterface routerInterface = optRouterInterface.get();
154             String routerName = routerInterface.getRouterName();
155             LOG.debug("Handling UP event for router interface {} in Router {}", interfaceName, routerName);
156             vpnInterfaceManager.addToNeutronRouterDpnsMap(routerName, interfaceName);
157         } else {
158             LOG.debug("No Router interface configured to handle UP event for {}", interfaceName);
159         }
160     }
161
162     void handleRouterInterfacesDownEvent(String interfaceName,BigInteger dpnId) {
163         Optional<RouterInterface> optRouterInterface = VpnUtil.read(broker, LogicalDatastoreType.CONFIGURATION, VpnUtil.getRouterInterfaceId(interfaceName));
164         if(optRouterInterface.isPresent()) {
165             RouterInterface routerInterface = optRouterInterface.get();
166             String routerName = routerInterface.getRouterName();
167             LOG.debug("Handling DOWN event for router interface {} in Router {}", interfaceName, routerName);
168             vpnInterfaceManager.removeFromNeutronRouterDpnsMap(routerName, interfaceName,dpnId);
169         } else {
170             LOG.debug("No Router interface configured to handle  DOWN event for {}", interfaceName);
171         }
172     }
173
174 }