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 {
62 private static final String RESOURCE_NAME = "Bgpvpn";
63 private NeutronBgpvpn extractFields(NeutronBgpvpn o, List<String> fields) {
64 return o.extractFields(fields);
67 private NeutronCRUDInterfaces getNeutronInterfaces() {
68 NeutronCRUDInterfaces answer = new NeutronCRUDInterfaces().fetchINeutronBgpvpnCRUD(this);
69 if (answer.getBgpvpnInterface() == null) {
70 throw new ServiceUnavailableException("Service is unavailable");
76 * Returns a list of all Bgpvpns */
79 @Produces({ MediaType.APPLICATION_JSON })
80 //@TypeHint(OpenStackBgpvpns.class)
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(
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,
98 @QueryParam("limit") Integer limit,
99 @QueryParam("marker") String marker,
100 @DefaultValue("false") @QueryParam("page_reverse") Boolean pageReverse
101 // sorting not supported
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);
115 if (queryAutoAggregate != null) {
116 bAutoAggregate = Boolean.valueOf(queryAutoAggregate);
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));
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();
139 return Response.status(HttpURLConnection.HTTP_OK).entity(new NeutronBgpvpnRequest(ans)).build();
144 * Returns a specific Bgpvpn */
146 @Path("{bgpvpnUUID}")
148 @Produces({ MediaType.APPLICATION_JSON })
149 //@TypeHint(OpenStackBgpvpns.class)
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,
159 @QueryParam("fields") List<String> fields
161 INeutronBgpvpnCRUD bgpvpnInterface = getNeutronInterfaces().getBgpvpnInterface();
162 if (!bgpvpnInterface.bgpvpnExists(bgpvpnUUID)) {
163 throw new ResourceNotFoundException("UUID does not exist");
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();
170 return Response.status(HttpURLConnection.HTTP_OK).entity(
171 new NeutronBgpvpnRequest(bgpvpnInterface.getBgpvpn(bgpvpnUUID))).build();
176 * Creates new Bgpvpns */
178 @Produces({ MediaType.APPLICATION_JSON })
179 @Consumes({ MediaType.APPLICATION_JSON })
180 @TypeHint(NeutronBgpvpn.class)
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();
189 // add bgpvpn to MDSAL
190 singleton.initDefaults();
191 bgpvpnInterface.addBgpvpn(singleton);
193 // add items to MDSAL
194 for (NeutronBgpvpn test : input.getBulk()) {
196 bgpvpnInterface.addBgpvpn(test);
199 return Response.status(HttpURLConnection.HTTP_CREATED).entity(input).build();
203 * Updates a Bgpvpn */
204 @Path("{bgpvpnUUID}")
206 @Produces({ MediaType.APPLICATION_JSON })
207 @Consumes({ MediaType.APPLICATION_JSON })
208 //@TypeHint(OpenStackBgpvpns.class)
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
215 INeutronBgpvpnCRUD bgpvpnInterface = getNeutronInterfaces().getBgpvpnInterface();
217 NeutronBgpvpn updatedObject = input.getSingleton();
218 NeutronBgpvpn original = bgpvpnInterface.getBgpvpn(bgpvpnUUID);
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
227 updatedObject.setID(bgpvpnUUID);
228 updatedObject.setTenantID(original.getTenantID());
229 //Fill in defaults if they're missing in update
230 updatedObject.initDefaults();
232 // update bgpvpn object
233 bgpvpnInterface.updateBgpvpn(bgpvpnUUID, updatedObject);
235 return Response.status(HttpURLConnection.HTTP_OK).entity(
236 new NeutronBgpvpnRequest(bgpvpnInterface.getBgpvpn(bgpvpnUUID))).build();
240 * Deletes a Bgpvpn */
242 @Path("{bgpvpnUUID}")
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();
251 if (!bgpvpnInterface.removeBgpvpn(bgpvpnUUID)) {
252 throw new InternalServerErrorException("Could not delete bgpvpn");
254 return Response.status(HttpURLConnection.HTTP_NO_CONTENT).build();