Neutron*Northbound: consolidate crud logic
[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
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.DefaultValue;
20 import javax.ws.rs.GET;
21 import javax.ws.rs.POST;
22 import javax.ws.rs.PUT;
23 import javax.ws.rs.Path;
24 import javax.ws.rs.PathParam;
25 import javax.ws.rs.Produces;
26 import javax.ws.rs.QueryParam;
27 import javax.ws.rs.core.Context;
28 import javax.ws.rs.core.MediaType;
29 import javax.ws.rs.core.Response;
30 import javax.ws.rs.core.UriInfo;
31
32 import org.codehaus.enunciate.jaxrs.ResponseCode;
33 import org.codehaus.enunciate.jaxrs.StatusCodes;
34 import org.opendaylight.neutron.spi.INeutronNetworkCRUD;
35 import org.opendaylight.neutron.spi.INeutronSubnetAware;
36 import org.opendaylight.neutron.spi.INeutronSubnetCRUD;
37 import org.opendaylight.neutron.spi.NeutronCRUDInterfaces;
38 import org.opendaylight.neutron.spi.NeutronSubnet;
39
40 /**
41  * Neutron Northbound REST APIs for Subnets.<br>
42  * This class provides REST APIs for managing neutron Subnets
43  *
44  * <br>
45  * <br>
46  * Authentication scheme : <b>HTTP Basic</b><br>
47  * Authentication realm : <b>opendaylight</b><br>
48  * Transport : <b>HTTP and HTTPS</b><br>
49  * <br>
50  * HTTPS Authentication is disabled by default. Administrator can enable it in
51  * tomcat-server.xml after adding a proper keystore / SSL certificate from a
52  * trusted authority.<br>
53  * More info :
54  * http://tomcat.apache.org/tomcat-7.0-doc/ssl-howto.html#Configuration
55  *
56  */
57
58 @Path("/subnets")
59 public class NeutronSubnetsNorthbound
60     extends AbstractNeutronNorthbound<NeutronSubnet, NeutronSubnetRequest, INeutronSubnetCRUD, INeutronSubnetAware> {
61     private static final String RESOURCE_NAME = "Subnet";
62
63     @Override
64     protected String getResourceName() {
65         return RESOURCE_NAME;
66     }
67
68     @Override
69     protected NeutronSubnet extractFields(NeutronSubnet o, List<String> fields) {
70         return o.extractFields(fields);
71     }
72
73     private NeutronCRUDInterfaces getNeutronInterfaces(boolean needNetwork) {
74         NeutronCRUDInterfaces answer = new NeutronCRUDInterfaces().fetchINeutronSubnetCRUD(this);
75         if (answer.getSubnetInterface() == null) {
76             throw new ServiceUnavailableException(serviceUnavailable());
77         }
78         if (needNetwork) {
79             answer = answer.fetchINeutronNetworkCRUD(this);
80             if (answer.getNetworkInterface() == null) {
81                 throw new ServiceUnavailableException("Network CRUD Interface "
82                     + RestMessages.SERVICEUNAVAILABLE.toString());
83             }
84         }
85         return answer;
86     }
87
88     @Override
89     protected INeutronSubnetCRUD getNeutronCRUD() {
90         return getNeutronInterfaces(false).getSubnetInterface();
91     }
92
93     @Override
94     protected NeutronSubnetRequest newNeutronRequest(NeutronSubnet o) {
95         return new NeutronSubnetRequest(o);
96     }
97
98     @Override
99     protected Object[] getInstances() {
100         return NeutronUtil.getInstances(INeutronSubnetAware.class, this);
101     }
102
103     @Override
104     protected int canCreate(Object instance, NeutronSubnet singleton) {
105         INeutronSubnetAware service = (INeutronSubnetAware) instance;
106         return service.canCreateSubnet(singleton);
107     }
108
109     @Override
110     protected void created(Object instance, NeutronSubnet singleton) {
111         INeutronSubnetAware service = (INeutronSubnetAware) instance;
112         service.neutronSubnetCreated(singleton);
113     }
114
115     @Override
116     protected int canUpdate(Object instance, NeutronSubnet delta, NeutronSubnet original) {
117         INeutronSubnetAware service = (INeutronSubnetAware) instance;
118         return service.canUpdateSubnet(delta, original);
119     }
120
121     @Override
122     protected void updated(Object instance, NeutronSubnet updated) {
123         INeutronSubnetAware service = (INeutronSubnetAware) instance;
124         service.neutronSubnetUpdated(updated);
125     }
126
127     @Override
128     protected int canDelete(Object instance, NeutronSubnet singleton) {
129         INeutronSubnetAware service = (INeutronSubnetAware) instance;
130         return service.canDeleteSubnet(singleton);
131     }
132
133     @Override
134     protected void deleted(Object instance, NeutronSubnet singleton) {
135         INeutronSubnetAware service = (INeutronSubnetAware) instance;
136         service.neutronSubnetDeleted(singleton);
137     }
138
139     @Context
140     UriInfo uriInfo;
141
142     /**
143      * Returns a list of all Subnets */
144     @GET
145     @Produces({ MediaType.APPLICATION_JSON })
146     //@TypeHint(OpenStackSubnets.class)
147     @StatusCodes({
148             @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
149             @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
150             @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
151             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
152     public Response listSubnets(
153             // return fields
154             @QueryParam("fields") List<String> fields,
155             // note: openstack isn't clear about filtering on lists, so we aren't handling them
156             @QueryParam("id") String queryID,
157             @QueryParam("network_id") String queryNetworkID,
158             @QueryParam("name") String queryName,
159             @QueryParam("ip_version") String queryIPVersion,
160             @QueryParam("cidr") String queryCIDR,
161             @QueryParam("gateway_ip") String queryGatewayIP,
162             @QueryParam("enable_dhcp") String queryEnableDHCP,
163             @QueryParam("tenant_id") String queryTenantID,
164             @QueryParam("ipv6_address_mode") String queryIpV6AddressMode,
165             @QueryParam("ipv6_ra_mode") String queryIpV6RaMode,
166             // linkTitle
167             @QueryParam("limit") Integer limit,
168             @QueryParam("marker") String marker,
169             @DefaultValue("false") @QueryParam("page_reverse") Boolean pageReverse
170             // sorting not supported
171             ) {
172         INeutronSubnetCRUD subnetInterface = getNeutronInterfaces(false).getSubnetInterface();
173         List<NeutronSubnet> allNetworks = subnetInterface.getAllSubnets();
174         List<NeutronSubnet> ans = new ArrayList<NeutronSubnet>();
175         Iterator<NeutronSubnet> i = allNetworks.iterator();
176         while (i.hasNext()) {
177             NeutronSubnet oSS = i.next();
178             if ((queryID == null || queryID.equals(oSS.getID())) &&
179                     (queryNetworkID == null || queryNetworkID.equals(oSS.getNetworkUUID())) &&
180                     (queryName == null || queryName.equals(oSS.getName())) &&
181                     (queryIPVersion == null || queryIPVersion.equals(oSS.getIpVersion())) &&
182                     (queryCIDR == null || queryCIDR.equals(oSS.getCidr())) &&
183                     (queryGatewayIP == null || queryGatewayIP.equals(oSS.getGatewayIP())) &&
184                     (queryEnableDHCP == null || queryEnableDHCP.equals(oSS.getEnableDHCP())) &&
185                     (queryTenantID == null || queryTenantID.equals(oSS.getTenantID())) &&
186                     (queryIpV6AddressMode == null || queryIpV6AddressMode.equals(oSS.getIpV6AddressMode())) &&
187                     (queryIpV6RaMode == null || queryIpV6RaMode.equals(oSS.getIpV6RaMode()))){
188                 if (fields.size() > 0) {
189                     ans.add(extractFields(oSS,fields));
190                 } else {
191                     ans.add(oSS);
192                 }
193             }
194         }
195
196         if (limit != null && ans.size() > 1) {
197             // Return a paginated request
198             NeutronSubnetRequest request = (NeutronSubnetRequest) PaginatedRequestFactory.createRequest(limit,
199                     marker, pageReverse, uriInfo, ans, NeutronSubnet.class);
200             return Response.status(HttpURLConnection.HTTP_OK).entity(request).build();
201         }
202
203         return Response.status(HttpURLConnection.HTTP_OK).entity(
204                 new NeutronSubnetRequest(ans)).build();
205     }
206
207     /**
208      * Returns a specific Subnet */
209
210     @Path("{subnetUUID}")
211     @GET
212     @Produces({ MediaType.APPLICATION_JSON })
213     //@TypeHint(OpenStackSubnets.class)
214     @StatusCodes({
215             @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
216             @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
217             @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
218             @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
219             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
220     public Response showSubnet(
221             @PathParam("subnetUUID") String subnetUUID,
222             // return fields
223             @QueryParam("fields") List<String> fields) {
224         return show(subnetUUID, fields);
225     }
226
227     /**
228      * Creates new Subnets */
229
230     @POST
231     @Produces({ MediaType.APPLICATION_JSON })
232     @Consumes({ MediaType.APPLICATION_JSON })
233     //@TypeHint(OpenStackSubnets.class)
234     @StatusCodes({
235             @ResponseCode(code = HttpURLConnection.HTTP_CREATED, condition = "Created"),
236             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
237     public Response createSubnets(final NeutronSubnetRequest input) {
238         getNeutronInterfaces(true); // Ensure that network service is loaded
239         return create(input);
240     }
241
242     @Override
243     protected void updateDelta(String uuid, NeutronSubnet delta, NeutronSubnet original) {
244         /*
245          * note: what we get appears to not be a delta, but rather a
246          * complete updated object.  So, that needs to be sent down to
247          * folks to check
248          */
249
250         delta.setID(uuid);
251         delta.setNetworkUUID(original.getNetworkUUID());
252         delta.setTenantID(original.getTenantID());
253         delta.setIpVersion(original.getIpVersion());
254         delta.setCidr(original.getCidr());
255     }
256
257     /**
258      * Updates a Subnet */
259
260     @Path("{subnetUUID}")
261     @PUT
262     @Produces({ MediaType.APPLICATION_JSON })
263     @Consumes({ MediaType.APPLICATION_JSON })
264     //@TypeHint(OpenStackSubnets.class)
265     @StatusCodes({
266             @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
267             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
268     public Response updateSubnet(
269             @PathParam("subnetUUID") String subnetUUID, final NeutronSubnetRequest input
270             ) {
271         return update(subnetUUID, input);
272     }
273
274     /**
275      * Deletes a Subnet */
276
277     @Path("{subnetUUID}")
278     @DELETE
279     @StatusCodes({
280             @ResponseCode(code = HttpURLConnection.HTTP_NO_CONTENT, condition = "No Content"),
281             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
282     public Response deleteSubnet(
283             @PathParam("subnetUUID") String subnetUUID) {
284         return delete(subnetUUID);
285     }
286 }