2 * Copyright (c) 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.elan.l2gw.listeners;
10 import static org.opendaylight.mdsal.binding.util.Datastore.CONFIGURATION;
12 import com.google.common.util.concurrent.ListenableFuture;
13 import java.util.Collections;
14 import java.util.List;
15 import javax.annotation.PreDestroy;
16 import javax.inject.Inject;
17 import javax.inject.Singleton;
18 import org.opendaylight.genius.utils.batching.ResourceBatchingManager;
19 import org.opendaylight.infrautils.utils.concurrent.Executors;
20 import org.opendaylight.infrautils.utils.concurrent.LoggingFutures;
21 import org.opendaylight.mdsal.binding.api.DataBroker;
22 import org.opendaylight.mdsal.binding.util.ManagedNewTransactionRunner;
23 import org.opendaylight.mdsal.binding.util.ManagedNewTransactionRunnerImpl;
24 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
25 import org.opendaylight.netvirt.elan.l2gw.recovery.impl.L2GatewayServiceRecoveryHandler;
26 import org.opendaylight.netvirt.elan.l2gw.utils.L2GatewayConnectionUtils;
27 import org.opendaylight.netvirt.elan.utils.ElanClusterUtils;
28 import org.opendaylight.serviceutils.srm.RecoverableListener;
29 import org.opendaylight.serviceutils.srm.ServiceRecoveryRegistry;
30 import org.opendaylight.serviceutils.tools.listener.AbstractClusteredAsyncDataTreeChangeListener;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.ElanForwardingTables;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.ElanInstances;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.forwarding.tables.MacTable;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.forwarding.tables.MacTableKey;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstance;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateway.connections.attributes.L2gatewayConnections;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateway.connections.attributes.l2gatewayconnections.L2gatewayConnection;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150712.Neutron;
39 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
44 public class ElanInstanceListener extends AbstractClusteredAsyncDataTreeChangeListener<ElanInstance>
45 implements RecoverableListener {
47 private static final Logger LOG = LoggerFactory.getLogger(ElanInstanceListener.class);
49 private final DataBroker broker;
50 private final ManagedNewTransactionRunner txRunner;
51 private final ElanClusterUtils elanClusterUtils;
54 public ElanInstanceListener(final DataBroker db, final ElanClusterUtils elanClusterUtils,
55 final L2GatewayServiceRecoveryHandler l2GatewayServiceRecoveryHandler,
56 final ServiceRecoveryRegistry serviceRecoveryRegistry) {
57 super(db, LogicalDatastoreType.CONFIGURATION, InstanceIdentifier.create(ElanInstances.class)
58 .child(ElanInstance.class),
59 Executors.newListeningSingleThreadExecutor("ElanInstanceListener", LOG));
61 this.txRunner = new ManagedNewTransactionRunnerImpl(db);
62 this.elanClusterUtils = elanClusterUtils;
63 serviceRecoveryRegistry.addRecoverableListener(l2GatewayServiceRecoveryHandler.buildServiceRegistryKey(), this);
64 ResourceBatchingManager.getInstance().registerDefaultBatchHandlers(db);
75 Executors.shutdownAndAwaitTermination(getExecutorService());
79 public void registerListener() {
81 LOG.info("Registering ElanInstanceListener");
87 public void deregisterListener() {
89 LOG.info("Deregistering ElanInstanceListener");
93 public void remove(final InstanceIdentifier<ElanInstance> identifier,
94 final ElanInstance del) {
95 elanClusterUtils.runOnlyInOwnerNode(del.getElanInstanceName(), "delete Elan instance",
97 LOG.info("Elan instance {} deleted from Configuration tree ", del.getElanInstanceName());
98 List<L2gatewayConnection> connections =
99 L2GatewayConnectionUtils.getL2GwConnectionsByElanName(
100 this.broker, del.getElanInstanceName());
101 if (connections.isEmpty()) {
102 return Collections.emptyList();
104 ListenableFuture<?> future = txRunner.callWithNewReadWriteTransactionAndSubmit(CONFIGURATION,
106 for (L2gatewayConnection connection : connections) {
107 InstanceIdentifier<L2gatewayConnection> iid =
108 InstanceIdentifier.create(Neutron.class).child(
109 L2gatewayConnections.class).child(
110 L2gatewayConnection.class, connection.key());
114 LoggingFutures.addErrorLogging(future, LOG,
115 "Failed to delete associate L2 gateway connection while deleting network");
116 txRunner.callWithNewReadWriteTransactionAndSubmit(CONFIGURATION, tx -> {
117 InstanceIdentifier<MacTable> macTableIid = getElanMacEntryPath(del.getElanInstanceName());
118 tx.delete(macTableIid);
120 return Collections.singletonList(future);
124 public static InstanceIdentifier<MacTable> getElanMacEntryPath(String elanName) {
125 return InstanceIdentifier.builder(ElanForwardingTables.class).child(MacTable.class,
126 new MacTableKey(elanName)).build();
130 public void update(InstanceIdentifier<ElanInstance> identifier, ElanInstance original, ElanInstance update) {
135 public void add(InstanceIdentifier<ElanInstance> identifier, ElanInstance add) {