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.ops4j.pax.cdi.api.OsgiService;
32 * Neutron Northbound REST APIs for Metering Lable Rules.<br>
35 @Path("/metering/metering-label-rules")
36 public final class NeutronMeteringLabelRulesNorthbound extends AbstractNeutronNorthbound<NeutronMeteringLabelRule,
37 NeutronMeteringLabelRuleRequest, INeutronMeteringLabelRuleCRUD> {
39 private static final String RESOURCE_NAME = "Metering Label Rule";
42 public NeutronMeteringLabelRulesNorthbound(@OsgiService INeutronMeteringLabelRuleCRUD neutronCRUD) {
47 protected String getResourceName() {
52 * Returns a list of all metering label rules.
55 @Produces({ MediaType.APPLICATION_JSON })
56 //@TypeHint(OpenStackNetworks.class)
57 @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
58 @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
59 @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
60 @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
61 public Response listMeteringLabelRules(
63 @QueryParam("fields") List<String> fields,
65 @QueryParam("id") String queryID,
66 @QueryParam("direction") String queryDirection,
67 @QueryParam("remote_ip_prefix") String queryRemoteIpPrefix,
68 @QueryParam("metering_label_id") String queryLabelID
69 // pagination and sorting are TODO
71 INeutronMeteringLabelRuleCRUD ruleInterface = getNeutronCRUD();
72 List<NeutronMeteringLabelRule> allNeutronMeteringLabelRule = ruleInterface.getAll();
73 List<NeutronMeteringLabelRule> ans = new ArrayList<>();
74 for (NeutronMeteringLabelRule rule : allNeutronMeteringLabelRule) {
75 if ((queryID == null || queryID.equals(rule.getID()))
76 && (queryDirection == null || queryDirection.equals(rule.getMeteringLabelRuleDirection()))
77 && (queryRemoteIpPrefix == null
78 || queryRemoteIpPrefix.equals(rule.getMeteringLabelRuleRemoteIpPrefix()))
79 && (queryLabelID == null || queryLabelID.equals(rule.getMeteringLabelRuleLabelID()))) {
80 if (fields.size() > 0) {
81 ans.add(rule.extractFields(fields));
87 //TODO: apply pagination to results
88 return Response.status(HttpURLConnection.HTTP_OK).entity(new NeutronMeteringLabelRuleRequest(ans)).build();
92 * Returns a specific metering label rule.
96 @Produces({ MediaType.APPLICATION_JSON })
97 @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
98 @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
99 @ResponseCode(code = HttpURLConnection.HTTP_FORBIDDEN, condition = "Forbidden"),
100 @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
101 @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
102 @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
103 public Response showMeteringLabelRule(@PathParam("ruleUUID") String ruleUUID,
105 @QueryParam("fields") List<String> fields) {
106 return show(ruleUUID, fields);
110 * Creates new metering label rule.
113 @Produces({ MediaType.APPLICATION_JSON })
114 @Consumes({ MediaType.APPLICATION_JSON })
115 //@TypeHint(NeutronNetwork.class)
116 @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_CREATED, condition = "Created"),
117 @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
118 public Response createMeteringLabelRule(final NeutronMeteringLabelRuleRequest input) {
119 INeutronMeteringLabelRuleCRUD meteringLabelRuleInterface = getNeutronCRUD();
120 if (input.isSingleton()) {
121 NeutronMeteringLabelRule singleton = input.getSingleton();
124 * add meteringLabelRule to the cache
126 meteringLabelRuleInterface.add(singleton);
130 * only singleton meteringLabelRule creates supported
132 throw new BadRequestException("Only singleton meteringLabelRule creates supported");
134 return Response.status(HttpURLConnection.HTTP_CREATED).entity(input).build();
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);