Merge "Fix for bug 2648"
[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.util.ArrayList;
12 import java.util.HashMap;
13 import java.util.Iterator;
14 import java.util.List;
15
16 import javax.ws.rs.Consumes;
17 import javax.ws.rs.DELETE;
18 import javax.ws.rs.DefaultValue;
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 NeutronMeteringLabelRule extractFields(NeutronMeteringLabelRule o, List<String> fields) {
61         return o.extractFields(fields);
62     }
63
64     @Context
65     UriInfo uriInfo;
66
67     /**
68      * Returns a list of all metering label rules */
69
70     @GET
71     @Produces({ MediaType.APPLICATION_JSON })
72     //@TypeHint(OpenStackNetworks.class)
73     @StatusCodes({
74             @ResponseCode(code = 200, condition = "Operation successful"),
75             @ResponseCode(code = 401, condition = "Unauthorized"),
76             @ResponseCode(code = 501, condition = "Not Implemented"),
77             @ResponseCode(code = 503, condition = "No providers available") })
78     public Response listMeteringLabelRules(
79             // return fields
80             @QueryParam("fields") List<String> fields,
81             // filter fields
82             @QueryParam("id") String queryID,
83             @QueryParam("direction") String queryDirection,
84             @QueryParam("remote_ip_prefix") String queryRemoteIPPrefix,
85             @QueryParam("metering_label_id") String queryLabelID
86             // pagination and sorting are TODO
87             ) {
88         INeutronMeteringLabelRuleCRUD ruleInterface = NeutronCRUDInterfaces.getINeutronMeteringLabelRuleCRUD(this);
89         if (ruleInterface == null) {
90             throw new ServiceUnavailableException("NeutronMeteringLabelRule CRUD Interface "
91                     + RestMessages.SERVICEUNAVAILABLE.toString());
92         }
93         List<NeutronMeteringLabelRule> allNeutronMeteringLabelRules = ruleInterface.getAllNeutronMeteringLabelRules();
94         List<NeutronMeteringLabelRule> ans = new ArrayList<NeutronMeteringLabelRule>();
95         Iterator<NeutronMeteringLabelRule> i = allNeutronMeteringLabelRules.iterator();
96         while (i.hasNext()) {
97             NeutronMeteringLabelRule oSS = i.next();
98             if ((queryID == null || queryID.equals(oSS.getMeteringLabelRuleUUID())) &&
99                     (queryDirection == null || queryDirection.equals(oSS.getMeteringLabelRuleDirection())) &&
100                     (queryRemoteIPPrefix == null || queryRemoteIPPrefix.equals(oSS.getMeteringLabelRuleRemoteIPPrefix())) &&
101                     (queryLabelID == null || queryLabelID.equals(oSS.getMeteringLabelRuleLabelID()))) {
102                 if (fields.size() > 0)
103                     ans.add(extractFields(oSS,fields));
104                 else
105                     ans.add(oSS);
106             }
107         }
108         //TODO: apply pagination to results
109         return Response.status(200).entity(
110                 new NeutronMeteringLabelRuleRequest(ans)).build();
111     }
112
113     /**
114      * Returns a specific metering label rule */
115
116     @Path("{ruleUUID}")
117     @GET
118     @Produces({ MediaType.APPLICATION_JSON })
119     @StatusCodes({
120             @ResponseCode(code = 200, condition = "Operation successful"),
121             @ResponseCode(code = 401, condition = "Unauthorized"),
122             @ResponseCode(code = 403, condition = "Forbidden"),
123             @ResponseCode(code = 404, condition = "Not Found"),
124             @ResponseCode(code = 501, condition = "Not Implemented"),
125             @ResponseCode(code = 503, condition = "No providers available") })
126     public Response showMeteringLabelRule(
127             @PathParam("ruleUUID") String ruleUUID,
128             // return fields
129             @QueryParam("fields") List<String> fields) {
130         INeutronMeteringLabelRuleCRUD ruleInterface = NeutronCRUDInterfaces.getINeutronMeteringLabelRuleCRUD(this);
131         if (ruleInterface == null) {
132             throw new ServiceUnavailableException("MeteringLabelRule CRUD Interface "
133                     + RestMessages.SERVICEUNAVAILABLE.toString());
134         }
135         if (!ruleInterface.neutronMeteringLabelRuleExists(ruleUUID)) {
136             throw new ResourceNotFoundException("MeteringLabelRule UUID not found");
137         }
138         if (fields.size() > 0) {
139             NeutronMeteringLabelRule ans = ruleInterface.getNeutronMeteringLabelRule(ruleUUID);
140             return Response.status(200).entity(
141                     new NeutronMeteringLabelRuleRequest(extractFields(ans, fields))).build();
142         } else {
143             return Response.status(200).entity(
144                     new NeutronMeteringLabelRuleRequest(ruleInterface.getNeutronMeteringLabelRule(ruleUUID))).build();
145         }
146     }
147
148     /**
149      * Creates new metering label rule */
150     @POST
151     @Produces({ MediaType.APPLICATION_JSON })
152     @Consumes({ MediaType.APPLICATION_JSON })
153     //@TypeHint(NeutronNetwork.class)
154     @StatusCodes({
155             @ResponseCode(code = 201, condition = "Created"),
156             @ResponseCode(code = 400, condition = "Bad Request"),
157             @ResponseCode(code = 401, condition = "Unauthorized"),
158             @ResponseCode(code = 501, condition = "Not Implemented"),
159             @ResponseCode(code = 503, condition = "No providers available") })
160     public Response createMeteringLabelRule(final NeutronMeteringLabelRuleRequest input) {
161         INeutronMeteringLabelRuleCRUD meteringLabelRuleInterface = NeutronCRUDInterfaces.getINeutronMeteringLabelRuleCRUD(this);
162         if (meteringLabelRuleInterface == null) {
163             throw new ServiceUnavailableException("MeteringLabelRule CRUD Interface "
164                     + RestMessages.SERVICEUNAVAILABLE.toString());
165         }
166         if (input.isSingleton()) {
167             NeutronMeteringLabelRule singleton = input.getSingleton();
168
169             /*
170              * verify that the meteringLabelRule doesn't already exist (issue: is deeper inspection necessary?)
171              */
172             if (meteringLabelRuleInterface.neutronMeteringLabelRuleExists(singleton.getMeteringLabelRuleUUID()))
173                 throw new BadRequestException("meteringLabelRule UUID already exists");
174             Object[] instances = NeutronUtil.getInstances(INeutronMeteringLabelRuleAware.class, this);
175             if (instances != null) {
176                 if (instances.length > 0) {
177                     for (Object instance : instances) {
178                         INeutronMeteringLabelRuleAware service = (INeutronMeteringLabelRuleAware) instance;
179                         int status = service.canCreateMeteringLabelRule(singleton);
180                         if (status < 200 || status > 299)
181                             return Response.status(status).build();
182                     }
183                 } else {
184                     throw new ServiceUnavailableException("No providers registered.  Please try again later");
185                 }
186             } else {
187                 throw new ServiceUnavailableException("Couldn't get providers list.  Please try again later");
188             }
189
190             /*
191              * add meteringLabelRule to the cache
192              */
193             meteringLabelRuleInterface.addNeutronMeteringLabelRule(singleton);
194             if (instances != null) {
195                 for (Object instance : instances) {
196                     INeutronMeteringLabelRuleAware service = (INeutronMeteringLabelRuleAware) instance;
197                     service.neutronMeteringLabelRuleCreated(singleton);
198                 }
199             }
200         } else {
201
202             /*
203              * only singleton meteringLabelRule creates supported
204              */
205             throw new BadRequestException("Only singleton meteringLabelRule creates supported");
206         }
207         return Response.status(201).entity(input).build();
208     }
209
210     /**
211      * Deletes a Metering Label rule */
212
213     @Path("{ruleUUID}")
214     @DELETE
215     @StatusCodes({
216             @ResponseCode(code = 204, condition = "No Content"),
217             @ResponseCode(code = 401, condition = "Unauthorized"),
218             @ResponseCode(code = 404, condition = "Not Found"),
219             @ResponseCode(code = 409, condition = "Conflict"),
220             @ResponseCode(code = 501, condition = "Not Implemented"),
221             @ResponseCode(code = 503, condition = "No providers available") })
222     public Response deleteMeteringLabelRule(
223             @PathParam("ruleUUID") String ruleUUID) {
224         INeutronMeteringLabelRuleCRUD meteringLabelRuleInterface = NeutronCRUDInterfaces.getINeutronMeteringLabelRuleCRUD(this);
225         if (meteringLabelRuleInterface == null) {
226             throw new ServiceUnavailableException("MeteringLabelRule CRUD Interface "
227                     + RestMessages.SERVICEUNAVAILABLE.toString());
228         }
229
230         /*
231          * verify that the meteringLabelRule exists and is not in use before removing it
232          */
233         if (!meteringLabelRuleInterface.neutronMeteringLabelRuleExists(ruleUUID))
234             throw new ResourceNotFoundException("MeteringLabelRule UUID not found");
235         NeutronMeteringLabelRule singleton = meteringLabelRuleInterface.getNeutronMeteringLabelRule(ruleUUID);
236         Object[] instances = NeutronUtil.getInstances(INeutronMeteringLabelRuleAware.class, this);
237         if (instances != null) {
238             if (instances.length > 0) {
239                 for (Object instance : instances) {
240                     INeutronMeteringLabelRuleAware service = (INeutronMeteringLabelRuleAware) instance;
241                     int status = service.canDeleteMeteringLabelRule(singleton);
242                     if (status < 200 || status > 299)
243                         return Response.status(status).build();
244                 }
245             } else {
246                 throw new ServiceUnavailableException("No providers registered.  Please try again later");
247             }
248         } else {
249             throw new ServiceUnavailableException("Couldn't get providers list.  Please try again later");
250         }
251         meteringLabelRuleInterface.removeNeutronMeteringLabelRule(ruleUUID);
252         if (instances != null) {
253             for (Object instance : instances) {
254                 INeutronMeteringLabelRuleAware service = (INeutronMeteringLabelRuleAware) instance;
255                 service.neutronMeteringLabelRuleDeleted(singleton);
256             }
257         }
258         return Response.status(204).build();
259     }
260 }