checkstyle: enable LocalVariableName
[neutron.git] / northbound-api / src / main / java / org / opendaylight / neutron / northbound / api / NeutronSubnetsNorthbound.java
1 /*
2  * Copyright (c) 2013, 2015 IBM Corporation 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 import java.util.ArrayList;
13 import java.util.List;
14 import javax.ws.rs.Consumes;
15 import javax.ws.rs.DELETE;
16 import javax.ws.rs.DefaultValue;
17 import javax.ws.rs.GET;
18 import javax.ws.rs.POST;
19 import javax.ws.rs.PUT;
20 import javax.ws.rs.Path;
21 import javax.ws.rs.PathParam;
22 import javax.ws.rs.Produces;
23 import javax.ws.rs.QueryParam;
24 import javax.ws.rs.core.Context;
25 import javax.ws.rs.core.MediaType;
26 import javax.ws.rs.core.Response;
27 import javax.ws.rs.core.UriInfo;
28 import org.codehaus.enunciate.jaxrs.ResponseCode;
29 import org.codehaus.enunciate.jaxrs.StatusCodes;
30 import org.opendaylight.neutron.spi.INeutronSubnetCRUD;
31 import org.opendaylight.neutron.spi.NeutronSubnet;
32
33 /**
34  * Neutron Northbound REST APIs for Subnets.<br>
35  * This class provides REST APIs for managing neutron Subnets
36  *
37  * <br>
38  * <br>
39  * Authentication scheme : <b>HTTP Basic</b><br>
40  * Authentication realm : <b>opendaylight</b><br>
41  * Transport : <b>HTTP and HTTPS</b><br>
42  * <br>
43  * HTTPS Authentication is disabled by default. Administrator can enable it in
44  * tomcat-server.xml after adding a proper keystore / SSL certificate from a
45  * trusted authority.<br>
46  * More info :
47  * http://tomcat.apache.org/tomcat-7.0-doc/ssl-howto.html#Configuration
48  *
49  */
50
51 @Path("/subnets")
52 public final class NeutronSubnetsNorthbound
53         extends AbstractNeutronNorthbound<NeutronSubnet, NeutronSubnetRequest, INeutronSubnetCRUD> {
54     private static final String RESOURCE_NAME = "Subnet";
55
56     @Override
57     protected String getResourceName() {
58         return RESOURCE_NAME;
59     }
60
61     @Context
62     UriInfo uriInfo;
63
64     /**
65      * Returns a list of all Subnets */
66     @GET
67     @Produces({ MediaType.APPLICATION_JSON })
68     //@TypeHint(OpenStackSubnets.class)
69     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
70             @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
71             @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
72             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
73     public Response listSubnets(
74             // return fields
75             @QueryParam("fields") List<String> fields,
76             // note: openstack isn't clear about filtering on lists, so we aren't handling them
77             @QueryParam("id") String queryID,
78             @QueryParam("network_id") String queryNetworkID,
79             @QueryParam("name") String queryName,
80             @QueryParam("ip_version") Integer queryIPVersion,
81             @QueryParam("cidr") String queryCIDR,
82             @QueryParam("gateway_ip") String queryGatewayIP,
83             @QueryParam("enable_dhcp") Boolean queryEnableDHCP,
84             @QueryParam("tenant_id") String queryTenantID,
85             @QueryParam("ipv6_address_mode") String queryIpV6AddressMode,
86             @QueryParam("ipv6_ra_mode") String queryIpV6RaMode,
87             // linkTitle
88             @QueryParam("limit") Integer limit,
89             @QueryParam("marker") String marker,
90             @DefaultValue("false") @QueryParam("page_reverse") Boolean pageReverse
91     // sorting not supported
92     ) {
93         INeutronSubnetCRUD subnetInterface = getNeutronCRUD();
94         List<NeutronSubnet> allSubnets = subnetInterface.getAll();
95         List<NeutronSubnet> ans = new ArrayList<>();
96         for (NeutronSubnet subnet : allSubnets) {
97             if ((queryID == null || queryID.equals(subnet.getID()))
98                     && (queryNetworkID == null || queryNetworkID.equals(subnet.getNetworkUUID()))
99                     && (queryName == null || queryName.equals(subnet.getName()))
100                     && (queryIPVersion == null || queryIPVersion.equals(subnet.getIpVersion()))
101                     && (queryCIDR == null || queryCIDR.equals(subnet.getCidr()))
102                     && (queryGatewayIP == null || queryGatewayIP.equals(subnet.getGatewayIP()))
103                     && (queryEnableDHCP == null || queryEnableDHCP.equals(subnet.getEnableDHCP()))
104                     && (queryTenantID == null || queryTenantID.equals(subnet.getTenantID()))
105                     && (queryIpV6AddressMode == null || queryIpV6AddressMode.equals(subnet.getIpV6AddressMode()))
106                     && (queryIpV6RaMode == null || queryIpV6RaMode.equals(subnet.getIpV6RaMode()))) {
107                 if (fields.size() > 0) {
108                     ans.add(subnet.extractFields(fields));
109                 } else {
110                     ans.add(subnet);
111                 }
112             }
113         }
114
115         if (limit != null && ans.size() > 1) {
116             // Return a paginated request
117             NeutronSubnetRequest request = (NeutronSubnetRequest) PaginatedRequestFactory.createRequest(limit, marker,
118                     pageReverse, uriInfo, ans, NeutronSubnet.class);
119             return Response.status(HttpURLConnection.HTTP_OK).entity(request).build();
120         }
121
122         return Response.status(HttpURLConnection.HTTP_OK).entity(new NeutronSubnetRequest(ans)).build();
123     }
124
125     /**
126      * Returns a specific Subnet */
127
128     @Path("{subnetUUID}")
129     @GET
130     @Produces({ MediaType.APPLICATION_JSON })
131     //@TypeHint(OpenStackSubnets.class)
132     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
133             @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
134             @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
135             @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
136             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
137     public Response showSubnet(@PathParam("subnetUUID") String subnetUUID,
138             // return fields
139             @QueryParam("fields") List<String> fields) {
140         return show(subnetUUID, fields);
141     }
142
143     /**
144      * Creates new Subnets */
145
146     @POST
147     @Produces({ MediaType.APPLICATION_JSON })
148     @Consumes({ MediaType.APPLICATION_JSON })
149     //@TypeHint(OpenStackSubnets.class)
150     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_CREATED, condition = "Created"),
151             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
152     public Response createSubnets(final NeutronSubnetRequest input) {
153         return create(input);
154     }
155
156     @Override
157     protected void updateDelta(String uuid, NeutronSubnet delta, NeutronSubnet original) {
158         /*
159          * note: what we get appears to not be a delta, but rather a
160          * complete updated object.  So, that needs to be sent down to
161          * folks to check
162          */
163
164         delta.setID(uuid);
165         delta.setNetworkUUID(original.getNetworkUUID());
166         delta.setTenantID(original.getTenantID());
167         delta.setIpVersion(original.getIpVersion());
168         delta.setCidr(original.getCidr());
169     }
170
171     /**
172      * Updates a Subnet */
173
174     @Path("{subnetUUID}")
175     @PUT
176     @Produces({ MediaType.APPLICATION_JSON })
177     @Consumes({ MediaType.APPLICATION_JSON })
178     //@TypeHint(OpenStackSubnets.class)
179     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
180             @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
181             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
182     public Response updateSubnet(@PathParam("subnetUUID") String subnetUUID, final NeutronSubnetRequest input) {
183         return update(subnetUUID, input);
184     }
185
186     /**
187      * Deletes a Subnet */
188
189     @Path("{subnetUUID}")
190     @DELETE
191     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_NO_CONTENT, condition = "No Content"),
192             @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
193             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
194     public Response deleteSubnet(@PathParam("subnetUUID") String subnetUUID) {
195         return delete(subnetUUID);
196     }
197 }