2 * Copyright (c) 2013, 2015 IBM Corporation and others. All rights reserved.
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
8 package org.opendaylight.neutron.northbound.api;
10 import java.net.HttpURLConnection;
11 import java.util.ArrayList;
12 import java.util.List;
13 import javax.ws.rs.Consumes;
14 import javax.ws.rs.DELETE;
15 import javax.ws.rs.DefaultValue;
16 import javax.ws.rs.GET;
17 import javax.ws.rs.POST;
18 import javax.ws.rs.PUT;
19 import javax.ws.rs.Path;
20 import javax.ws.rs.PathParam;
21 import javax.ws.rs.Produces;
22 import javax.ws.rs.QueryParam;
23 import javax.ws.rs.core.Context;
24 import javax.ws.rs.core.MediaType;
25 import javax.ws.rs.core.Response;
26 import javax.ws.rs.core.UriInfo;
27 import org.codehaus.enunciate.jaxrs.ResponseCode;
28 import org.codehaus.enunciate.jaxrs.StatusCodes;
29 import org.opendaylight.neutron.spi.INeutronSubnetCRUD;
30 import org.opendaylight.neutron.spi.NeutronSubnet;
33 * Neutron Northbound REST APIs for Subnets.<br>
36 public final class NeutronSubnetsNorthbound
37 extends AbstractNeutronNorthbound<NeutronSubnet, NeutronSubnetRequest, INeutronSubnetCRUD> {
39 private static final String RESOURCE_NAME = "Subnet";
42 protected String getResourceName() {
50 * Returns a list of all Subnets.
53 @Produces({ MediaType.APPLICATION_JSON })
54 //@TypeHint(OpenStackSubnets.class)
55 @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
56 @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
57 @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
58 @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
59 public Response listSubnets(
61 @QueryParam("fields") List<String> fields,
62 // note: openstack isn't clear about filtering on lists, so we aren't handling them
63 @QueryParam("id") String queryID,
64 @QueryParam("network_id") String queryNetworkID,
65 @QueryParam("name") String queryName,
66 @QueryParam("ip_version") Integer queryIPVersion,
67 @QueryParam("cidr") String queryCIDR,
68 @QueryParam("gateway_ip") String queryGatewayIp,
69 @QueryParam("enable_dhcp") Boolean queryEnableDHCP,
70 @QueryParam("tenant_id") String queryTenantID,
71 @QueryParam("ipv6_address_mode") String queryIpV6AddressMode,
72 @QueryParam("ipv6_ra_mode") String queryIpV6RaMode,
74 @QueryParam("limit") Integer limit,
75 @QueryParam("marker") String marker,
76 @DefaultValue("false") @QueryParam("page_reverse") Boolean pageReverse
77 // sorting not supported
79 INeutronSubnetCRUD subnetInterface = getNeutronCRUD();
80 List<NeutronSubnet> allSubnets = subnetInterface.getAll();
81 List<NeutronSubnet> ans = new ArrayList<>();
82 for (NeutronSubnet subnet : allSubnets) {
83 if ((queryID == null || queryID.equals(subnet.getID()))
84 && (queryNetworkID == null || queryNetworkID.equals(subnet.getNetworkUUID()))
85 && (queryName == null || queryName.equals(subnet.getName()))
86 && (queryIPVersion == null || queryIPVersion.equals(subnet.getIpVersion()))
87 && (queryCIDR == null || queryCIDR.equals(subnet.getCidr()))
88 && (queryGatewayIp == null || queryGatewayIp.equals(subnet.getGatewayIp()))
89 && (queryEnableDHCP == null || queryEnableDHCP.equals(subnet.getEnableDHCP()))
90 && (queryTenantID == null || queryTenantID.equals(subnet.getTenantID()))
91 && (queryIpV6AddressMode == null || queryIpV6AddressMode.equals(subnet.getIpV6AddressMode()))
92 && (queryIpV6RaMode == null || queryIpV6RaMode.equals(subnet.getIpV6RaMode()))) {
93 if (fields.size() > 0) {
94 ans.add(subnet.extractFields(fields));
101 if (limit != null && ans.size() > 1) {
102 // Return a paginated request
103 NeutronSubnetRequest request = (NeutronSubnetRequest) PaginatedRequestFactory.createRequest(limit, marker,
104 pageReverse, uriInfo, ans, NeutronSubnet.class);
105 return Response.status(HttpURLConnection.HTTP_OK).entity(request).build();
108 return Response.status(HttpURLConnection.HTTP_OK).entity(new NeutronSubnetRequest(ans)).build();
112 * Returns a specific Subnet.
114 @Path("{subnetUUID}")
116 @Produces({ MediaType.APPLICATION_JSON })
117 //@TypeHint(OpenStackSubnets.class)
118 @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
119 @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
120 @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
121 @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
122 @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
123 public Response showSubnet(@PathParam("subnetUUID") String subnetUUID,
125 @QueryParam("fields") List<String> fields) {
126 return show(subnetUUID, fields);
130 * Creates new Subnets.
133 @Produces({ MediaType.APPLICATION_JSON })
134 @Consumes({ MediaType.APPLICATION_JSON })
135 //@TypeHint(OpenStackSubnets.class)
136 @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_CREATED, condition = "Created"),
137 @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
138 public Response createSubnets(final NeutronSubnetRequest input) {
139 return create(input);
143 protected void updateDelta(String uuid, NeutronSubnet delta, NeutronSubnet original) {
145 * note: what we get appears to not be a delta, but rather a
146 * complete updated object. So, that needs to be sent down to
150 delta.setNetworkUUID(original.getNetworkUUID());
151 delta.setTenantID(original.getTenantID());
152 delta.setIpVersion(original.getIpVersion());
153 delta.setCidr(original.getCidr());
159 @Path("{subnetUUID}")
161 @Produces({ MediaType.APPLICATION_JSON })
162 @Consumes({ MediaType.APPLICATION_JSON })
163 //@TypeHint(OpenStackSubnets.class)
164 @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
165 @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
166 @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
167 public Response updateSubnet(@PathParam("subnetUUID") String subnetUUID, final NeutronSubnetRequest input) {
168 return update(subnetUUID, input);
174 @Path("{subnetUUID}")
176 @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_NO_CONTENT, condition = "No Content"),
177 @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
178 @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
179 public Response deleteSubnet(@PathParam("subnetUUID") String subnetUUID) {
180 return delete(subnetUUID);