Merge "Make neutron a simple osgi app"
[controller.git] / opendaylight / networkconfiguration / neutron / northbound / src / main / java / org / opendaylight / controller / networkconfig / neutron / northbound / 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.controller.networkconfig.neutron.northbound;
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.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;
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
69     public Response listGroups(
70             // return fields
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,
80             // pagination
81             @QueryParam("limit") String limit,
82             @QueryParam("marker") String marker,
83             @QueryParam("page_reverse") String pageReverse
84             // sorting not supported
85     ) {
86         INeutronLoadBalancerCRUD loadBalancerInterface = NeutronCRUDInterfaces.getINeutronLoadBalancerCRUD(this);
87
88         if (loadBalancerInterface == null) {
89             throw new ServiceUnavailableException("LoadBalancer CRUD Interface "
90                     + RestMessages.SERVICEUNAVAILABLE.toString());
91         }
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();
97         while (i.hasNext()) {
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));
113                 } else {
114                     ans.add(nsg);
115                 }
116             }
117         }
118         return Response.status(200).entity(
119                 new NeutronLoadBalancerRequest(ans)).build();
120     }
121
122     /**
123      * Returns a specific LoadBalancer */
124
125     @Path("{loadBalancerID}")
126     @GET
127     @Produces({ MediaType.APPLICATION_JSON })
128
129     @StatusCodes({
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,
135             // return fields
136             @QueryParam("fields") List<String> fields) {
137         INeutronLoadBalancerCRUD loadBalancerInterface = NeutronCRUDInterfaces.getINeutronLoadBalancerCRUD(
138                 this);
139         if (loadBalancerInterface == null) {
140             throw new ServiceUnavailableException("LoadBalancer CRUD Interface "
141                     + RestMessages.SERVICEUNAVAILABLE.toString());
142         }
143         if (!loadBalancerInterface.neutronLoadBalancerExists(loadBalancerID)) {
144             throw new ResourceNotFoundException("LoadBalancer UUID does not exist.");
145         }
146         if (fields.size() > 0) {
147             NeutronLoadBalancer ans = loadBalancerInterface.getNeutronLoadBalancer(loadBalancerID);
148             return Response.status(200).entity(
149                     new NeutronLoadBalancerRequest(extractFields(ans, fields))).build();
150         } else {
151             return Response.status(200).entity(new NeutronLoadBalancerRequest(loadBalancerInterface.getNeutronLoadBalancer(
152                     loadBalancerID))).build();
153         }
154     }
155
156     /**
157      * Creates new LoadBalancer */
158
159     @POST
160     @Produces({ MediaType.APPLICATION_JSON })
161     @Consumes({ MediaType.APPLICATION_JSON })
162
163     @StatusCodes({
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(
173                 this);
174         if (loadBalancerInterface == null) {
175             throw new ServiceUnavailableException("LoadBalancer CRUD Interface "
176                     + RestMessages.SERVICEUNAVAILABLE.toString());
177         }
178         if (input.isSingleton()) {
179             NeutronLoadBalancer singleton = input.getSingleton();
180
181             /*
182              *  Verify that the LoadBalancer doesn't already exist.
183              */
184             if (loadBalancerInterface.neutronLoadBalancerExists(singleton.getLoadBalancerID())) {
185                 throw new BadRequestException("LoadBalancer UUID already exists");
186             }
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();
194                     }
195                 }
196             }
197             loadBalancerInterface.addNeutronLoadBalancer(singleton);
198             if (instances != null) {
199                 for (Object instance : instances) {
200                     INeutronLoadBalancerAware service = (INeutronLoadBalancerAware) instance;
201                     service.neutronLoadBalancerCreated(singleton);
202                 }
203             }
204         } else {
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();
211
212                 /*
213                  *  Verify that the loadbalancer doesn't already exist
214                  */
215
216                 if (loadBalancerInterface.neutronLoadBalancerExists(test.getLoadBalancerID())) {
217                     throw new BadRequestException("Load Balancer Pool UUID already is already created");
218                 }
219                 if (testMap.containsKey(test.getLoadBalancerID())) {
220                     throw new BadRequestException("Load Balancer Pool UUID already exists");
221                 }
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();
228                         }
229                     }
230                 }
231             }
232             /*
233              * now, each element of the bulk request can be added to the cache
234              */
235             i = bulk.iterator();
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);
243                     }
244                 }
245             }
246         }
247         return Response.status(201).entity(input).build();
248     }
249
250     /**
251      * Updates a LoadBalancer Policy
252      */
253     @Path("{loadBalancerID}")
254     @PUT
255     @Produces({ MediaType.APPLICATION_JSON })
256     @Consumes({ MediaType.APPLICATION_JSON })
257
258     @StatusCodes({
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(
268                 this);
269         if (loadBalancerInterface == null) {
270             throw new ServiceUnavailableException("LoadBalancer CRUD Interface "
271                     + RestMessages.SERVICEUNAVAILABLE.toString());
272         }
273
274         /*
275          * verify the LoadBalancer exists and there is only one delta provided
276          */
277         if (!loadBalancerInterface.neutronLoadBalancerExists(loadBalancerID)) {
278             throw new ResourceNotFoundException("LoadBalancer UUID does not exist.");
279         }
280         if (!input.isSingleton()) {
281             throw new BadRequestException("Only singleton edit supported");
282         }
283         NeutronLoadBalancer delta = input.getSingleton();
284         NeutronLoadBalancer original = loadBalancerInterface.getNeutronLoadBalancer(loadBalancerID);
285
286         /*
287          * updates restricted by Neutron
288          */
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");
297         }
298
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();
306                 }
307             }
308         }
309
310         /*
311          * update the object and return it
312          */
313         loadBalancerInterface.updateNeutronLoadBalancer(loadBalancerID, delta);
314         NeutronLoadBalancer updatedLoadBalancer = loadBalancerInterface.getNeutronLoadBalancer(
315                 loadBalancerID);
316         if (instances != null) {
317             for (Object instance : instances) {
318                 INeutronLoadBalancerAware service = (INeutronLoadBalancerAware) instance;
319                 service.neutronLoadBalancerUpdated(updatedLoadBalancer);
320             }
321         }
322         return Response.status(200).entity(new NeutronLoadBalancerRequest(loadBalancerInterface.getNeutronLoadBalancer(
323                 loadBalancerID))).build();
324     }
325
326     /**
327      * Deletes a LoadBalancer */
328
329     @Path("{loadBalancerID}")
330     @DELETE
331     @StatusCodes({
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(
340                 this);
341         if (loadBalancerInterface == null) {
342             throw new ServiceUnavailableException("LoadBalancer CRUD Interface "
343                     + RestMessages.SERVICEUNAVAILABLE.toString());
344         }
345
346         /*
347          * verify the LoadBalancer exists and it isn't currently in use
348          */
349         if (!loadBalancerInterface.neutronLoadBalancerExists(loadBalancerID)) {
350             throw new ResourceNotFoundException("LoadBalancer UUID does not exist.");
351         }
352         if (loadBalancerInterface.neutronLoadBalancerInUse(loadBalancerID)) {
353             return Response.status(409).build();
354         }
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();
363                 }
364             }
365         }
366
367         loadBalancerInterface.removeNeutronLoadBalancer(loadBalancerID);
368         if (instances != null) {
369             for (Object instance : instances) {
370                 INeutronLoadBalancerAware service = (INeutronLoadBalancerAware) instance;
371                 service.neutronLoadBalancerDeleted(singleton);
372             }
373         }
374         return Response.status(204).build();
375     }
376 }