Merge "Bug 4354 - neutron tenant_id doens't contain '-'(dash)"
[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
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 extends AbstractNeutronNorthbound {
59     private static final String RESOURCE_NAME = "Metering Label Rule";
60
61     private NeutronMeteringLabelRule extractFields(NeutronMeteringLabelRule o, List<String> fields) {
62         return o.extractFields(fields);
63     }
64
65     private NeutronCRUDInterfaces getNeutronInterfaces() {
66         NeutronCRUDInterfaces answer = new NeutronCRUDInterfaces().fetchINeutronMeteringLabelRuleCRUD(this);
67         if (answer.getMeteringLabelRuleInterface() == null) {
68             throw new ServiceUnavailableException("NeutronMeteringLabelRule CRUD Interface "
69                     + RestMessages.SERVICEUNAVAILABLE.toString());
70         }
71         return answer;
72     }
73
74     @Context
75     UriInfo uriInfo;
76
77     /**
78      * Returns a list of all metering label rules */
79
80     @GET
81     @Produces({ MediaType.APPLICATION_JSON })
82     //@TypeHint(OpenStackNetworks.class)
83     @StatusCodes({
84             @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
85             @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
86             @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
87             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
88     public Response listMeteringLabelRules(
89             // return fields
90             @QueryParam("fields") List<String> fields,
91             // filter fields
92             @QueryParam("id") String queryID,
93             @QueryParam("direction") String queryDirection,
94             @QueryParam("remote_ip_prefix") String queryRemoteIPPrefix,
95             @QueryParam("metering_label_id") String queryLabelID
96             // pagination and sorting are TODO
97             ) {
98         INeutronMeteringLabelRuleCRUD ruleInterface = getNeutronInterfaces().getMeteringLabelRuleInterface();
99         List<NeutronMeteringLabelRule> allNeutronMeteringLabelRules = ruleInterface.getAllNeutronMeteringLabelRules();
100         List<NeutronMeteringLabelRule> ans = new ArrayList<NeutronMeteringLabelRule>();
101         Iterator<NeutronMeteringLabelRule> i = allNeutronMeteringLabelRules.iterator();
102         while (i.hasNext()) {
103             NeutronMeteringLabelRule oSS = i.next();
104             if ((queryID == null || queryID.equals(oSS.getID())) &&
105                     (queryDirection == null || queryDirection.equals(oSS.getMeteringLabelRuleDirection())) &&
106                     (queryRemoteIPPrefix == null || queryRemoteIPPrefix.equals(oSS.getMeteringLabelRuleRemoteIPPrefix())) &&
107                     (queryLabelID == null || queryLabelID.equals(oSS.getMeteringLabelRuleLabelID()))) {
108                 if (fields.size() > 0) {
109                     ans.add(extractFields(oSS,fields));
110                 } else {
111                     ans.add(oSS);
112                 }
113             }
114         }
115         //TODO: apply pagination to results
116         return Response.status(HttpURLConnection.HTTP_OK).entity(
117                 new NeutronMeteringLabelRuleRequest(ans)).build();
118     }
119
120     /**
121      * Returns a specific metering label rule */
122
123     @Path("{ruleUUID}")
124     @GET
125     @Produces({ MediaType.APPLICATION_JSON })
126     @StatusCodes({
127             @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
128             @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
129             @ResponseCode(code = HttpURLConnection.HTTP_FORBIDDEN, condition = "Forbidden"),
130             @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
131             @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
132             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
133     public Response showMeteringLabelRule(
134             @PathParam("ruleUUID") String ruleUUID,
135             // return fields
136             @QueryParam("fields") List<String> fields) {
137         INeutronMeteringLabelRuleCRUD ruleInterface = getNeutronInterfaces().getMeteringLabelRuleInterface();
138         if (!ruleInterface.neutronMeteringLabelRuleExists(ruleUUID)) {
139             throw new ResourceNotFoundException("MeteringLabelRule UUID not found");
140         }
141         if (fields.size() > 0) {
142             NeutronMeteringLabelRule ans = ruleInterface.getNeutronMeteringLabelRule(ruleUUID);
143             return Response.status(HttpURLConnection.HTTP_OK).entity(
144                     new NeutronMeteringLabelRuleRequest(extractFields(ans, fields))).build();
145         } else {
146             return Response.status(HttpURLConnection.HTTP_OK).entity(
147                     new NeutronMeteringLabelRuleRequest(ruleInterface.getNeutronMeteringLabelRule(ruleUUID))).build();
148         }
149     }
150
151     /**
152      * Creates new metering label rule */
153     @POST
154     @Produces({ MediaType.APPLICATION_JSON })
155     @Consumes({ MediaType.APPLICATION_JSON })
156     //@TypeHint(NeutronNetwork.class)
157     @StatusCodes({
158             @ResponseCode(code = HttpURLConnection.HTTP_CREATED, condition = "Created"),
159             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
160     public Response createMeteringLabelRule(final NeutronMeteringLabelRuleRequest input) {
161         INeutronMeteringLabelRuleCRUD meteringLabelRuleInterface = getNeutronInterfaces().getMeteringLabelRuleInterface();
162         if (input.isSingleton()) {
163             NeutronMeteringLabelRule singleton = input.getSingleton();
164
165             Object[] instances = NeutronUtil.getInstances(INeutronMeteringLabelRuleAware.class, this);
166             if (instances != null) {
167                 if (instances.length > 0) {
168                     for (Object instance : instances) {
169                         INeutronMeteringLabelRuleAware service = (INeutronMeteringLabelRuleAware) instance;
170                         int status = service.canCreateMeteringLabelRule(singleton);
171                         if (status < HTTP_OK_BOTTOM || status > HTTP_OK_TOP) {
172                             return Response.status(status).build();
173                         }
174                     }
175                 } else {
176                     throw new ServiceUnavailableException("No providers registered.  Please try again later");
177                 }
178             } else {
179                 throw new ServiceUnavailableException("Couldn't get providers list.  Please try again later");
180             }
181
182             /*
183              * add meteringLabelRule to the cache
184              */
185             meteringLabelRuleInterface.addNeutronMeteringLabelRule(singleton);
186             if (instances != null) {
187                 for (Object instance : instances) {
188                     INeutronMeteringLabelRuleAware service = (INeutronMeteringLabelRuleAware) instance;
189                     service.neutronMeteringLabelRuleCreated(singleton);
190                 }
191             }
192         } else {
193
194             /*
195              * only singleton meteringLabelRule creates supported
196              */
197             throw new BadRequestException("Only singleton meteringLabelRule creates supported");
198         }
199         return Response.status(HttpURLConnection.HTTP_CREATED).entity(input).build();
200     }
201
202     /**
203      * Deletes a Metering Label rule */
204
205     @Path("{ruleUUID}")
206     @DELETE
207     @StatusCodes({
208             @ResponseCode(code = HttpURLConnection.HTTP_NO_CONTENT, condition = "No Content"),
209             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
210     public Response deleteMeteringLabelRule(
211             @PathParam("ruleUUID") String ruleUUID) {
212         final INeutronMeteringLabelRuleCRUD meteringLabelRuleInterface = getNeutronInterfaces().getMeteringLabelRuleInterface();
213
214         NeutronMeteringLabelRule singleton = meteringLabelRuleInterface.getNeutronMeteringLabelRule(ruleUUID);
215         Object[] instances = NeutronUtil.getInstances(INeutronMeteringLabelRuleAware.class, this);
216         if (instances != null) {
217             if (instances.length > 0) {
218                 for (Object instance : instances) {
219                     INeutronMeteringLabelRuleAware service = (INeutronMeteringLabelRuleAware) instance;
220                     int status = service.canDeleteMeteringLabelRule(singleton);
221                     if (status < HTTP_OK_BOTTOM || status > HTTP_OK_TOP) {
222                         return Response.status(status).build();
223                     }
224                 }
225             } else {
226                 throw new ServiceUnavailableException("No providers registered.  Please try again later");
227             }
228         } else {
229             throw new ServiceUnavailableException("Couldn't get providers list.  Please try again later");
230         }
231         deleteUuid(RESOURCE_NAME, ruleUUID,
232                    new Remover() {
233                        public boolean remove(String uuid) {
234                            return meteringLabelRuleInterface.removeNeutronMeteringLabelRule(uuid);
235                        }
236                    });
237         if (instances != null) {
238             for (Object instance : instances) {
239                 INeutronMeteringLabelRuleAware service = (INeutronMeteringLabelRuleAware) instance;
240                 service.neutronMeteringLabelRuleDeleted(singleton);
241             }
242         }
243         return Response.status(HttpURLConnection.HTTP_NO_CONTENT).build();
244     }
245 }