2 * Copyright (c) 2015 IBM Corporation 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
8 package org.opendaylight.neutron.northbound.api;
10 import java.net.HttpURLConnection;
11 import java.util.ArrayList;
12 import java.util.List;
13 import javax.inject.Inject;
14 import javax.inject.Singleton;
15 import javax.ws.rs.Consumes;
16 import javax.ws.rs.DELETE;
17 import javax.ws.rs.GET;
18 import javax.ws.rs.POST;
19 import javax.ws.rs.Path;
20 import javax.ws.rs.PathParam;
21 import javax.ws.rs.Produces;
22 import javax.ws.rs.QueryParam;
23 import javax.ws.rs.core.MediaType;
24 import javax.ws.rs.core.Response;
25 import org.codehaus.enunciate.jaxrs.ResponseCode;
26 import org.codehaus.enunciate.jaxrs.StatusCodes;
27 import org.opendaylight.neutron.spi.INeutronMeteringLabelRuleCRUD;
28 import org.opendaylight.neutron.spi.NeutronMeteringLabelRule;
29 import org.opendaylight.yangtools.yang.common.OperationFailedException;
30 import org.ops4j.pax.cdi.api.OsgiService;
33 * Neutron Northbound REST APIs for Metering Lable Rules.<br>
36 @Path("/metering/metering-label-rules")
37 public final class NeutronMeteringLabelRulesNorthbound extends AbstractNeutronNorthbound<NeutronMeteringLabelRule,
38 NeutronMeteringLabelRuleRequest, INeutronMeteringLabelRuleCRUD> {
40 private static final String RESOURCE_NAME = "Metering Label Rule";
43 public NeutronMeteringLabelRulesNorthbound(@OsgiService INeutronMeteringLabelRuleCRUD neutronCRUD) {
48 protected String getResourceName() {
53 * Returns a list of all metering label rules.
56 @Produces({ MediaType.APPLICATION_JSON })
57 //@TypeHint(OpenStackNetworks.class)
58 @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
59 @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
60 @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
61 @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
62 public Response listMeteringLabelRules(
64 @QueryParam("fields") List<String> fields,
66 @QueryParam("id") String queryID,
67 @QueryParam("direction") String queryDirection,
68 @QueryParam("remote_ip_prefix") String queryRemoteIpPrefix,
69 @QueryParam("metering_label_id") String queryLabelID
70 // pagination and sorting are TODO
72 INeutronMeteringLabelRuleCRUD ruleInterface = getNeutronCRUD();
73 List<NeutronMeteringLabelRule> allNeutronMeteringLabelRule = ruleInterface.getAll();
74 List<NeutronMeteringLabelRule> ans = new ArrayList<>();
75 for (NeutronMeteringLabelRule rule : allNeutronMeteringLabelRule) {
76 if ((queryID == null || queryID.equals(rule.getID()))
77 && (queryDirection == null || queryDirection.equals(rule.getMeteringLabelRuleDirection()))
78 && (queryRemoteIpPrefix == null
79 || queryRemoteIpPrefix.equals(rule.getMeteringLabelRuleRemoteIpPrefix()))
80 && (queryLabelID == null || queryLabelID.equals(rule.getMeteringLabelRuleLabelID()))) {
81 if (fields.size() > 0) {
82 ans.add(rule.extractFields(fields));
88 //TODO: apply pagination to results
89 return Response.status(HttpURLConnection.HTTP_OK).entity(new NeutronMeteringLabelRuleRequest(ans)).build();
93 * Returns a specific metering label rule.
97 @Produces({ MediaType.APPLICATION_JSON })
98 @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
99 @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
100 @ResponseCode(code = HttpURLConnection.HTTP_FORBIDDEN, condition = "Forbidden"),
101 @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
102 @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
103 @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
104 public Response showMeteringLabelRule(@PathParam("ruleUUID") String ruleUUID,
106 @QueryParam("fields") List<String> fields) {
107 return show(ruleUUID, fields);
111 * Creates new metering label rule.
114 @Produces({ MediaType.APPLICATION_JSON })
115 @Consumes({ MediaType.APPLICATION_JSON })
116 //@TypeHint(NeutronNetwork.class)
117 @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_CREATED, condition = "Created"),
118 @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
119 public Response createMeteringLabelRule(final NeutronMeteringLabelRuleRequest input) {
120 if (input.isSingleton()) {
121 NeutronMeteringLabelRule singleton = input.getSingleton();
123 // add meteringLabelRule to the cache
124 INeutronMeteringLabelRuleCRUD meteringLabelRuleInterface = getNeutronCRUD();
125 meteringLabelRuleInterface.add(singleton);
126 return Response.status(HttpURLConnection.HTTP_CREATED).entity(input).build();
128 } catch (OperationFailedException e) {
129 throw new DatastoreOperationFailedWebApplicationException(e);
132 // only singleton meteringLabelRule creates supported
133 throw new BadRequestException("Only singleton meteringLabelRule creates supported");
138 * Deletes a Metering Label rule.
142 @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_NO_CONTENT, condition = "No Content"),
143 @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
144 @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
145 public Response deleteMeteringLabelRule(@PathParam("ruleUUID") String ruleUUID) {
146 return delete(ruleUUID);