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.netvirt.vpnmanager;
10 import java.math.BigInteger;
12 import com.google.common.base.Optional;
13 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
14 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
15 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker;
16 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
17 import org.opendaylight.netvirt.vpnmanager.utilities.InterfaceUtils;
18 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.interfaces.VpnInterface;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana._if.type.rev140508.Tunnel;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfacesState;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
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 public class SubnetRouteInterfaceStateChangeListener extends AbstractDataChangeListener<Interface>
28 implements AutoCloseable {
29 private static final Logger LOG = LoggerFactory.getLogger(InterfaceStateChangeListener.class);
30 private ListenerRegistration<DataChangeListener> listenerRegistration;
31 private final DataBroker dataBroker;
32 private final VpnInterfaceManager vpnInterfaceManager;
33 private final VpnSubnetRouteHandler vpnSubnetRouteHandler;
35 public SubnetRouteInterfaceStateChangeListener(final DataBroker dataBroker,
36 final VpnInterfaceManager vpnInterfaceManager,
37 final VpnSubnetRouteHandler vpnSubnetRouteHandler) {
38 super(Interface.class);
39 this.dataBroker = dataBroker;
40 this.vpnInterfaceManager = vpnInterfaceManager;
41 this.vpnSubnetRouteHandler = vpnSubnetRouteHandler;
45 LOG.info("{} start", getClass().getSimpleName());
46 listenerRegistration = dataBroker.registerDataChangeListener(LogicalDatastoreType.OPERATIONAL,
47 getWildCardPath(), this, AsyncDataBroker.DataChangeScope.SUBTREE);
50 private InstanceIdentifier<Interface> getWildCardPath() {
51 return InstanceIdentifier.create(InterfacesState.class).child(Interface.class);
55 public void close() throws Exception {
56 if (listenerRegistration != null) {
57 listenerRegistration.close();
58 listenerRegistration = null;
60 LOG.info("{} close", getClass().getSimpleName());
64 protected void add(InstanceIdentifier<Interface> identifier, Interface intrf) {
65 LOG.trace("SubnetRouteInterfaceListener add: Received interface {} up event", intrf);
67 String interfaceName = intrf.getName();
68 LOG.info("SubnetRouteInterfaceListener add: Received port UP event for interface {} ", interfaceName);
69 org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface
70 configInterface = InterfaceUtils.getInterface(dataBroker, interfaceName);
71 if (configInterface != null && (configInterface.getType() != null)) {
72 if (!configInterface.getType().equals(Tunnel.class)) {
73 final VpnInterface vpnInterface = VpnUtil.getConfiguredVpnInterface(dataBroker, interfaceName);
74 if (vpnInterface != null) {
75 BigInteger dpnId = BigInteger.ZERO;
77 dpnId = InterfaceUtils.getDpIdFromInterface(intrf);
78 } catch (Exception e) {
79 LOG.error("SubnetRouteInterfaceListener add: Unable to obtain dpnId for interface {},",
80 " subnetroute inclusion for this interface failed with exception {}",
84 vpnSubnetRouteHandler.onInterfaceUp(dpnId, intrf.getName());
88 } catch (Exception e) {
89 LOG.error("SubnetRouteInterfaceListener add: Exception observed in handling addition for VPN Interface {}. ", intrf.getName(), e);
94 protected void remove(InstanceIdentifier<Interface> identifier, Interface intrf) {
95 LOG.trace("SubnetRouteInterfaceListener remove: Received interface {} down event", intrf);
97 String interfaceName = intrf.getName();
98 BigInteger dpnId = BigInteger.ZERO;
99 LOG.info("SubnetRouteInterfaceListener remove: Received port DOWN event for interface {} ", interfaceName);
100 if (intrf != null && intrf.getType() != null && (!intrf.getType().equals(Tunnel.class))) {
101 InstanceIdentifier<VpnInterface> id = VpnUtil.getVpnInterfaceIdentifier(interfaceName);
102 Optional<VpnInterface> optVpnInterface = VpnUtil.read(dataBroker, LogicalDatastoreType.OPERATIONAL, id);
103 if (!optVpnInterface.isPresent()) {
104 LOG.debug("SubnetRouteInterfaceListener remove: Interface {} is not a vpninterface, ignoring.", intrf.getName());
107 VpnInterface vpnInterface = optVpnInterface.get();
109 dpnId = InterfaceUtils.getDpIdFromInterface(intrf);
110 } catch (Exception e) {
111 LOG.error("SubnetRouteInterfaceListener remove: Unable to retrieve dpnId for interface {}. " +
112 "Fetching from vpn interface itself due to exception {}",
114 dpnId = vpnInterface.getDpnId();
116 vpnSubnetRouteHandler.onInterfaceDown(dpnId, intrf.getName());
118 } catch (Exception e) {
119 LOG.error("SubnetRouteInterfaceListener remove: Exception observed in handling deletion of VPN Interface {}. ",
125 protected void update(InstanceIdentifier<Interface> identifier,
126 Interface original, Interface update) {
127 LOG.trace("SubnetRouteInterfaceListener update: Operation Interface update event - Old: {}, New: {}", original, update);
128 String interfaceName = update.getName();
129 BigInteger dpnId = BigInteger.ZERO;
130 if (update != null && (update.getType() != null)) {
131 if (!update.getType().equals(Tunnel.class)) {
132 final VpnInterface vpnInterface = VpnUtil.getConfiguredVpnInterface(dataBroker, interfaceName);
133 if (vpnInterface != null) {
134 if (update.getOperStatus().equals(Interface.OperStatus.Up)) {
136 dpnId = InterfaceUtils.getDpIdFromInterface(update);
137 } catch (Exception e) {
138 LOG.error("SubnetRouteInterfaceListener update: Unable to obtain dpnId for interface {} port up,",
139 " subnetroute inclusion for this interface failed with exception {}",
143 vpnSubnetRouteHandler.onInterfaceUp(dpnId, update.getName());
144 } else if (update.getOperStatus().equals(Interface.OperStatus.Down)) {
146 dpnId = InterfaceUtils.getDpIdFromInterface(update);
147 } catch (Exception e) {
148 LOG.error("SubnetRouteInterfaceListener update: Unable to obtain dpnId for interface {} port down,",
149 " subnetroute exclusion for this interface failed with exception {}",
153 vpnSubnetRouteHandler.onInterfaceDown(dpnId, update.getName());