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.opendaylight.neutron.ports.rev150712.port.attributes.FixedIps;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.ports.attributes.Ports;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.ports.attributes.ports.Port;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150712.Neutron;
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;
25 import java.util.ArrayList;
26 import java.util.Iterator;
27 import java.util.List;
30 public class NeutronPortChangeListener extends AbstractDataChangeListener<Port> implements AutoCloseable {
31 private static final Logger LOG = LoggerFactory.getLogger(NeutronPortChangeListener.class);
33 private ListenerRegistration<DataChangeListener> listenerRegistration;
34 private final DataBroker broker;
35 private NeutronvpnManager nvpnManager;
38 public NeutronPortChangeListener(final DataBroker db, NeutronvpnManager nVpnMgr) {
41 nvpnManager = nVpnMgr;
46 public void close() throws Exception {
47 if (listenerRegistration != null) {
49 listenerRegistration.close();
50 } catch (final Exception e) {
51 LOG.error("Error when cleaning up DataChangeListener.", e);
53 listenerRegistration = null;
55 LOG.info("N_Port listener Closed");
59 private void registerListener(final DataBroker db) {
61 listenerRegistration = db.registerDataChangeListener(LogicalDatastoreType.CONFIGURATION,
62 InstanceIdentifier.create(Neutron.class).child(Ports.class).child(Port.class),
63 NeutronPortChangeListener.this, DataChangeScope.SUBTREE);
64 } catch (final Exception e) {
65 LOG.error("Neutron Manager Port DataChange listener registration fail!", e);
66 throw new IllegalStateException("Neutron Manager Port DataChange listener registration failed.", e);
71 protected void add(InstanceIdentifier<Port> identifier, Port input) {
72 if (LOG.isTraceEnabled()) {
73 LOG.trace("Adding Port : key: " + identifier + ", value=" + input);
75 nvpnManager.handleNeutronPortCreated(input);
80 protected void remove(InstanceIdentifier<Port> identifier, Port input) {
81 if (LOG.isTraceEnabled()) {
82 LOG.trace("Removing Port : key: " + identifier + ", value=" + input);
84 nvpnManager.handleNeutronPortDeleted(input);
89 protected void update(InstanceIdentifier<Port> identifier, Port original, Port update) {
90 if (LOG.isTraceEnabled()) {
91 LOG.trace("Updating Port : key: " + identifier + ", original value=" + original + ", update value=" +
94 List<FixedIps> oldIPs = (original.getFixedIps() != null) ? original.getFixedIps() : new ArrayList<FixedIps>();
95 List<FixedIps> newIPs = (update.getFixedIps() != null) ? update.getFixedIps() : new ArrayList<FixedIps>();
97 if (!oldIPs.equals(newIPs)) {
98 Iterator<FixedIps> iterator = newIPs.iterator();
99 while (iterator.hasNext()) {
100 FixedIps ip = iterator.next();
101 if (oldIPs.remove(ip)) {
105 nvpnManager.handleNeutronPortUpdated(original, update);