Merge "Add UT for SouthboundMapper and SouthboundProvider"
[netvirt.git] / openstack / net-virt / src / main / java / org / opendaylight / ovsdb / openstack / netvirt / FloatingIPHandler.java
1 /*
2  * Copyright (c) 2014, 2015 Red Hat, 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
9 package org.opendaylight.ovsdb.openstack.netvirt;
10
11 import java.net.HttpURLConnection;
12
13 import org.opendaylight.neutron.spi.INeutronFloatingIPAware;
14 import org.opendaylight.neutron.spi.NeutronFloatingIP;
15 import org.opendaylight.ovsdb.openstack.netvirt.api.Action;
16 import org.opendaylight.ovsdb.openstack.netvirt.api.EventDispatcher;
17 import org.opendaylight.ovsdb.openstack.netvirt.impl.NeutronL3Adapter;
18 import org.opendaylight.ovsdb.utils.servicehelper.ServiceHelper;
19 import org.osgi.framework.ServiceReference;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 /**
24  * Handle requests for Neutron Floating IP.
25  */
26 public class FloatingIPHandler extends AbstractHandler
27         implements INeutronFloatingIPAware, ConfigInterface {
28
29     private static final Logger LOG = LoggerFactory.getLogger(FloatingIPHandler.class);
30
31     // The implementation for each of these services is resolved by the OSGi Service Manager
32     private volatile NeutronL3Adapter neutronL3Adapter;
33
34     /**
35      * Services provide this interface method to indicate if the specified floatingIP can be created
36      *
37      * @param floatingIP
38      *            instance of proposed new Neutron FloatingIP object
39      * @return integer
40      *            the return value is understood to be a HTTP status code.  A return value outside of 200 through 299
41      *            results in the create operation being interrupted and the returned status value reflected in the
42      *            HTTP response.
43      */
44     @Override
45     public int canCreateFloatingIP(NeutronFloatingIP floatingIP) {
46         return HttpURLConnection.HTTP_OK;
47     }
48
49
50     /**
51      * Services provide this interface method for taking action after a floatingIP has been created
52      *
53      * @param floatingIP
54      *            instance of new Neutron FloatingIP object
55      */
56     @Override
57     public void neutronFloatingIPCreated(NeutronFloatingIP floatingIP) {
58         enqueueEvent(new NorthboundEvent(floatingIP, Action.ADD));
59     }
60
61     /**
62      * Services provide this interface method to indicate if the specified floatingIP can be changed using the specified
63      * delta
64      *
65      * @param delta
66      *            updates to the floatingIP object using patch semantics
67      * @param original
68      *            instance of the Neutron FloatingIP object to be updated
69      * @return integer
70      *            the return value is understood to be a HTTP status code.  A return value outside of 200 through 299
71      *            results in the update operation being interrupted and the returned status value reflected in the
72      *            HTTP response.
73      */
74     @Override
75     public int canUpdateFloatingIP(NeutronFloatingIP delta, NeutronFloatingIP original) {
76         return HttpURLConnection.HTTP_OK;
77     }
78
79     /**
80      * Services provide this interface method for taking action after a floatingIP has been updated
81      *
82      * @param floatingIP
83      *            instance of modified Neutron FloatingIP object
84      */
85     @Override
86     public void neutronFloatingIPUpdated(NeutronFloatingIP floatingIP) {
87         enqueueEvent(new NorthboundEvent(floatingIP, Action.UPDATE));
88     }
89
90     /**
91      * Services provide this interface method to indicate if the specified floatingIP can be deleted
92      *
93      * @param floatingIP
94      *            instance of the Neutron FloatingIP object to be deleted
95      * @return integer
96      *            the return value is understood to be a HTTP status code.  A return value outside of 200 through 299
97      *            results in the delete operation being interrupted and the returned status value reflected in the
98      *            HTTP response.
99      */
100     @Override
101     public int canDeleteFloatingIP(NeutronFloatingIP floatingIP) {
102         return HttpURLConnection.HTTP_OK;
103     }
104
105     /**
106      * Services provide this interface method for taking action after a floatingIP has been deleted
107      *
108      * @param floatingIP
109      *            instance of deleted Neutron FloatingIP object
110      */
111     @Override
112     public void neutronFloatingIPDeleted(NeutronFloatingIP floatingIP) {
113         enqueueEvent(new NorthboundEvent(floatingIP, Action.DELETE));
114     }
115
116     /**
117      * Process the event.
118      *
119      * @param abstractEvent the {@link org.opendaylight.ovsdb.openstack.netvirt.AbstractEvent} event to be handled.
120      * @see org.opendaylight.ovsdb.openstack.netvirt.api.EventDispatcher
121      */
122     @Override
123     public void processEvent(AbstractEvent abstractEvent) {
124         if (!(abstractEvent instanceof NorthboundEvent)) {
125             LOG.error("Unable to process abstract event {}", abstractEvent);
126             return;
127         }
128         NorthboundEvent ev = (NorthboundEvent) abstractEvent;
129         switch (ev.getAction()) {
130             case ADD:
131                 // fall through
132             case DELETE:
133                 // fall through
134             case UPDATE:
135                 neutronL3Adapter.handleNeutronFloatingIPEvent(ev.getNeutronFloatingIP(), ev.getAction());
136                 break;
137             default:
138                 LOG.warn("Unable to process event action {}", ev.getAction());
139                 break;
140         }
141     }
142
143     @Override
144     public void setDependencies(ServiceReference serviceReference) {
145         eventDispatcher =
146                 (EventDispatcher) ServiceHelper.getGlobalInstance(EventDispatcher.class, this);
147         neutronL3Adapter =
148                 (NeutronL3Adapter) ServiceHelper.getGlobalInstance(NeutronL3Adapter.class, this);
149         eventDispatcher.eventHandlerAdded(serviceReference, this);
150     }
151
152     @Override
153     public void setDependencies(Object impl) {}
154 }