Fixed package directory issues.
[neutron.git] / northbound-api / src / main / java / org / opendaylight / neutron / northbound / api / NeutronLoadBalancerNorthbound.java
1 /*
2  * Copyright (C) 2014 Red Hat, Inc.
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.neutron.northbound.api;
10
11
12 import java.util.ArrayList;
13 import java.util.HashMap;
14 import java.util.Iterator;
15 import java.util.List;
16
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;
28
29 import org.codehaus.enunciate.jaxrs.ResponseCode;
30 import org.codehaus.enunciate.jaxrs.StatusCodes;
31 import org.opendaylight.neutron.spi.INeutronLoadBalancerAware;
32 import org.opendaylight.neutron.spi.INeutronLoadBalancerCRUD;
33 import org.opendaylight.neutron.spi.NeutronCRUDInterfaces;
34 import org.opendaylight.neutron.spi.NeutronLoadBalancer;
35
36 /**
37  * Neutron Northbound REST APIs for LoadBalancers.<br>
38  * This class provides REST APIs for managing neutron LoadBalancers
39  *
40  * <br>
41  * <br>
42  * Authentication scheme : <b>HTTP Basic</b><br>
43  * Authentication realm : <b>opendaylight</b><br>
44  * Transport : <b>HTTP and HTTPS</b><br>
45  * <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>
49  * More info :
50  * http://tomcat.apache.org/tomcat-7.0-doc/ssl-howto.html#Configuration
51  *
52  */
53 @Path("/loadbalancers")
54 public class NeutronLoadBalancerNorthbound {
55
56     private NeutronLoadBalancer extractFields(NeutronLoadBalancer o, List<String> fields) {
57         return o.extractFields(fields);
58     }
59
60     /**
61      * Returns a list of all LoadBalancer */
62     @GET
63     @Produces({ MediaType.APPLICATION_JSON })
64     @StatusCodes({
65             @ResponseCode(code = 200, condition = "Operation successful"),
66             @ResponseCode(code = 401, condition = "Unauthorized"),
67             @ResponseCode(code = 501, condition = "Not Implemented"),
68             @ResponseCode(code = 503, condition = "No providers available") })
69
70     public Response listGroups(
71             // return fields
72             @QueryParam("fields") List<String> fields,
73             // OpenStack LoadBalancer attributes
74             @QueryParam("id") String queryLoadBalancerID,
75             @QueryParam("tenant_id") String queryLoadBalancerTenantID,
76             @QueryParam("name") String queryLoadBalancerName,
77             @QueryParam("description") String queryLoadBalancerDescription,
78             @QueryParam("status") String queryLoadBalancerStatus,
79             @QueryParam("vip_address") String queryLoadBalancerVipAddress,
80             @QueryParam("vip_subnet") String queryLoadBalancerVipSubnet,
81             // pagination
82             @QueryParam("limit") String limit,
83             @QueryParam("marker") String marker,
84             @QueryParam("page_reverse") String pageReverse
85             // sorting not supported
86     ) {
87         INeutronLoadBalancerCRUD loadBalancerInterface = NeutronCRUDInterfaces.getINeutronLoadBalancerCRUD(this);
88
89         if (loadBalancerInterface == null) {
90             throw new ServiceUnavailableException("LoadBalancer CRUD Interface "
91                     + RestMessages.SERVICEUNAVAILABLE.toString());
92         }
93         List<NeutronLoadBalancer> allLoadBalancers = loadBalancerInterface.getAllNeutronLoadBalancers();
94         //        List<NeutronLoadBalancerRule> allLoadBalancerRules = firewallRuleInterface.getAllNeutronLoadBalancerRules();
95         List<NeutronLoadBalancer> ans = new ArrayList<NeutronLoadBalancer>();
96         //        List<NeutronLoadBalancerRule> rules = new ArrayList<NeutronLoadBalancerRule>();
97         Iterator<NeutronLoadBalancer> i = allLoadBalancers.iterator();
98         while (i.hasNext()) {
99             NeutronLoadBalancer nsg = i.next();
100             if ((queryLoadBalancerID == null ||
101                     queryLoadBalancerID.equals(nsg.getLoadBalancerID())) &&
102                     (queryLoadBalancerTenantID == null ||
103                             queryLoadBalancerTenantID.equals(nsg.getLoadBalancerTenantID())) &&
104                     (queryLoadBalancerName == null ||
105                             queryLoadBalancerName.equals(nsg.getLoadBalancerName())) &&
106                     (queryLoadBalancerDescription == null ||
107                             queryLoadBalancerDescription.equals(nsg.getLoadBalancerDescription())) &&
108                     (queryLoadBalancerVipAddress == null ||
109                             queryLoadBalancerVipAddress.equals(nsg.getLoadBalancerVipAddress())) &&
110                     (queryLoadBalancerVipSubnet == null ||
111                             queryLoadBalancerVipSubnet.equals(nsg.getLoadBalancerVipSubnetID()))) {
112                 if (fields.size() > 0) {
113                     ans.add(extractFields(nsg,fields));
114                 } else {
115                     ans.add(nsg);
116                 }
117             }
118         }
119         return Response.status(200).entity(
120                 new NeutronLoadBalancerRequest(ans)).build();
121     }
122
123     /**
124      * Returns a specific LoadBalancer */
125
126     @Path("{loadBalancerID}")
127     @GET
128     @Produces({ MediaType.APPLICATION_JSON })
129
130     @StatusCodes({
131             @ResponseCode(code = 200, condition = "Operation successful"),
132             @ResponseCode(code = 401, condition = "Unauthorized"),
133             @ResponseCode(code = 404, condition = "Not Found"),
134             @ResponseCode(code = 501, condition = "Not Implemented"),
135             @ResponseCode(code = 503, condition = "No providers available") })
136     public Response showLoadBalancer(@PathParam("loadBalancerID") String loadBalancerID,
137             // return fields
138             @QueryParam("fields") List<String> fields) {
139         INeutronLoadBalancerCRUD loadBalancerInterface = NeutronCRUDInterfaces.getINeutronLoadBalancerCRUD(
140                 this);
141         if (loadBalancerInterface == null) {
142             throw new ServiceUnavailableException("LoadBalancer CRUD Interface "
143                     + RestMessages.SERVICEUNAVAILABLE.toString());
144         }
145         if (!loadBalancerInterface.neutronLoadBalancerExists(loadBalancerID)) {
146             throw new ResourceNotFoundException("LoadBalancer UUID does not exist.");
147         }
148         if (fields.size() > 0) {
149             NeutronLoadBalancer ans = loadBalancerInterface.getNeutronLoadBalancer(loadBalancerID);
150             return Response.status(200).entity(
151                     new NeutronLoadBalancerRequest(extractFields(ans, fields))).build();
152         } else {
153             return Response.status(200).entity(new NeutronLoadBalancerRequest(loadBalancerInterface.getNeutronLoadBalancer(
154                     loadBalancerID))).build();
155         }
156     }
157
158     /**
159      * Creates new LoadBalancer */
160
161     @POST
162     @Produces({ MediaType.APPLICATION_JSON })
163     @Consumes({ MediaType.APPLICATION_JSON })
164
165     @StatusCodes({
166             @ResponseCode(code = 201, condition = "Created"),
167             @ResponseCode(code = 400, condition = "Bad Request"),
168             @ResponseCode(code = 401, condition = "Unauthorized"),
169             @ResponseCode(code = 403, condition = "Forbidden"),
170             @ResponseCode(code = 404, condition = "Not Found"),
171             @ResponseCode(code = 409, condition = "Conflict"),
172             @ResponseCode(code = 501, condition = "Not Implemented"),
173             @ResponseCode(code = 503, condition = "No providers available") })
174     public Response createLoadBalancers(final NeutronLoadBalancerRequest input) {
175         INeutronLoadBalancerCRUD loadBalancerInterface = NeutronCRUDInterfaces.getINeutronLoadBalancerCRUD(
176                 this);
177         if (loadBalancerInterface == null) {
178             throw new ServiceUnavailableException("LoadBalancer CRUD Interface "
179                     + RestMessages.SERVICEUNAVAILABLE.toString());
180         }
181         if (input.isSingleton()) {
182             NeutronLoadBalancer singleton = input.getSingleton();
183
184             /*
185              *  Verify that the LoadBalancer doesn't already exist.
186              */
187             if (loadBalancerInterface.neutronLoadBalancerExists(singleton.getLoadBalancerID())) {
188                 throw new BadRequestException("LoadBalancer UUID already exists");
189             }
190             Object[] instances = NeutronUtil.getInstances(INeutronLoadBalancerAware.class, this);
191             if (instances != null) {
192                 if (instances.length > 0) {
193                     for (Object instance : instances) {
194                         INeutronLoadBalancerAware service = (INeutronLoadBalancerAware) instance;
195                         int status = service.canCreateNeutronLoadBalancer(singleton);
196                         if (status < 200 || status > 299) {
197                             return Response.status(status).build();
198                         }
199                     }
200                 } else {
201                     throw new ServiceUnavailableException("No providers registered.  Please try again later");
202                 }
203             } else {
204                 throw new ServiceUnavailableException("Couldn't get providers list.  Please try again later");
205             }
206
207             loadBalancerInterface.addNeutronLoadBalancer(singleton);
208             if (instances != null) {
209                 for (Object instance : instances) {
210                     INeutronLoadBalancerAware service = (INeutronLoadBalancerAware) instance;
211                     service.neutronLoadBalancerCreated(singleton);
212                 }
213             }
214         } else {
215             List<NeutronLoadBalancer> bulk = input.getBulk();
216             Iterator<NeutronLoadBalancer> i = bulk.iterator();
217             HashMap<String, NeutronLoadBalancer> testMap = new HashMap<String, NeutronLoadBalancer>();
218             Object[] instances = NeutronUtil.getInstances(INeutronLoadBalancerAware.class, this);
219             while (i.hasNext()) {
220                 NeutronLoadBalancer test = i.next();
221
222                 /*
223                  *  Verify that the loadbalancer doesn't already exist
224                  */
225
226                 if (loadBalancerInterface.neutronLoadBalancerExists(test.getLoadBalancerID())) {
227                     throw new BadRequestException("Load Balancer Pool UUID already is already created");
228                 }
229                 if (testMap.containsKey(test.getLoadBalancerID())) {
230                     throw new BadRequestException("Load Balancer Pool UUID already exists");
231                 }
232                 if (instances != null) {
233                     if (instances.length > 0) {
234                         for (Object instance : instances) {
235                             INeutronLoadBalancerAware service = (INeutronLoadBalancerAware) instance;
236                             int status = service.canCreateNeutronLoadBalancer(test);
237                             if (status < 200 || status > 299) {
238                                 return Response.status(status).build();
239                             }
240                         }
241                     } else {
242                         throw new ServiceUnavailableException("No providers registered.  Please try again later");
243                     }
244                 } else {
245                     throw new ServiceUnavailableException("Couldn't get providers list.  Please try again later");
246                 }
247             }
248             /*
249              * now, each element of the bulk request can be added to the cache
250              */
251             i = bulk.iterator();
252             while (i.hasNext()) {
253                 NeutronLoadBalancer test = i.next();
254                 loadBalancerInterface.addNeutronLoadBalancer(test);
255                 if (instances != null) {
256                     for (Object instance : instances) {
257                         INeutronLoadBalancerAware service = (INeutronLoadBalancerAware) instance;
258                         service.neutronLoadBalancerCreated(test);
259                     }
260                 }
261             }
262         }
263         return Response.status(201).entity(input).build();
264     }
265
266     /**
267      * Updates a LoadBalancer Policy
268      */
269     @Path("{loadBalancerID}")
270     @PUT
271     @Produces({ MediaType.APPLICATION_JSON })
272     @Consumes({ MediaType.APPLICATION_JSON })
273
274     @StatusCodes({
275             @ResponseCode(code = 200, condition = "Operation successful"),
276             @ResponseCode(code = 400, condition = "Bad Request"),
277             @ResponseCode(code = 401, condition = "Unauthorized"),
278             @ResponseCode(code = 403, condition = "Forbidden"),
279             @ResponseCode(code = 404, condition = "Not Found"),
280             @ResponseCode(code = 501, condition = "Not Implemented"),
281             @ResponseCode(code = 503, condition = "No providers available") })
282     public Response updateLoadBalancer(
283             @PathParam("loadBalancerID") String loadBalancerID, final NeutronLoadBalancerRequest input) {
284         INeutronLoadBalancerCRUD loadBalancerInterface = NeutronCRUDInterfaces.getINeutronLoadBalancerCRUD(
285                 this);
286         if (loadBalancerInterface == null) {
287             throw new ServiceUnavailableException("LoadBalancer CRUD Interface "
288                     + RestMessages.SERVICEUNAVAILABLE.toString());
289         }
290
291         /*
292          * verify the LoadBalancer exists and there is only one delta provided
293          */
294         if (!loadBalancerInterface.neutronLoadBalancerExists(loadBalancerID)) {
295             throw new ResourceNotFoundException("LoadBalancer UUID does not exist.");
296         }
297         if (!input.isSingleton()) {
298             throw new BadRequestException("Only singleton edit supported");
299         }
300         NeutronLoadBalancer delta = input.getSingleton();
301         NeutronLoadBalancer original = loadBalancerInterface.getNeutronLoadBalancer(loadBalancerID);
302
303         /*
304          * updates restricted by Neutron
305          */
306         if (delta.getLoadBalancerID() != null ||
307                 delta.getLoadBalancerTenantID() != null ||
308                 delta.getLoadBalancerName() != null ||
309                 delta.getLoadBalancerDescription() != null ||
310                 delta.getLoadBalancerStatus() != null ||
311                 delta.getLoadBalancerVipAddress() != null ||
312                 delta.getLoadBalancerVipSubnetID() != null) {
313             throw new BadRequestException("Attribute edit blocked by Neutron");
314         }
315
316         Object[] instances = NeutronUtil.getInstances(INeutronLoadBalancerAware.class, this);
317         if (instances != null) {
318             if (instances.length > 0) {
319                 for (Object instance : instances) {
320                     INeutronLoadBalancerAware service = (INeutronLoadBalancerAware) instance;
321                     int status = service.canUpdateNeutronLoadBalancer(delta, original);
322                     if (status < 200 || status > 299) {
323                         return Response.status(status).build();
324                     }
325                 }
326             } else {
327                 throw new ServiceUnavailableException("No providers registered.  Please try again later");
328             }
329         } else {
330             throw new ServiceUnavailableException("Couldn't get providers list.  Please try again later");
331         }
332
333         /*
334          * update the object and return it
335          */
336         loadBalancerInterface.updateNeutronLoadBalancer(loadBalancerID, delta);
337         NeutronLoadBalancer updatedLoadBalancer = loadBalancerInterface.getNeutronLoadBalancer(
338                 loadBalancerID);
339         if (instances != null) {
340             for (Object instance : instances) {
341                 INeutronLoadBalancerAware service = (INeutronLoadBalancerAware) instance;
342                 service.neutronLoadBalancerUpdated(updatedLoadBalancer);
343             }
344         }
345         return Response.status(200).entity(new NeutronLoadBalancerRequest(loadBalancerInterface.getNeutronLoadBalancer(
346                 loadBalancerID))).build();
347     }
348
349     /**
350      * Deletes a LoadBalancer */
351
352     @Path("{loadBalancerID}")
353     @DELETE
354     @StatusCodes({
355             @ResponseCode(code = 204, condition = "No Content"),
356             @ResponseCode(code = 401, condition = "Unauthorized"),
357             @ResponseCode(code = 404, condition = "Not Found"),
358             @ResponseCode(code = 409, condition = "Conflict"),
359             @ResponseCode(code = 501, condition = "Not Implemented"),
360             @ResponseCode(code = 503, condition = "No providers available") })
361     public Response deleteLoadBalancer(
362             @PathParam("loadBalancerID") String loadBalancerID) {
363         INeutronLoadBalancerCRUD loadBalancerInterface = NeutronCRUDInterfaces.getINeutronLoadBalancerCRUD(
364                 this);
365         if (loadBalancerInterface == null) {
366             throw new ServiceUnavailableException("LoadBalancer CRUD Interface "
367                     + RestMessages.SERVICEUNAVAILABLE.toString());
368         }
369
370         /*
371          * verify the LoadBalancer exists and it isn't currently in use
372          */
373         if (!loadBalancerInterface.neutronLoadBalancerExists(loadBalancerID)) {
374             throw new ResourceNotFoundException("LoadBalancer UUID does not exist.");
375         }
376         if (loadBalancerInterface.neutronLoadBalancerInUse(loadBalancerID)) {
377             return Response.status(409).build();
378         }
379         NeutronLoadBalancer singleton = loadBalancerInterface.getNeutronLoadBalancer(loadBalancerID);
380         Object[] instances = NeutronUtil.getInstances(INeutronLoadBalancerAware.class, this);
381         if (instances != null) {
382             if (instances.length > 0) {
383                 for (Object instance : instances) {
384                     INeutronLoadBalancerAware service = (INeutronLoadBalancerAware) instance;
385                     int status = service.canDeleteNeutronLoadBalancer(singleton);
386                     if (status < 200 || status > 299) {
387                         return Response.status(status).build();
388                     }
389                 }
390             } else {
391                 throw new ServiceUnavailableException("No providers registered.  Please try again later");
392             }
393         } else {
394             throw new ServiceUnavailableException("Couldn't get providers list.  Please try again later");
395         }
396
397
398         loadBalancerInterface.removeNeutronLoadBalancer(loadBalancerID);
399         if (instances != null) {
400             for (Object instance : instances) {
401                 INeutronLoadBalancerAware service = (INeutronLoadBalancerAware) instance;
402                 service.neutronLoadBalancerDeleted(singleton);
403             }
404         }
405         return Response.status(204).build();
406     }
407 }