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.ws.rs.Consumes;
14 import javax.ws.rs.DELETE;
15 import javax.ws.rs.GET;
16 import javax.ws.rs.POST;
17 import javax.ws.rs.Path;
18 import javax.ws.rs.PathParam;
19 import javax.ws.rs.Produces;
20 import javax.ws.rs.QueryParam;
21 import javax.ws.rs.core.MediaType;
22 import javax.ws.rs.core.Response;
23 import org.codehaus.enunciate.jaxrs.ResponseCode;
24 import org.codehaus.enunciate.jaxrs.StatusCodes;
25 import org.opendaylight.neutron.spi.INeutronMeteringLabelRuleCRUD;
26 import org.opendaylight.neutron.spi.NeutronMeteringLabelRule;
29 * Neutron Northbound REST APIs for Metering Lable Rules.<br>
31 @Path("/metering/metering-label-rules")
32 public final class NeutronMeteringLabelRulesNorthbound extends AbstractNeutronNorthbound<NeutronMeteringLabelRule,
33 NeutronMeteringLabelRuleRequest, INeutronMeteringLabelRuleCRUD> {
35 private static final String RESOURCE_NAME = "Metering Label Rule";
38 protected String getResourceName() {
43 * Returns a list of all metering label rules.
46 @Produces({ MediaType.APPLICATION_JSON })
47 //@TypeHint(OpenStackNetworks.class)
48 @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
49 @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
50 @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
51 @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
52 public Response listMeteringLabelRules(
54 @QueryParam("fields") List<String> fields,
56 @QueryParam("id") String queryID,
57 @QueryParam("direction") String queryDirection,
58 @QueryParam("remote_ip_prefix") String queryRemoteIpPrefix,
59 @QueryParam("metering_label_id") String queryLabelID
60 // pagination and sorting are TODO
62 INeutronMeteringLabelRuleCRUD ruleInterface = getNeutronCRUD();
63 List<NeutronMeteringLabelRule> allNeutronMeteringLabelRule = ruleInterface.getAll();
64 List<NeutronMeteringLabelRule> ans = new ArrayList<>();
65 for (NeutronMeteringLabelRule rule : allNeutronMeteringLabelRule) {
66 if ((queryID == null || queryID.equals(rule.getID()))
67 && (queryDirection == null || queryDirection.equals(rule.getMeteringLabelRuleDirection()))
68 && (queryRemoteIpPrefix == null
69 || queryRemoteIpPrefix.equals(rule.getMeteringLabelRuleRemoteIpPrefix()))
70 && (queryLabelID == null || queryLabelID.equals(rule.getMeteringLabelRuleLabelID()))) {
71 if (fields.size() > 0) {
72 ans.add(rule.extractFields(fields));
78 //TODO: apply pagination to results
79 return Response.status(HttpURLConnection.HTTP_OK).entity(new NeutronMeteringLabelRuleRequest(ans)).build();
83 * Returns a specific metering label rule.
87 @Produces({ MediaType.APPLICATION_JSON })
88 @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
89 @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
90 @ResponseCode(code = HttpURLConnection.HTTP_FORBIDDEN, condition = "Forbidden"),
91 @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
92 @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
93 @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
94 public Response showMeteringLabelRule(@PathParam("ruleUUID") String ruleUUID,
96 @QueryParam("fields") List<String> fields) {
97 return show(ruleUUID, fields);
101 * Creates new metering label rule.
104 @Produces({ MediaType.APPLICATION_JSON })
105 @Consumes({ MediaType.APPLICATION_JSON })
106 //@TypeHint(NeutronNetwork.class)
107 @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_CREATED, condition = "Created"),
108 @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
109 public Response createMeteringLabelRule(final NeutronMeteringLabelRuleRequest input) {
110 INeutronMeteringLabelRuleCRUD meteringLabelRuleInterface = getNeutronCRUD();
111 if (input.isSingleton()) {
112 NeutronMeteringLabelRule singleton = input.getSingleton();
115 * add meteringLabelRule to the cache
117 meteringLabelRuleInterface.add(singleton);
121 * only singleton meteringLabelRule creates supported
123 throw new BadRequestException("Only singleton meteringLabelRule creates supported");
125 return Response.status(HttpURLConnection.HTTP_CREATED).entity(input).build();
129 * Deletes a Metering Label rule.
133 @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_NO_CONTENT, condition = "No Content"),
134 @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
135 @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
136 public Response deleteMeteringLabelRule(@PathParam("ruleUUID") String ruleUUID) {
137 return delete(ruleUUID);