Updated L2Gw changes in "neutronvpn", "elanmanager" and "dhcpservice" modules
[vpnservice.git] / elanmanager / elanmanager-impl / src / main / java / org / opendaylight / vpnservice / elan / l2gw / listeners / L2GatewayConnectionListener.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.vpnservice.elan.l2gw.listeners;
9
10 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
11 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
12 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
13 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
14 import org.opendaylight.controller.md.sal.binding.api.ClusteredDataChangeListener;
15 import org.opendaylight.vpnservice.datastoreutils.AsyncClusteredDataChangeListenerBase;
16 import org.opendaylight.vpnservice.elan.internal.ElanInstanceManager;
17 import org.opendaylight.vpnservice.elan.l2gw.utils.L2GatewayConnectionUtils;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateway.connections.attributes.L2gatewayConnections;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateway.connections.attributes.l2gatewayconnections.L2gatewayConnection;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150712.Neutron;
21 import org.opendaylight.yangtools.concepts.ListenerRegistration;
22 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 public class L2GatewayConnectionListener extends AsyncClusteredDataChangeListenerBase<L2gatewayConnection,
27         L2GatewayConnectionListener> implements AutoCloseable {
28     private static final Logger LOG = LoggerFactory.getLogger(L2GatewayConnectionListener.class);
29
30     private ListenerRegistration<DataChangeListener> listenerRegistration;
31     private final DataBroker broker;
32     private ElanInstanceManager elanInstanceManager;
33
34     public L2GatewayConnectionListener(final DataBroker db, ElanInstanceManager elanInstanceManager) {
35         super(L2gatewayConnection.class, L2GatewayConnectionListener.class);
36         broker = db;
37         this.elanInstanceManager = elanInstanceManager;
38         registerListener(db);
39     }
40
41     @Override
42     public void close() throws Exception {
43         if (listenerRegistration != null) {
44             try {
45                 listenerRegistration.close();
46             } catch (final Exception e) {
47                 LOG.error("Error when cleaning up DataChangeListener.", e);
48             }
49             listenerRegistration = null;
50         }
51         LOG.info("L2 Gateway Connection listener Closed");
52     }
53
54     private void registerListener(final DataBroker db) {
55         try {
56             listenerRegistration = db.registerDataChangeListener(LogicalDatastoreType.CONFIGURATION,
57                     InstanceIdentifier.create(Neutron.class).child(L2gatewayConnections.class)
58                             .child(L2gatewayConnection.class),
59                     L2GatewayConnectionListener.this, DataChangeScope.SUBTREE);
60         } catch (final Exception e) {
61             LOG.error("Neutron Manager L2 Gateway Connection DataChange listener registration fail!", e);
62             throw new IllegalStateException(
63                     "Neutron Manager L2 Gateway Connection DataChange listener registration failed.", e);
64         }
65     }
66
67     @Override
68     protected void add(final InstanceIdentifier<L2gatewayConnection> identifier, final L2gatewayConnection input) {
69         if (LOG.isTraceEnabled()) {
70             LOG.trace("Adding L2gatewayConnection: {}", input);
71         }
72
73         // Get associated L2GwId from 'input'
74         // Create logical switch in each of the L2GwDevices part of L2Gw
75         // Logical switch name is network UUID
76         // Add L2GwDevices to ELAN
77         L2GatewayConnectionUtils.addL2GatewayConnection(input);
78     }
79
80     @Override
81     protected void remove(InstanceIdentifier<L2gatewayConnection> identifier, L2gatewayConnection input) {
82         if (LOG.isTraceEnabled()) {
83             LOG.trace("Removing L2gatewayConnection: {}", input);
84         }
85
86         L2GatewayConnectionUtils.deleteL2GatewayConnection(input);
87     }
88
89     @Override
90     protected void update(InstanceIdentifier<L2gatewayConnection> identifier, L2gatewayConnection original,
91             L2gatewayConnection update) {
92         if (LOG.isTraceEnabled()) {
93             LOG.trace("Updating L2gatewayConnection : original value={}, updated value={}", original, update);
94         }
95     }
96
97     @Override
98     protected InstanceIdentifier<L2gatewayConnection> getWildCardPath() {
99         return InstanceIdentifier.create(L2gatewayConnection.class);
100     }
101
102     @Override
103     protected ClusteredDataChangeListener getDataChangeListener() {
104         return L2GatewayConnectionListener.this;
105     }
106
107     @Override
108     protected DataChangeScope getDataChangeScope() {
109         return DataChangeScope.BASE;
110     }
111 }