2 * Copyright (c) 2017 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.elan.internal;
10 import static org.opendaylight.genius.infra.Datastore.CONFIGURATION;
12 import java.util.Collections;
13 import java.util.Optional;
14 import javax.annotation.PreDestroy;
15 import javax.inject.Inject;
16 import javax.inject.Singleton;
17 import org.opendaylight.genius.infra.ManagedNewTransactionRunner;
18 import org.opendaylight.genius.infra.ManagedNewTransactionRunnerImpl;
19 import org.opendaylight.infrautils.jobcoordinator.JobCoordinator;
20 import org.opendaylight.infrautils.utils.concurrent.Executors;
21 import org.opendaylight.mdsal.binding.api.DataBroker;
22 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
23 import org.opendaylight.netvirt.elan.cache.ElanInterfaceCache;
24 import org.opendaylight.netvirt.elan.utils.ElanConstants;
25 import org.opendaylight.netvirt.elan.utils.ElanUtils;
26 import org.opendaylight.serviceutils.tools.listener.AbstractAsyncDataTreeChangeListener;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.Interfaces;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.IfL2vlan;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.interfaces.ElanInterface;
31 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
36 public class ElanInterfaceConfigListener
37 extends AbstractAsyncDataTreeChangeListener<Interface> {
39 private static final Logger LOG = LoggerFactory.getLogger(ElanInterfaceConfigListener.class);
41 private final DataBroker dataBroker;
42 private final ManagedNewTransactionRunner txRunner;
43 private final ElanInterfaceManager elanInterfaceManager;
44 private final JobCoordinator jobCoordinator;
45 private final ElanInterfaceCache elanInterfaceCache;
48 public ElanInterfaceConfigListener(DataBroker dataBroker, ElanInterfaceManager elanInterfaceManager,
49 JobCoordinator jobCoordinator, ElanInterfaceCache elanInterfaceCache) {
50 super(dataBroker, LogicalDatastoreType.CONFIGURATION, InstanceIdentifier.create(Interfaces.class)
51 .child(Interface.class),
52 Executors.newListeningSingleThreadExecutor("ElanInterfaceConfigListener", LOG));
53 this.dataBroker = dataBroker;
54 this.txRunner = new ManagedNewTransactionRunnerImpl(dataBroker);
55 this.elanInterfaceManager = elanInterfaceManager;
56 this.jobCoordinator = jobCoordinator;
57 this.elanInterfaceCache = elanInterfaceCache;
61 LOG.info("ElanInterfaceConfigListener init");
65 public void remove(InstanceIdentifier<Interface> key, Interface intrf) {
66 // Sometimes elan service is not unbound on the interface when the user does nova delete followed
67 // by neutron port delete since interface config is deleted a bit later. so adding logic to
68 // unbind service for interface config removal.
69 if (intrf == null || intrf.augmentation(IfL2vlan.class) == null) {
70 LOG.debug("The interface {} is not a L2 interface. Ignoring it", intrf);
74 String interfaceName = intrf.getName();
75 Optional<ElanInterface> elanInterface = elanInterfaceCache.get(interfaceName);
76 if (!elanInterface.isPresent()) {
77 LOG.debug("There is no ELAN service for interface {}. Ignoring it", interfaceName);
80 jobCoordinator.enqueueJob(ElanUtils.getElanInterfaceJobKey(interfaceName),
81 () -> Collections.singletonList(txRunner.callWithNewReadWriteTransactionAndSubmit(CONFIGURATION, tx -> {
82 LOG.debug("unbinding elan service on interface {} for its config removal", interfaceName);
83 elanInterfaceManager.unbindService(interfaceName, tx);
84 })), ElanConstants.JOB_MAX_RETRIES);
88 public void update(InstanceIdentifier<Interface> key, Interface dataObjectModificationBefore,
89 Interface dataObjectModificationAfter) {
90 // Not required to handle this event
94 public void add(InstanceIdentifier<Interface> key, Interface dataObjectModification) {
95 // Not required to handle this event
100 public void close() {
102 Executors.shutdownAndAwaitTermination(getExecutorService());