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