55fc3a67f008fa688182fdce2742c8ac9d6cdc5d
[ovsdb.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.impl.NeutronL3Adapter;
16
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
19
20 import java.net.HttpURLConnection;
21
22 /**
23  * Handle requests for Neutron Floating IP.
24  */
25 public class FloatingIPHandler extends AbstractHandler
26         implements INeutronFloatingIPAware {
27
28     /**
29      * Logger instance.
30      */
31     static final Logger logger = 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 EventDispatcher
123      */
124     @Override
125     public void processEvent(AbstractEvent abstractEvent) {
126         if (!(abstractEvent instanceof NorthboundEvent)) {
127             logger.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                 logger.warn("Unable to process event action " + ev.getAction());
141                 break;
142         }
143     }
144 }