Align BGPVPN code with merged changes
[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
59     @Context
60     UriInfo uriInfo;
61
62     private static final String RESOURCE_NAME = "Bgpvpn";
63     private NeutronBgpvpn extractFields(NeutronBgpvpn o, List<String> fields) {
64         return o.extractFields(fields);
65     }
66
67     private NeutronCRUDInterfaces getNeutronInterfaces() {
68         NeutronCRUDInterfaces answer = new NeutronCRUDInterfaces().fetchINeutronBgpvpnCRUD(this);
69         if (answer.getBgpvpnInterface() == null) {
70             throw new ServiceUnavailableException("Service is unavailable");
71         }
72         return answer;
73     }
74
75     /**
76      * Returns a list of all Bgpvpns */
77
78     @GET
79     @Produces({ MediaType.APPLICATION_JSON })
80     //@TypeHint(OpenStackBgpvpns.class)
81     @StatusCodes({
82         @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
83         @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
84         @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
85         @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
86     public Response listBgpvpns(
87             // return fields
88             @QueryParam("fields") List<String> fields,
89             // note: openstack isn't clear about filtering on lists, so we aren't handling them
90             @QueryParam("id") String queryID,
91             @QueryParam("name") String queryName,
92             @QueryParam("admin_state_up") String queryAdminStateUp,
93             @QueryParam("status") String queryStatus,
94             @QueryParam("tenant_id") String queryTenantID,
95             @QueryParam("type") String queryType,
96             @QueryParam("auto_aggregate") String queryAutoAggregate,
97             // pagination
98             @QueryParam("limit") Integer limit,
99             @QueryParam("marker") String marker,
100             @DefaultValue("false") @QueryParam("page_reverse") Boolean pageReverse
101             // sorting not supported
102             ) {
103         INeutronBgpvpnCRUD bgpvpnInterface = getNeutronInterfaces().getBgpvpnInterface();
104         List<NeutronBgpvpn> allBgpvpns = bgpvpnInterface.getAllBgpvpns();
105         List<NeutronBgpvpn> ans = new ArrayList<NeutronBgpvpn>();
106         Iterator<NeutronBgpvpn> i = allBgpvpns.iterator();
107         while (i.hasNext()) {
108             NeutronBgpvpn oSN = i.next();
109             //match filters: TODO provider extension
110             Boolean bAdminStateUp = null;
111             Boolean bAutoAggregate = null;
112             if (queryAdminStateUp != null) {
113                 bAdminStateUp = Boolean.valueOf(queryAdminStateUp);
114             }
115             if (queryAutoAggregate != null) {
116                 bAutoAggregate = Boolean.valueOf(queryAutoAggregate);
117             }
118             if ((queryID == null || queryID.equals(oSN.getID())) &&
119                     (queryName == null || queryName.equals(oSN.getBgpvpnName())) &&
120                     (bAdminStateUp == null || bAdminStateUp.booleanValue() == oSN.isAdminStateUp()) &&
121                     (queryStatus == null || queryStatus.equals(oSN.getStatus())) &&
122                     (bAutoAggregate == null || bAutoAggregate.booleanValue() == oSN.isAutoAggregate()) &&
123                     (queryTenantID == null || queryTenantID.equals(oSN.getTenantID()))) {
124                 if (fields.size() > 0) {
125                     ans.add(extractFields(oSN,fields));
126                 } else {
127                     ans.add(oSN);
128                 }
129             }
130         }
131
132         if (limit != null && ans.size() > 1) {
133             // Return a paginated request
134             NeutronBgpvpnRequest request = (NeutronBgpvpnRequest) PaginatedRequestFactory.createRequest(limit,
135                     marker, pageReverse, uriInfo, ans, NeutronBgpvpn.class);
136             return Response.status(HttpURLConnection.HTTP_OK).entity(request).build();
137         }
138
139     return Response.status(HttpURLConnection.HTTP_OK).entity(new NeutronBgpvpnRequest(ans)).build();
140
141     }
142
143     /**
144      * Returns a specific Bgpvpn */
145
146     @Path("{bgpvpnUUID}")
147     @GET
148     @Produces({ MediaType.APPLICATION_JSON })
149     //@TypeHint(OpenStackBgpvpns.class)
150     @StatusCodes({
151         @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
152         @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
153         @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
154         @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
155         @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
156     public Response showBgpvpn(
157             @PathParam("bgpvpnUUID") String bgpvpnUUID,
158             // return fields
159             @QueryParam("fields") List<String> fields
160             ) {
161         INeutronBgpvpnCRUD bgpvpnInterface = getNeutronInterfaces().getBgpvpnInterface();
162         if (!bgpvpnInterface.bgpvpnExists(bgpvpnUUID)) {
163             throw new ResourceNotFoundException("UUID does not exist");
164         }
165         if (fields.size() > 0) {
166             NeutronBgpvpn ans = bgpvpnInterface.getBgpvpn(bgpvpnUUID);
167             return Response.status(HttpURLConnection.HTTP_OK).entity(
168                     new NeutronBgpvpnRequest(extractFields(ans, fields))).build();
169         } else {
170             return Response.status(HttpURLConnection.HTTP_OK).entity(
171                     new NeutronBgpvpnRequest(bgpvpnInterface.getBgpvpn(bgpvpnUUID))).build();
172         }
173     }
174
175     /**
176      * Creates new Bgpvpns */
177     @POST
178     @Produces({ MediaType.APPLICATION_JSON })
179     @Consumes({ MediaType.APPLICATION_JSON })
180     @TypeHint(NeutronBgpvpn.class)
181     @StatusCodes({
182         @ResponseCode(code = HttpURLConnection.HTTP_CREATED, condition = "Created"),
183         @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
184     public Response createBgpvpns(final NeutronBgpvpnRequest input) {
185         INeutronBgpvpnCRUD bgpvpnInterface = getNeutronInterfaces().getBgpvpnInterface();
186         if (input.isSingleton()) {
187             NeutronBgpvpn singleton = input.getSingleton();
188
189             // add bgpvpn to MDSAL
190             singleton.initDefaults();
191             bgpvpnInterface.addBgpvpn(singleton);
192         } else {
193             // add items to MDSAL
194             for (NeutronBgpvpn test : input.getBulk()) {
195                 test.initDefaults();
196                 bgpvpnInterface.addBgpvpn(test);
197             }
198         }
199         return Response.status(HttpURLConnection.HTTP_CREATED).entity(input).build();
200     }
201
202     /**
203      * Updates a Bgpvpn */
204     @Path("{bgpvpnUUID}")
205     @PUT
206     @Produces({ MediaType.APPLICATION_JSON })
207     @Consumes({ MediaType.APPLICATION_JSON })
208     //@TypeHint(OpenStackBgpvpns.class)
209     @StatusCodes({
210         @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
211         @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
212     public Response updateBgpvpn(
213             @PathParam("bgpvpnUUID") String bgpvpnUUID, final NeutronBgpvpnRequest input
214             ) {
215         INeutronBgpvpnCRUD bgpvpnInterface = getNeutronInterfaces().getBgpvpnInterface();
216
217         NeutronBgpvpn updatedObject = input.getSingleton();
218         NeutronBgpvpn original = bgpvpnInterface.getBgpvpn(bgpvpnUUID);
219
220         /*
221          *  note: what we get appears to not be a delta but
222          * rather an incomplete updated object.  So we need to set
223          * the ID to complete the object and then send that down
224          * for folks to check
225          */
226
227         updatedObject.setID(bgpvpnUUID);
228         updatedObject.setTenantID(original.getTenantID());
229         //Fill in defaults if they're missing in update
230         updatedObject.initDefaults();
231
232         // update bgpvpn object
233         bgpvpnInterface.updateBgpvpn(bgpvpnUUID, updatedObject);
234
235         return Response.status(HttpURLConnection.HTTP_OK).entity(
236                 new NeutronBgpvpnRequest(bgpvpnInterface.getBgpvpn(bgpvpnUUID))).build();
237     }
238
239     /**
240      * Deletes a Bgpvpn */
241
242     @Path("{bgpvpnUUID}")
243     @DELETE
244     @StatusCodes({
245         @ResponseCode(code = HttpURLConnection.HTTP_NO_CONTENT, condition = "No Content"),
246         @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
247     public Response deleteBgpvpn(
248             @PathParam("bgpvpnUUID") String bgpvpnUUID) {
249         INeutronBgpvpnCRUD bgpvpnInterface = getNeutronInterfaces().getBgpvpnInterface();
250
251         if (!bgpvpnInterface.removeBgpvpn(bgpvpnUUID)) {
252             throw new InternalServerErrorException("Could not delete bgpvpn");
253         }
254         return Response.status(HttpURLConnection.HTTP_NO_CONTENT).build();
255     }
256 }