Merge "Startup archetype: remove 'Impl' from config subsystem Module name."
[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                 if (instances.length > 0) {
190                     for (Object instance : instances) {
191                         INeutronLoadBalancerAware service = (INeutronLoadBalancerAware) instance;
192                         int status = service.canCreateNeutronLoadBalancer(singleton);
193                         if (status < 200 || status > 299) {
194                             return Response.status(status).build();
195                         }
196                     }
197                 } else {
198                     throw new ServiceUnavailableException("No providers registered.  Please try again later");
199                 }
200             } else {
201                 throw new ServiceUnavailableException("Couldn't get providers list.  Please try again later");
202             }
203
204             loadBalancerInterface.addNeutronLoadBalancer(singleton);
205             if (instances != null) {
206                 for (Object instance : instances) {
207                     INeutronLoadBalancerAware service = (INeutronLoadBalancerAware) instance;
208                     service.neutronLoadBalancerCreated(singleton);
209                 }
210             }
211         } else {
212             List<NeutronLoadBalancer> bulk = input.getBulk();
213             Iterator<NeutronLoadBalancer> i = bulk.iterator();
214             HashMap<String, NeutronLoadBalancer> testMap = new HashMap<String, NeutronLoadBalancer>();
215             Object[] instances = NeutronUtil.getInstances(INeutronLoadBalancerAware.class, this);
216             while (i.hasNext()) {
217                 NeutronLoadBalancer test = i.next();
218
219                 /*
220                  *  Verify that the loadbalancer doesn't already exist
221                  */
222
223                 if (loadBalancerInterface.neutronLoadBalancerExists(test.getLoadBalancerID())) {
224                     throw new BadRequestException("Load Balancer Pool UUID already is already created");
225                 }
226                 if (testMap.containsKey(test.getLoadBalancerID())) {
227                     throw new BadRequestException("Load Balancer Pool UUID already exists");
228                 }
229                 if (instances != null) {
230                     if (instances.length > 0) {
231                         for (Object instance : instances) {
232                             INeutronLoadBalancerAware service = (INeutronLoadBalancerAware) instance;
233                             int status = service.canCreateNeutronLoadBalancer(test);
234                             if (status < 200 || status > 299) {
235                                 return Response.status(status).build();
236                             }
237                         }
238                     } else {
239                         throw new ServiceUnavailableException("No providers registered.  Please try again later");
240                     }
241                 } else {
242                     throw new ServiceUnavailableException("Couldn't get providers list.  Please try again later");
243                 }
244             }
245             /*
246              * now, each element of the bulk request can be added to the cache
247              */
248             i = bulk.iterator();
249             while (i.hasNext()) {
250                 NeutronLoadBalancer test = i.next();
251                 loadBalancerInterface.addNeutronLoadBalancer(test);
252                 if (instances != null) {
253                     for (Object instance : instances) {
254                         INeutronLoadBalancerAware service = (INeutronLoadBalancerAware) instance;
255                         service.neutronLoadBalancerCreated(test);
256                     }
257                 }
258             }
259         }
260         return Response.status(201).entity(input).build();
261     }
262
263     /**
264      * Updates a LoadBalancer Policy
265      */
266     @Path("{loadBalancerID}")
267     @PUT
268     @Produces({ MediaType.APPLICATION_JSON })
269     @Consumes({ MediaType.APPLICATION_JSON })
270
271     @StatusCodes({
272             @ResponseCode(code = 200, condition = "Operation successful"),
273             @ResponseCode(code = 400, condition = "Bad Request"),
274             @ResponseCode(code = 401, condition = "Unauthorized"),
275             @ResponseCode(code = 403, condition = "Forbidden"),
276             @ResponseCode(code = 404, condition = "Not Found"),
277             @ResponseCode(code = 501, condition = "Not Implemented") })
278     public Response updateLoadBalancer(
279             @PathParam("loadBalancerID") String loadBalancerID, final NeutronLoadBalancerRequest input) {
280         INeutronLoadBalancerCRUD loadBalancerInterface = NeutronCRUDInterfaces.getINeutronLoadBalancerCRUD(
281                 this);
282         if (loadBalancerInterface == null) {
283             throw new ServiceUnavailableException("LoadBalancer CRUD Interface "
284                     + RestMessages.SERVICEUNAVAILABLE.toString());
285         }
286
287         /*
288          * verify the LoadBalancer exists and there is only one delta provided
289          */
290         if (!loadBalancerInterface.neutronLoadBalancerExists(loadBalancerID)) {
291             throw new ResourceNotFoundException("LoadBalancer UUID does not exist.");
292         }
293         if (!input.isSingleton()) {
294             throw new BadRequestException("Only singleton edit supported");
295         }
296         NeutronLoadBalancer delta = input.getSingleton();
297         NeutronLoadBalancer original = loadBalancerInterface.getNeutronLoadBalancer(loadBalancerID);
298
299         /*
300          * updates restricted by Neutron
301          */
302         if (delta.getLoadBalancerID() != null ||
303                 delta.getLoadBalancerTenantID() != null ||
304                 delta.getLoadBalancerName() != null ||
305                 delta.getLoadBalancerDescription() != null ||
306                 delta.getLoadBalancerStatus() != null ||
307                 delta.getLoadBalancerVipAddress() != null ||
308                 delta.getLoadBalancerVipSubnetID() != null) {
309             throw new BadRequestException("Attribute edit blocked by Neutron");
310         }
311
312         Object[] instances = NeutronUtil.getInstances(INeutronLoadBalancerAware.class, this);
313         if (instances != null) {
314             if (instances.length > 0) {
315                 for (Object instance : instances) {
316                     INeutronLoadBalancerAware service = (INeutronLoadBalancerAware) instance;
317                     int status = service.canUpdateNeutronLoadBalancer(delta, original);
318                     if (status < 200 || status > 299) {
319                         return Response.status(status).build();
320                     }
321                 }
322             } else {
323                 throw new ServiceUnavailableException("No providers registered.  Please try again later");
324             }
325         } else {
326             throw new ServiceUnavailableException("Couldn't get providers list.  Please try again later");
327         }
328
329         /*
330          * update the object and return it
331          */
332         loadBalancerInterface.updateNeutronLoadBalancer(loadBalancerID, delta);
333         NeutronLoadBalancer updatedLoadBalancer = loadBalancerInterface.getNeutronLoadBalancer(
334                 loadBalancerID);
335         if (instances != null) {
336             for (Object instance : instances) {
337                 INeutronLoadBalancerAware service = (INeutronLoadBalancerAware) instance;
338                 service.neutronLoadBalancerUpdated(updatedLoadBalancer);
339             }
340         }
341         return Response.status(200).entity(new NeutronLoadBalancerRequest(loadBalancerInterface.getNeutronLoadBalancer(
342                 loadBalancerID))).build();
343     }
344
345     /**
346      * Deletes a LoadBalancer */
347
348     @Path("{loadBalancerID}")
349     @DELETE
350     @StatusCodes({
351             @ResponseCode(code = 204, condition = "No Content"),
352             @ResponseCode(code = 401, condition = "Unauthorized"),
353             @ResponseCode(code = 404, condition = "Not Found"),
354             @ResponseCode(code = 409, condition = "Conflict"),
355             @ResponseCode(code = 501, condition = "Not Implemented") })
356     public Response deleteLoadBalancer(
357             @PathParam("loadBalancerID") String loadBalancerID) {
358         INeutronLoadBalancerCRUD loadBalancerInterface = NeutronCRUDInterfaces.getINeutronLoadBalancerCRUD(
359                 this);
360         if (loadBalancerInterface == null) {
361             throw new ServiceUnavailableException("LoadBalancer CRUD Interface "
362                     + RestMessages.SERVICEUNAVAILABLE.toString());
363         }
364
365         /*
366          * verify the LoadBalancer exists and it isn't currently in use
367          */
368         if (!loadBalancerInterface.neutronLoadBalancerExists(loadBalancerID)) {
369             throw new ResourceNotFoundException("LoadBalancer UUID does not exist.");
370         }
371         if (loadBalancerInterface.neutronLoadBalancerInUse(loadBalancerID)) {
372             return Response.status(409).build();
373         }
374         NeutronLoadBalancer singleton = loadBalancerInterface.getNeutronLoadBalancer(loadBalancerID);
375         Object[] instances = NeutronUtil.getInstances(INeutronLoadBalancerAware.class, this);
376         if (instances != null) {
377             if (instances.length > 0) {
378                 for (Object instance : instances) {
379                     INeutronLoadBalancerAware service = (INeutronLoadBalancerAware) instance;
380                     int status = service.canDeleteNeutronLoadBalancer(singleton);
381                     if (status < 200 || status > 299) {
382                         return Response.status(status).build();
383                     }
384                 }
385             } else {
386                 throw new ServiceUnavailableException("No providers registered.  Please try again later");
387             }
388         } else {
389             throw new ServiceUnavailableException("Couldn't get providers list.  Please try again later");
390         }
391
392
393         loadBalancerInterface.removeNeutronLoadBalancer(loadBalancerID);
394         if (instances != null) {
395             for (Object instance : instances) {
396                 INeutronLoadBalancerAware service = (INeutronLoadBalancerAware) instance;
397                 service.neutronLoadBalancerDeleted(singleton);
398             }
399         }
400         return Response.status(204).build();
401     }
402 }