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