spi: consolidate common member
[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 import java.util.ArrayList;
13 import java.util.Iterator;
14 import java.util.List;
15 import javax.ws.rs.Consumes;
16 import javax.ws.rs.DELETE;
17 import javax.ws.rs.GET;
18 import javax.ws.rs.POST;
19 import javax.ws.rs.Path;
20 import javax.ws.rs.PathParam;
21 import javax.ws.rs.Produces;
22 import javax.ws.rs.QueryParam;
23 import javax.ws.rs.core.MediaType;
24 import javax.ws.rs.core.Response;
25 import org.codehaus.enunciate.jaxrs.ResponseCode;
26 import org.codehaus.enunciate.jaxrs.StatusCodes;
27 import org.opendaylight.neutron.spi.INeutronMeteringLabelCRUD;
28 import org.opendaylight.neutron.spi.NeutronMeteringLabel;
29
30 /**
31  * Neutron Northbound REST APIs for Metering Lables.<br>
32  * This class provides REST APIs for managing neutron metering labels
33  *
34  * <br>
35  * <br>
36  * Authentication scheme : <b>HTTP Basic</b><br>
37  * Authentication realm : <b>opendaylight</b><br>
38  * Transport : <b>HTTP and HTTPS</b><br>
39  * <br>
40  * HTTPS Authentication is disabled by default. Administrator can enable it in
41  * tomcat-server.xml after adding a proper keystore / SSL certificate from a
42  * trusted authority.<br>
43  * More info :
44  * http://tomcat.apache.org/tomcat-7.0-doc/ssl-howto.html#Configuration
45  *
46  */
47
48 @Path("/metering/metering-labels")
49 public final class NeutronMeteringLabelsNorthbound extends
50         AbstractNeutronNorthbound<NeutronMeteringLabel, NeutronMeteringLabelRequest, INeutronMeteringLabelCRUD> {
51     private static final String RESOURCE_NAME = "Metering Label";
52
53     @Override
54     protected String getResourceName() {
55         return RESOURCE_NAME;
56     }
57
58     /**
59      * Returns a list of all metering labels */
60
61     @GET
62     @Produces({ MediaType.APPLICATION_JSON })
63     //@TypeHint(OpenStackNetworks.class)
64     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
65             @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
66             @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
67             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
68     public Response listMeteringLabels(
69             // return fields
70             @QueryParam("fields") List<String> fields,
71             // filter fields
72             @QueryParam("id") String queryID,
73             @QueryParam("name") String queryName,
74             @QueryParam("tenant_id") String queryTenantID
75     // pagination and sorting are TODO
76     ) {
77         INeutronMeteringLabelCRUD labelInterface = getNeutronCRUD();
78         List<NeutronMeteringLabel> allNeutronMeteringLabel = labelInterface.getAll();
79         List<NeutronMeteringLabel> ans = new ArrayList<>();
80         Iterator<NeutronMeteringLabel> i = allNeutronMeteringLabel.iterator();
81         while (i.hasNext()) {
82             NeutronMeteringLabel oSS = i.next();
83             if ((queryID == null || queryID.equals(oSS.getID()))
84                     && (queryName == null || queryName.equals(oSS.getName()))
85                     && (queryTenantID == null || queryTenantID.equals(oSS.getTenantID()))) {
86                 if (fields.size() > 0) {
87                     ans.add(oSS.extractFields(fields));
88                 } else {
89                     ans.add(oSS);
90                 }
91             }
92         }
93         //TODO: apply pagination to results
94         return Response.status(HttpURLConnection.HTTP_OK).entity(new NeutronMeteringLabelRequest(ans)).build();
95     }
96
97     /**
98      * Returns a specific metering label */
99
100     @Path("{labelUUID}")
101     @GET
102     @Produces({ MediaType.APPLICATION_JSON })
103     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
104             @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
105             @ResponseCode(code = HttpURLConnection.HTTP_FORBIDDEN, condition = "Forbidden"),
106             @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
107             @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
108             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
109     public Response showMeteringLabel(@PathParam("labelUUID") String labelUUID,
110             // return fields
111             @QueryParam("fields") List<String> fields) {
112         return show(labelUUID, fields);
113     }
114
115     /**
116      * Creates new metering label */
117     @POST
118     @Produces({ MediaType.APPLICATION_JSON })
119     @Consumes({ MediaType.APPLICATION_JSON })
120     //@TypeHint(NeutronNetwork.class)
121     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_CREATED, condition = "Created"),
122             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
123     public Response createMeteringLabel(final NeutronMeteringLabelRequest input) {
124         return create(input);
125     }
126
127     /**
128      * Deletes a Metering Label */
129
130     @Path("{labelUUID}")
131     @DELETE
132     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_NO_CONTENT, condition = "No Content"),
133             @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
134             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
135     public Response deleteMeteringLabel(@PathParam("labelUUID") String labelUUID) {
136         return delete(labelUUID);
137     }
138 }