2 * Copyright (c) 2015 Ericsson India Global Services Pvt Ltd. 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
9 package org.opendaylight.neutron.northbound.api;
11 import java.net.HttpURLConnection;
12 import java.util.ArrayList;
13 import java.util.Iterator;
14 import java.util.List;
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;
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;
39 * Neutron Northbound REST APIs for Bgpvpn.<br>
40 * This class provides REST APIs for managing neutron Bgpvpns
44 * Authentication scheme : <b>HTTP Basic</b><br>
45 * Authentication realm : <b>opendaylight</b><br>
46 * Transport : <b>HTTP and HTTPS</b><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>
52 * http://tomcat.apache.org/tomcat-7.0-doc/ssl-howto.html#Configuration
57 public class NeutronBgpvpnsNorthbound
58 extends AbstractNeutronNorthbound<NeutronBgpvpn, NeutronBgpvpnRequest, INeutronBgpvpnCRUD> {
63 private static final String RESOURCE_NAME = "Bgpvpn";
66 protected String getResourceName() {
71 protected NeutronBgpvpn extractFields(NeutronBgpvpn o, List<String> fields) {
72 return o.extractFields(fields);
76 protected NeutronBgpvpnRequest newNeutronRequest(NeutronBgpvpn o) {
77 return new NeutronBgpvpnRequest(o);
81 protected INeutronBgpvpnCRUD getNeutronCRUD() {
82 NeutronCRUDInterfaces answer = new NeutronCRUDInterfaces().fetchINeutronBgpvpnCRUD(this);
83 if (answer.getBgpvpnInterface() == null) {
84 throw new ServiceUnavailableException(serviceUnavailable());
86 return answer.getBgpvpnInterface();
90 * Returns a list of all Bgpvpns */
93 @Produces({ MediaType.APPLICATION_JSON })
94 //@TypeHint(OpenStackBgpvpns.class)
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(
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,
112 @QueryParam("limit") Integer limit,
113 @QueryParam("marker") String marker,
114 @DefaultValue("false") @QueryParam("page_reverse") Boolean pageReverse
115 // sorting not supported
117 INeutronBgpvpnCRUD bgpvpnInterface = getNeutronCRUD();
118 List<NeutronBgpvpn> allBgpvpns = bgpvpnInterface.getAllBgpvpns();
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);
129 if (queryAutoAggregate != null) {
130 bAutoAggregate = Boolean.valueOf(queryAutoAggregate);
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));
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();
153 return Response.status(HttpURLConnection.HTTP_OK).entity(new NeutronBgpvpnRequest(ans)).build();
158 * Returns a specific Bgpvpn */
160 @Path("{bgpvpnUUID}")
162 @Produces({ MediaType.APPLICATION_JSON })
163 //@TypeHint(OpenStackBgpvpns.class)
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,
173 @QueryParam("fields") List<String> fields
175 INeutronBgpvpnCRUD bgpvpnInterface = getNeutronCRUD();
176 if (!bgpvpnInterface.bgpvpnExists(bgpvpnUUID)) {
177 throw new ResourceNotFoundException(uuidNoExist());
179 if (fields.size() > 0) {
180 NeutronBgpvpn ans = bgpvpnInterface.getBgpvpn(bgpvpnUUID);
181 return Response.status(HttpURLConnection.HTTP_OK).entity(
182 new NeutronBgpvpnRequest(extractFields(ans, fields))).build();
184 return Response.status(HttpURLConnection.HTTP_OK).entity(
185 new NeutronBgpvpnRequest(bgpvpnInterface.getBgpvpn(bgpvpnUUID))).build();
190 * Creates new Bgpvpns */
192 @Produces({ MediaType.APPLICATION_JSON })
193 @Consumes({ MediaType.APPLICATION_JSON })
194 @TypeHint(NeutronBgpvpn.class)
196 @ResponseCode(code = HttpURLConnection.HTTP_CREATED, condition = "Created"),
197 @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
198 public Response createBgpvpns(final NeutronBgpvpnRequest input) {
199 INeutronBgpvpnCRUD bgpvpnInterface = getNeutronCRUD();
200 if (input.isSingleton()) {
201 NeutronBgpvpn singleton = input.getSingleton();
203 // add bgpvpn to MDSAL
204 singleton.initDefaults();
205 bgpvpnInterface.addBgpvpn(singleton);
207 // add items to MDSAL
208 for (NeutronBgpvpn test : input.getBulk()) {
210 bgpvpnInterface.addBgpvpn(test);
213 return Response.status(HttpURLConnection.HTTP_CREATED).entity(input).build();
217 * Updates a Bgpvpn */
218 @Path("{bgpvpnUUID}")
220 @Produces({ MediaType.APPLICATION_JSON })
221 @Consumes({ MediaType.APPLICATION_JSON })
222 //@TypeHint(OpenStackBgpvpns.class)
224 @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
225 @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
226 public Response updateBgpvpn(
227 @PathParam("bgpvpnUUID") String bgpvpnUUID, final NeutronBgpvpnRequest input
229 INeutronBgpvpnCRUD bgpvpnInterface = getNeutronCRUD();
231 NeutronBgpvpn updatedObject = input.getSingleton();
232 NeutronBgpvpn original = bgpvpnInterface.getBgpvpn(bgpvpnUUID);
235 * note: what we get appears to not be a delta but
236 * rather an incomplete updated object. So we need to set
237 * the ID to complete the object and then send that down
241 updatedObject.setID(bgpvpnUUID);
242 updatedObject.setTenantID(original.getTenantID());
243 //Fill in defaults if they're missing in update
244 updatedObject.initDefaults();
246 // update bgpvpn object
247 bgpvpnInterface.updateBgpvpn(bgpvpnUUID, updatedObject);
249 return Response.status(HttpURLConnection.HTTP_OK).entity(
250 new NeutronBgpvpnRequest(bgpvpnInterface.getBgpvpn(bgpvpnUUID))).build();
254 * Deletes a Bgpvpn */
256 @Path("{bgpvpnUUID}")
259 @ResponseCode(code = HttpURLConnection.HTTP_NO_CONTENT, condition = "No Content"),
260 @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
261 public Response deleteBgpvpn(
262 @PathParam("bgpvpnUUID") String bgpvpnUUID) {
263 INeutronBgpvpnCRUD bgpvpnInterface = getNeutronCRUD();
265 if (!bgpvpnInterface.removeBgpvpn(bgpvpnUUID)) {
266 throw new InternalServerErrorException("Could not delete bgpvpn");
268 return Response.status(HttpURLConnection.HTTP_NO_CONTENT).build();