Merge "Bug 5478 - northbound: improve 404 check on update/delete"
[neutron.git] / northbound-api / src / main / java / org / opendaylight / neutron / northbound / api / NeutronBgpvpnsNorthbound.java
1 /*
2  * Copyright (c) 2015 Ericsson India Global Services Pvt Ltd. 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.Iterator;
14 import java.util.List;
15
16 import javax.ws.rs.Consumes;
17 import javax.ws.rs.DELETE;
18 import javax.ws.rs.DefaultValue;
19 import javax.ws.rs.GET;
20 import javax.ws.rs.POST;
21 import javax.ws.rs.PUT;
22 import javax.ws.rs.Path;
23 import javax.ws.rs.PathParam;
24 import javax.ws.rs.Produces;
25 import javax.ws.rs.QueryParam;
26 import javax.ws.rs.core.Context;
27 import javax.ws.rs.core.MediaType;
28 import javax.ws.rs.core.Response;
29 import javax.ws.rs.core.UriInfo;
30
31 import org.codehaus.enunciate.jaxrs.ResponseCode;
32 import org.codehaus.enunciate.jaxrs.StatusCodes;
33 import org.codehaus.enunciate.jaxrs.TypeHint;
34 import org.opendaylight.neutron.spi.INeutronBgpvpnCRUD;
35 import org.opendaylight.neutron.spi.NeutronCRUDInterfaces;
36 import org.opendaylight.neutron.spi.NeutronBgpvpn;
37
38 /**
39  * Neutron Northbound REST APIs for Bgpvpn.<br>
40  * This class provides REST APIs for managing neutron Bgpvpns
41  *
42  * <br>
43  * <br>
44  * Authentication scheme : <b>HTTP Basic</b><br>
45  * Authentication realm : <b>opendaylight</b><br>
46  * Transport : <b>HTTP and HTTPS</b><br>
47  * <br>
48  * HTTPS Authentication is disabled by default. Administrator can enable it in
49  * tomcat-server.xml after adding a proper keystore / SSL certificate from a
50  * trusted authority.<br>
51  * More info :
52  * http://tomcat.apache.org/tomcat-7.0-doc/ssl-howto.html#Configuration
53  *
54  */
55
56 @Path("/bgpvpns")
57 public class NeutronBgpvpnsNorthbound
58     extends AbstractNeutronNorthbound<NeutronBgpvpn, NeutronBgpvpnRequest, INeutronBgpvpnCRUD> {
59
60     @Context
61     UriInfo uriInfo;
62
63     private static final String RESOURCE_NAME = "Bgpvpn";
64
65     @Override
66     protected String getResourceName() {
67         return RESOURCE_NAME;
68     }
69
70     @Override
71     protected NeutronBgpvpn extractFields(NeutronBgpvpn o, List<String> fields) {
72         return o.extractFields(fields);
73     }
74
75     @Override
76     protected NeutronBgpvpnRequest newNeutronRequest(NeutronBgpvpn o) {
77         return new NeutronBgpvpnRequest(o);
78     }
79
80     @Override
81     protected INeutronBgpvpnCRUD getNeutronCRUD() {
82         NeutronCRUDInterfaces answer = new NeutronCRUDInterfaces().fetchINeutronBgpvpnCRUD(this);
83         if (answer.getBgpvpnInterface() == null) {
84             throw new ServiceUnavailableException(serviceUnavailable());
85         }
86         return answer.getBgpvpnInterface();
87     }
88
89     /**
90      * Returns a list of all Bgpvpns */
91
92     @GET
93     @Produces({ MediaType.APPLICATION_JSON })
94     //@TypeHint(OpenStackBgpvpns.class)
95     @StatusCodes({
96         @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
97         @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
98         @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
99         @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
100     public Response listBgpvpns(
101             // return fields
102             @QueryParam("fields") List<String> fields,
103             // note: openstack isn't clear about filtering on lists, so we aren't handling them
104             @QueryParam("id") String queryID,
105             @QueryParam("name") String queryName,
106             @QueryParam("admin_state_up") String queryAdminStateUp,
107             @QueryParam("status") String queryStatus,
108             @QueryParam("tenant_id") String queryTenantID,
109             @QueryParam("type") String queryType,
110             @QueryParam("auto_aggregate") String queryAutoAggregate,
111             // pagination
112             @QueryParam("limit") Integer limit,
113             @QueryParam("marker") String marker,
114             @DefaultValue("false") @QueryParam("page_reverse") Boolean pageReverse
115             // sorting not supported
116             ) {
117         INeutronBgpvpnCRUD bgpvpnInterface = getNeutronCRUD();
118         List<NeutronBgpvpn> allBgpvpns = bgpvpnInterface.getAll();
119         List<NeutronBgpvpn> ans = new ArrayList<NeutronBgpvpn>();
120         Iterator<NeutronBgpvpn> i = allBgpvpns.iterator();
121         while (i.hasNext()) {
122             NeutronBgpvpn oSN = i.next();
123             //match filters: TODO provider extension
124             Boolean bAdminStateUp = null;
125             Boolean bAutoAggregate = null;
126             if (queryAdminStateUp != null) {
127                 bAdminStateUp = Boolean.valueOf(queryAdminStateUp);
128             }
129             if (queryAutoAggregate != null) {
130                 bAutoAggregate = Boolean.valueOf(queryAutoAggregate);
131             }
132             if ((queryID == null || queryID.equals(oSN.getID())) &&
133                     (queryName == null || queryName.equals(oSN.getBgpvpnName())) &&
134                     (bAdminStateUp == null || bAdminStateUp.booleanValue() == oSN.isAdminStateUp()) &&
135                     (queryStatus == null || queryStatus.equals(oSN.getStatus())) &&
136                     (bAutoAggregate == null || bAutoAggregate.booleanValue() == oSN.isAutoAggregate()) &&
137                     (queryTenantID == null || queryTenantID.equals(oSN.getTenantID()))) {
138                 if (fields.size() > 0) {
139                     ans.add(extractFields(oSN,fields));
140                 } else {
141                     ans.add(oSN);
142                 }
143             }
144         }
145
146         if (limit != null && ans.size() > 1) {
147             // Return a paginated request
148             NeutronBgpvpnRequest request = (NeutronBgpvpnRequest) PaginatedRequestFactory.createRequest(limit,
149                     marker, pageReverse, uriInfo, ans, NeutronBgpvpn.class);
150             return Response.status(HttpURLConnection.HTTP_OK).entity(request).build();
151         }
152
153     return Response.status(HttpURLConnection.HTTP_OK).entity(new NeutronBgpvpnRequest(ans)).build();
154
155     }
156
157     /**
158      * Returns a specific Bgpvpn */
159
160     @Path("{bgpvpnUUID}")
161     @GET
162     @Produces({ MediaType.APPLICATION_JSON })
163     //@TypeHint(OpenStackBgpvpns.class)
164     @StatusCodes({
165         @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
166         @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
167         @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
168         @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
169         @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
170     public Response showBgpvpn(
171             @PathParam("bgpvpnUUID") String bgpvpnUUID,
172             // return fields
173             @QueryParam("fields") List<String> fields
174             ) {
175         return show(bgpvpnUUID, fields);
176     }
177
178     /**
179      * Creates new Bgpvpns */
180     @POST
181     @Produces({ MediaType.APPLICATION_JSON })
182     @Consumes({ MediaType.APPLICATION_JSON })
183     @TypeHint(NeutronBgpvpn.class)
184     @StatusCodes({
185         @ResponseCode(code = HttpURLConnection.HTTP_CREATED, condition = "Created"),
186         @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
187     public Response createBgpvpns(final NeutronBgpvpnRequest input) {
188         return create(input);
189     }
190
191     /**
192      * Updates a Bgpvpn */
193     @Override
194     protected void updateDelta(String uuid,
195                                NeutronBgpvpn delta, NeutronBgpvpn original) {
196         //Fill in defaults if they're missing in update
197         delta.initDefaults();
198         delta.setID(uuid);
199         delta.setTenantID(original.getTenantID());
200     }
201
202     @Path("{bgpvpnUUID}")
203     @PUT
204     @Produces({ MediaType.APPLICATION_JSON })
205     @Consumes({ MediaType.APPLICATION_JSON })
206     //@TypeHint(OpenStackBgpvpns.class)
207     @StatusCodes({
208         @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
209         @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
210         @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
211     public Response updateBgpvpn(
212             @PathParam("bgpvpnUUID") String bgpvpnUUID, final NeutronBgpvpnRequest input
213             ) {
214         return update(bgpvpnUUID, input);
215     }
216
217     /**
218      * Deletes a Bgpvpn */
219
220     @Path("{bgpvpnUUID}")
221     @DELETE
222     @StatusCodes({
223         @ResponseCode(code = HttpURLConnection.HTTP_NO_CONTENT, condition = "No Content"),
224         @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
225         @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
226     public Response deleteBgpvpn(
227             @PathParam("bgpvpnUUID") String bgpvpnUUID) {
228         return delete(bgpvpnUUID);
229     }
230 }