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