--- /dev/null
+/*
+ * Copyright (c) 2015 - 2016 Ericsson India Global Services Pvt Ltd. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.vpnservice.neutronvpn;
+
+
+import org.opendaylight.controller.md.sal.binding.api.DataBroker;
+import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
+import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
+import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
+import org.opendaylight.vpnservice.mdsalutil.AbstractDataChangeListener;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.port.attributes.FixedIps;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.ports.attributes.Ports;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.ports.attributes.ports.Port;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150712.Neutron;
+import org.opendaylight.yangtools.concepts.ListenerRegistration;
+import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+
+public class NeutronPortChangeListener extends AbstractDataChangeListener<Port> implements AutoCloseable {
+ private static final Logger LOG = LoggerFactory.getLogger(NeutronPortChangeListener.class);
+
+ private ListenerRegistration<DataChangeListener> listenerRegistration;
+ private final DataBroker broker;
+ private NeutronvpnManager nvpnManager;
+
+
+ public NeutronPortChangeListener(final DataBroker db, NeutronvpnManager nVpnMgr) {
+ super(Port.class);
+ broker = db;
+ nvpnManager = nVpnMgr;
+ registerListener(db);
+ }
+
+ @Override
+ public void close() throws Exception {
+ if (listenerRegistration != null) {
+ try {
+ listenerRegistration.close();
+ } catch (final Exception e) {
+ LOG.error("Error when cleaning up DataChangeListener.", e);
+ }
+ listenerRegistration = null;
+ }
+ LOG.info("N_Port listener Closed");
+ }
+
+
+ private void registerListener(final DataBroker db) {
+ try {
+ listenerRegistration = db.registerDataChangeListener(LogicalDatastoreType.CONFIGURATION,
+ InstanceIdentifier.create(Neutron.class).child(Ports.class).child(Port.class),
+ NeutronPortChangeListener.this, DataChangeScope.SUBTREE);
+ } catch (final Exception e) {
+ LOG.error("Neutron Manager Port DataChange listener registration fail!", e);
+ throw new IllegalStateException("Neutron Manager Port DataChange listener registration failed.", e);
+ }
+ }
+
+ @Override
+ protected void add(InstanceIdentifier<Port> identifier, Port input) {
+ if (LOG.isTraceEnabled()) {
+ LOG.trace("Adding Port : key: " + identifier + ", value=" + input);
+ }
+ nvpnManager.handleNeutronPortCreated(input);
+
+ }
+
+ @Override
+ protected void remove(InstanceIdentifier<Port> identifier, Port input) {
+ if (LOG.isTraceEnabled()) {
+ LOG.trace("Removing Port : key: " + identifier + ", value=" + input);
+ }
+ nvpnManager.handleNeutronPortDeleted(input);
+
+ }
+
+ @Override
+ protected void update(InstanceIdentifier<Port> identifier, Port original, Port update) {
+ if (LOG.isTraceEnabled()) {
+ LOG.trace("Updating Port : key: " + identifier + ", original value=" + original + ", update value=" +
+ update);
+ }
+ List<FixedIps> oldIPs = (original.getFixedIps() != null) ? original.getFixedIps() : new ArrayList<FixedIps>();
+ List<FixedIps> newIPs = (update.getFixedIps() != null) ? update.getFixedIps() : new ArrayList<FixedIps>();
+
+ if (!oldIPs.equals(newIPs)) {
+ Iterator<FixedIps> iterator = newIPs.iterator();
+ while (iterator.hasNext()) {
+ FixedIps ip = iterator.next();
+ if (oldIPs.remove(ip)) {
+ iterator.remove();
+ }
+ }
+ nvpnManager.handleNeutronPortUpdated(original, update);
+ }
+ }
+}