2 * Copyright (C) 2014 SDN Hub, LLC.
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
8 * Authors : Srini Seetharaman
11 package org.opendaylight.controller.networkconfig.neutron.northbound;
14 import java.util.ArrayList;
15 import java.util.HashMap;
16 import java.util.Iterator;
17 import java.util.List;
19 import javax.ws.rs.Consumes;
20 import javax.ws.rs.DELETE;
21 import javax.ws.rs.GET;
22 import javax.ws.rs.POST;
23 import javax.ws.rs.PUT;
24 import javax.ws.rs.Path;
25 import javax.ws.rs.PathParam;
26 import javax.ws.rs.Produces;
27 import javax.ws.rs.QueryParam;
28 import javax.ws.rs.core.MediaType;
29 import javax.ws.rs.core.Response;
31 import org.codehaus.enunciate.jaxrs.ResponseCode;
32 import org.codehaus.enunciate.jaxrs.StatusCodes;
33 import org.opendaylight.controller.networkconfig.neutron.INeutronLoadBalancerPoolAware;
34 import org.opendaylight.controller.networkconfig.neutron.INeutronLoadBalancerPoolCRUD;
35 import org.opendaylight.controller.networkconfig.neutron.NeutronCRUDInterfaces;
36 import org.opendaylight.controller.networkconfig.neutron.NeutronLoadBalancerPool;
37 import org.opendaylight.controller.networkconfig.neutron.NeutronLoadBalancerPoolMember;
40 * Neutron Northbound REST APIs for LoadBalancerPool Policies.<br>
41 * This class provides REST APIs for managing neutron LoadBalancerPool Policies
45 * Authentication scheme : <b>HTTP Basic</b><br>
46 * Authentication realm : <b>opendaylight</b><br>
47 * Transport : <b>HTTP and HTTPS</b><br>
49 * HTTPS Authentication is disabled by default. Administrator can enable it in
50 * tomcat-server.xml after adding a proper keystore / SSL certificate from a
51 * trusted authority.<br>
53 * http://tomcat.apache.org/tomcat-7.0-doc/ssl-howto.html#Configuration
58 * For now, the LB pool member data is maintained with the INeutronLoadBalancerPoolCRUD,
59 * and not duplicated within the INeutronLoadBalancerPoolMemberCRUD's cache.
63 public class NeutronLoadBalancerPoolNorthbound {
65 private NeutronLoadBalancerPool extractFields(NeutronLoadBalancerPool o, List<String> fields) {
66 return o.extractFields(fields);
70 * Returns a list of all LoadBalancerPool
73 @Produces({ MediaType.APPLICATION_JSON })
75 @ResponseCode(code = 200, condition = "Operation successful"),
76 @ResponseCode(code = 401, condition = "Unauthorized"),
77 @ResponseCode(code = 501, condition = "Not Implemented") })
79 public Response listGroups(
81 @QueryParam("fields") List<String> fields,
82 // OpenStack LoadBalancerPool attributes
83 @QueryParam("id") String queryLoadBalancerPoolID,
84 @QueryParam("tenant_id") String queryLoadBalancerPoolTenantID,
85 @QueryParam("name") String queryLoadBalancerPoolName,
86 @QueryParam("description") String queryLoadBalancerDescription,
87 @QueryParam("protocol") String queryLoadBalancerProtocol,
88 @QueryParam("lb_algorithm") String queryLoadBalancerPoolLbAlgorithm,
89 @QueryParam("healthmonitor_id") String queryLoadBalancerPoolHealthMonitorID,
90 @QueryParam("admin_state_up") String queryLoadBalancerIsAdminStateUp,
91 @QueryParam("status") String queryLoadBalancerPoolStatus,
92 @QueryParam("members") List<NeutronLoadBalancerPoolMember> queryLoadBalancerPoolMembers,
94 @QueryParam("limit") String limit,
95 @QueryParam("marker") String marker,
96 @QueryParam("page_reverse") String pageReverse
97 // sorting not supported
99 INeutronLoadBalancerPoolCRUD loadBalancerPoolInterface = NeutronCRUDInterfaces.getINeutronLoadBalancerPoolCRUD(this);
100 if (loadBalancerPoolInterface == null) {
101 throw new ServiceUnavailableException("LoadBalancerPool CRUD Interface "
102 + RestMessages.SERVICEUNAVAILABLE.toString());
104 List<NeutronLoadBalancerPool> allLoadBalancerPools = loadBalancerPoolInterface.getAllNeutronLoadBalancerPools();
105 List<NeutronLoadBalancerPool> ans = new ArrayList<NeutronLoadBalancerPool>();
106 Iterator<NeutronLoadBalancerPool> i = allLoadBalancerPools.iterator();
107 while (i.hasNext()) {
108 NeutronLoadBalancerPool nsg = i.next();
109 if ((queryLoadBalancerPoolID == null ||
110 queryLoadBalancerPoolID.equals(nsg.getLoadBalancerPoolID())) &&
111 (queryLoadBalancerPoolTenantID == null ||
112 queryLoadBalancerPoolTenantID.equals(nsg.getLoadBalancerPoolTenantID())) &&
113 (queryLoadBalancerPoolName == null ||
114 queryLoadBalancerPoolName.equals(nsg.getLoadBalancerPoolName())) &&
115 (queryLoadBalancerDescription == null ||
116 queryLoadBalancerDescription.equals(nsg.getLoadBalancerPoolDescription())) &&
117 (queryLoadBalancerPoolLbAlgorithm == null ||
118 queryLoadBalancerPoolLbAlgorithm.equals(nsg.getLoadBalancerPoolLbAlgorithm())) &&
119 (queryLoadBalancerPoolHealthMonitorID == null ||
120 queryLoadBalancerPoolHealthMonitorID.equals(nsg.getNeutronLoadBalancerPoolHealthMonitorID())) &&
121 (queryLoadBalancerIsAdminStateUp == null ||
122 queryLoadBalancerIsAdminStateUp.equals(nsg.getLoadBalancerPoolAdminIsStateIsUp())) &&
123 (queryLoadBalancerPoolStatus == null ||
124 queryLoadBalancerPoolStatus.equals(nsg.getLoadBalancerPoolStatus())) &&
125 (queryLoadBalancerPoolMembers.size() == 0 ||
126 queryLoadBalancerPoolMembers.equals(nsg.getLoadBalancerPoolMembers()))) {
127 if (fields.size() > 0) {
128 ans.add(extractFields(nsg,fields));
134 return Response.status(200).entity(
135 new NeutronLoadBalancerPoolRequest(ans)).build();
139 * Returns a specific LoadBalancerPool */
141 @Path("{loadBalancerPoolID}")
143 @Produces({ MediaType.APPLICATION_JSON })
145 @ResponseCode(code = 200, condition = "Operation successful"),
146 @ResponseCode(code = 401, condition = "Unauthorized"),
147 @ResponseCode(code = 404, condition = "Not Found"),
148 @ResponseCode(code = 501, condition = "Not Implemented") })
149 public Response showLoadBalancerPool(@PathParam("loadBalancerPoolID") String loadBalancerPoolID,
151 @QueryParam("fields") List<String> fields) {
152 INeutronLoadBalancerPoolCRUD loadBalancerPoolInterface = NeutronCRUDInterfaces.getINeutronLoadBalancerPoolCRUD(this);
153 if (loadBalancerPoolInterface == null) {
154 throw new ServiceUnavailableException("LoadBalancerPool CRUD Interface "
155 + RestMessages.SERVICEUNAVAILABLE.toString());
157 if (!loadBalancerPoolInterface.neutronLoadBalancerPoolExists(loadBalancerPoolID)) {
158 throw new ResourceNotFoundException("LoadBalancerPool UUID does not exist.");
160 if (fields.size() > 0) {
161 NeutronLoadBalancerPool ans = loadBalancerPoolInterface.getNeutronLoadBalancerPool(loadBalancerPoolID);
162 return Response.status(200).entity(
163 new NeutronLoadBalancerPoolRequest(extractFields(ans, fields))).build();
165 return Response.status(200).entity(new NeutronLoadBalancerPoolRequest(loadBalancerPoolInterface.getNeutronLoadBalancerPool(loadBalancerPoolID))).build();
170 * Creates new LoadBalancerPool */
173 @Produces({ MediaType.APPLICATION_JSON })
174 @Consumes({ MediaType.APPLICATION_JSON })
176 @ResponseCode(code = 201, condition = "Created"),
177 @ResponseCode(code = 400, condition = "Bad Request"),
178 @ResponseCode(code = 401, condition = "Unauthorized"),
179 @ResponseCode(code = 403, condition = "Forbidden"),
180 @ResponseCode(code = 404, condition = "Not Found"),
181 @ResponseCode(code = 409, condition = "Conflict"),
182 @ResponseCode(code = 501, condition = "Not Implemented") })
183 public Response createLoadBalancerPools(final NeutronLoadBalancerPoolRequest input) {
184 INeutronLoadBalancerPoolCRUD loadBalancerPoolInterface = NeutronCRUDInterfaces.getINeutronLoadBalancerPoolCRUD(this);
185 if (loadBalancerPoolInterface == null) {
186 throw new ServiceUnavailableException("LoadBalancerPool CRUD Interface "
187 + RestMessages.SERVICEUNAVAILABLE.toString());
189 if (input.isSingleton()) {
190 NeutronLoadBalancerPool singleton = input.getSingleton();
193 * Verify that the LoadBalancerPool doesn't already exist.
195 if (loadBalancerPoolInterface.neutronLoadBalancerPoolExists(singleton.getLoadBalancerPoolID())) {
196 throw new BadRequestException("LoadBalancerPool UUID already exists");
199 Object[] instances = NeutronUtil.getInstances(INeutronLoadBalancerPoolAware.class, this);
200 if (instances != null) {
201 if (instances.length > 0) {
202 for (Object instance : instances) {
203 INeutronLoadBalancerPoolAware service = (INeutronLoadBalancerPoolAware) instance;
204 int status = service.canCreateNeutronLoadBalancerPool(singleton);
205 if (status < 200 || status > 299) {
206 return Response.status(status).build();
210 throw new ServiceUnavailableException("No providers registered. Please try again later");
213 throw new ServiceUnavailableException("Couldn't get providers list. Please try again later");
215 loadBalancerPoolInterface.addNeutronLoadBalancerPool(singleton);
216 if (instances != null) {
217 for (Object instance : instances) {
218 INeutronLoadBalancerPoolAware service = (INeutronLoadBalancerPoolAware) instance;
219 service.neutronLoadBalancerPoolCreated(singleton);
223 List<NeutronLoadBalancerPool> bulk = input.getBulk();
224 Iterator<NeutronLoadBalancerPool> i = bulk.iterator();
225 HashMap<String, NeutronLoadBalancerPool> testMap = new HashMap<String, NeutronLoadBalancerPool>();
226 Object[] instances = NeutronUtil.getInstances(INeutronLoadBalancerPoolAware.class, this);
227 while (i.hasNext()) {
228 NeutronLoadBalancerPool test = i.next();
231 * Verify that the loadBalancerPool doesn't already exist
234 if (loadBalancerPoolInterface.neutronLoadBalancerPoolExists(test.getLoadBalancerPoolID())) {
235 throw new BadRequestException("Load Balancer Pool UUID already is already created");
237 if (testMap.containsKey(test.getLoadBalancerPoolID())) {
238 throw new BadRequestException("Load Balancer Pool UUID already exists");
240 if (instances != null) {
241 if (instances.length > 0) {
242 for (Object instance : instances) {
243 INeutronLoadBalancerPoolAware service = (INeutronLoadBalancerPoolAware) instance;
244 int status = service.canCreateNeutronLoadBalancerPool(test);
245 if (status < 200 || status > 299) {
246 return Response.status(status).build();
250 throw new ServiceUnavailableException("No providers registered. Please try again later");
253 throw new ServiceUnavailableException("Couldn't get providers list. Please try again later");
257 * now, each element of the bulk request can be added to the cache
260 while (i.hasNext()) {
261 NeutronLoadBalancerPool test = i.next();
262 loadBalancerPoolInterface.addNeutronLoadBalancerPool(test);
263 if (instances != null) {
264 for (Object instance : instances) {
265 INeutronLoadBalancerPoolAware service = (INeutronLoadBalancerPoolAware) instance;
266 service.neutronLoadBalancerPoolCreated(test);
271 return Response.status(201).entity(input).build();
275 * Updates a LoadBalancerPool Policy
277 @Path("{loadBalancerPoolID}")
279 @Produces({ MediaType.APPLICATION_JSON })
280 @Consumes({ MediaType.APPLICATION_JSON })
282 @ResponseCode(code = 200, condition = "Operation successful"),
283 @ResponseCode(code = 400, condition = "Bad Request"),
284 @ResponseCode(code = 401, condition = "Unauthorized"),
285 @ResponseCode(code = 403, condition = "Forbidden"),
286 @ResponseCode(code = 404, condition = "Not Found"),
287 @ResponseCode(code = 501, condition = "Not Implemented") })
288 public Response updateLoadBalancerPool(
289 @PathParam("loadBalancerPoolID") String loadBalancerPoolID, final NeutronLoadBalancerPoolRequest input) {
290 INeutronLoadBalancerPoolCRUD loadBalancerPoolInterface = NeutronCRUDInterfaces.getINeutronLoadBalancerPoolCRUD(this);
291 if (loadBalancerPoolInterface == null) {
292 throw new ServiceUnavailableException("LoadBalancerPool CRUD Interface "
293 + RestMessages.SERVICEUNAVAILABLE.toString());
297 * verify the LoadBalancerPool exists and there is only one delta provided
299 if (!loadBalancerPoolInterface.neutronLoadBalancerPoolExists(loadBalancerPoolID)) {
300 throw new ResourceNotFoundException("LoadBalancerPool UUID does not exist.");
302 if (!input.isSingleton()) {
303 throw new BadRequestException("Only singleton edit supported");
305 NeutronLoadBalancerPool delta = input.getSingleton();
306 NeutronLoadBalancerPool original = loadBalancerPoolInterface.getNeutronLoadBalancerPool(loadBalancerPoolID);
309 * updates restricted by Neutron
311 if (delta.getLoadBalancerPoolID() != null ||
312 delta.getLoadBalancerPoolTenantID() != null ||
313 delta.getLoadBalancerPoolName() != null ||
314 delta.getLoadBalancerPoolDescription() != null ||
315 delta.getLoadBalancerPoolProtocol() != null ||
316 delta.getLoadBalancerPoolLbAlgorithm() != null ||
317 delta.getNeutronLoadBalancerPoolHealthMonitorID() != null ||
318 delta.getLoadBalancerPoolAdminIsStateIsUp() != null ||
319 delta.getLoadBalancerPoolStatus() != null ||
320 delta.getLoadBalancerPoolMembers() != null) {
321 throw new BadRequestException("Attribute edit blocked by Neutron");
324 Object[] instances = NeutronUtil.getInstances(INeutronLoadBalancerPoolAware.class, this);
325 if (instances != null) {
326 if (instances.length > 0) {
327 for (Object instance : instances) {
328 INeutronLoadBalancerPoolAware service = (INeutronLoadBalancerPoolAware) instance;
329 int status = service.canUpdateNeutronLoadBalancerPool(delta, original);
330 if (status < 200 || status > 299) {
331 return Response.status(status).build();
335 throw new ServiceUnavailableException("No providers registered. Please try again later");
338 throw new ServiceUnavailableException("Couldn't get providers list. Please try again later");
342 * update the object and return it
344 loadBalancerPoolInterface.updateNeutronLoadBalancerPool(loadBalancerPoolID, delta);
345 NeutronLoadBalancerPool updatedLoadBalancerPool = loadBalancerPoolInterface.getNeutronLoadBalancerPool(loadBalancerPoolID);
346 if (instances != null) {
347 for (Object instance : instances) {
348 INeutronLoadBalancerPoolAware service = (INeutronLoadBalancerPoolAware) instance;
349 service.neutronLoadBalancerPoolUpdated(updatedLoadBalancerPool);
352 return Response.status(200).entity(new NeutronLoadBalancerPoolRequest(loadBalancerPoolInterface.getNeutronLoadBalancerPool(loadBalancerPoolID))).build();
356 * Deletes a LoadBalancerPool
359 @Path("{loadBalancerPoolUUID}")
362 @ResponseCode(code = 204, condition = "No Content"),
363 @ResponseCode(code = 401, condition = "Unauthorized"),
364 @ResponseCode(code = 404, condition = "Not Found"),
365 @ResponseCode(code = 409, condition = "Conflict"),
366 @ResponseCode(code = 501, condition = "Not Implemented") })
367 public Response deleteLoadBalancerPool(
368 @PathParam("loadBalancerPoolUUID") String loadBalancerPoolUUID) {
369 INeutronLoadBalancerPoolCRUD loadBalancerPoolInterface = NeutronCRUDInterfaces.getINeutronLoadBalancerPoolCRUD(this);
370 if (loadBalancerPoolInterface == null) {
371 throw new ServiceUnavailableException("LoadBalancerPool CRUD Interface "
372 + RestMessages.SERVICEUNAVAILABLE.toString());
376 * verify the LoadBalancerPool exists and it isn't currently in use
378 if (!loadBalancerPoolInterface.neutronLoadBalancerPoolExists(loadBalancerPoolUUID)) {
379 throw new ResourceNotFoundException("LoadBalancerPool UUID does not exist.");
381 if (loadBalancerPoolInterface.neutronLoadBalancerPoolInUse(loadBalancerPoolUUID)) {
382 return Response.status(409).build();
384 NeutronLoadBalancerPool singleton = loadBalancerPoolInterface.getNeutronLoadBalancerPool(loadBalancerPoolUUID);
385 Object[] instances = NeutronUtil.getInstances(INeutronLoadBalancerPoolAware.class, this);
386 if (instances != null) {
387 if (instances.length > 0) {
388 for (Object instance : instances) {
389 INeutronLoadBalancerPoolAware service = (INeutronLoadBalancerPoolAware) instance;
390 int status = service.canDeleteNeutronLoadBalancerPool(singleton);
391 if (status < 200 || status > 299) {
392 return Response.status(status).build();
396 throw new ServiceUnavailableException("No providers registered. Please try again later");
399 throw new ServiceUnavailableException("Couldn't get providers list. Please try again later");
403 * remove it and return 204 status
405 loadBalancerPoolInterface.removeNeutronLoadBalancerPool(loadBalancerPoolUUID);
406 if (instances != null) {
407 for (Object instance : instances) {
408 INeutronLoadBalancerPoolAware service = (INeutronLoadBalancerPoolAware) instance;
409 service.neutronLoadBalancerPoolDeleted(singleton);
412 return Response.status(204).build();