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