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.util.ArrayList;
12 import java.util.HashMap;
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.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.INeutronMeteringLabelRuleAware;
35 import org.opendaylight.neutron.spi.INeutronMeteringLabelRuleCRUD;
36 import org.opendaylight.neutron.spi.NeutronCRUDInterfaces;
37 import org.opendaylight.neutron.spi.NeutronMeteringLabelRule;
40 * Neutron Northbound REST APIs for Metering Lable Rules.<br>
41 * This class provides REST APIs for managing neutron metering label rules
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-label-rules")
58 public class NeutronMeteringLabelRulesNorthbound {
60 private NeutronMeteringLabelRule extractFields(NeutronMeteringLabelRule o, List<String> fields) {
61 return o.extractFields(fields);
68 * Returns a list of all metering label rules */
71 @Produces({ MediaType.APPLICATION_JSON })
72 //@TypeHint(OpenStackNetworks.class)
74 @ResponseCode(code = 200, condition = "Operation successful"),
75 @ResponseCode(code = 401, condition = "Unauthorized"),
76 @ResponseCode(code = 501, condition = "Not Implemented"),
77 @ResponseCode(code = 503, condition = "No providers available") })
78 public Response listMeteringLabelRules(
80 @QueryParam("fields") List<String> fields,
82 @QueryParam("id") String queryID,
83 @QueryParam("direction") String queryDirection,
84 @QueryParam("remote_ip_prefix") String queryRemoteIPPrefix,
85 @QueryParam("metering_label_id") String queryLabelID
86 // pagination and sorting are TODO
88 INeutronMeteringLabelRuleCRUD ruleInterface = NeutronCRUDInterfaces.getINeutronMeteringLabelRuleCRUD(this);
89 if (ruleInterface == null) {
90 throw new ServiceUnavailableException("NeutronMeteringLabelRule CRUD Interface "
91 + RestMessages.SERVICEUNAVAILABLE.toString());
93 List<NeutronMeteringLabelRule> allNeutronMeteringLabelRules = ruleInterface.getAllNeutronMeteringLabelRules();
94 List<NeutronMeteringLabelRule> ans = new ArrayList<NeutronMeteringLabelRule>();
95 Iterator<NeutronMeteringLabelRule> i = allNeutronMeteringLabelRules.iterator();
97 NeutronMeteringLabelRule oSS = i.next();
98 if ((queryID == null || queryID.equals(oSS.getMeteringLabelRuleUUID())) &&
99 (queryDirection == null || queryDirection.equals(oSS.getMeteringLabelRuleDirection())) &&
100 (queryRemoteIPPrefix == null || queryRemoteIPPrefix.equals(oSS.getMeteringLabelRuleRemoteIPPrefix())) &&
101 (queryLabelID == null || queryLabelID.equals(oSS.getMeteringLabelRuleLabelID()))) {
102 if (fields.size() > 0)
103 ans.add(extractFields(oSS,fields));
108 //TODO: apply pagination to results
109 return Response.status(200).entity(
110 new NeutronMeteringLabelRuleRequest(ans)).build();
114 * Returns a specific metering label rule */
118 @Produces({ MediaType.APPLICATION_JSON })
120 @ResponseCode(code = 200, condition = "Operation successful"),
121 @ResponseCode(code = 401, condition = "Unauthorized"),
122 @ResponseCode(code = 403, condition = "Forbidden"),
123 @ResponseCode(code = 404, condition = "Not Found"),
124 @ResponseCode(code = 501, condition = "Not Implemented"),
125 @ResponseCode(code = 503, condition = "No providers available") })
126 public Response showMeteringLabelRule(
127 @PathParam("ruleUUID") String ruleUUID,
129 @QueryParam("fields") List<String> fields) {
130 INeutronMeteringLabelRuleCRUD ruleInterface = NeutronCRUDInterfaces.getINeutronMeteringLabelRuleCRUD(this);
131 if (ruleInterface == null) {
132 throw new ServiceUnavailableException("MeteringLabelRule CRUD Interface "
133 + RestMessages.SERVICEUNAVAILABLE.toString());
135 if (!ruleInterface.neutronMeteringLabelRuleExists(ruleUUID)) {
136 throw new ResourceNotFoundException("MeteringLabelRule UUID not found");
138 if (fields.size() > 0) {
139 NeutronMeteringLabelRule ans = ruleInterface.getNeutronMeteringLabelRule(ruleUUID);
140 return Response.status(200).entity(
141 new NeutronMeteringLabelRuleRequest(extractFields(ans, fields))).build();
143 return Response.status(200).entity(
144 new NeutronMeteringLabelRuleRequest(ruleInterface.getNeutronMeteringLabelRule(ruleUUID))).build();
149 * Creates new metering label rule */
151 @Produces({ MediaType.APPLICATION_JSON })
152 @Consumes({ MediaType.APPLICATION_JSON })
153 //@TypeHint(NeutronNetwork.class)
155 @ResponseCode(code = 201, condition = "Created"),
156 @ResponseCode(code = 400, condition = "Bad Request"),
157 @ResponseCode(code = 401, condition = "Unauthorized"),
158 @ResponseCode(code = 501, condition = "Not Implemented"),
159 @ResponseCode(code = 503, condition = "No providers available") })
160 public Response createMeteringLabelRule(final NeutronMeteringLabelRuleRequest input) {
161 INeutronMeteringLabelRuleCRUD meteringLabelRuleInterface = NeutronCRUDInterfaces.getINeutronMeteringLabelRuleCRUD(this);
162 if (meteringLabelRuleInterface == null) {
163 throw new ServiceUnavailableException("MeteringLabelRule CRUD Interface "
164 + RestMessages.SERVICEUNAVAILABLE.toString());
166 if (input.isSingleton()) {
167 NeutronMeteringLabelRule singleton = input.getSingleton();
170 * verify that the meteringLabelRule doesn't already exist (issue: is deeper inspection necessary?)
172 if (meteringLabelRuleInterface.neutronMeteringLabelRuleExists(singleton.getMeteringLabelRuleUUID()))
173 throw new BadRequestException("meteringLabelRule UUID already exists");
174 Object[] instances = NeutronUtil.getInstances(INeutronMeteringLabelRuleAware.class, this);
175 if (instances != null) {
176 if (instances.length > 0) {
177 for (Object instance : instances) {
178 INeutronMeteringLabelRuleAware service = (INeutronMeteringLabelRuleAware) instance;
179 int status = service.canCreateMeteringLabelRule(singleton);
180 if (status < 200 || status > 299)
181 return Response.status(status).build();
184 throw new ServiceUnavailableException("No providers registered. Please try again later");
187 throw new ServiceUnavailableException("Couldn't get providers list. Please try again later");
191 * add meteringLabelRule to the cache
193 meteringLabelRuleInterface.addNeutronMeteringLabelRule(singleton);
194 if (instances != null) {
195 for (Object instance : instances) {
196 INeutronMeteringLabelRuleAware service = (INeutronMeteringLabelRuleAware) instance;
197 service.neutronMeteringLabelRuleCreated(singleton);
203 * only singleton meteringLabelRule creates supported
205 throw new BadRequestException("Only singleton meteringLabelRule creates supported");
207 return Response.status(201).entity(input).build();
211 * Deletes a Metering Label rule */
216 @ResponseCode(code = 204, condition = "No Content"),
217 @ResponseCode(code = 401, condition = "Unauthorized"),
218 @ResponseCode(code = 404, condition = "Not Found"),
219 @ResponseCode(code = 409, condition = "Conflict"),
220 @ResponseCode(code = 501, condition = "Not Implemented"),
221 @ResponseCode(code = 503, condition = "No providers available") })
222 public Response deleteMeteringLabelRule(
223 @PathParam("ruleUUID") String ruleUUID) {
224 INeutronMeteringLabelRuleCRUD meteringLabelRuleInterface = NeutronCRUDInterfaces.getINeutronMeteringLabelRuleCRUD(this);
225 if (meteringLabelRuleInterface == null) {
226 throw new ServiceUnavailableException("MeteringLabelRule CRUD Interface "
227 + RestMessages.SERVICEUNAVAILABLE.toString());
231 * verify that the meteringLabelRule exists and is not in use before removing it
233 if (!meteringLabelRuleInterface.neutronMeteringLabelRuleExists(ruleUUID))
234 throw new ResourceNotFoundException("MeteringLabelRule UUID not found");
235 NeutronMeteringLabelRule singleton = meteringLabelRuleInterface.getNeutronMeteringLabelRule(ruleUUID);
236 Object[] instances = NeutronUtil.getInstances(INeutronMeteringLabelRuleAware.class, this);
237 if (instances != null) {
238 if (instances.length > 0) {
239 for (Object instance : instances) {
240 INeutronMeteringLabelRuleAware service = (INeutronMeteringLabelRuleAware) instance;
241 int status = service.canDeleteMeteringLabelRule(singleton);
242 if (status < 200 || status > 299)
243 return Response.status(status).build();
246 throw new ServiceUnavailableException("No providers registered. Please try again later");
249 throw new ServiceUnavailableException("Couldn't get providers list. Please try again later");
251 meteringLabelRuleInterface.removeNeutronMeteringLabelRule(ruleUUID);
252 if (instances != null) {
253 for (Object instance : instances) {
254 INeutronMeteringLabelRuleAware service = (INeutronMeteringLabelRuleAware) instance;
255 service.neutronMeteringLabelRuleDeleted(singleton);
258 return Response.status(204).build();