2f4f4a2db14cae06f4d1375e25e3fb5da076e676
[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      */
67     @GET
68     @Produces({ MediaType.APPLICATION_JSON })
69     //@TypeHint(OpenStackSubnets.class)
70     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
71             @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
72             @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
73             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
74     public Response listSubnets(
75             // return fields
76             @QueryParam("fields") List<String> fields,
77             // note: openstack isn't clear about filtering on lists, so we aren't handling them
78             @QueryParam("id") String queryID,
79             @QueryParam("network_id") String queryNetworkID,
80             @QueryParam("name") String queryName,
81             @QueryParam("ip_version") Integer queryIPVersion,
82             @QueryParam("cidr") String queryCIDR,
83             @QueryParam("gateway_ip") String queryGatewayIp,
84             @QueryParam("enable_dhcp") Boolean queryEnableDHCP,
85             @QueryParam("tenant_id") String queryTenantID,
86             @QueryParam("ipv6_address_mode") String queryIpV6AddressMode,
87             @QueryParam("ipv6_ra_mode") String queryIpV6RaMode,
88             // linkTitle
89             @QueryParam("limit") Integer limit,
90             @QueryParam("marker") String marker,
91             @DefaultValue("false") @QueryParam("page_reverse") Boolean pageReverse
92     // sorting not supported
93     ) {
94         INeutronSubnetCRUD subnetInterface = getNeutronCRUD();
95         List<NeutronSubnet> allSubnets = subnetInterface.getAll();
96         List<NeutronSubnet> ans = new ArrayList<>();
97         for (NeutronSubnet subnet : allSubnets) {
98             if ((queryID == null || queryID.equals(subnet.getID()))
99                     && (queryNetworkID == null || queryNetworkID.equals(subnet.getNetworkUUID()))
100                     && (queryName == null || queryName.equals(subnet.getName()))
101                     && (queryIPVersion == null || queryIPVersion.equals(subnet.getIpVersion()))
102                     && (queryCIDR == null || queryCIDR.equals(subnet.getCidr()))
103                     && (queryGatewayIp == null || queryGatewayIp.equals(subnet.getGatewayIp()))
104                     && (queryEnableDHCP == null || queryEnableDHCP.equals(subnet.getEnableDHCP()))
105                     && (queryTenantID == null || queryTenantID.equals(subnet.getTenantID()))
106                     && (queryIpV6AddressMode == null || queryIpV6AddressMode.equals(subnet.getIpV6AddressMode()))
107                     && (queryIpV6RaMode == null || queryIpV6RaMode.equals(subnet.getIpV6RaMode()))) {
108                 if (fields.size() > 0) {
109                     ans.add(subnet.extractFields(fields));
110                 } else {
111                     ans.add(subnet);
112                 }
113             }
114         }
115
116         if (limit != null && ans.size() > 1) {
117             // Return a paginated request
118             NeutronSubnetRequest request = (NeutronSubnetRequest) PaginatedRequestFactory.createRequest(limit, marker,
119                     pageReverse, uriInfo, ans, NeutronSubnet.class);
120             return Response.status(HttpURLConnection.HTTP_OK).entity(request).build();
121         }
122
123         return Response.status(HttpURLConnection.HTTP_OK).entity(new NeutronSubnetRequest(ans)).build();
124     }
125
126     /**
127      * Returns a specific Subnet.
128      */
129
130     @Path("{subnetUUID}")
131     @GET
132     @Produces({ MediaType.APPLICATION_JSON })
133     //@TypeHint(OpenStackSubnets.class)
134     @StatusCodes({ @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 showSubnet(@PathParam("subnetUUID") String subnetUUID,
140             // return fields
141             @QueryParam("fields") List<String> fields) {
142         return show(subnetUUID, fields);
143     }
144
145     /**
146      * Creates new Subnets.
147      */
148
149     @POST
150     @Produces({ MediaType.APPLICATION_JSON })
151     @Consumes({ MediaType.APPLICATION_JSON })
152     //@TypeHint(OpenStackSubnets.class)
153     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_CREATED, condition = "Created"),
154             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
155     public Response createSubnets(final NeutronSubnetRequest input) {
156         return create(input);
157     }
158
159     @Override
160     protected void updateDelta(String uuid, NeutronSubnet delta, NeutronSubnet original) {
161         /*
162          * note: what we get appears to not be a delta, but rather a
163          * complete updated object.  So, that needs to be sent down to
164          * folks to check
165          */
166
167         delta.setID(uuid);
168         delta.setNetworkUUID(original.getNetworkUUID());
169         delta.setTenantID(original.getTenantID());
170         delta.setIpVersion(original.getIpVersion());
171         delta.setCidr(original.getCidr());
172     }
173
174     /**
175      * Updates a Subnet.
176      */
177
178     @Path("{subnetUUID}")
179     @PUT
180     @Produces({ MediaType.APPLICATION_JSON })
181     @Consumes({ MediaType.APPLICATION_JSON })
182     //@TypeHint(OpenStackSubnets.class)
183     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
184             @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
185             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
186     public Response updateSubnet(@PathParam("subnetUUID") String subnetUUID, final NeutronSubnetRequest input) {
187         return update(subnetUUID, input);
188     }
189
190     /**
191      * Deletes a Subnet.
192      */
193
194     @Path("{subnetUUID}")
195     @DELETE
196     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_NO_CONTENT, condition = "No Content"),
197             @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
198             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
199     public Response deleteSubnet(@PathParam("subnetUUID") String subnetUUID) {
200         return delete(subnetUUID);
201     }
202 }