Bug 1859 - Remove the fake limitation that LBaaS only works with
[netvirt.git] / openstack / net-virt / src / main / java / org / opendaylight / ovsdb / openstack / netvirt / RouterHandler.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.controller.networkconfig.neutron.INeutronRouterAware;
13 import org.opendaylight.controller.networkconfig.neutron.NeutronRouter;
14 import org.opendaylight.controller.networkconfig.neutron.NeutronRouter_Interface;
15 import org.opendaylight.ovsdb.openstack.netvirt.api.Action;
16 import org.opendaylight.ovsdb.openstack.netvirt.impl.NeutronL3Adapter;
17
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20
21 import java.net.HttpURLConnection;
22
23 /**
24  * Handle requests for Neutron Router.
25  */
26 public class RouterHandler extends AbstractHandler
27         implements INeutronRouterAware {
28
29     /**
30      * Logger instance.
31      */
32     static final Logger logger = LoggerFactory.getLogger(RouterHandler.class);
33
34     // The implementation for each of these services is resolved by the OSGi Service Manager
35     private volatile NeutronL3Adapter neutronL3Adapter;
36
37     /**
38      * Services provide this interface method to indicate if the specified router can be created
39      *
40      * @param router
41      *            instance of proposed new Neutron Router object
42      * @return integer
43      *            the return value is understood to be a HTTP status code.  A return value outside of 200 through 299
44      *            results in the create operation being interrupted and the returned status value reflected in the
45      *            HTTP response.
46      */
47     @Override
48     public int canCreateRouter(NeutronRouter router) {
49         return HttpURLConnection.HTTP_OK;
50     }
51
52     /**
53      * Services provide this interface method for taking action after a router has been created
54      *
55      * @param router
56      *            instance of new Neutron Router object
57      */
58     @Override
59     public void neutronRouterCreated(NeutronRouter router) {
60         enqueueEvent(new NorthboundEvent(router, Action.ADD));
61     }
62
63     /**
64      * Services provide this interface method to indicate if the specified router can be changed using the specified
65      * delta
66      *
67      * @param delta
68      *            updates to the router object using patch semantics
69      * @param original
70      *            instance of the Neutron Router 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 canUpdateRouter(NeutronRouter delta, NeutronRouter original) {
78         return HttpURLConnection.HTTP_OK;
79     }
80
81     /**
82      * Services provide this interface method for taking action after a router has been updated
83      *
84      * @param router
85      *            instance of modified Neutron Router object
86      */
87     @Override
88     public void neutronRouterUpdated(NeutronRouter router) {
89         enqueueEvent(new NorthboundEvent(router, Action.UPDATE));
90     }
91
92     /**
93      * Services provide this interface method to indicate if the specified router can be deleted
94      *
95      * @param router
96      *            instance of the Neutron Router 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 canDeleteRouter(NeutronRouter router) {
104         return HttpURLConnection.HTTP_OK;
105     }
106
107     /**
108      * Services provide this interface method for taking action after a router has been deleted
109      *
110      * @param router
111      *            instance of deleted Router Network object
112      */
113     @Override
114     public void neutronRouterDeleted(NeutronRouter router) {
115         enqueueEvent(new NorthboundEvent(router, Action.DELETE));
116     }
117
118     /**
119      * Services provide this interface method to indicate if the specified interface can be attached to the specified
120      * route
121      *
122      * @param router
123      *            instance of the base Neutron Router object
124      * @param routerInterface
125      *            instance of the NeutronRouter_Interface to be attached to the router
126      * @return integer
127      *            the return value is understood to be a HTTP status code.  A return value outside of 200 through 299
128      *            results in the attach operation being interrupted and the returned status value reflected in the
129      *            HTTP response.
130      */
131     @Override
132     public int canAttachInterface(NeutronRouter router, NeutronRouter_Interface routerInterface) {
133         logger.debug(" Router {} asked if it can attach interface {}. Subnet {}",
134                      router.getName(),
135                      routerInterface.getPortUUID(),
136                      routerInterface.getSubnetUUID());
137         return HttpURLConnection.HTTP_OK;
138     }
139
140     /**
141      * Services provide this interface method for taking action after an interface has been added to a router
142      *
143      * @param router
144      *            instance of the base Neutron Router object
145      * @param routerInterface
146      *            instance of the NeutronRouter_Interface being attached to the router
147      */
148     @Override
149     public void neutronRouterInterfaceAttached(NeutronRouter router, NeutronRouter_Interface routerInterface) {
150         enqueueEvent(new NorthboundEvent(router, routerInterface, Action.ADD));
151     }
152
153     /**
154      * Services provide this interface method to indicate if the specified interface can be detached from the specified
155      * router
156      *
157      * @param router
158      *            instance of the base Neutron Router object
159      * @param routerInterface
160      *            instance of the NeutronRouter_Interface to be detached to the router
161      * @return integer
162      *            the return value is understood to be a HTTP status code.  A return value outside of 200 through 299
163      *            results in the detach operation being interrupted and the returned status value reflected in the
164      *            HTTP response.
165      */
166     @Override
167     public int canDetachInterface(NeutronRouter router, NeutronRouter_Interface routerInterface) {
168         logger.debug(" Router {} asked if it can detach interface {}. Subnet {}",
169                      router.getName(),
170                      routerInterface.getPortUUID(),
171                      routerInterface.getSubnetUUID());
172
173         return HttpURLConnection.HTTP_OK;
174     }
175
176     /**
177      * Services provide this interface method for taking action after an interface has been removed from a router
178      *
179      * @param router
180      *            instance of the base Neutron Router object
181      * @param routerInterface
182      *            instance of the NeutronRouter_Interface being detached from the router
183      */
184     @Override
185     public void neutronRouterInterfaceDetached(NeutronRouter router, NeutronRouter_Interface routerInterface) {
186         enqueueEvent(new NorthboundEvent(router, routerInterface, Action.DELETE));
187     }
188
189     /**
190      * Process the event.
191      *
192      * @param abstractEvent the {@link org.opendaylight.ovsdb.openstack.netvirt.AbstractEvent} event to be handled.
193      * @see org.opendaylight.ovsdb.openstack.netvirt.api.EventDispatcher
194      */
195     @Override
196     public void processEvent(AbstractEvent abstractEvent) {
197         if (!(abstractEvent instanceof NorthboundEvent)) {
198             logger.error("Unable to process abstract event " + abstractEvent);
199             return;
200         }
201         NorthboundEvent ev = (NorthboundEvent) abstractEvent;
202         switch (ev.getAction()) {
203             case ADD:
204                 // fall through
205             case DELETE:
206                 // fall through
207             case UPDATE:
208                 if (ev.getRouterInterface() == null) {
209                     neutronL3Adapter.handleNeutronRouterEvent(ev.getRouter(), ev.getAction());
210                 } else {
211                     neutronL3Adapter.handleNeutronRouterInterfaceEvent(ev.getRouter(),
212                                                                       ev.getRouterInterface(),
213                                                                       ev.getAction());
214                 }
215                 break;
216             default:
217                 logger.warn("Unable to process event action " + ev.getAction());
218                 break;
219         }
220     }
221 }