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.vpnservice.elan.l2gw.listeners;
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;
26 public class L2GatewayConnectionListener extends AsyncClusteredDataChangeListenerBase<L2gatewayConnection,
27 L2GatewayConnectionListener> implements AutoCloseable {
28 private static final Logger LOG = LoggerFactory.getLogger(L2GatewayConnectionListener.class);
30 private ListenerRegistration<DataChangeListener> listenerRegistration;
31 private final DataBroker broker;
32 private ElanInstanceManager elanInstanceManager;
34 public L2GatewayConnectionListener(final DataBroker db, ElanInstanceManager elanInstanceManager) {
35 super(L2gatewayConnection.class, L2GatewayConnectionListener.class);
37 this.elanInstanceManager = elanInstanceManager;
42 public void close() throws Exception {
43 if (listenerRegistration != null) {
45 listenerRegistration.close();
46 } catch (final Exception e) {
47 LOG.error("Error when cleaning up DataChangeListener.", e);
49 listenerRegistration = null;
51 LOG.info("L2 Gateway Connection listener Closed");
54 private void registerListener(final DataBroker db) {
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);
68 protected void add(final InstanceIdentifier<L2gatewayConnection> identifier, final L2gatewayConnection input) {
69 if (LOG.isTraceEnabled()) {
70 LOG.trace("Adding L2gatewayConnection: {}", input);
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);
81 protected void remove(InstanceIdentifier<L2gatewayConnection> identifier, L2gatewayConnection input) {
82 if (LOG.isTraceEnabled()) {
83 LOG.trace("Removing L2gatewayConnection: {}", input);
86 L2GatewayConnectionUtils.deleteL2GatewayConnection(input);
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);
98 protected InstanceIdentifier<L2gatewayConnection> getWildCardPath() {
99 return InstanceIdentifier.create(L2gatewayConnection.class);
103 protected ClusteredDataChangeListener getDataChangeListener() {
104 return L2GatewayConnectionListener.this;
108 protected DataChangeScope getDataChangeScope() {
109 return DataChangeScope.BASE;