NEUTRON-208: BGPVPN network and router association
[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 package org.opendaylight.neutron.northbound.api;
9
10 import com.webcohesion.enunciate.metadata.rs.ResponseCode;
11 import com.webcohesion.enunciate.metadata.rs.StatusCodes;
12 import java.net.HttpURLConnection;
13 import java.util.ArrayList;
14 import java.util.List;
15 import javax.inject.Inject;
16 import javax.inject.Singleton;
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.MediaType;
26 import javax.ws.rs.core.Response;
27 import org.apache.aries.blueprint.annotation.service.Reference;
28 import org.opendaylight.neutron.spi.INeutronMeteringLabelCRUD;
29 import org.opendaylight.neutron.spi.NeutronMeteringLabel;
30
31 /**
32  * Neutron Northbound REST APIs for Metering Lables.
33  */
34 @Singleton
35 @Path("/metering/metering-labels")
36 public final class NeutronMeteringLabelsNorthbound extends
37         AbstractNeutronNorthbound<NeutronMeteringLabel, NeutronMeteringLabelRequest, INeutronMeteringLabelCRUD> {
38
39     private static final String RESOURCE_NAME = "Metering Label";
40
41     @Inject
42     public NeutronMeteringLabelsNorthbound(@Reference INeutronMeteringLabelCRUD neutronCRUD) {
43         super(neutronCRUD);
44     }
45
46     @Override
47     protected String getResourceName() {
48         return RESOURCE_NAME;
49     }
50
51     /**
52      * Returns a list of all metering labels.
53      */
54     @GET
55     @Produces({ MediaType.APPLICATION_JSON })
56     //@TypeHint(OpenStackNetworks.class)
57     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
58             @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
59             @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
60             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
61     public Response listMeteringLabels(
62             // return fields
63             @QueryParam("fields") List<String> fields,
64             // filter fields
65             @QueryParam("id") String queryID,
66             @QueryParam("name") String queryName,
67             @QueryParam("tenant_id") String queryTenantID
68     // pagination and sorting are TODO
69     ) {
70         INeutronMeteringLabelCRUD labelInterface = getNeutronCRUD();
71         List<NeutronMeteringLabel> allNeutronMeteringLabel = labelInterface.getAll();
72         List<NeutronMeteringLabel> ans = new ArrayList<>();
73         for (NeutronMeteringLabel label : allNeutronMeteringLabel) {
74             if ((queryID == null || queryID.equals(label.getID()))
75                     && (queryName == null || queryName.equals(label.getName()))
76                     && (queryTenantID == null || queryTenantID.equals(label.getTenantID()))) {
77                 if (fields.size() > 0) {
78                     ans.add(label.extractFields(fields));
79                 } else {
80                     ans.add(label);
81                 }
82             }
83         }
84         //TODO: apply pagination to results
85         return Response.status(HttpURLConnection.HTTP_OK).entity(new NeutronMeteringLabelRequest(ans)).build();
86     }
87
88     /**
89      * Returns a specific metering label.
90      */
91     @Path("{labelUUID}")
92     @GET
93     @Produces({ MediaType.APPLICATION_JSON })
94     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
95             @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
96             @ResponseCode(code = HttpURLConnection.HTTP_FORBIDDEN, condition = "Forbidden"),
97             @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
98             @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
99             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
100     public Response showMeteringLabel(@PathParam("labelUUID") String labelUUID,
101             // return fields
102             @QueryParam("fields") List<String> fields) {
103         return show(labelUUID, fields);
104     }
105
106     /**
107      * Creates new metering label.
108      */
109     @POST
110     @Produces({ MediaType.APPLICATION_JSON })
111     @Consumes({ MediaType.APPLICATION_JSON })
112     //@TypeHint(NeutronNetwork.class)
113     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_CREATED, condition = "Created"),
114             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
115     public Response createMeteringLabel(final NeutronMeteringLabelRequest input) {
116         return create(input);
117     }
118
119     /**
120      * Deletes a Metering Label.
121      */
122     @Path("{labelUUID}")
123     @DELETE
124     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_NO_CONTENT, condition = "No Content"),
125             @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
126             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
127     public Response deleteMeteringLabel(@PathParam("labelUUID") String labelUUID) {
128         return delete(labelUUID);
129     }
130 }