2 * Copyright IBM Corporation, 2015. 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;
13 import java.util.ArrayList;
14 import java.util.Iterator;
15 import java.util.List;
17 import javax.ws.rs.Consumes;
18 import javax.ws.rs.DELETE;
19 import javax.ws.rs.GET;
20 import javax.ws.rs.POST;
21 import javax.ws.rs.Path;
22 import javax.ws.rs.PathParam;
23 import javax.ws.rs.Produces;
24 import javax.ws.rs.QueryParam;
25 import javax.ws.rs.core.Context;
26 import javax.ws.rs.core.MediaType;
27 import javax.ws.rs.core.Response;
28 import javax.ws.rs.core.UriInfo;
30 import org.codehaus.enunciate.jaxrs.ResponseCode;
31 import org.codehaus.enunciate.jaxrs.StatusCodes;
32 import org.codehaus.enunciate.jaxrs.TypeHint;
34 import org.opendaylight.neutron.spi.INeutronMeteringLabelAware;
35 import org.opendaylight.neutron.spi.INeutronMeteringLabelCRUD;
36 import org.opendaylight.neutron.spi.NeutronCRUDInterfaces;
37 import org.opendaylight.neutron.spi.NeutronMeteringLabel;
40 * Neutron Northbound REST APIs for Metering Lables.<br>
41 * This class provides REST APIs for managing neutron metering labels
45 * Authentication scheme : <b>HTTP Basic</b><br>
46 * Authentication realm : <b>opendaylight</b><br>
47 * Transport : <b>HTTP and HTTPS</b><br>
49 * HTTPS Authentication is disabled by default. Administrator can enable it in
50 * tomcat-server.xml after adding a proper keystore / SSL certificate from a
51 * trusted authority.<br>
53 * http://tomcat.apache.org/tomcat-7.0-doc/ssl-howto.html#Configuration
57 @Path("/metering/metering-labels")
58 public class NeutronMeteringLabelsNorthbound {
60 private static final int HTTP_OK_BOTTOM = 200;
61 private static final int HTTP_OK_TOP = 299;
63 private NeutronMeteringLabel extractFields(NeutronMeteringLabel o, List<String> fields) {
64 return o.extractFields(fields);
67 private NeutronCRUDInterfaces getNeutronInterfaces() {
68 NeutronCRUDInterfaces answer = new NeutronCRUDInterfaces().fetchINeutronMeteringLabelCRUD(this);
69 if (answer.getMeteringLabelInterface() == null) {
70 throw new ServiceUnavailableException("NeutronMeteringLabel CRUD Interface "
71 + RestMessages.SERVICEUNAVAILABLE.toString());
80 * Returns a list of all metering labels */
83 @Produces({ MediaType.APPLICATION_JSON })
84 //@TypeHint(OpenStackNetworks.class)
86 @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
87 @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
88 @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
89 @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
90 public Response listMeteringLabels(
92 @QueryParam("fields") List<String> fields,
94 @QueryParam("id") String queryID,
95 @QueryParam("name") String queryName,
96 @QueryParam("tenant_id") String queryTenantID,
97 @QueryParam("description") String queryDescription
98 // pagination and sorting are TODO
100 INeutronMeteringLabelCRUD labelInterface = getNeutronInterfaces().getMeteringLabelInterface();
101 List<NeutronMeteringLabel> allNeutronMeteringLabels = labelInterface.getAllNeutronMeteringLabels();
102 List<NeutronMeteringLabel> ans = new ArrayList<NeutronMeteringLabel>();
103 Iterator<NeutronMeteringLabel> i = allNeutronMeteringLabels.iterator();
104 while (i.hasNext()) {
105 NeutronMeteringLabel oSS = i.next();
106 if ((queryID == null || queryID.equals(oSS.getMeteringLabelUUID())) &&
107 (queryName == null || queryName.equals(oSS.getMeteringLabelName())) &&
108 (queryDescription == null || queryDescription.equals(oSS.getMeteringLabelDescription())) &&
109 (queryTenantID == null || queryTenantID.equals(oSS.getMeteringLabelTenantID()))) {
110 if (fields.size() > 0) {
111 ans.add(extractFields(oSS,fields));
117 //TODO: apply pagination to results
118 return Response.status(HttpURLConnection.HTTP_OK).entity(
119 new NeutronMeteringLabelRequest(ans)).build();
123 * Returns a specific metering label */
127 @Produces({ MediaType.APPLICATION_JSON })
129 @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
130 @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
131 @ResponseCode(code = HttpURLConnection.HTTP_FORBIDDEN, condition = "Forbidden"),
132 @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
133 @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
134 @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
135 public Response showMeteringLabel(
136 @PathParam("labelUUID") String labelUUID,
138 @QueryParam("fields") List<String> fields) {
139 INeutronMeteringLabelCRUD labelInterface = getNeutronInterfaces().getMeteringLabelInterface();
140 if (!labelInterface.neutronMeteringLabelExists(labelUUID)) {
141 throw new ResourceNotFoundException("MeteringLabel UUID not found");
143 if (fields.size() > 0) {
144 NeutronMeteringLabel ans = labelInterface.getNeutronMeteringLabel(labelUUID);
145 return Response.status(HttpURLConnection.HTTP_OK).entity(
146 new NeutronMeteringLabelRequest(extractFields(ans, fields))).build();
148 return Response.status(HttpURLConnection.HTTP_OK).entity(
149 new NeutronMeteringLabelRequest(labelInterface.getNeutronMeteringLabel(labelUUID))).build();
154 * Creates new metering label */
156 @Produces({ MediaType.APPLICATION_JSON })
157 @Consumes({ MediaType.APPLICATION_JSON })
158 //@TypeHint(NeutronNetwork.class)
160 @ResponseCode(code = HttpURLConnection.HTTP_CREATED, condition = "Created"),
161 @ResponseCode(code = HttpURLConnection.HTTP_BAD_REQUEST, condition = "Bad Request"),
162 @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
163 @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
164 @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
165 public Response createMeteringLabel(final NeutronMeteringLabelRequest input) {
166 INeutronMeteringLabelCRUD meteringLabelInterface = getNeutronInterfaces().getMeteringLabelInterface();
167 if (input.isSingleton()) {
168 NeutronMeteringLabel singleton = input.getSingleton();
171 * verify that the meteringLabel doesn't already exist (issue: is deeper inspection necessary?)
173 if (meteringLabelInterface.neutronMeteringLabelExists(singleton.getMeteringLabelUUID())) {
174 throw new BadRequestException("meteringLabel UUID already exists");
176 Object[] instances = NeutronUtil.getInstances(INeutronMeteringLabelAware.class, this);
177 if (instances != null) {
178 if (instances.length > 0) {
179 for (Object instance : instances) {
180 INeutronMeteringLabelAware service = (INeutronMeteringLabelAware) instance;
181 int status = service.canCreateMeteringLabel(singleton);
182 if (status < HTTP_OK_BOTTOM || status > HTTP_OK_TOP) {
183 return Response.status(status).build();
187 throw new ServiceUnavailableException("No providers registered. Please try again later");
190 throw new ServiceUnavailableException("Couldn't get providers list. Please try again later");
194 * add meteringLabel to the cache
196 meteringLabelInterface.addNeutronMeteringLabel(singleton);
197 if (instances != null) {
198 for (Object instance : instances) {
199 INeutronMeteringLabelAware service = (INeutronMeteringLabelAware) instance;
200 service.neutronMeteringLabelCreated(singleton);
206 * only singleton meteringLabel creates supported
208 throw new BadRequestException("Only singleton meteringLabel creates supported");
210 return Response.status(HttpURLConnection.HTTP_CREATED).entity(input).build();
214 * Deletes a Metering Label */
219 @ResponseCode(code = HttpURLConnection.HTTP_NO_CONTENT, condition = "No Content"),
220 @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
221 @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
222 @ResponseCode(code = HttpURLConnection.HTTP_CONFLICT, condition = "Conflict"),
223 @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
224 @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
225 public Response deleteMeteringLabel(
226 @PathParam("labelUUID") String labelUUID) {
227 INeutronMeteringLabelCRUD meteringLabelInterface = getNeutronInterfaces().getMeteringLabelInterface();
230 * verify that the meteringLabel exists and is not in use before removing it
232 if (!meteringLabelInterface.neutronMeteringLabelExists(labelUUID)) {
233 throw new ResourceNotFoundException("MeteringLabel UUID not found");
235 NeutronMeteringLabel singleton = meteringLabelInterface.getNeutronMeteringLabel(labelUUID);
236 Object[] instances = NeutronUtil.getInstances(INeutronMeteringLabelAware.class, this);
237 if (instances != null) {
238 if (instances.length > 0) {
239 for (Object instance : instances) {
240 INeutronMeteringLabelAware service = (INeutronMeteringLabelAware) instance;
241 int status = service.canDeleteMeteringLabel(singleton);
242 if (status < HTTP_OK_BOTTOM || status > HTTP_OK_TOP) {
243 return Response.status(status).build();
247 throw new ServiceUnavailableException("No providers registered. Please try again later");
250 throw new ServiceUnavailableException("Couldn't get providers list. Please try again later");
252 meteringLabelInterface.removeNeutronMeteringLabel(labelUUID);
253 if (instances != null) {
254 for (Object instance : instances) {
255 INeutronMeteringLabelAware service = (INeutronMeteringLabelAware) instance;
256 service.neutronMeteringLabelDeleted(singleton);
259 return Response.status(HttpURLConnection.HTTP_NO_CONTENT).build();