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