/* * Copyright (c) 2015, 2017 Ericsson India Global Services Pvt Ltd. and others. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html */ package org.opendaylight.netvirt.vpnmanager; import com.google.common.util.concurrent.ListenableFuture; import java.util.ArrayList; import java.util.List; import java.util.Optional; import java.util.concurrent.ExecutionException; import javax.annotation.PreDestroy; import javax.inject.Inject; import javax.inject.Singleton; import org.eclipse.jdt.annotation.NonNull; import org.opendaylight.genius.datastoreutils.SingleTransactionDataBroker; import org.opendaylight.infrautils.jobcoordinator.JobCoordinator; import org.opendaylight.infrautils.utils.concurrent.Executors; import org.opendaylight.mdsal.binding.api.DataBroker; import org.opendaylight.mdsal.common.api.LogicalDatastoreType; import org.opendaylight.netvirt.neutronvpn.api.utils.NeutronUtils; import org.opendaylight.netvirt.neutronvpn.interfaces.INeutronVpnManager; import org.opendaylight.netvirt.vpnmanager.api.InterfaceUtils; import org.opendaylight.serviceutils.tools.listener.AbstractAsyncDataTreeChangeListener; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana._if.type.rev170119.L2vlan; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfacesState; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid; import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.port.op.data.PortOpDataEntry; import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.vpn._interface.op.data.VpnInterfaceOpDataEntry; import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.l3vpn.rev200204.vpn.interfaces.VpnInterface; import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.l3vpn.rev200204.vpn.interfaces.vpn._interface.VpnInstanceNames; import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.port.attributes.FixedIps; import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.ports.attributes.ports.Port; import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; import org.opendaylight.yangtools.yang.common.Uint64; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @Singleton public class SubnetRouteInterfaceStateChangeListener extends AbstractAsyncDataTreeChangeListener { private static final Logger LOG = LoggerFactory.getLogger(SubnetRouteInterfaceStateChangeListener.class); private static final String LOGGING_PREFIX = "SUBNETROUTE:"; private final DataBroker dataBroker; private final VpnSubnetRouteHandler vpnSubnetRouteHandler; private final SubnetOpDpnManager subOpDpnManager; private final INeutronVpnManager neutronVpnManager; private final JobCoordinator jobCoordinator; @Inject public SubnetRouteInterfaceStateChangeListener(final DataBroker dataBroker, final VpnSubnetRouteHandler vpnSubnetRouteHandler, final SubnetOpDpnManager subnetOpDpnManager, final INeutronVpnManager neutronVpnService, final JobCoordinator jobCoordinator) { super(dataBroker, LogicalDatastoreType.OPERATIONAL, InstanceIdentifier.create(InterfacesState.class).child(Interface.class), Executors.newListeningSingleThreadExecutor("SubnetRouteInterfaceStateChangeListener", LOG)); this.dataBroker = dataBroker; this.vpnSubnetRouteHandler = vpnSubnetRouteHandler; this.subOpDpnManager = subnetOpDpnManager; this.neutronVpnManager = neutronVpnService; this.jobCoordinator = jobCoordinator; start(); } public void start() { LOG.info("{} start", getClass().getSimpleName()); } @Override @PreDestroy public void close() { super.close(); Executors.shutdownAndAwaitTermination(getExecutorService()); } @Override public void add(InstanceIdentifier identifier, Interface intrf) { LOG.trace("{} add: Received interface {} up event", LOGGING_PREFIX, intrf); if (L2vlan.class.equals(intrf.getType())) { LOG.trace("SubnetRouteInterfaceListener add: Received interface {} up event", intrf); if (Interface.OperStatus.Up.equals(intrf.getOperStatus())) { List subnetIdList = getSubnetId(intrf); if (subnetIdList.isEmpty()) { LOG.trace("SubnetRouteInterfaceListener add: Port {} doesn't exist in configDS", intrf.getName()); return; } for (Uuid subnetId : subnetIdList) { jobCoordinator.enqueueJob("SUBNETROUTE-" + subnetId, () -> { String interfaceName = intrf.getName(); Uint64 dpnId = Uint64.ZERO; LOG.info("{} add: Received port UP event for interface {} subnetId {}", LOGGING_PREFIX, interfaceName, subnetId); try { dpnId = InterfaceUtils.getDpIdFromInterface(intrf); } catch (NullPointerException e) { LOG.error("{} add: Unable to obtain dpnId for interface {} in subnet {}," + " subnetroute inclusion for this interface failed", LOGGING_PREFIX, interfaceName, subnetId, e); } List> futures = new ArrayList<>(); try { InstanceIdentifier id = VpnUtil.getVpnInterfaceIdentifier(interfaceName); Optional cfgVpnInterface = SingleTransactionDataBroker.syncReadOptional( dataBroker, LogicalDatastoreType.CONFIGURATION, id); if (!cfgVpnInterface.isPresent()) { return futures; } vpnSubnetRouteHandler.onInterfaceUp(dpnId, intrf.getName(), subnetId); LOG.info("{} add: Processed interface {} up event", LOGGING_PREFIX, intrf.getName()); } catch (InterruptedException | ExecutionException e) { LOG.error("add: Failed to read data store for interface {} dpn {}", interfaceName, dpnId); } return futures; }); } } } } // TODO Clean up the exception handling @SuppressWarnings("checkstyle:IllegalCatch") @Override public void remove(InstanceIdentifier identifier, Interface intrf) { if (L2vlan.class.equals(intrf.getType())) { LOG.trace("SubnetRouteInterfaceListener remove: Received interface {} down event", intrf); List subnetIdList = getSubnetId(intrf); if (subnetIdList.isEmpty()) { LOG.trace("SubnetRouteInterfaceListener remove: Port {} doesn't exist in configDS", intrf.getName()); return; } LOG.trace("{} remove: Processing interface {} down event in ", LOGGING_PREFIX, intrf.getName()); for (Uuid subnetId : subnetIdList) { jobCoordinator.enqueueJob("SUBNETROUTE-" + subnetId, () -> { List> futures = new ArrayList<>(); try { String interfaceName = intrf.getName(); Uint64 dpnId = Uint64.ZERO; LOG.info("{} remove: Received port DOWN event for interface {} in subnet {} ", LOGGING_PREFIX, interfaceName, subnetId); try { dpnId = InterfaceUtils.getDpIdFromInterface(intrf); } catch (Exception e) { LOG.error("{} remove: Unable to retrieve dpnId for interface {} in subnet {}. " + "Fetching from vpn interface itself", LOGGING_PREFIX, intrf.getName(), subnetId, e); } InstanceIdentifier id = VpnUtil .getVpnInterfaceIdentifier(interfaceName); Optional cfgVpnInterface = SingleTransactionDataBroker.syncReadOptional( dataBroker, LogicalDatastoreType.CONFIGURATION, id); if (!cfgVpnInterface.isPresent()) { return futures; } boolean interfaceDownEligible = false; for (VpnInstanceNames vpnInterfaceVpnInstance : cfgVpnInterface.get().nonnullVpnInstanceNames()) { String vpnName = vpnInterfaceVpnInstance.getVpnName(); InstanceIdentifier idOper = VpnUtil .getVpnInterfaceOpDataEntryIdentifier(interfaceName, vpnName); Optional optVpnInterface = SingleTransactionDataBroker .syncReadOptional(dataBroker, LogicalDatastoreType.OPERATIONAL, idOper); if (optVpnInterface.isPresent()) { Uint64 dpnIdLocal = dpnId; if (Uint64.ZERO.equals(dpnIdLocal)) { dpnIdLocal = optVpnInterface.get().getDpnId(); } if (!Uint64.ZERO.equals(dpnIdLocal)) { interfaceDownEligible = true; break; } } } if (interfaceDownEligible) { vpnSubnetRouteHandler.onInterfaceDown(dpnId, intrf.getName(), subnetId); } LOG.info("{} remove: Processed interface {} down event in ", LOGGING_PREFIX, intrf.getName()); } catch (InterruptedException | ExecutionException e) { LOG.error("{} remove: Failed to read data store for {}", LOGGING_PREFIX, intrf.getName()); } return futures; }); } } } @Override public void update(InstanceIdentifier identifier, Interface original, Interface update) { String interfaceName = update.getName(); if (L2vlan.class.equals(update.getType())) { LOG.trace("{} update: Operation Interface update event - Old: {}, New: {}", LOGGING_PREFIX, original, update); List subnetIdList = getSubnetId(update); if (subnetIdList.isEmpty()) { LOG.error("SubnetRouteInterfaceListener update: Port {} doesn't exist in configDS", update.getName()); return; } for (Uuid subnetId : subnetIdList) { jobCoordinator.enqueueJob("SUBNETROUTE-" + subnetId, () -> { Uint64 dpnId = Uint64.ZERO; try { dpnId = InterfaceUtils.getDpIdFromInterface(update); } catch (NullPointerException e) { LOG.error("{} remove: Unable to retrieve dpnId for interface {} in subnet {}. " + "Fetching from vpn interface itself", LOGGING_PREFIX, update.getName(), subnetId, e); } List> futures = new ArrayList<>(); try { InstanceIdentifier id = VpnUtil .getVpnInterfaceIdentifier(interfaceName); Optional cfgVpnInterface = SingleTransactionDataBroker.syncReadOptional( dataBroker, LogicalDatastoreType.CONFIGURATION, id); if (!cfgVpnInterface.isPresent()) { return futures; } boolean interfaceChangeEligible = false; for (VpnInstanceNames vpnInterfaceVpnInstance : cfgVpnInterface.get().nonnullVpnInstanceNames()) { String vpnName = vpnInterfaceVpnInstance.getVpnName(); InstanceIdentifier idOper = VpnUtil .getVpnInterfaceOpDataEntryIdentifier(interfaceName, vpnName); Optional optVpnInterface = SingleTransactionDataBroker .syncReadOptional(dataBroker, LogicalDatastoreType.OPERATIONAL, idOper); if (optVpnInterface.isPresent()) { Uint64 dpnIdLocal = dpnId; if (Uint64.ZERO.equals(dpnIdLocal)) { dpnIdLocal = optVpnInterface.get().getDpnId(); } if (!Uint64.ZERO.equals(dpnIdLocal)) { interfaceChangeEligible = true; break; } } } if (interfaceChangeEligible) { if (Interface.OperStatus.Up.equals(update.getOperStatus())) { LOG.info("{} update: Received port UP event for interface {} in subnet {}", LOGGING_PREFIX, update.getName(), subnetId); vpnSubnetRouteHandler.onInterfaceUp(dpnId, update.getName(), subnetId); } else if (Interface.OperStatus.Down.equals(update.getOperStatus()) || Interface.OperStatus.Unknown.equals(update.getOperStatus())) { /* * If the interface went down voluntarily (or) if the interface is not * reachable from control-path involuntarily, trigger subnetRoute election */ LOG.info("{} update: Received port {} event for interface {} in subnet {} ", LOGGING_PREFIX, Interface.OperStatus.Unknown.equals(update.getOperStatus()) ? "UNKNOWN" : "DOWN", update.getName(), subnetId); vpnSubnetRouteHandler.onInterfaceDown(dpnId, update.getName(), subnetId); } } } catch (InterruptedException | ExecutionException e) { LOG.error("update: Failed to read data store for interface {} dpn {}", interfaceName, dpnId); } return futures; }); } } LOG.info("{} update: Processed Interface {} update event", LOGGING_PREFIX, update.getName()); } @NonNull protected List getSubnetId(Interface intrf) { List listSubnetIds = new ArrayList<>(); if (!NeutronUtils.isUuid(intrf.getName())) { LOG.debug("SubnetRouteInterfaceListener: Interface {} doesn't have valid uuid pattern", intrf.getName()); return listSubnetIds; } PortOpDataEntry portOpEntry = subOpDpnManager.getPortOpDataEntry(intrf.getName()); if (portOpEntry != null) { List subnet = portOpEntry.getSubnetIds(); if (subnet != null) { return subnet; } return listSubnetIds; } LOG.trace("SubnetRouteInterfaceListener : Received Port {} event for {} that is not part of subnetRoute", intrf.getOperStatus(), intrf.getName()); Port port = neutronVpnManager.getNeutronPort(intrf.getName()); if (port == null) { return listSubnetIds; } List portIps = port.getFixedIps(); if (portIps != null) { for (FixedIps portIp : portIps) { listSubnetIds.add(portIp.getSubnetId()); } } return listSubnetIds; } }