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 org.codehaus.enunciate.jaxrs.ResponseCode;
13 import org.codehaus.enunciate.jaxrs.StatusCodes;
14 import org.opendaylight.controller.networkconfig.neutron.INeutronLoadBalancerPoolAware;
15 import org.opendaylight.controller.networkconfig.neutron.INeutronLoadBalancerPoolCRUD;
16 import org.opendaylight.controller.networkconfig.neutron.NeutronCRUDInterfaces;
17 import org.opendaylight.controller.networkconfig.neutron.NeutronLoadBalancerPool;
18 import org.opendaylight.controller.northbound.commons.RestMessages;
19 import org.opendaylight.controller.northbound.commons.exception.BadRequestException;
20 import org.opendaylight.controller.northbound.commons.exception.ResourceNotFoundException;
21 import org.opendaylight.controller.northbound.commons.exception.ServiceUnavailableException;
22 import org.opendaylight.controller.sal.utils.ServiceHelper;
24 import javax.ws.rs.Consumes;
25 import javax.ws.rs.GET;
26 import javax.ws.rs.POST;
27 import javax.ws.rs.PUT;
28 import javax.ws.rs.Path;
29 import javax.ws.rs.PathParam;
30 import javax.ws.rs.Produces;
31 import javax.ws.rs.QueryParam;
32 import javax.ws.rs.core.MediaType;
33 import javax.ws.rs.core.Response;
34 import java.util.ArrayList;
35 import java.util.HashMap;
36 import java.util.Iterator;
37 import java.util.List;
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
57 public class NeutronLoadBalancerPoolNorthbound {
59 private NeutronLoadBalancerPool extractFields(NeutronLoadBalancerPool o, List<String> fields) {
60 return o.extractFields(fields);
64 * Returns a list of all LoadBalancerPool
67 @Produces({ MediaType.APPLICATION_JSON })
69 @ResponseCode(code = 200, condition = "Operation successful"),
70 @ResponseCode(code = 401, condition = "Unauthorized"),
71 @ResponseCode(code = 501, condition = "Not Implemented") })
73 public Response listGroups(
75 @QueryParam("fields") List<String> fields,
76 // OpenStack LoadBalancerPool attributes
77 @QueryParam("id") String queryLoadBalancerPoolID,
78 @QueryParam("tenant_id") String queryLoadBalancerPoolTenantID,
79 @QueryParam("name") String queryLoadBalancerPoolName,
80 @QueryParam("description") String queryLoadBalancerDescription,
81 @QueryParam("protocol") String queryLoadBalancerProtocol,
82 @QueryParam("lb_algorithm") String queryLoadBalancerPoolLbAlgorithm,
83 @QueryParam("healthmonitor_id") String queryLoadBalancerPoolHealthMonitorID,
84 @QueryParam("admin_state_up") String queryLoadBalancerIsAdminStateUp,
85 @QueryParam("status") String queryLoadBalancerPoolStatus,
86 @QueryParam("members") List queryLoadBalancerPoolMembers,
88 @QueryParam("limit") String limit,
89 @QueryParam("marker") String marker,
90 @QueryParam("page_reverse") String pageReverse
91 // sorting not supported
93 INeutronLoadBalancerPoolCRUD loadBalancerPoolInterface = NeutronCRUDInterfaces.getINeutronLoadBalancerPoolCRUD(this);
94 if (loadBalancerPoolInterface == null) {
95 throw new ServiceUnavailableException("LoadBalancerPool CRUD Interface "
96 + RestMessages.SERVICEUNAVAILABLE.toString());
98 List<NeutronLoadBalancerPool> allLoadBalancerPools = loadBalancerPoolInterface.getAllNeutronLoadBalancerPools();
99 List<NeutronLoadBalancerPool> ans = new ArrayList<NeutronLoadBalancerPool>();
100 Iterator<NeutronLoadBalancerPool> i = allLoadBalancerPools.iterator();
101 while (i.hasNext()) {
102 NeutronLoadBalancerPool nsg = i.next();
103 if ((queryLoadBalancerPoolID == null ||
104 queryLoadBalancerPoolID.equals(nsg.getLoadBalancerPoolID())) &&
105 (queryLoadBalancerPoolTenantID == null ||
106 queryLoadBalancerPoolTenantID.equals(nsg.getLoadBalancerPoolTenantID())) &&
107 (queryLoadBalancerPoolName == null ||
108 queryLoadBalancerPoolName.equals(nsg.getLoadBalancerPoolName())) &&
109 (queryLoadBalancerDescription == null ||
110 queryLoadBalancerDescription.equals(nsg.getLoadBalancerPoolDescription())) &&
111 (queryLoadBalancerPoolLbAlgorithm == null ||
112 queryLoadBalancerPoolLbAlgorithm.equals(nsg.getLoadBalancerPoolLbAlgorithm())) &&
113 (queryLoadBalancerPoolHealthMonitorID == null ||
114 queryLoadBalancerPoolHealthMonitorID.equals(nsg.getNeutronLoadBalancerPoolHealthMonitorID())) &&
115 (queryLoadBalancerIsAdminStateUp == null ||
116 queryLoadBalancerIsAdminStateUp.equals(nsg.getLoadBalancerPoolAdminIsStateIsUp())) &&
117 (queryLoadBalancerPoolStatus == null ||
118 queryLoadBalancerPoolStatus.equals(nsg.getLoadBalancerPoolStatus())) &&
119 (queryLoadBalancerPoolMembers.size() == 0 ||
120 queryLoadBalancerPoolMembers.equals(nsg.getLoadBalancerPoolMembers()))) {
121 if (fields.size() > 0) {
122 ans.add(extractFields(nsg,fields));
128 return Response.status(200).entity(
129 new NeutronLoadBalancerPoolRequest(ans)).build();
133 * Returns a specific LoadBalancerPool */
135 @Path("{loadBalancerPoolID}")
137 @Produces({ MediaType.APPLICATION_JSON })
139 @ResponseCode(code = 200, condition = "Operation successful"),
140 @ResponseCode(code = 401, condition = "Unauthorized"),
141 @ResponseCode(code = 404, condition = "Not Found"),
142 @ResponseCode(code = 501, condition = "Not Implemented") })
143 public Response showLoadBalancerPool(@PathParam("loadBalancerPoolID") String loadBalancerPoolID,
145 @QueryParam("fields") List<String> fields) {
146 INeutronLoadBalancerPoolCRUD loadBalancerPoolInterface = NeutronCRUDInterfaces.getINeutronLoadBalancerPoolCRUD(this);
147 if (loadBalancerPoolInterface == null) {
148 throw new ServiceUnavailableException("LoadBalancerPool CRUD Interface "
149 + RestMessages.SERVICEUNAVAILABLE.toString());
151 if (!loadBalancerPoolInterface.neutronLoadBalancerPoolExists(loadBalancerPoolID)) {
152 throw new ResourceNotFoundException("LoadBalancerPool UUID does not exist.");
154 if (fields.size() > 0) {
155 NeutronLoadBalancerPool ans = loadBalancerPoolInterface.getNeutronLoadBalancerPool(loadBalancerPoolID);
156 return Response.status(200).entity(
157 new NeutronLoadBalancerPoolRequest(extractFields(ans, fields))).build();
159 return Response.status(200).entity(new NeutronLoadBalancerPoolRequest(loadBalancerPoolInterface.getNeutronLoadBalancerPool(loadBalancerPoolID))).build();
164 * Creates new LoadBalancerPool */
167 @Produces({ MediaType.APPLICATION_JSON })
168 @Consumes({ MediaType.APPLICATION_JSON })
170 @ResponseCode(code = 201, condition = "Created"),
171 @ResponseCode(code = 400, condition = "Bad Request"),
172 @ResponseCode(code = 401, condition = "Unauthorized"),
173 @ResponseCode(code = 403, condition = "Forbidden"),
174 @ResponseCode(code = 404, condition = "Not Found"),
175 @ResponseCode(code = 409, condition = "Conflict"),
176 @ResponseCode(code = 501, condition = "Not Implemented") })
177 public Response createLoadBalancerPools(final NeutronLoadBalancerPoolRequest input) {
178 INeutronLoadBalancerPoolCRUD loadBalancerPoolInterface = NeutronCRUDInterfaces.getINeutronLoadBalancerPoolCRUD(this);
179 if (loadBalancerPoolInterface == null) {
180 throw new ServiceUnavailableException("LoadBalancerPool CRUD Interface "
181 + RestMessages.SERVICEUNAVAILABLE.toString());
183 if (input.isSingleton()) {
184 NeutronLoadBalancerPool singleton = input.getSingleton();
187 * Verify that the LoadBalancerPool doesn't already exist.
189 if (loadBalancerPoolInterface.neutronLoadBalancerPoolExists(singleton.getLoadBalancerPoolID())) {
190 throw new BadRequestException("LoadBalancerPool UUID already exists");
192 loadBalancerPoolInterface.addNeutronLoadBalancerPool(singleton);
194 Object[] instances = ServiceHelper.getGlobalInstances(INeutronLoadBalancerPoolAware.class, this, null);
195 if (instances != null) {
196 for (Object instance : instances) {
197 INeutronLoadBalancerPoolAware service = (INeutronLoadBalancerPoolAware) instance;
198 int status = service.canCreateNeutronLoadBalancerPool(singleton);
199 if (status < 200 || status > 299) {
200 return Response.status(status).build();
204 loadBalancerPoolInterface.addNeutronLoadBalancerPool(singleton);
205 if (instances != null) {
206 for (Object instance : instances) {
207 INeutronLoadBalancerPoolAware service = (INeutronLoadBalancerPoolAware) instance;
208 service.neutronLoadBalancerPoolCreated(singleton);
212 List<NeutronLoadBalancerPool> bulk = input.getBulk();
213 Iterator<NeutronLoadBalancerPool> i = bulk.iterator();
214 HashMap<String, NeutronLoadBalancerPool> testMap = new HashMap<String, NeutronLoadBalancerPool>();
215 Object[] instances = ServiceHelper.getGlobalInstances(INeutronLoadBalancerPoolAware.class, this, null);
216 while (i.hasNext()) {
217 NeutronLoadBalancerPool test = i.next();
220 * Verify that the firewall doesn't already exist
223 if (loadBalancerPoolInterface.neutronLoadBalancerPoolExists(test.getLoadBalancerPoolID())) {
224 throw new BadRequestException("Load Balancer Pool UUID already is already created");
226 if (testMap.containsKey(test.getLoadBalancerPoolID())) {
227 throw new BadRequestException("Load Balancer Pool UUID already exists");
229 if (instances != null) {
230 for (Object instance : instances) {
231 INeutronLoadBalancerPoolAware service = (INeutronLoadBalancerPoolAware) instance;
232 int status = service.canCreateNeutronLoadBalancerPool(test);
233 if (status < 200 || status > 299) {
234 return Response.status(status).build();
240 * now, each element of the bulk request can be added to the cache
243 while (i.hasNext()) {
244 NeutronLoadBalancerPool test = i.next();
245 loadBalancerPoolInterface.addNeutronLoadBalancerPool(test);
246 if (instances != null) {
247 for (Object instance : instances) {
248 INeutronLoadBalancerPoolAware service = (INeutronLoadBalancerPoolAware) instance;
249 service.neutronLoadBalancerPoolCreated(test);
254 return Response.status(201).entity(input).build();
258 * Updates a LoadBalancerPool Policy
260 @Path("{loadBalancerPoolID}")
262 @Produces({ MediaType.APPLICATION_JSON })
263 @Consumes({ MediaType.APPLICATION_JSON })
265 @ResponseCode(code = 200, condition = "Operation successful"),
266 @ResponseCode(code = 400, condition = "Bad Request"),
267 @ResponseCode(code = 401, condition = "Unauthorized"),
268 @ResponseCode(code = 403, condition = "Forbidden"),
269 @ResponseCode(code = 404, condition = "Not Found"),
270 @ResponseCode(code = 501, condition = "Not Implemented") })
271 public Response updateLoadBalancerPool(
272 @PathParam("loadBalancerPoolID") String loadBalancerPoolID, final NeutronLoadBalancerPoolRequest input) {
273 INeutronLoadBalancerPoolCRUD loadBalancerPoolInterface = NeutronCRUDInterfaces.getINeutronLoadBalancerPoolCRUD(this);
274 if (loadBalancerPoolInterface == null) {
275 throw new ServiceUnavailableException("LoadBalancerPool CRUD Interface "
276 + RestMessages.SERVICEUNAVAILABLE.toString());
280 * verify the LoadBalancerPool exists and there is only one delta provided
282 if (!loadBalancerPoolInterface.neutronLoadBalancerPoolExists(loadBalancerPoolID)) {
283 throw new ResourceNotFoundException("LoadBalancerPool UUID does not exist.");
285 if (!input.isSingleton()) {
286 throw new BadRequestException("Only singleton edit supported");
288 NeutronLoadBalancerPool delta = input.getSingleton();
289 NeutronLoadBalancerPool original = loadBalancerPoolInterface.getNeutronLoadBalancerPool(loadBalancerPoolID);
292 * updates restricted by Neutron
294 if (delta.getLoadBalancerPoolID() != null ||
295 delta.getLoadBalancerPoolTenantID() != null ||
296 delta.getLoadBalancerPoolName() != null ||
297 delta.getLoadBalancerPoolDescription() != null ||
298 delta.getLoadBalancerPoolProtocol() != null ||
299 delta.getLoadBalancerPoolLbAlgorithm() != null ||
300 delta.getNeutronLoadBalancerPoolHealthMonitorID() != null ||
301 delta.getLoadBalancerPoolAdminIsStateIsUp() != null ||
302 delta.getLoadBalancerPoolStatus() != null ||
303 delta.getLoadBalancerPoolMembers() != null) {
304 throw new BadRequestException("Attribute edit blocked by Neutron");
307 Object[] instances = ServiceHelper.getGlobalInstances(INeutronLoadBalancerPoolAware.class, this, null);
308 if (instances != null) {
309 for (Object instance : instances) {
310 INeutronLoadBalancerPoolAware service = (INeutronLoadBalancerPoolAware) instance;
311 int status = service.canUpdateNeutronLoadBalancerPool(delta, original);
312 if (status < 200 || status > 299) {
313 return Response.status(status).build();
319 * update the object and return it
321 loadBalancerPoolInterface.updateNeutronLoadBalancerPool(loadBalancerPoolID, delta);
322 NeutronLoadBalancerPool updatedLoadBalancerPool = loadBalancerPoolInterface.getNeutronLoadBalancerPool(loadBalancerPoolID);
323 if (instances != null) {
324 for (Object instance : instances) {
325 INeutronLoadBalancerPoolAware service = (INeutronLoadBalancerPoolAware) instance;
326 service.neutronLoadBalancerPoolUpdated(updatedLoadBalancerPool);
329 return Response.status(200).entity(new NeutronLoadBalancerPoolRequest(loadBalancerPoolInterface.getNeutronLoadBalancerPool(loadBalancerPoolID))).build();