Merge "BUG #4044 binding:host_id is string, not UUID"
[neutron.git] / northbound-api / src / main / java / org / opendaylight / neutron / northbound / api / NeutronMeteringLabelRulesNorthbound.java
1 /*
2  * Copyright IBM Corporation, 2015.  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
13 import java.util.ArrayList;
14 import java.util.Iterator;
15 import java.util.List;
16
17 import javax.ws.rs.Consumes;
18 import javax.ws.rs.DELETE;
19 import javax.ws.rs.GET;
20 import javax.ws.rs.POST;
21 import javax.ws.rs.Path;
22 import javax.ws.rs.PathParam;
23 import javax.ws.rs.Produces;
24 import javax.ws.rs.QueryParam;
25 import javax.ws.rs.core.Context;
26 import javax.ws.rs.core.MediaType;
27 import javax.ws.rs.core.Response;
28 import javax.ws.rs.core.UriInfo;
29
30 import org.codehaus.enunciate.jaxrs.ResponseCode;
31 import org.codehaus.enunciate.jaxrs.StatusCodes;
32 import org.codehaus.enunciate.jaxrs.TypeHint;
33
34 import org.opendaylight.neutron.spi.INeutronMeteringLabelRuleAware;
35 import org.opendaylight.neutron.spi.INeutronMeteringLabelRuleCRUD;
36 import org.opendaylight.neutron.spi.NeutronCRUDInterfaces;
37 import org.opendaylight.neutron.spi.NeutronMeteringLabelRule;
38
39 /**
40  * Neutron Northbound REST APIs for Metering Lable Rules.<br>
41  * This class provides REST APIs for managing neutron metering label rules
42  *
43  * <br>
44  * <br>
45  * Authentication scheme : <b>HTTP Basic</b><br>
46  * Authentication realm : <b>opendaylight</b><br>
47  * Transport : <b>HTTP and HTTPS</b><br>
48  * <br>
49  * HTTPS Authentication is disabled by default. Administrator can enable it in
50  * tomcat-server.xml after adding a proper keystore / SSL certificate from a
51  * trusted authority.<br>
52  * More info :
53  * http://tomcat.apache.org/tomcat-7.0-doc/ssl-howto.html#Configuration
54  *
55  */
56
57 @Path("/metering/metering-label-rules")
58 public class NeutronMeteringLabelRulesNorthbound {
59
60     private static final int HTTP_OK_BOTTOM = 200;
61     private static final int HTTP_OK_TOP = 299;
62
63     private NeutronMeteringLabelRule extractFields(NeutronMeteringLabelRule o, List<String> fields) {
64         return o.extractFields(fields);
65     }
66
67     private NeutronCRUDInterfaces getNeutronInterfaces() {
68         NeutronCRUDInterfaces answer = new NeutronCRUDInterfaces().fetchINeutronMeteringLabelRuleCRUD(this);
69         if (answer.getMeteringLabelRuleInterface() == null) {
70             throw new ServiceUnavailableException("NeutronMeteringLabelRule CRUD Interface "
71                     + RestMessages.SERVICEUNAVAILABLE.toString());
72         }
73         return answer;
74     }
75
76     @Context
77     UriInfo uriInfo;
78
79     /**
80      * Returns a list of all metering label rules */
81
82     @GET
83     @Produces({ MediaType.APPLICATION_JSON })
84     //@TypeHint(OpenStackNetworks.class)
85     @StatusCodes({
86             @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
87             @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
88             @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
89             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
90     public Response listMeteringLabelRules(
91             // return fields
92             @QueryParam("fields") List<String> fields,
93             // filter fields
94             @QueryParam("id") String queryID,
95             @QueryParam("direction") String queryDirection,
96             @QueryParam("remote_ip_prefix") String queryRemoteIPPrefix,
97             @QueryParam("metering_label_id") String queryLabelID
98             // pagination and sorting are TODO
99             ) {
100         INeutronMeteringLabelRuleCRUD ruleInterface = getNeutronInterfaces().getMeteringLabelRuleInterface();
101         List<NeutronMeteringLabelRule> allNeutronMeteringLabelRules = ruleInterface.getAllNeutronMeteringLabelRules();
102         List<NeutronMeteringLabelRule> ans = new ArrayList<NeutronMeteringLabelRule>();
103         Iterator<NeutronMeteringLabelRule> i = allNeutronMeteringLabelRules.iterator();
104         while (i.hasNext()) {
105             NeutronMeteringLabelRule oSS = i.next();
106             if ((queryID == null || queryID.equals(oSS.getMeteringLabelRuleUUID())) &&
107                     (queryDirection == null || queryDirection.equals(oSS.getMeteringLabelRuleDirection())) &&
108                     (queryRemoteIPPrefix == null || queryRemoteIPPrefix.equals(oSS.getMeteringLabelRuleRemoteIPPrefix())) &&
109                     (queryLabelID == null || queryLabelID.equals(oSS.getMeteringLabelRuleLabelID()))) {
110                 if (fields.size() > 0) {
111                     ans.add(extractFields(oSS,fields));
112                 } else {
113                     ans.add(oSS);
114                 }
115             }
116         }
117         //TODO: apply pagination to results
118         return Response.status(HttpURLConnection.HTTP_OK).entity(
119                 new NeutronMeteringLabelRuleRequest(ans)).build();
120     }
121
122     /**
123      * Returns a specific metering label rule */
124
125     @Path("{ruleUUID}")
126     @GET
127     @Produces({ MediaType.APPLICATION_JSON })
128     @StatusCodes({
129             @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
130             @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
131             @ResponseCode(code = HttpURLConnection.HTTP_FORBIDDEN, condition = "Forbidden"),
132             @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
133             @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
134             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
135     public Response showMeteringLabelRule(
136             @PathParam("ruleUUID") String ruleUUID,
137             // return fields
138             @QueryParam("fields") List<String> fields) {
139         INeutronMeteringLabelRuleCRUD ruleInterface = getNeutronInterfaces().getMeteringLabelRuleInterface();
140         if (!ruleInterface.neutronMeteringLabelRuleExists(ruleUUID)) {
141             throw new ResourceNotFoundException("MeteringLabelRule UUID not found");
142         }
143         if (fields.size() > 0) {
144             NeutronMeteringLabelRule ans = ruleInterface.getNeutronMeteringLabelRule(ruleUUID);
145             return Response.status(HttpURLConnection.HTTP_OK).entity(
146                     new NeutronMeteringLabelRuleRequest(extractFields(ans, fields))).build();
147         } else {
148             return Response.status(HttpURLConnection.HTTP_OK).entity(
149                     new NeutronMeteringLabelRuleRequest(ruleInterface.getNeutronMeteringLabelRule(ruleUUID))).build();
150         }
151     }
152
153     /**
154      * Creates new metering label rule */
155     @POST
156     @Produces({ MediaType.APPLICATION_JSON })
157     @Consumes({ MediaType.APPLICATION_JSON })
158     //@TypeHint(NeutronNetwork.class)
159     @StatusCodes({
160             @ResponseCode(code = HttpURLConnection.HTTP_CREATED, condition = "Created"),
161             @ResponseCode(code = HttpURLConnection.HTTP_BAD_REQUEST, condition = "Bad Request"),
162             @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
163             @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
164             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
165     public Response createMeteringLabelRule(final NeutronMeteringLabelRuleRequest input) {
166         INeutronMeteringLabelRuleCRUD meteringLabelRuleInterface = getNeutronInterfaces().getMeteringLabelRuleInterface();
167         if (input.isSingleton()) {
168             NeutronMeteringLabelRule singleton = input.getSingleton();
169
170             Object[] instances = NeutronUtil.getInstances(INeutronMeteringLabelRuleAware.class, this);
171             if (instances != null) {
172                 if (instances.length > 0) {
173                     for (Object instance : instances) {
174                         INeutronMeteringLabelRuleAware service = (INeutronMeteringLabelRuleAware) instance;
175                         int status = service.canCreateMeteringLabelRule(singleton);
176                         if (status < HTTP_OK_BOTTOM || status > HTTP_OK_TOP) {
177                             return Response.status(status).build();
178                         }
179                     }
180                 } else {
181                     throw new ServiceUnavailableException("No providers registered.  Please try again later");
182                 }
183             } else {
184                 throw new ServiceUnavailableException("Couldn't get providers list.  Please try again later");
185             }
186
187             /*
188              * add meteringLabelRule to the cache
189              */
190             meteringLabelRuleInterface.addNeutronMeteringLabelRule(singleton);
191             if (instances != null) {
192                 for (Object instance : instances) {
193                     INeutronMeteringLabelRuleAware service = (INeutronMeteringLabelRuleAware) instance;
194                     service.neutronMeteringLabelRuleCreated(singleton);
195                 }
196             }
197         } else {
198
199             /*
200              * only singleton meteringLabelRule creates supported
201              */
202             throw new BadRequestException("Only singleton meteringLabelRule creates supported");
203         }
204         return Response.status(HttpURLConnection.HTTP_CREATED).entity(input).build();
205     }
206
207     /**
208      * Deletes a Metering Label rule */
209
210     @Path("{ruleUUID}")
211     @DELETE
212     @StatusCodes({
213             @ResponseCode(code = HttpURLConnection.HTTP_NO_CONTENT, condition = "No Content"),
214             @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
215             @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
216             @ResponseCode(code = HttpURLConnection.HTTP_CONFLICT, condition = "Conflict"),
217             @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
218             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
219     public Response deleteMeteringLabelRule(
220             @PathParam("ruleUUID") String ruleUUID) {
221         INeutronMeteringLabelRuleCRUD meteringLabelRuleInterface = getNeutronInterfaces().getMeteringLabelRuleInterface();
222
223         NeutronMeteringLabelRule singleton = meteringLabelRuleInterface.getNeutronMeteringLabelRule(ruleUUID);
224         Object[] instances = NeutronUtil.getInstances(INeutronMeteringLabelRuleAware.class, this);
225         if (instances != null) {
226             if (instances.length > 0) {
227                 for (Object instance : instances) {
228                     INeutronMeteringLabelRuleAware service = (INeutronMeteringLabelRuleAware) instance;
229                     int status = service.canDeleteMeteringLabelRule(singleton);
230                     if (status < HTTP_OK_BOTTOM || status > HTTP_OK_TOP) {
231                         return Response.status(status).build();
232                     }
233                 }
234             } else {
235                 throw new ServiceUnavailableException("No providers registered.  Please try again later");
236             }
237         } else {
238             throw new ServiceUnavailableException("Couldn't get providers list.  Please try again later");
239         }
240         meteringLabelRuleInterface.removeNeutronMeteringLabelRule(ruleUUID);
241         if (instances != null) {
242             for (Object instance : instances) {
243                 INeutronMeteringLabelRuleAware service = (INeutronMeteringLabelRuleAware) instance;
244                 service.neutronMeteringLabelRuleDeleted(singleton);
245             }
246         }
247         return Response.status(HttpURLConnection.HTTP_NO_CONTENT).build();
248     }
249 }