2 * Copyright (C) 2014 Red Hat, Inc.
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
9 package org.opendaylight.controller.networkconfig.neutron.northbound;
12 import java.util.ArrayList;
13 import java.util.HashMap;
14 import java.util.Iterator;
15 import java.util.List;
17 import javax.ws.rs.Consumes;
18 import javax.ws.rs.DELETE;
19 import javax.ws.rs.GET;
20 import javax.ws.rs.POST;
21 import javax.ws.rs.PUT;
22 import javax.ws.rs.Path;
23 import javax.ws.rs.PathParam;
24 import javax.ws.rs.Produces;
25 import javax.ws.rs.QueryParam;
26 import javax.ws.rs.core.MediaType;
27 import javax.ws.rs.core.Response;
29 import org.codehaus.enunciate.jaxrs.ResponseCode;
30 import org.codehaus.enunciate.jaxrs.StatusCodes;
31 import org.opendaylight.controller.networkconfig.neutron.INeutronLoadBalancerAware;
32 import org.opendaylight.controller.networkconfig.neutron.INeutronLoadBalancerCRUD;
33 import org.opendaylight.controller.networkconfig.neutron.NeutronCRUDInterfaces;
34 import org.opendaylight.controller.networkconfig.neutron.NeutronLoadBalancer;
37 * Neutron Northbound REST APIs for LoadBalancers.<br>
38 * This class provides REST APIs for managing neutron LoadBalancers
42 * Authentication scheme : <b>HTTP Basic</b><br>
43 * Authentication realm : <b>opendaylight</b><br>
44 * Transport : <b>HTTP and HTTPS</b><br>
46 * HTTPS Authentication is disabled by default. Administrator can enable it in
47 * tomcat-server.xml after adding a proper keystore / SSL certificate from a
48 * trusted authority.<br>
50 * http://tomcat.apache.org/tomcat-7.0-doc/ssl-howto.html#Configuration
53 @Path("/loadbalancers")
54 public class NeutronLoadBalancerNorthbound {
56 private NeutronLoadBalancer extractFields(NeutronLoadBalancer o, List<String> fields) {
57 return o.extractFields(fields);
61 * Returns a list of all LoadBalancer */
63 @Produces({ MediaType.APPLICATION_JSON })
65 @ResponseCode(code = 200, condition = "Operation successful"),
66 @ResponseCode(code = 401, condition = "Unauthorized"),
67 @ResponseCode(code = 501, condition = "Not Implemented") })
69 public Response listGroups(
71 @QueryParam("fields") List<String> fields,
72 // OpenStack LoadBalancer attributes
73 @QueryParam("id") String queryLoadBalancerID,
74 @QueryParam("tenant_id") String queryLoadBalancerTenantID,
75 @QueryParam("name") String queryLoadBalancerName,
76 @QueryParam("description") String queryLoadBalancerDescription,
77 @QueryParam("status") String queryLoadBalancerStatus,
78 @QueryParam("vip_address") String queryLoadBalancerVipAddress,
79 @QueryParam("vip_subnet") String queryLoadBalancerVipSubnet,
81 @QueryParam("limit") String limit,
82 @QueryParam("marker") String marker,
83 @QueryParam("page_reverse") String pageReverse
84 // sorting not supported
86 INeutronLoadBalancerCRUD loadBalancerInterface = NeutronCRUDInterfaces.getINeutronLoadBalancerCRUD(this);
88 if (loadBalancerInterface == null) {
89 throw new ServiceUnavailableException("LoadBalancer CRUD Interface "
90 + RestMessages.SERVICEUNAVAILABLE.toString());
92 List<NeutronLoadBalancer> allLoadBalancers = loadBalancerInterface.getAllNeutronLoadBalancers();
93 // List<NeutronLoadBalancerRule> allLoadBalancerRules = firewallRuleInterface.getAllNeutronLoadBalancerRules();
94 List<NeutronLoadBalancer> ans = new ArrayList<NeutronLoadBalancer>();
95 // List<NeutronLoadBalancerRule> rules = new ArrayList<NeutronLoadBalancerRule>();
96 Iterator<NeutronLoadBalancer> i = allLoadBalancers.iterator();
98 NeutronLoadBalancer nsg = i.next();
99 if ((queryLoadBalancerID == null ||
100 queryLoadBalancerID.equals(nsg.getLoadBalancerID())) &&
101 (queryLoadBalancerTenantID == null ||
102 queryLoadBalancerTenantID.equals(nsg.getLoadBalancerTenantID())) &&
103 (queryLoadBalancerName == null ||
104 queryLoadBalancerName.equals(nsg.getLoadBalancerName())) &&
105 (queryLoadBalancerDescription == null ||
106 queryLoadBalancerDescription.equals(nsg.getLoadBalancerDescription())) &&
107 (queryLoadBalancerVipAddress == null ||
108 queryLoadBalancerVipAddress.equals(nsg.getLoadBalancerVipAddress())) &&
109 (queryLoadBalancerVipSubnet == null ||
110 queryLoadBalancerVipSubnet.equals(nsg.getLoadBalancerVipSubnetID()))) {
111 if (fields.size() > 0) {
112 ans.add(extractFields(nsg,fields));
118 return Response.status(200).entity(
119 new NeutronLoadBalancerRequest(ans)).build();
123 * Returns a specific LoadBalancer */
125 @Path("{loadBalancerID}")
127 @Produces({ MediaType.APPLICATION_JSON })
130 @ResponseCode(code = 200, condition = "Operation successful"),
131 @ResponseCode(code = 401, condition = "Unauthorized"),
132 @ResponseCode(code = 404, condition = "Not Found"),
133 @ResponseCode(code = 501, condition = "Not Implemented") })
134 public Response showLoadBalancer(@PathParam("loadBalancerID") String loadBalancerID,
136 @QueryParam("fields") List<String> fields) {
137 INeutronLoadBalancerCRUD loadBalancerInterface = NeutronCRUDInterfaces.getINeutronLoadBalancerCRUD(
139 if (loadBalancerInterface == null) {
140 throw new ServiceUnavailableException("LoadBalancer CRUD Interface "
141 + RestMessages.SERVICEUNAVAILABLE.toString());
143 if (!loadBalancerInterface.neutronLoadBalancerExists(loadBalancerID)) {
144 throw new ResourceNotFoundException("LoadBalancer UUID does not exist.");
146 if (fields.size() > 0) {
147 NeutronLoadBalancer ans = loadBalancerInterface.getNeutronLoadBalancer(loadBalancerID);
148 return Response.status(200).entity(
149 new NeutronLoadBalancerRequest(extractFields(ans, fields))).build();
151 return Response.status(200).entity(new NeutronLoadBalancerRequest(loadBalancerInterface.getNeutronLoadBalancer(
152 loadBalancerID))).build();
157 * Creates new LoadBalancer */
160 @Produces({ MediaType.APPLICATION_JSON })
161 @Consumes({ MediaType.APPLICATION_JSON })
164 @ResponseCode(code = 201, condition = "Created"),
165 @ResponseCode(code = 400, condition = "Bad Request"),
166 @ResponseCode(code = 401, condition = "Unauthorized"),
167 @ResponseCode(code = 403, condition = "Forbidden"),
168 @ResponseCode(code = 404, condition = "Not Found"),
169 @ResponseCode(code = 409, condition = "Conflict"),
170 @ResponseCode(code = 501, condition = "Not Implemented") })
171 public Response createLoadBalancers(final NeutronLoadBalancerRequest input) {
172 INeutronLoadBalancerCRUD loadBalancerInterface = NeutronCRUDInterfaces.getINeutronLoadBalancerCRUD(
174 if (loadBalancerInterface == null) {
175 throw new ServiceUnavailableException("LoadBalancer CRUD Interface "
176 + RestMessages.SERVICEUNAVAILABLE.toString());
178 if (input.isSingleton()) {
179 NeutronLoadBalancer singleton = input.getSingleton();
182 * Verify that the LoadBalancer doesn't already exist.
184 if (loadBalancerInterface.neutronLoadBalancerExists(singleton.getLoadBalancerID())) {
185 throw new BadRequestException("LoadBalancer UUID already exists");
187 Object[] instances = NeutronUtil.getInstances(INeutronLoadBalancerAware.class, this);
188 if (instances != null) {
189 if (instances.length > 0) {
190 for (Object instance : instances) {
191 INeutronLoadBalancerAware service = (INeutronLoadBalancerAware) instance;
192 int status = service.canCreateNeutronLoadBalancer(singleton);
193 if (status < 200 || status > 299) {
194 return Response.status(status).build();
198 throw new ServiceUnavailableException("No providers registered. Please try again later");
201 throw new ServiceUnavailableException("Couldn't get providers list. Please try again later");
204 loadBalancerInterface.addNeutronLoadBalancer(singleton);
205 if (instances != null) {
206 for (Object instance : instances) {
207 INeutronLoadBalancerAware service = (INeutronLoadBalancerAware) instance;
208 service.neutronLoadBalancerCreated(singleton);
212 List<NeutronLoadBalancer> bulk = input.getBulk();
213 Iterator<NeutronLoadBalancer> i = bulk.iterator();
214 HashMap<String, NeutronLoadBalancer> testMap = new HashMap<String, NeutronLoadBalancer>();
215 Object[] instances = NeutronUtil.getInstances(INeutronLoadBalancerAware.class, this);
216 while (i.hasNext()) {
217 NeutronLoadBalancer test = i.next();
220 * Verify that the loadbalancer doesn't already exist
223 if (loadBalancerInterface.neutronLoadBalancerExists(test.getLoadBalancerID())) {
224 throw new BadRequestException("Load Balancer Pool UUID already is already created");
226 if (testMap.containsKey(test.getLoadBalancerID())) {
227 throw new BadRequestException("Load Balancer Pool UUID already exists");
229 if (instances != null) {
230 if (instances.length > 0) {
231 for (Object instance : instances) {
232 INeutronLoadBalancerAware service = (INeutronLoadBalancerAware) instance;
233 int status = service.canCreateNeutronLoadBalancer(test);
234 if (status < 200 || status > 299) {
235 return Response.status(status).build();
239 throw new ServiceUnavailableException("No providers registered. Please try again later");
242 throw new ServiceUnavailableException("Couldn't get providers list. Please try again later");
246 * now, each element of the bulk request can be added to the cache
249 while (i.hasNext()) {
250 NeutronLoadBalancer test = i.next();
251 loadBalancerInterface.addNeutronLoadBalancer(test);
252 if (instances != null) {
253 for (Object instance : instances) {
254 INeutronLoadBalancerAware service = (INeutronLoadBalancerAware) instance;
255 service.neutronLoadBalancerCreated(test);
260 return Response.status(201).entity(input).build();
264 * Updates a LoadBalancer Policy
266 @Path("{loadBalancerID}")
268 @Produces({ MediaType.APPLICATION_JSON })
269 @Consumes({ MediaType.APPLICATION_JSON })
272 @ResponseCode(code = 200, condition = "Operation successful"),
273 @ResponseCode(code = 400, condition = "Bad Request"),
274 @ResponseCode(code = 401, condition = "Unauthorized"),
275 @ResponseCode(code = 403, condition = "Forbidden"),
276 @ResponseCode(code = 404, condition = "Not Found"),
277 @ResponseCode(code = 501, condition = "Not Implemented") })
278 public Response updateLoadBalancer(
279 @PathParam("loadBalancerID") String loadBalancerID, final NeutronLoadBalancerRequest input) {
280 INeutronLoadBalancerCRUD loadBalancerInterface = NeutronCRUDInterfaces.getINeutronLoadBalancerCRUD(
282 if (loadBalancerInterface == null) {
283 throw new ServiceUnavailableException("LoadBalancer CRUD Interface "
284 + RestMessages.SERVICEUNAVAILABLE.toString());
288 * verify the LoadBalancer exists and there is only one delta provided
290 if (!loadBalancerInterface.neutronLoadBalancerExists(loadBalancerID)) {
291 throw new ResourceNotFoundException("LoadBalancer UUID does not exist.");
293 if (!input.isSingleton()) {
294 throw new BadRequestException("Only singleton edit supported");
296 NeutronLoadBalancer delta = input.getSingleton();
297 NeutronLoadBalancer original = loadBalancerInterface.getNeutronLoadBalancer(loadBalancerID);
300 * updates restricted by Neutron
302 if (delta.getLoadBalancerID() != null ||
303 delta.getLoadBalancerTenantID() != null ||
304 delta.getLoadBalancerName() != null ||
305 delta.getLoadBalancerDescription() != null ||
306 delta.getLoadBalancerStatus() != null ||
307 delta.getLoadBalancerVipAddress() != null ||
308 delta.getLoadBalancerVipSubnetID() != null) {
309 throw new BadRequestException("Attribute edit blocked by Neutron");
312 Object[] instances = NeutronUtil.getInstances(INeutronLoadBalancerAware.class, this);
313 if (instances != null) {
314 if (instances.length > 0) {
315 for (Object instance : instances) {
316 INeutronLoadBalancerAware service = (INeutronLoadBalancerAware) instance;
317 int status = service.canUpdateNeutronLoadBalancer(delta, original);
318 if (status < 200 || status > 299) {
319 return Response.status(status).build();
323 throw new ServiceUnavailableException("No providers registered. Please try again later");
326 throw new ServiceUnavailableException("Couldn't get providers list. Please try again later");
330 * update the object and return it
332 loadBalancerInterface.updateNeutronLoadBalancer(loadBalancerID, delta);
333 NeutronLoadBalancer updatedLoadBalancer = loadBalancerInterface.getNeutronLoadBalancer(
335 if (instances != null) {
336 for (Object instance : instances) {
337 INeutronLoadBalancerAware service = (INeutronLoadBalancerAware) instance;
338 service.neutronLoadBalancerUpdated(updatedLoadBalancer);
341 return Response.status(200).entity(new NeutronLoadBalancerRequest(loadBalancerInterface.getNeutronLoadBalancer(
342 loadBalancerID))).build();
346 * Deletes a LoadBalancer */
348 @Path("{loadBalancerID}")
351 @ResponseCode(code = 204, condition = "No Content"),
352 @ResponseCode(code = 401, condition = "Unauthorized"),
353 @ResponseCode(code = 404, condition = "Not Found"),
354 @ResponseCode(code = 409, condition = "Conflict"),
355 @ResponseCode(code = 501, condition = "Not Implemented") })
356 public Response deleteLoadBalancer(
357 @PathParam("loadBalancerID") String loadBalancerID) {
358 INeutronLoadBalancerCRUD loadBalancerInterface = NeutronCRUDInterfaces.getINeutronLoadBalancerCRUD(
360 if (loadBalancerInterface == null) {
361 throw new ServiceUnavailableException("LoadBalancer CRUD Interface "
362 + RestMessages.SERVICEUNAVAILABLE.toString());
366 * verify the LoadBalancer exists and it isn't currently in use
368 if (!loadBalancerInterface.neutronLoadBalancerExists(loadBalancerID)) {
369 throw new ResourceNotFoundException("LoadBalancer UUID does not exist.");
371 if (loadBalancerInterface.neutronLoadBalancerInUse(loadBalancerID)) {
372 return Response.status(409).build();
374 NeutronLoadBalancer singleton = loadBalancerInterface.getNeutronLoadBalancer(loadBalancerID);
375 Object[] instances = NeutronUtil.getInstances(INeutronLoadBalancerAware.class, this);
376 if (instances != null) {
377 if (instances.length > 0) {
378 for (Object instance : instances) {
379 INeutronLoadBalancerAware service = (INeutronLoadBalancerAware) instance;
380 int status = service.canDeleteNeutronLoadBalancer(singleton);
381 if (status < 200 || status > 299) {
382 return Response.status(status).build();
386 throw new ServiceUnavailableException("No providers registered. Please try again later");
389 throw new ServiceUnavailableException("Couldn't get providers list. Please try again later");
393 loadBalancerInterface.removeNeutronLoadBalancer(loadBalancerID);
394 if (instances != null) {
395 for (Object instance : instances) {
396 INeutronLoadBalancerAware service = (INeutronLoadBalancerAware) instance;
397 service.neutronLoadBalancerDeleted(singleton);
400 return Response.status(204).build();