Merge changes from topic 'northbound-refactor'
[neutron.git] / northbound-api / src / main / java / org / opendaylight / neutron / northbound / api / NeutronMeteringLabelsNorthbound.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.INeutronMeteringLabelAware;
35 import org.opendaylight.neutron.spi.INeutronMeteringLabelCRUD;
36 import org.opendaylight.neutron.spi.NeutronCRUDInterfaces;
37 import org.opendaylight.neutron.spi.NeutronMeteringLabel;
38
39 /**
40  * Neutron Northbound REST APIs for Metering Lables.<br>
41  * This class provides REST APIs for managing neutron metering labels
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-labels")
58 public class NeutronMeteringLabelsNorthbound
59     extends AbstractNeutronNorthbound<NeutronMeteringLabel, NeutronMeteringLabelRequest, INeutronMeteringLabelCRUD, INeutronMeteringLabelAware> {
60     private static final String RESOURCE_NAME = "Metering Label";
61
62     @Override
63     protected String getResourceName() {
64         return RESOURCE_NAME;
65     }
66
67     @Override
68     protected NeutronMeteringLabel extractFields(NeutronMeteringLabel o, List<String> fields) {
69         return o.extractFields(fields);
70     }
71
72     @Override
73     protected INeutronMeteringLabelCRUD getNeutronCRUD() {
74         NeutronCRUDInterfaces answer = new NeutronCRUDInterfaces().fetchINeutronMeteringLabelCRUD(this);
75         if (answer.getMeteringLabelInterface() == null) {
76             throw new ServiceUnavailableException(serviceUnavailable());
77         }
78         return answer.getMeteringLabelInterface();
79     }
80
81     @Override
82     protected NeutronMeteringLabelRequest newNeutronRequest(NeutronMeteringLabel o) {
83         return new NeutronMeteringLabelRequest(o);
84     }
85
86     @Override
87     protected Object[] getInstances() {
88         return NeutronUtil.getInstances(INeutronMeteringLabelAware.class, this);
89     }
90
91     @Override
92     protected int canCreate(Object instance, NeutronMeteringLabel singleton) {
93         INeutronMeteringLabelAware service = (INeutronMeteringLabelAware) instance;
94         return service.canCreateMeteringLabel(singleton);
95     }
96
97     @Override
98     protected void created(Object instance, NeutronMeteringLabel singleton) {
99         INeutronMeteringLabelAware service = (INeutronMeteringLabelAware) instance;
100         service.neutronMeteringLabelCreated(singleton);
101     }
102
103     @Override
104     protected int canUpdate(Object instance, NeutronMeteringLabel delta, NeutronMeteringLabel original) {
105         return 0;
106     }
107
108     @Override
109     protected void updated(Object instance, NeutronMeteringLabel updated) {
110         throw new UnsupportedOperationException();
111     }
112
113     @Override
114     protected int canDelete(Object instance, NeutronMeteringLabel singleton) {
115         INeutronMeteringLabelAware service = (INeutronMeteringLabelAware) instance;
116         return service.canDeleteMeteringLabel(singleton);
117     }
118
119     @Override
120     protected void deleted(Object instance, NeutronMeteringLabel singleton) {
121         INeutronMeteringLabelAware service = (INeutronMeteringLabelAware) instance;
122         service.neutronMeteringLabelDeleted(singleton);
123     }
124
125     @Context
126     UriInfo uriInfo;
127
128     /**
129      * Returns a list of all metering labels */
130
131     @GET
132     @Produces({ MediaType.APPLICATION_JSON })
133     //@TypeHint(OpenStackNetworks.class)
134     @StatusCodes({
135             @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
136             @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
137             @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
138             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
139     public Response listMeteringLabels(
140             // return fields
141             @QueryParam("fields") List<String> fields,
142             // filter fields
143             @QueryParam("id") String queryID,
144             @QueryParam("name") String queryName,
145             @QueryParam("tenant_id") String queryTenantID,
146             @QueryParam("description") String queryDescription
147             // pagination and sorting are TODO
148             ) {
149         INeutronMeteringLabelCRUD labelInterface = getNeutronCRUD();
150         List<NeutronMeteringLabel> allNeutronMeteringLabel = labelInterface.getAllNeutronMeteringLabels();
151         List<NeutronMeteringLabel> ans = new ArrayList<NeutronMeteringLabel>();
152         Iterator<NeutronMeteringLabel> i = allNeutronMeteringLabel.iterator();
153         while (i.hasNext()) {
154             NeutronMeteringLabel oSS = i.next();
155             if ((queryID == null || queryID.equals(oSS.getID())) &&
156                     (queryName == null || queryName.equals(oSS.getMeteringLabelName())) &&
157                     (queryDescription == null || queryDescription.equals(oSS.getMeteringLabelDescription())) &&
158                     (queryTenantID == null || queryTenantID.equals(oSS.getTenantID()))) {
159                 if (fields.size() > 0) {
160                     ans.add(extractFields(oSS,fields));
161                 } else {
162                     ans.add(oSS);
163                 }
164             }
165         }
166         //TODO: apply pagination to results
167         return Response.status(HttpURLConnection.HTTP_OK).entity(
168                 new NeutronMeteringLabelRequest(ans)).build();
169     }
170
171     /**
172      * Returns a specific metering label */
173
174     @Path("{labelUUID}")
175     @GET
176     @Produces({ MediaType.APPLICATION_JSON })
177     @StatusCodes({
178             @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
179             @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
180             @ResponseCode(code = HttpURLConnection.HTTP_FORBIDDEN, condition = "Forbidden"),
181             @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
182             @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
183             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
184     public Response showMeteringLabel(
185             @PathParam("labelUUID") String labelUUID,
186             // return fields
187             @QueryParam("fields") List<String> fields) {
188         return show(labelUUID, fields);
189     }
190
191     /**
192      * Creates new metering label */
193     @POST
194     @Produces({ MediaType.APPLICATION_JSON })
195     @Consumes({ MediaType.APPLICATION_JSON })
196     //@TypeHint(NeutronNetwork.class)
197     @StatusCodes({
198             @ResponseCode(code = HttpURLConnection.HTTP_CREATED, condition = "Created"),
199             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
200     public Response createMeteringLabel(final NeutronMeteringLabelRequest input) {
201         return create(input);
202     }
203
204     /**
205      * Deletes a Metering Label */
206
207     @Path("{labelUUID}")
208     @DELETE
209     @StatusCodes({
210             @ResponseCode(code = HttpURLConnection.HTTP_NO_CONTENT, condition = "No Content"),
211             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
212     public Response deleteMeteringLabel(
213             @PathParam("labelUUID") String labelUUID) {
214         return delete(labelUUID);
215     }
216 }