remove irrelevant old JavaDoc from a long bygone era in northbound.api
[neutron.git] / northbound-api / src / main / java / org / opendaylight / neutron / northbound / api / NeutronMeteringLabelRulesNorthbound.java
1 /*
2  * Copyright (c) 2015 IBM Corporation and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.neutron.northbound.api;
9
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;
27
28 /**
29  * Neutron Northbound REST APIs for Metering Lable Rules.<br>
30  */
31 @Path("/metering/metering-label-rules")
32 public final class NeutronMeteringLabelRulesNorthbound extends AbstractNeutronNorthbound<NeutronMeteringLabelRule,
33         NeutronMeteringLabelRuleRequest, INeutronMeteringLabelRuleCRUD> {
34
35     private static final String RESOURCE_NAME = "Metering Label Rule";
36
37     @Override
38     protected String getResourceName() {
39         return RESOURCE_NAME;
40     }
41
42     /**
43      * Returns a list of all metering label rules.
44      */
45     @GET
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(
53             // return fields
54             @QueryParam("fields") List<String> fields,
55             // filter 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
61     ) {
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));
73                 } else {
74                     ans.add(rule);
75                 }
76             }
77         }
78         //TODO: apply pagination to results
79         return Response.status(HttpURLConnection.HTTP_OK).entity(new NeutronMeteringLabelRuleRequest(ans)).build();
80     }
81
82     /**
83      * Returns a specific metering label rule.
84      */
85     @Path("{ruleUUID}")
86     @GET
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,
95             // return fields
96             @QueryParam("fields") List<String> fields) {
97         return show(ruleUUID, fields);
98     }
99
100     /**
101      * Creates new metering label rule.
102      */
103     @POST
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();
113
114             /*
115              * add meteringLabelRule to the cache
116              */
117             meteringLabelRuleInterface.add(singleton);
118         } else {
119
120             /*
121              * only singleton meteringLabelRule creates supported
122              */
123             throw new BadRequestException("Only singleton meteringLabelRule creates supported");
124         }
125         return Response.status(HttpURLConnection.HTTP_CREATED).entity(input).build();
126     }
127
128     /**
129      * Deletes a Metering Label rule.
130      */
131     @Path("{ruleUUID}")
132     @DELETE
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);
138     }
139 }