e8b213d4c74d399d259bdfba2fabf92843c585b0
[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
9 package org.opendaylight.neutron.northbound.api;
10
11 import java.net.HttpURLConnection;
12 import java.util.ArrayList;
13 import java.util.List;
14 import javax.ws.rs.Consumes;
15 import javax.ws.rs.DELETE;
16 import javax.ws.rs.GET;
17 import javax.ws.rs.POST;
18 import javax.ws.rs.Path;
19 import javax.ws.rs.PathParam;
20 import javax.ws.rs.Produces;
21 import javax.ws.rs.QueryParam;
22 import javax.ws.rs.core.MediaType;
23 import javax.ws.rs.core.Response;
24 import org.codehaus.enunciate.jaxrs.ResponseCode;
25 import org.codehaus.enunciate.jaxrs.StatusCodes;
26 import org.opendaylight.neutron.spi.INeutronMeteringLabelRuleCRUD;
27 import org.opendaylight.neutron.spi.NeutronMeteringLabelRule;
28
29 /**
30  * Neutron Northbound REST APIs for Metering Lable Rules.<br>
31  * This class provides REST APIs for managing neutron metering label rules
32  *
33  * <br>
34  * <br>
35  * Authentication scheme : <b>HTTP Basic</b><br>
36  * Authentication realm : <b>opendaylight</b><br>
37  * Transport : <b>HTTP and HTTPS</b><br>
38  * <br>
39  * HTTPS Authentication is disabled by default. Administrator can enable it in
40  * tomcat-server.xml after adding a proper keystore / SSL certificate from a
41  * trusted authority.<br>
42  * More info :
43  * http://tomcat.apache.org/tomcat-7.0-doc/ssl-howto.html#Configuration
44  *
45  */
46
47 @Path("/metering/metering-label-rules")
48 public final class NeutronMeteringLabelRulesNorthbound extends AbstractNeutronNorthbound<NeutronMeteringLabelRule,
49         NeutronMeteringLabelRuleRequest, INeutronMeteringLabelRuleCRUD> {
50     private static final String RESOURCE_NAME = "Metering Label Rule";
51
52     @Override
53     protected String getResourceName() {
54         return RESOURCE_NAME;
55     }
56
57     /**
58      * Returns a list of all metering label rules.
59      */
60
61     @GET
62     @Produces({ MediaType.APPLICATION_JSON })
63     //@TypeHint(OpenStackNetworks.class)
64     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
65             @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
66             @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
67             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
68     public Response listMeteringLabelRules(
69             // return fields
70             @QueryParam("fields") List<String> fields,
71             // filter fields
72             @QueryParam("id") String queryID,
73             @QueryParam("direction") String queryDirection,
74             @QueryParam("remote_ip_prefix") String queryRemoteIpPrefix,
75             @QueryParam("metering_label_id") String queryLabelID
76     // pagination and sorting are TODO
77     ) {
78         INeutronMeteringLabelRuleCRUD ruleInterface = getNeutronCRUD();
79         List<NeutronMeteringLabelRule> allNeutronMeteringLabelRule = ruleInterface.getAll();
80         List<NeutronMeteringLabelRule> ans = new ArrayList<>();
81         for (NeutronMeteringLabelRule rule : allNeutronMeteringLabelRule) {
82             if ((queryID == null || queryID.equals(rule.getID()))
83                     && (queryDirection == null || queryDirection.equals(rule.getMeteringLabelRuleDirection()))
84                     && (queryRemoteIpPrefix == null
85                             || queryRemoteIpPrefix.equals(rule.getMeteringLabelRuleRemoteIpPrefix()))
86                     && (queryLabelID == null || queryLabelID.equals(rule.getMeteringLabelRuleLabelID()))) {
87                 if (fields.size() > 0) {
88                     ans.add(rule.extractFields(fields));
89                 } else {
90                     ans.add(rule);
91                 }
92             }
93         }
94         //TODO: apply pagination to results
95         return Response.status(HttpURLConnection.HTTP_OK).entity(new NeutronMeteringLabelRuleRequest(ans)).build();
96     }
97
98     /**
99      * Returns a specific metering label rule.
100      */
101
102     @Path("{ruleUUID}")
103     @GET
104     @Produces({ MediaType.APPLICATION_JSON })
105     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
106             @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
107             @ResponseCode(code = HttpURLConnection.HTTP_FORBIDDEN, condition = "Forbidden"),
108             @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
109             @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
110             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
111     public Response showMeteringLabelRule(@PathParam("ruleUUID") String ruleUUID,
112             // return fields
113             @QueryParam("fields") List<String> fields) {
114         return show(ruleUUID, fields);
115     }
116
117     /**
118      * Creates new metering label rule.
119      */
120     @POST
121     @Produces({ MediaType.APPLICATION_JSON })
122     @Consumes({ MediaType.APPLICATION_JSON })
123     //@TypeHint(NeutronNetwork.class)
124     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_CREATED, condition = "Created"),
125             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
126     public Response createMeteringLabelRule(final NeutronMeteringLabelRuleRequest input) {
127         INeutronMeteringLabelRuleCRUD meteringLabelRuleInterface = getNeutronCRUD();
128         if (input.isSingleton()) {
129             NeutronMeteringLabelRule singleton = input.getSingleton();
130
131             /*
132              * add meteringLabelRule to the cache
133              */
134             meteringLabelRuleInterface.add(singleton);
135         } else {
136
137             /*
138              * only singleton meteringLabelRule creates supported
139              */
140             throw new BadRequestException("Only singleton meteringLabelRule creates supported");
141         }
142         return Response.status(HttpURLConnection.HTTP_CREATED).entity(input).build();
143     }
144
145     /**
146      * Deletes a Metering Label rule.
147      */
148
149     @Path("{ruleUUID}")
150     @DELETE
151     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_NO_CONTENT, condition = "No Content"),
152             @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
153             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
154     public Response deleteMeteringLabelRule(@PathParam("ruleUUID") String ruleUUID) {
155         return delete(ruleUUID);
156     }
157 }