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