Merge "Bug 4224 - remote_ip_prefix of metering rule should be type of ip-prefix"
[neutron.git] / northbound-api / src / main / java / org / opendaylight / neutron / northbound / api / NeutronLoadBalancerNorthbound.java
1 /*
2  * Copyright (c) 2014, 2015 Red Hat, Inc. and others.  All rights reserved.
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 import java.net.HttpURLConnection;
12
13 import java.util.ArrayList;
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("/lbaas/loadbalancers")
54 public class NeutronLoadBalancerNorthbound extends AbstractNeutronNorthbound {
55
56     private static final String RESOURCE_NAME = "LoadBalancer";
57
58     private NeutronLoadBalancer extractFields(NeutronLoadBalancer o, List<String> fields) {
59         return o.extractFields(fields);
60     }
61
62     private NeutronCRUDInterfaces getNeutronInterfaces() {
63         NeutronCRUDInterfaces answer = new NeutronCRUDInterfaces().fetchINeutronLoadBalancerCRUD(this);
64         if (answer.getLoadBalancerInterface() == null) {
65             throw new ServiceUnavailableException(serviceUnavailable(RESOURCE_NAME));
66         }
67         return answer;
68     }
69
70     /**
71      * Returns a list of all LoadBalancer */
72     @GET
73     @Produces({ MediaType.APPLICATION_JSON })
74     @StatusCodes({
75             @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
76             @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
77             @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
78             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
79
80     public Response listGroups(
81             // return fields
82             @QueryParam("fields") List<String> fields,
83             // OpenStack LoadBalancer attributes
84             @QueryParam("id") String queryLoadBalancerID,
85             @QueryParam("tenant_id") String queryLoadBalancerTenantID,
86             @QueryParam("name") String queryLoadBalancerName,
87             @QueryParam("description") String queryLoadBalancerDescription,
88             @QueryParam("status") String queryLoadBalancerStatus,
89             @QueryParam("vip_address") String queryLoadBalancerVipAddress,
90             @QueryParam("vip_subnet") String queryLoadBalancerVipSubnet,
91             // pagination
92             @QueryParam("limit") String limit,
93             @QueryParam("marker") String marker,
94             @QueryParam("page_reverse") String pageReverse
95             // sorting not supported
96     ) {
97         INeutronLoadBalancerCRUD loadBalancerInterface = getNeutronInterfaces().getLoadBalancerInterface();
98         List<NeutronLoadBalancer> allLoadBalancers = loadBalancerInterface.getAllNeutronLoadBalancers();
99         List<NeutronLoadBalancer> ans = new ArrayList<NeutronLoadBalancer>();
100         Iterator<NeutronLoadBalancer> i = allLoadBalancers.iterator();
101         while (i.hasNext()) {
102             NeutronLoadBalancer nsg = i.next();
103             if ((queryLoadBalancerID == null ||
104                     queryLoadBalancerID.equals(nsg.getID())) &&
105                     (queryLoadBalancerTenantID == null ||
106                             queryLoadBalancerTenantID.equals(nsg.getTenantID())) &&
107                     (queryLoadBalancerName == null ||
108                             queryLoadBalancerName.equals(nsg.getLoadBalancerName())) &&
109                     (queryLoadBalancerDescription == null ||
110                             queryLoadBalancerDescription.equals(nsg.getLoadBalancerDescription())) &&
111                     (queryLoadBalancerVipAddress == null ||
112                             queryLoadBalancerVipAddress.equals(nsg.getLoadBalancerVipAddress())) &&
113                     (queryLoadBalancerVipSubnet == null ||
114                             queryLoadBalancerVipSubnet.equals(nsg.getLoadBalancerVipSubnetID()))) {
115                 if (fields.size() > 0) {
116                     ans.add(extractFields(nsg,fields));
117                 } else {
118                     ans.add(nsg);
119                 }
120             }
121         }
122         return Response.status(HttpURLConnection.HTTP_OK).entity(
123                 new NeutronLoadBalancerRequest(ans)).build();
124     }
125
126     /**
127      * Returns a specific LoadBalancer */
128
129     @Path("{loadBalancerID}")
130     @GET
131     @Produces({ MediaType.APPLICATION_JSON })
132
133     @StatusCodes({
134             @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
135             @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
136             @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
137             @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
138             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
139     public Response showLoadBalancer(@PathParam("loadBalancerID") String loadBalancerID,
140             // return fields
141             @QueryParam("fields") List<String> fields) {
142         INeutronLoadBalancerCRUD loadBalancerInterface = getNeutronInterfaces().getLoadBalancerInterface();
143         if (!loadBalancerInterface.neutronLoadBalancerExists(loadBalancerID)) {
144             throw new ResourceNotFoundException(uuidNoExist(RESOURCE_NAME));
145         }
146         if (fields.size() > 0) {
147             NeutronLoadBalancer ans = loadBalancerInterface.getNeutronLoadBalancer(loadBalancerID);
148             return Response.status(HttpURLConnection.HTTP_OK).entity(
149                     new NeutronLoadBalancerRequest(extractFields(ans, fields))).build();
150         } else {
151             return Response.status(HttpURLConnection.HTTP_OK).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 = HttpURLConnection.HTTP_CREATED, condition = "Created"),
165             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
166     public Response createLoadBalancers(final NeutronLoadBalancerRequest input) {
167         INeutronLoadBalancerCRUD loadBalancerInterface = getNeutronInterfaces().getLoadBalancerInterface();
168         if (input.isSingleton()) {
169             NeutronLoadBalancer singleton = input.getSingleton();
170
171             Object[] instances = NeutronUtil.getInstances(INeutronLoadBalancerAware.class, this);
172             if (instances != null) {
173                 if (instances.length > 0) {
174                     for (Object instance : instances) {
175                         INeutronLoadBalancerAware service = (INeutronLoadBalancerAware) instance;
176                         int status = service.canCreateNeutronLoadBalancer(singleton);
177                         if (status < HTTP_OK_BOTTOM || status > HTTP_OK_TOP) {
178                             return Response.status(status).build();
179                         }
180                     }
181                 } else {
182                     throw new ServiceUnavailableException(NO_PROVIDERS);
183                 }
184             } else {
185                 throw new ServiceUnavailableException(NO_PROVIDER_LIST);
186             }
187
188             loadBalancerInterface.addNeutronLoadBalancer(singleton);
189             if (instances != null) {
190                 for (Object instance : instances) {
191                     INeutronLoadBalancerAware service = (INeutronLoadBalancerAware) instance;
192                     service.neutronLoadBalancerCreated(singleton);
193                 }
194             }
195         } else {
196             Object[] instances = NeutronUtil.getInstances(INeutronLoadBalancerAware.class, this);
197             for (NeutronLoadBalancer test : input.getBulk()) {
198                 if (instances != null) {
199                     if (instances.length > 0) {
200                         for (Object instance : instances) {
201                             INeutronLoadBalancerAware service = (INeutronLoadBalancerAware) instance;
202                             int status = service.canCreateNeutronLoadBalancer(test);
203                             if (status < HTTP_OK_BOTTOM || status > HTTP_OK_TOP) {
204                                 return Response.status(status).build();
205                             }
206                         }
207                     } else {
208                         throw new ServiceUnavailableException(NO_PROVIDERS);
209                     }
210                 } else {
211                     throw new ServiceUnavailableException(NO_PROVIDER_LIST);
212                 }
213             }
214             /*
215              * now, each element of the bulk request can be added to the cache
216              */
217             for (NeutronLoadBalancer test : input.getBulk()) {
218                 loadBalancerInterface.addNeutronLoadBalancer(test);
219                 if (instances != null) {
220                     for (Object instance : instances) {
221                         INeutronLoadBalancerAware service = (INeutronLoadBalancerAware) instance;
222                         service.neutronLoadBalancerCreated(test);
223                     }
224                 }
225             }
226         }
227         return Response.status(HttpURLConnection.HTTP_CREATED).entity(input).build();
228     }
229
230     /**
231      * Updates a LoadBalancer Policy
232      */
233     @Path("{loadBalancerID}")
234     @PUT
235     @Produces({ MediaType.APPLICATION_JSON })
236     @Consumes({ MediaType.APPLICATION_JSON })
237
238     @StatusCodes({
239             @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
240             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
241     public Response updateLoadBalancer(
242             @PathParam("loadBalancerID") String loadBalancerID, final NeutronLoadBalancerRequest input) {
243         INeutronLoadBalancerCRUD loadBalancerInterface = getNeutronInterfaces().getLoadBalancerInterface();
244
245         NeutronLoadBalancer delta = input.getSingleton();
246         NeutronLoadBalancer original = loadBalancerInterface.getNeutronLoadBalancer(loadBalancerID);
247
248         Object[] instances = NeutronUtil.getInstances(INeutronLoadBalancerAware.class, this);
249         if (instances != null) {
250             if (instances.length > 0) {
251                 for (Object instance : instances) {
252                     INeutronLoadBalancerAware service = (INeutronLoadBalancerAware) instance;
253                     int status = service.canUpdateNeutronLoadBalancer(delta, original);
254                     if (status < HTTP_OK_BOTTOM || status > HTTP_OK_TOP) {
255                         return Response.status(status).build();
256                     }
257                 }
258             } else {
259                 throw new ServiceUnavailableException(NO_PROVIDERS);
260             }
261         } else {
262             throw new ServiceUnavailableException(NO_PROVIDER_LIST);
263         }
264
265         /*
266          * update the object and return it
267          */
268         loadBalancerInterface.updateNeutronLoadBalancer(loadBalancerID, delta);
269         NeutronLoadBalancer updatedLoadBalancer = loadBalancerInterface.getNeutronLoadBalancer(
270                 loadBalancerID);
271         if (instances != null) {
272             for (Object instance : instances) {
273                 INeutronLoadBalancerAware service = (INeutronLoadBalancerAware) instance;
274                 service.neutronLoadBalancerUpdated(updatedLoadBalancer);
275             }
276         }
277         return Response.status(HttpURLConnection.HTTP_OK).entity(new NeutronLoadBalancerRequest(loadBalancerInterface.getNeutronLoadBalancer(
278                 loadBalancerID))).build();
279     }
280
281     /**
282      * Deletes a LoadBalancer */
283
284     @Path("{loadBalancerID}")
285     @DELETE
286     @StatusCodes({
287             @ResponseCode(code = HttpURLConnection.HTTP_NO_CONTENT, condition = "No Content"),
288             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
289     public Response deleteLoadBalancer(
290             @PathParam("loadBalancerID") String loadBalancerID) {
291         final INeutronLoadBalancerCRUD loadBalancerInterface = getNeutronInterfaces().getLoadBalancerInterface();
292
293         NeutronLoadBalancer singleton = loadBalancerInterface.getNeutronLoadBalancer(loadBalancerID);
294         Object[] instances = NeutronUtil.getInstances(INeutronLoadBalancerAware.class, this);
295         if (instances != null) {
296             if (instances.length > 0) {
297                 for (Object instance : instances) {
298                     INeutronLoadBalancerAware service = (INeutronLoadBalancerAware) instance;
299                     int status = service.canDeleteNeutronLoadBalancer(singleton);
300                     if (status < HTTP_OK_BOTTOM || status > HTTP_OK_TOP) {
301                         return Response.status(status).build();
302                     }
303                 }
304             } else {
305                 throw new ServiceUnavailableException(NO_PROVIDERS);
306             }
307         } else {
308             throw new ServiceUnavailableException(NO_PROVIDER_LIST);
309         }
310
311         deleteUuid(RESOURCE_NAME, loadBalancerID,
312                    new Remover() {
313                        public boolean remove(String uuid) {
314                            return loadBalancerInterface.removeNeutronLoadBalancer(uuid);
315                        }
316                    });
317         if (instances != null) {
318             for (Object instance : instances) {
319                 INeutronLoadBalancerAware service = (INeutronLoadBalancerAware) instance;
320                 service.neutronLoadBalancerDeleted(singleton);
321             }
322         }
323         return Response.status(HttpURLConnection.HTTP_NO_CONTENT).build();
324     }
325 }