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