2 * Copyright (c) 2015 - 2016 Ericsson India Global Services Pvt Ltd. and others. All rights reserved.
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
8 package org.opendaylight.vpnservice;
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;
27 import java.math.BigInteger;
29 public class InterfaceStateChangeListener extends AbstractDataChangeListener<Interface> implements AutoCloseable {
30 private static final Logger LOG = LoggerFactory.getLogger(InterfaceStateChangeListener.class);
32 private ListenerRegistration<DataChangeListener> listenerRegistration;
33 private final DataBroker broker;
34 private VpnInterfaceManager vpnInterfaceManager;
35 private OdlInterfaceRpcService interfaceManager;
38 public InterfaceStateChangeListener(final DataBroker db, VpnInterfaceManager vpnInterfaceManager) {
39 super(Interface.class);
41 this.vpnInterfaceManager = vpnInterfaceManager;
45 public void setInterfaceManager(OdlInterfaceRpcService interfaceManager) {
46 this.interfaceManager = interfaceManager;
50 public void close() throws Exception {
51 if (listenerRegistration != null) {
53 listenerRegistration.close();
54 } catch (final Exception e) {
55 LOG.error("Error when cleaning up DataChangeListener.", e);
57 listenerRegistration = null;
59 LOG.info("Interface listener Closed");
63 private void registerListener(final DataBroker db) {
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);
74 protected void add(InstanceIdentifier<Interface> identifier, Interface intrf) {
75 LOG.trace("Received interface {} up event", intrf);
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);
89 vpnInterfaceManager.processVpnInterfaceUp(dpnId, interfaceName, intrf.getIfIndex());
90 vpnInterfaceManager.getVpnSubnetRouteHandler().onInterfaceUp(intrf);
91 handleRouterInterfacesUpEvent(interfaceName);
93 } catch (Exception e) {
94 LOG.error("Exception caught in Interface Operational State Up event", e);
99 private InstanceIdentifier<Interface> getWildCardPath() {
100 return InstanceIdentifier.create(InterfacesState.class).child(Interface.class);
104 protected void remove(InstanceIdentifier<Interface> identifier, Interface intrf) {
105 LOG.trace("Received interface {} down event", intrf);
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);
115 if (VpnUtil.isVpnInterfaceConfigured(broker, interfaceName)) {
116 vpnInterfaceManager.processVpnInterfaceDown(dpId, interfaceName, intrf.getIfIndex(), true);
117 vpnInterfaceManager.getVpnSubnetRouteHandler().onInterfaceDown(intrf);
118 handleRouterInterfacesDownEvent(interfaceName,dpId);
121 } catch (Exception e) {
122 LOG.error("Exception caught in onVlanInterfaceOperationalStateDown", e);
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)) {
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);
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);
158 LOG.debug("No Router interface configured to handle UP event for {}", interfaceName);
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);
170 LOG.debug("No Router interface configured to handle DOWN event for {}", interfaceName);