224e36358823eba8ca5659d8e0d08ad0b4fe3086
[netvirt.git] / vpnservice / elanmanager / 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 java.util.ArrayList;
11 import java.util.List;
12 import java.util.Map;
13 import java.util.concurrent.ConcurrentHashMap;
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;
29
30 public class ElanInstanceListener extends AsyncClusteredDataTreeChangeListenerBase<ElanInstance,
31         ElanInstanceListener> {
32
33     private static final Logger LOG = LoggerFactory.getLogger(ElanInstanceListener.class);
34
35     private final DataBroker broker;
36     private final ElanClusterUtils elanClusterUtils;
37     private static final Map<String, List<Runnable>> WAITING_JOB_LIST = new ConcurrentHashMap<>();
38
39     public ElanInstanceListener(final DataBroker db, final ElanClusterUtils elanClusterUtils) {
40         super(ElanInstance.class, ElanInstanceListener.class);
41         broker = db;
42         this.elanClusterUtils = elanClusterUtils;
43         registerListener(LogicalDatastoreType.CONFIGURATION, db);
44     }
45
46     public void init() {
47     }
48
49     @Override
50     public void close() {
51     }
52
53     @Override
54     protected void remove(final InstanceIdentifier<ElanInstance> identifier,
55                           final ElanInstance del) {
56         elanClusterUtils.runOnlyInOwnerNode(del.getElanInstanceName(), "delete Elan instance",
57             () -> {
58                 LOG.info("Elan instance {} deleted from Configuration tree ", del);
59                 List<L2gatewayConnection> connections =
60                         L2GatewayConnectionUtils.getL2GwConnectionsByElanName(
61                                 this.broker, del.getElanInstanceName());
62                 if (connections == null || connections.isEmpty()) {
63                     return null;
64                 }
65                 try {
66                     ReadWriteTransaction tx = this.broker.newReadWriteTransaction();
67                     for (L2gatewayConnection connection : connections) {
68                         InstanceIdentifier<L2gatewayConnection> iid = InstanceIdentifier.create(Neutron.class)
69                             .child(L2gatewayConnections.class).child(L2gatewayConnection.class, connection.getKey());
70                         tx.delete(LogicalDatastoreType.CONFIGURATION, iid);
71                     }
72                     tx.submit().checkedGet();
73                 } catch (TransactionCommitFailedException e) {
74                     LOG.error("Failed to delete associated l2gwconnection while deleting network", e);
75                 }
76                 return null;
77             });
78     }
79
80     @Override
81     protected void update(InstanceIdentifier<ElanInstance> identifier, ElanInstance original, ElanInstance update) {
82
83     }
84
85     @Override
86     protected void add(InstanceIdentifier<ElanInstance> identifier, ElanInstance add) {
87         List<Runnable> runnables = WAITING_JOB_LIST.get(add.getElanInstanceName());
88         if (runnables != null) {
89             runnables.forEach(Runnable::run);
90         }
91     }
92
93     public static void runJobAfterElanIsAvailable(String elanName, Runnable runnable) {
94         WAITING_JOB_LIST.computeIfAbsent(elanName, (name) -> new ArrayList<>());
95         WAITING_JOB_LIST.get(elanName).add(runnable);
96     }
97
98     @Override
99     protected ElanInstanceListener getDataTreeChangeListener() {
100         return ElanInstanceListener.this;
101     }
102
103     @Override
104     protected InstanceIdentifier<ElanInstance> getWildCardPath() {
105         return InstanceIdentifier.create(ElanInstances.class).child(ElanInstance.class);
106     }
107
108 }