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