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 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;
25 import java.math.BigInteger;
27 public class InterfaceStateChangeListener extends AbstractDataChangeListener<Interface> implements AutoCloseable {
28 private static final Logger LOG = LoggerFactory.getLogger(InterfaceStateChangeListener.class);
30 private ListenerRegistration<DataChangeListener> listenerRegistration;
31 private final DataBroker broker;
32 private VpnInterfaceManager vpnInterfaceManager;
33 private OdlInterfaceRpcService interfaceManager;
36 public InterfaceStateChangeListener(final DataBroker db, VpnInterfaceManager vpnInterfaceManager) {
37 super(Interface.class);
39 this.vpnInterfaceManager = vpnInterfaceManager;
43 public void setInterfaceManager(OdlInterfaceRpcService interfaceManager) {
44 this.interfaceManager = interfaceManager;
48 public void close() throws Exception {
49 if (listenerRegistration != null) {
51 listenerRegistration.close();
52 } catch (final Exception e) {
53 LOG.error("Error when cleaning up DataChangeListener.", e);
55 listenerRegistration = null;
57 LOG.info("Interface listener Closed");
61 private void registerListener(final DataBroker db) {
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);
72 protected void add(InstanceIdentifier<Interface> identifier, Interface intrf) {
73 LOG.trace("Received interface {} up event", intrf);
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);
86 vpnInterfaceManager.processVpnInterfaceUp(dpnId, interfaceName, intrf.getIfIndex());
88 } catch (Exception e) {
89 LOG.error("Exception caught in Interface Operational State Up event", e);
94 private InstanceIdentifier<Interface> getWildCardPath() {
95 return InstanceIdentifier.create(InterfacesState.class).child(Interface.class);
99 protected void remove(InstanceIdentifier<Interface> identifier, Interface intrf) {
100 LOG.trace("Received interface {} down event", intrf);
102 String interfaceName = intrf.getName();
103 LOG.info("Received port DOWN event for interface {} ", interfaceName);
104 BigInteger dpId = InterfaceUtils.getDpIdFromInterface(intrf);
105 if (intrf != null && intrf.getType() != null && intrf.getType().equals(Tunnel.class)) {
106 //withdraw all prefixes in all vpns for this dpn from bgp
107 vpnInterfaceManager.updatePrefixesForDPN(dpId, VpnInterfaceManager.UpdateRouteAction.WITHDRAW_ROUTE);
109 if (VpnUtil.isVpnInterfaceConfigured(broker, interfaceName)) {
110 vpnInterfaceManager.processVpnInterfaceDown(dpId, interfaceName, intrf.getIfIndex(), true);
113 } catch (Exception e) {
114 LOG.error("Exception caught in onVlanInterfaceOperationalStateDown", e);
119 protected void update(InstanceIdentifier<Interface> identifier,
120 Interface original, Interface update) {
121 LOG.trace("Operation Interface update event - Old: {}, New: {}", original, update);
122 String interfaceName = update.getName();
123 org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface
124 intf = InterfaceUtils.getInterface(broker, interfaceName);
125 if (intf != null && intf.getType().equals(Tunnel.class)) {
126 BigInteger dpnId = InterfaceUtils.getDpIdFromInterface(update);
127 if(update.getOperStatus().equals(Interface.OperStatus.Up)) {
128 //advertise all prefixes in all vpns for this dpn to bgp
129 vpnInterfaceManager.updatePrefixesForDPN(dpnId, VpnInterfaceManager.UpdateRouteAction.ADVERTISE_ROUTE);
130 } else if(update.getOperStatus().equals(Interface.OperStatus.Down)) {
131 //withdraw all prefixes in all vpns for this dpn from bgp
132 vpnInterfaceManager.updatePrefixesForDPN(dpnId, VpnInterfaceManager.UpdateRouteAction.WITHDRAW_ROUTE);