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 java.util.List;
11 import javax.annotation.PostConstruct;
12 import javax.inject.Inject;
13 import javax.inject.Singleton;
14 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
15 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
16 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
17 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
18 import org.opendaylight.genius.datastoreutils.AsyncClusteredDataTreeChangeListenerBase;
19 import org.opendaylight.netvirt.elan.l2gw.utils.L2GatewayConnectionUtils;
20 import org.opendaylight.netvirt.elan.utils.ElanClusterUtils;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.ElanInstances;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstance;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateway.connections.attributes.L2gatewayConnections;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateway.connections.attributes.l2gatewayconnections.L2gatewayConnection;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150712.Neutron;
26 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
31 public class ElanInstanceListener extends AsyncClusteredDataTreeChangeListenerBase<ElanInstance, ElanInstanceListener> {
33 private static final Logger LOG = LoggerFactory.getLogger(ElanInstanceListener.class);
35 private final DataBroker broker;
36 private final ElanClusterUtils elanClusterUtils;
39 public ElanInstanceListener(final DataBroker db, final ElanClusterUtils elanClusterUtils) {
40 super(ElanInstance.class, ElanInstanceListener.class);
42 this.elanClusterUtils = elanClusterUtils;
47 registerListener(LogicalDatastoreType.CONFIGURATION, broker);
51 protected void remove(final InstanceIdentifier<ElanInstance> identifier,
52 final ElanInstance del) {
53 elanClusterUtils.runOnlyInOwnerNode(del.getElanInstanceName(), "delete Elan instance",
55 LOG.info("Elan instance {} deleted from Configuration tree ", del);
56 List<L2gatewayConnection> connections =
57 L2GatewayConnectionUtils.getL2GwConnectionsByElanName(
58 this.broker, del.getElanInstanceName());
59 if (connections == null || connections.isEmpty()) {
63 ReadWriteTransaction tx = this.broker.newReadWriteTransaction();
64 for (L2gatewayConnection connection : connections) {
65 InstanceIdentifier<L2gatewayConnection> iid = InstanceIdentifier.create(Neutron.class)
66 .child(L2gatewayConnections.class).child(L2gatewayConnection.class, connection.getKey());
67 tx.delete(LogicalDatastoreType.CONFIGURATION, iid);
69 tx.submit().checkedGet();
70 } catch (TransactionCommitFailedException e) {
71 LOG.error("Failed to delete associated l2gwconnection while deleting network", e);
78 protected void update(InstanceIdentifier<ElanInstance> identifier, ElanInstance original, ElanInstance update) {
83 protected void add(InstanceIdentifier<ElanInstance> identifier, ElanInstance add) {
87 protected ElanInstanceListener getDataTreeChangeListener() {
88 return ElanInstanceListener.this;
92 protected InstanceIdentifier<ElanInstance> getWildCardPath() {
93 return InstanceIdentifier.create(ElanInstances.class).child(ElanInstance.class);