Port ELAN service to datastore-constrained txns
[netvirt.git] / elanmanager / impl / src / main / java / org / opendaylight / netvirt / elan / l2gw / listeners / ElanInstanceListener.java
1 /*
2  * Copyright (c) 2016 Ericsson India Global Services Pvt Ltd. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.netvirt.elan.l2gw.listeners;
9
10 import static org.opendaylight.genius.infra.Datastore.CONFIGURATION;
11
12 import com.google.common.util.concurrent.ListenableFuture;
13 import java.util.Collections;
14 import java.util.List;
15 import javax.annotation.PostConstruct;
16 import javax.inject.Inject;
17 import javax.inject.Singleton;
18 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
19 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
20 import org.opendaylight.genius.datastoreutils.AsyncClusteredDataTreeChangeListenerBase;
21 import org.opendaylight.genius.infra.ManagedNewTransactionRunner;
22 import org.opendaylight.genius.infra.ManagedNewTransactionRunnerImpl;
23 import org.opendaylight.infrautils.utils.concurrent.ListenableFutures;
24 import org.opendaylight.netvirt.elan.l2gw.utils.L2GatewayConnectionUtils;
25 import org.opendaylight.netvirt.elan.utils.ElanClusterUtils;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.ElanInstances;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstance;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateway.connections.attributes.L2gatewayConnections;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateway.connections.attributes.l2gatewayconnections.L2gatewayConnection;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150712.Neutron;
31 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 @Singleton
36 public class ElanInstanceListener extends AsyncClusteredDataTreeChangeListenerBase<ElanInstance, ElanInstanceListener> {
37
38     private static final Logger LOG = LoggerFactory.getLogger(ElanInstanceListener.class);
39
40     private final DataBroker broker;
41     private final ManagedNewTransactionRunner txRunner;
42     private final ElanClusterUtils elanClusterUtils;
43
44     @Inject
45     public ElanInstanceListener(final DataBroker db, final ElanClusterUtils elanClusterUtils) {
46         super(ElanInstance.class, ElanInstanceListener.class);
47         broker = db;
48         this.txRunner = new ManagedNewTransactionRunnerImpl(db);
49         this.elanClusterUtils = elanClusterUtils;
50     }
51
52     @PostConstruct
53     public void init() {
54         registerListener(LogicalDatastoreType.CONFIGURATION, broker);
55     }
56
57     @Override
58     protected void remove(final InstanceIdentifier<ElanInstance> identifier,
59                           final ElanInstance del) {
60         elanClusterUtils.runOnlyInOwnerNode(del.getElanInstanceName(), "delete Elan instance",
61             () -> {
62                 LOG.info("Elan instance {} deleted from Configuration tree ", del);
63                 List<L2gatewayConnection> connections =
64                         L2GatewayConnectionUtils.getL2GwConnectionsByElanName(
65                                 this.broker, del.getElanInstanceName());
66                 if (connections.isEmpty()) {
67                     return Collections.emptyList();
68                 }
69                 ListenableFuture<Void> future = txRunner.callWithNewReadWriteTransactionAndSubmit(CONFIGURATION,
70                     tx -> {
71                         for (L2gatewayConnection connection : connections) {
72                             InstanceIdentifier<L2gatewayConnection> iid =
73                                     InstanceIdentifier.create(Neutron.class).child(
74                                             L2gatewayConnections.class).child(
75                                             L2gatewayConnection.class, connection.key());
76                             tx.delete(iid);
77                         }
78                     });
79                 ListenableFutures.addErrorLogging(future, LOG,
80                         "Failed to delete associate L2 gateway connection while deleting network");
81                 return Collections.singletonList(future);
82             });
83     }
84
85     @Override
86     protected void update(InstanceIdentifier<ElanInstance> identifier, ElanInstance original, ElanInstance update) {
87
88     }
89
90     @Override
91     protected void add(InstanceIdentifier<ElanInstance> identifier, ElanInstance add) {
92     }
93
94     @Override
95     protected ElanInstanceListener getDataTreeChangeListener() {
96         return ElanInstanceListener.this;
97     }
98
99     @Override
100     protected InstanceIdentifier<ElanInstance> getWildCardPath() {
101         return InstanceIdentifier.create(ElanInstances.class).child(ElanInstance.class);
102     }
103
104 }