2 * Copyright (c) 2015 - 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.neutronvpn;
11 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
12 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
13 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
14 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
15 import org.opendaylight.vpnservice.mdsalutil.AbstractDataChangeListener;
16 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150712.Neutron;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.subnets.rev150712.subnets.attributes.Subnets;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.subnets.rev150712.subnets.attributes.subnets.Subnet;
20 import org.opendaylight.yangtools.concepts.ListenerRegistration;
21 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
26 public class NeutronSubnetChangeListener extends AbstractDataChangeListener<Subnet> implements AutoCloseable {
27 private static final Logger LOG = LoggerFactory.getLogger(NeutronSubnetChangeListener.class);
29 private ListenerRegistration<DataChangeListener> listenerRegistration;
30 private final DataBroker broker;
31 private NeutronvpnManager nvpnManager;
34 public NeutronSubnetChangeListener(final DataBroker db, NeutronvpnManager nVpnMgr) {
37 nvpnManager = nVpnMgr;
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("N_Subnet listener Closed");
55 private void registerListener(final DataBroker db) {
57 listenerRegistration = db.registerDataChangeListener(LogicalDatastoreType.CONFIGURATION,
58 InstanceIdentifier.create(Neutron.class).child(Subnets.class).child(Subnet.class),
59 NeutronSubnetChangeListener.this, DataChangeScope.SUBTREE);
60 } catch (final Exception e) {
61 LOG.error("Neutron Manager Subnet DataChange listener registration fail!", e);
62 throw new IllegalStateException("Neutron Manager Subnet DataChange listener registration failed.", e);
67 protected void add(InstanceIdentifier<Subnet> identifier, Subnet input) {
68 LOG.trace("Adding Subnet : key: " + identifier + ", value=" + input);
69 nvpnManager.updateSubnetNode(input.getUuid(), input.getTenantId(), input.getNetworkId(), null, null, null);
73 protected void remove(InstanceIdentifier<Subnet> identifier, Subnet input) {
74 LOG.trace("Removing subnet : key: " + identifier + ", value=" + input);
75 Uuid vpnId = NeutronvpnUtils.getVpnForNetwork(broker, input.getNetworkId());
77 nvpnManager.removeSubnetFromVpn(vpnId, input.getUuid());
82 protected void update(InstanceIdentifier<Subnet> identifier, Subnet original, Subnet update) {
83 if (LOG.isTraceEnabled()) {
84 LOG.trace("Updating Subnet : key: " + identifier + ", original value=" + original + ", update value=" +
87 nvpnManager.updateSubnetNode(update.getUuid(), update.getTenantId(), update.getNetworkId(), null, null, null);