09802e70a407a7fe30cb159c33dae85e686804bc
[netvirt.git] /
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.clustering.EntityOwnershipService;
17 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
18 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
19 import org.opendaylight.genius.datastoreutils.AsyncClusteredDataTreeChangeListenerBase;
20 import org.opendaylight.netvirt.elan.l2gw.utils.L2GatewayConnectionUtils;
21 import org.opendaylight.netvirt.elan.utils.ElanClusterUtils;
22 import org.opendaylight.netvirt.elan.utils.ElanUtils;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.ElanInstances;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstance;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateway.connections.attributes.L2gatewayConnections;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateway.connections.attributes.l2gatewayconnections.L2gatewayConnection;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150712.Neutron;
28 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 public class ElanInstanceListener extends AsyncClusteredDataTreeChangeListenerBase<ElanInstance,
33         ElanInstanceListener> implements AutoCloseable {
34
35     private static final Logger LOG = LoggerFactory.getLogger(ElanInstanceListener.class);
36
37     private final DataBroker broker;
38     private final L2GatewayConnectionUtils l2GatewayConnectionUtils;
39     private final EntityOwnershipService entityOwnershipService;
40     private static final Map<String, List<Runnable>> WAITING_JOB_LIST = new ConcurrentHashMap<>();
41
42     public ElanInstanceListener(final DataBroker db, EntityOwnershipService entityOwnershipService,
43                                 final ElanUtils elanUtils) {
44         super(ElanInstance.class, ElanInstanceListener.class);
45         broker = db;
46         this.entityOwnershipService = entityOwnershipService;
47         this.l2GatewayConnectionUtils = elanUtils.getL2GatewayConnectionUtils();
48         registerListener(LogicalDatastoreType.CONFIGURATION, db);
49     }
50
51     public void init() {
52     }
53
54     public void close() {
55     }
56
57     @Override
58     protected void remove(final InstanceIdentifier<ElanInstance> identifier,
59                           final ElanInstance del) {
60         ElanClusterUtils.runOnlyInLeaderNode(entityOwnershipService, del.getElanInstanceName(), () -> {
61             LOG.info("Elan instance {} deleted from Configuration tree ", del);
62             List<L2gatewayConnection> connections =
63                     L2GatewayConnectionUtils.getL2GwConnectionsByElanName(
64                             this.broker, del.getElanInstanceName());
65             if (connections == null || connections.isEmpty()) {
66                 return null;
67             }
68             try {
69                 ReadWriteTransaction tx = this.broker.newReadWriteTransaction();
70                 for (L2gatewayConnection connection : connections) {
71                     InstanceIdentifier<L2gatewayConnection> iid = InstanceIdentifier.create(Neutron.class)
72                             .child(L2gatewayConnections.class).child(L2gatewayConnection.class, connection.getKey());
73                     tx.delete(LogicalDatastoreType.CONFIGURATION, iid);
74                 }
75                 tx.submit().checkedGet();
76             } catch (TransactionCommitFailedException e) {
77                 LOG.error("Failed to delete associated l2gwconnection while deleting network", e);
78             }
79             return null;
80         });
81     }
82
83     @Override
84     protected void update(InstanceIdentifier<ElanInstance> identifier, ElanInstance original, ElanInstance update) {
85
86     }
87
88     @Override
89     protected void add(InstanceIdentifier<ElanInstance> identifier, ElanInstance add) {
90         List<Runnable> runnables = WAITING_JOB_LIST.get(add.getElanInstanceName());
91         if (runnables != null) {
92             runnables.forEach(Runnable::run);
93         }
94     }
95
96     public static void runJobAfterElanIsAvailable(String elanName, Runnable runnable) {
97         WAITING_JOB_LIST.computeIfAbsent(elanName, (name) -> new ArrayList<>());
98         WAITING_JOB_LIST.get(elanName).add(runnable);
99     }
100
101     @Override
102     protected ElanInstanceListener getDataTreeChangeListener() {
103         return ElanInstanceListener.this;
104     }
105
106     @Override
107     protected InstanceIdentifier<ElanInstance> getWildCardPath() {
108         return InstanceIdentifier.create(ElanInstances.class).child(ElanInstance.class);
109     }
110
111 }