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 for (Object instance : instances) {
190 INeutronLoadBalancerAware service = (INeutronLoadBalancerAware) instance;
191 int status = service.canCreateNeutronLoadBalancer(singleton);
192 if (status < 200 || status > 299) {
193 return Response.status(status).build();
197 loadBalancerInterface.addNeutronLoadBalancer(singleton);
198 if (instances != null) {
199 for (Object instance : instances) {
200 INeutronLoadBalancerAware service = (INeutronLoadBalancerAware) instance;
201 service.neutronLoadBalancerCreated(singleton);
205 List<NeutronLoadBalancer> bulk = input.getBulk();
206 Iterator<NeutronLoadBalancer> i = bulk.iterator();
207 HashMap<String, NeutronLoadBalancer> testMap = new HashMap<String, NeutronLoadBalancer>();
208 Object[] instances = NeutronUtil.getInstances(INeutronLoadBalancerAware.class, this);
209 while (i.hasNext()) {
210 NeutronLoadBalancer test = i.next();
213 * Verify that the loadbalancer doesn't already exist
216 if (loadBalancerInterface.neutronLoadBalancerExists(test.getLoadBalancerID())) {
217 throw new BadRequestException("Load Balancer Pool UUID already is already created");
219 if (testMap.containsKey(test.getLoadBalancerID())) {
220 throw new BadRequestException("Load Balancer Pool UUID already exists");
222 if (instances != null) {
223 for (Object instance : instances) {
224 INeutronLoadBalancerAware service = (INeutronLoadBalancerAware) instance;
225 int status = service.canCreateNeutronLoadBalancer(test);
226 if (status < 200 || status > 299) {
227 return Response.status(status).build();
233 * now, each element of the bulk request can be added to the cache
236 while (i.hasNext()) {
237 NeutronLoadBalancer test = i.next();
238 loadBalancerInterface.addNeutronLoadBalancer(test);
239 if (instances != null) {
240 for (Object instance : instances) {
241 INeutronLoadBalancerAware service = (INeutronLoadBalancerAware) instance;
242 service.neutronLoadBalancerCreated(test);
247 return Response.status(201).entity(input).build();
251 * Updates a LoadBalancer Policy
253 @Path("{loadBalancerID}")
255 @Produces({ MediaType.APPLICATION_JSON })
256 @Consumes({ MediaType.APPLICATION_JSON })
259 @ResponseCode(code = 200, condition = "Operation successful"),
260 @ResponseCode(code = 400, condition = "Bad Request"),
261 @ResponseCode(code = 401, condition = "Unauthorized"),
262 @ResponseCode(code = 403, condition = "Forbidden"),
263 @ResponseCode(code = 404, condition = "Not Found"),
264 @ResponseCode(code = 501, condition = "Not Implemented") })
265 public Response updateLoadBalancer(
266 @PathParam("loadBalancerID") String loadBalancerID, final NeutronLoadBalancerRequest input) {
267 INeutronLoadBalancerCRUD loadBalancerInterface = NeutronCRUDInterfaces.getINeutronLoadBalancerCRUD(
269 if (loadBalancerInterface == null) {
270 throw new ServiceUnavailableException("LoadBalancer CRUD Interface "
271 + RestMessages.SERVICEUNAVAILABLE.toString());
275 * verify the LoadBalancer exists and there is only one delta provided
277 if (!loadBalancerInterface.neutronLoadBalancerExists(loadBalancerID)) {
278 throw new ResourceNotFoundException("LoadBalancer UUID does not exist.");
280 if (!input.isSingleton()) {
281 throw new BadRequestException("Only singleton edit supported");
283 NeutronLoadBalancer delta = input.getSingleton();
284 NeutronLoadBalancer original = loadBalancerInterface.getNeutronLoadBalancer(loadBalancerID);
287 * updates restricted by Neutron
289 if (delta.getLoadBalancerID() != null ||
290 delta.getLoadBalancerTenantID() != null ||
291 delta.getLoadBalancerName() != null ||
292 delta.getLoadBalancerDescription() != null ||
293 delta.getLoadBalancerStatus() != null ||
294 delta.getLoadBalancerVipAddress() != null ||
295 delta.getLoadBalancerVipSubnetID() != null) {
296 throw new BadRequestException("Attribute edit blocked by Neutron");
299 Object[] instances = NeutronUtil.getInstances(INeutronLoadBalancerAware.class, this);
300 if (instances != null) {
301 for (Object instance : instances) {
302 INeutronLoadBalancerAware service = (INeutronLoadBalancerAware) instance;
303 int status = service.canUpdateNeutronLoadBalancer(delta, original);
304 if (status < 200 || status > 299) {
305 return Response.status(status).build();
311 * update the object and return it
313 loadBalancerInterface.updateNeutronLoadBalancer(loadBalancerID, delta);
314 NeutronLoadBalancer updatedLoadBalancer = loadBalancerInterface.getNeutronLoadBalancer(
316 if (instances != null) {
317 for (Object instance : instances) {
318 INeutronLoadBalancerAware service = (INeutronLoadBalancerAware) instance;
319 service.neutronLoadBalancerUpdated(updatedLoadBalancer);
322 return Response.status(200).entity(new NeutronLoadBalancerRequest(loadBalancerInterface.getNeutronLoadBalancer(
323 loadBalancerID))).build();
327 * Deletes a LoadBalancer */
329 @Path("{loadBalancerID}")
332 @ResponseCode(code = 204, condition = "No Content"),
333 @ResponseCode(code = 401, condition = "Unauthorized"),
334 @ResponseCode(code = 404, condition = "Not Found"),
335 @ResponseCode(code = 409, condition = "Conflict"),
336 @ResponseCode(code = 501, condition = "Not Implemented") })
337 public Response deleteLoadBalancer(
338 @PathParam("loadBalancerID") String loadBalancerID) {
339 INeutronLoadBalancerCRUD loadBalancerInterface = NeutronCRUDInterfaces.getINeutronLoadBalancerCRUD(
341 if (loadBalancerInterface == null) {
342 throw new ServiceUnavailableException("LoadBalancer CRUD Interface "
343 + RestMessages.SERVICEUNAVAILABLE.toString());
347 * verify the LoadBalancer exists and it isn't currently in use
349 if (!loadBalancerInterface.neutronLoadBalancerExists(loadBalancerID)) {
350 throw new ResourceNotFoundException("LoadBalancer UUID does not exist.");
352 if (loadBalancerInterface.neutronLoadBalancerInUse(loadBalancerID)) {
353 return Response.status(409).build();
355 NeutronLoadBalancer singleton = loadBalancerInterface.getNeutronLoadBalancer(loadBalancerID);
356 Object[] instances = NeutronUtil.getInstances(INeutronLoadBalancerAware.class, this);
357 if (instances != null) {
358 for (Object instance : instances) {
359 INeutronLoadBalancerAware service = (INeutronLoadBalancerAware) instance;
360 int status = service.canDeleteNeutronLoadBalancer(singleton);
361 if (status < 200 || status > 299) {
362 return Response.status(status).build();
367 loadBalancerInterface.removeNeutronLoadBalancer(loadBalancerID);
368 if (instances != null) {
369 for (Object instance : instances) {
370 INeutronLoadBalancerAware service = (INeutronLoadBalancerAware) instance;
371 service.neutronLoadBalancerDeleted(singleton);
374 return Response.status(204).build();