2 * Copyright © 2015, 2016 Brocade Communications Systems, Inc. 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.netvirt.openstack.netvirt.translator.iaware.impl;
10 import java.util.Collection;
11 import javax.annotation.Nonnull;
12 import javax.annotation.PostConstruct;
13 import org.opendaylight.controller.md.sal.binding.api.ClusteredDataTreeChangeListener;
14 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
15 import org.opendaylight.controller.md.sal.binding.api.DataTreeChangeListener;
16 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
17 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
18 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
19 import org.opendaylight.netvirt.openstack.netvirt.translator.NeutronFloatingIP;
20 import org.opendaylight.netvirt.openstack.netvirt.translator.iaware.INeutronFloatingIPAware;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.rev150712.floatingips.attributes.Floatingips;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.rev150712.floatingips.attributes.floatingips.Floatingip;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150712.Neutron;
24 import org.opendaylight.yangtools.concepts.ListenerRegistration;
25 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
29 public class NeutronFloatingIPDataTreeChangeListener
30 implements ClusteredDataTreeChangeListener<Floatingip>, AutoCloseable {
31 private static final Logger LOG = LoggerFactory.getLogger(NeutronFloatingIPDataTreeChangeListener.class);
33 private final DataBroker dataBroker;
34 private ListenerRegistration<DataTreeChangeListener<Floatingip>> registration;
36 public NeutronFloatingIPDataTreeChangeListener(DataBroker dataBroker) {
37 this.dataBroker = dataBroker;
42 InstanceIdentifier<Floatingip> path = InstanceIdentifier
43 .create(Neutron.class)
44 .child(Floatingips.class)
45 .child(Floatingip.class);
46 LOG.debug("Register listener for Neutron FloatingIp model data changes");
47 registration = dataBroker.registerDataTreeChangeListener(
48 new DataTreeIdentifier<>(LogicalDatastoreType.CONFIGURATION, path), this);
52 public void onDataTreeChanged(@Nonnull Collection<DataTreeModification<Floatingip>> changes) {
53 LOG.trace("Data changes : {}", changes);
55 Object[] subscribers = NeutronIAwareUtil.getInstances(INeutronFloatingIPAware.class, this);
56 createFloatingIP(changes, subscribers);
57 updateFloatingIP(changes, subscribers);
58 deleteFloatingIP(changes, subscribers);
61 private void createFloatingIP(
62 @Nonnull Collection<DataTreeModification<Floatingip>> changes,
63 Object[] subscribers) {
64 for (DataTreeModification<Floatingip> change : changes) {
65 if (change.getRootNode().getDataAfter() != null && change.getRootNode().getDataBefore() == null) {
66 NeutronFloatingIP floatingIp = fromMd(change.getRootNode().getDataAfter());
67 for (Object entry : subscribers) {
68 INeutronFloatingIPAware subscriber = (INeutronFloatingIPAware) entry;
69 subscriber.neutronFloatingIPCreated(floatingIp);
75 private void updateFloatingIP(
76 @Nonnull Collection<DataTreeModification<Floatingip>> changes,
77 Object[] subscribers) {
78 for (DataTreeModification<Floatingip> change : changes) {
79 if (change.getRootNode().getDataAfter() != null && change.getRootNode().getDataBefore() != null) {
80 NeutronFloatingIP floatingIp = fromMd(change.getRootNode().getDataAfter());
81 for (Object entry : subscribers) {
82 INeutronFloatingIPAware subscriber = (INeutronFloatingIPAware) entry;
83 subscriber.neutronFloatingIPUpdated(floatingIp);
89 private void deleteFloatingIP(
90 @Nonnull Collection<DataTreeModification<Floatingip>> changes,
91 Object[] subscribers) {
92 for (DataTreeModification<Floatingip> change : changes) {
93 if (change.getRootNode().getDataAfter() == null && change.getRootNode().getDataBefore() != null) {
94 NeutronFloatingIP floatingIp = fromMd(change.getRootNode().getDataBefore());
95 for (Object entry : subscribers) {
96 INeutronFloatingIPAware subscriber = (INeutronFloatingIPAware) entry;
97 subscriber.neutronFloatingIPDeleted(floatingIp);
104 * This method is borrowed from NeutronFloatingIPInterface.java class of Neutron Northbound class.
105 * We will be utilizing similar code from other classes from the same package of neutron project.
107 private NeutronFloatingIP fromMd(Floatingip fip) {
108 NeutronFloatingIP result = new NeutronFloatingIP();
109 result.setID(fip.getUuid().getValue());
110 if (fip.getFloatingNetworkId() != null) {
111 result.setFloatingNetworkUUID(fip.getFloatingNetworkId().getValue());
113 if (fip.getPortId() != null) {
114 result.setPortUUID(fip.getPortId().getValue());
116 if (fip.getFixedIpAddress() != null) {
117 result.setFixedIPAddress(String.valueOf(fip.getFixedIpAddress().getValue()));
119 if (fip.getFloatingIpAddress() != null) {
120 result.setFloatingIPAddress(String.valueOf(fip.getFloatingIpAddress().getValue()));
122 if (fip.getTenantId() != null) {
123 result.setTenantUUID(fip.getTenantId().getValue());
125 if (fip.getRouterId() != null) {
126 result.setRouterUUID(fip.getRouterId().getValue());
128 result.setStatus(fip.getStatus());
133 public void close() {
134 registration.close();