Fix NPE triggered after disabling SG on a port
[netvirt.git] / openstack / net-virt / src / main / java / org / opendaylight / netvirt / openstack / netvirt / translator / iaware / impl / NeutronFloatingIPDataTreeChangeListener.java
1 /*
2  * Copyright © 2015, 2016 Brocade Communications Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.netvirt.openstack.netvirt.translator.iaware.impl;
9
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;
28
29 public class NeutronFloatingIPDataTreeChangeListener
30         implements ClusteredDataTreeChangeListener<Floatingip>, AutoCloseable {
31     private static final Logger LOG = LoggerFactory.getLogger(NeutronFloatingIPDataTreeChangeListener.class);
32
33     private final DataBroker dataBroker;
34     private ListenerRegistration<DataTreeChangeListener<Floatingip>> registration;
35
36     public NeutronFloatingIPDataTreeChangeListener(DataBroker dataBroker) {
37         this.dataBroker = dataBroker;
38     }
39
40     @PostConstruct
41     public void init() {
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);
49     }
50
51     @Override
52     public void onDataTreeChanged(@Nonnull Collection<DataTreeModification<Floatingip>> changes) {
53         LOG.trace("Data changes : {}", changes);
54
55         Object[] subscribers = NeutronIAwareUtil.getInstances(INeutronFloatingIPAware.class, this);
56         createFloatingIP(changes, subscribers);
57         updateFloatingIP(changes, subscribers);
58         deleteFloatingIP(changes, subscribers);
59     }
60
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);
70                 }
71             }
72         }
73     }
74
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);
84                 }
85             }
86         }
87     }
88
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);
98                 }
99             }
100         }
101     }
102
103     /*
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.
106      */
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());
112         }
113         if (fip.getPortId() != null) {
114             result.setPortUUID(fip.getPortId().getValue());
115         }
116         if (fip.getFixedIpAddress() != null) {
117             result.setFixedIPAddress(String.valueOf(fip.getFixedIpAddress().getValue()));
118         }
119         if (fip.getFloatingIpAddress() != null) {
120             result.setFloatingIPAddress(String.valueOf(fip.getFloatingIpAddress().getValue()));
121         }
122         if (fip.getTenantId() != null) {
123             result.setTenantUUID(fip.getTenantId().getValue());
124         }
125         if (fip.getRouterId() != null) {
126             result.setRouterUUID(fip.getRouterId().getValue());
127         }
128         result.setStatus(fip.getStatus());
129         return result;
130     }
131
132     @Override
133     public void close() {
134         registration.close();
135     }
136 }