Merge "Use ${project.version} for internal dependencies"
[ovsdb.git] / openstack / net-virt / src / main / java / org / opendaylight / ovsdb / openstack / netvirt / translator / iaware / impl / NeutronFloatingIPChangeListener.java
1 /*
2  * Copyright (c) 2015 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.ovsdb.openstack.netvirt.translator.iaware.impl;
9
10 import java.util.Map.Entry;
11
12 import org.opendaylight.controller.md.sal.binding.api.ClusteredDataChangeListener;
13 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
14 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
15 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
16 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
17 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
18 import org.opendaylight.ovsdb.openstack.netvirt.translator.NeutronFloatingIP;
19 import org.opendaylight.ovsdb.openstack.netvirt.translator.iaware.INeutronFloatingIPAware;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.rev150712.floatingips.attributes.Floatingips;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.rev150712.floatingips.attributes.floatingips.Floatingip;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150712.Neutron;
23 import org.opendaylight.yangtools.concepts.ListenerRegistration;
24 import org.opendaylight.yangtools.yang.binding.DataObject;
25 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 public class NeutronFloatingIPChangeListener implements ClusteredDataChangeListener, AutoCloseable{
30     private static final Logger LOG = LoggerFactory.getLogger(NeutronFloatingIPChangeListener.class);
31
32     private ListenerRegistration<DataChangeListener> registration;
33     private DataBroker db;
34
35     public NeutronFloatingIPChangeListener(DataBroker db){
36         this.db = db;
37         InstanceIdentifier<Floatingip> path = InstanceIdentifier
38                 .create(Neutron.class)
39                 .child(Floatingips.class)
40                 .child(Floatingip.class);
41         LOG.debug("Register listener for Neutron FloatingIp model data changes");
42         registration =
43                 this.db.registerDataChangeListener(LogicalDatastoreType.CONFIGURATION, path, this, DataChangeScope.ONE);
44
45     }
46
47     @Override
48     public void onDataChanged(
49             AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> changes) {
50         LOG.trace("Data changes : {}",changes);
51
52         Object[] subscribers = NeutronIAwareUtil.getInstances(INeutronFloatingIPAware.class, this);
53         createFloatingIP(changes, subscribers);
54         updateFloatingIP(changes, subscribers);
55         deleteFloatingIP(changes, subscribers);
56     }
57
58     private void createFloatingIP(
59             AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> changes,
60             Object[] subscribers) {
61         for (Entry<InstanceIdentifier<?>, DataObject> newFloatingIP : changes.getCreatedData().entrySet()) {
62                 if(newFloatingIP.getValue() instanceof Floatingip){
63                 NeutronFloatingIP floatingip= fromMd((Floatingip)newFloatingIP.getValue());
64                 for(Object entry: subscribers){
65                     INeutronFloatingIPAware subscriber = (INeutronFloatingIPAware)entry;
66                     subscriber.neutronFloatingIPCreated(floatingip);
67                 }
68                 }
69         }
70     }
71
72     private void updateFloatingIP(
73             AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> changes,
74             Object[] subscribers) {
75         for (Entry<InstanceIdentifier<?>, DataObject> updateFloatingIP : changes.getUpdatedData().entrySet()) {
76                 if(updateFloatingIP.getValue() instanceof Floatingip){
77                 NeutronFloatingIP floatingip = fromMd((Floatingip)updateFloatingIP.getValue());
78                 for(Object entry: subscribers){
79                     INeutronFloatingIPAware subscriber = (INeutronFloatingIPAware)entry;
80                     subscriber.neutronFloatingIPUpdated(floatingip);
81                 }
82                 }
83         }
84     }
85
86     private void deleteFloatingIP(
87             AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> changes,
88             Object[] subscribers) {
89         for (InstanceIdentifier<?> deletedFloatingIPPath : changes.getRemovedPaths()) {
90                 if(deletedFloatingIPPath.getTargetType().equals(Floatingip.class)){
91                 NeutronFloatingIP floatingip = fromMd((Floatingip)changes.getOriginalData().get(deletedFloatingIPPath));
92                 for(Object entry: subscribers){
93                     INeutronFloatingIPAware subscriber = (INeutronFloatingIPAware)entry;
94                     subscriber.neutronFloatingIPDeleted(floatingip);
95                 }
96                 }
97         }
98     }
99
100     /*
101      * This method is borrowed from NeutronFloatingIPInterface.java class of Neutron Northbound class.
102      * We will be utilizing similar code from other classes from the same package of neutron project.
103      */
104     private NeutronFloatingIP fromMd(Floatingip fip) {
105         NeutronFloatingIP result = new NeutronFloatingIP();
106         result.setID(String.valueOf(fip.getUuid().getValue()));
107         if (fip.getFloatingNetworkId() != null) {
108             result.setFloatingNetworkUUID(String.valueOf(fip.getFloatingNetworkId().getValue()));
109         }
110         if (fip.getPortId() != null) {
111             result.setPortUUID(String.valueOf(fip.getPortId().getValue()));
112         }
113         if (fip.getFixedIpAddress() != null ) {
114             result.setFixedIPAddress(String.valueOf(fip.getFixedIpAddress().getValue()));
115         }
116         if (fip.getFloatingIpAddress() != null) {
117             result.setFloatingIPAddress(String.valueOf(fip.getFloatingIpAddress().getValue()));
118         }
119         if (fip.getTenantId() != null) {
120             result.setTenantUUID(String.valueOf(fip.getTenantId().getValue()));
121         }
122         if (fip.getRouterId() != null) {
123             result.setRouterUUID(String.valueOf(fip.getRouterId().getValue()));
124         }
125         result.setStatus(fip.getStatus());
126         return result;
127     }
128
129     @Override
130     public void close() throws Exception {
131         registration.close();
132     }
133 }