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