Checkstyle Import issues fix (SPI tests,Northbound API)
[neutron.git] / northbound-api / src / main / java / org / opendaylight / neutron / northbound / api / NeutronQosPolicyNorthbound.java
1 /*
2  * Copyright (c) 2016 Intel, Corp. 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.List;
14 import javax.ws.rs.Consumes;
15 import javax.ws.rs.DELETE;
16 import javax.ws.rs.GET;
17 import javax.ws.rs.POST;
18 import javax.ws.rs.PUT;
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.INeutronQosPolicyCRUD;
28 import org.opendaylight.neutron.spi.NeutronCRUDInterfaces;
29 import org.opendaylight.neutron.spi.NeutronQosPolicy;
30
31 @Path("/qos/policies")
32 public class NeutronQosPolicyNorthbound extends
33     AbstractNeutronNorthbound<NeutronQosPolicy, NeutronQosPolicyRequest, INeutronQosPolicyCRUD> {
34
35     private static final String RESOURCE_NAME = "Qos Policy";
36
37     @Override
38     protected String getResourceName() {
39         return RESOURCE_NAME;
40     }
41
42     @Override
43     protected NeutronQosPolicy extractFields(NeutronQosPolicy o, List<String> fields) {
44         return o.extractFields(fields);
45     }
46
47     @Override
48     protected NeutronQosPolicyRequest newNeutronRequest(NeutronQosPolicy o) {
49         return new NeutronQosPolicyRequest(o);
50     }
51
52     @Override
53     protected INeutronQosPolicyCRUD getNeutronCRUD() {
54         NeutronCRUDInterfaces answer = new NeutronCRUDInterfaces().fetchINeutronQosPolicyCRUD(this);
55         if (answer.getQosPolicyInterface() == null) {
56             throw new ServiceUnavailableException(serviceUnavailable());
57         }
58         return answer.getQosPolicyInterface();
59     }
60
61     /**
62      * Returns a list of all Qos Policies
63      */
64     @GET
65     @Produces({ MediaType.APPLICATION_JSON })
66     @StatusCodes({
67         @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
68         @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
69         @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
70         @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
71     public Response listGroups(
72         // return fields
73         @QueryParam("fields") List<String> fields,
74         // OpenStack qos Policy attributes
75         @QueryParam("id") String queryQosPolicyUUID,
76         @QueryParam("tenant_id") String queryQosPolicyTenantID,
77         @QueryParam("name") String queryQosPolicyName,
78         @QueryParam("shared") Boolean queryQosPolicyIsShared,
79         // pagination
80         @QueryParam("limit") String limit,
81         @QueryParam("marker") String marker,
82         @QueryParam("page_reverse") String pageReverse) {
83         INeutronQosPolicyCRUD qosPolicyInterface = getNeutronCRUD();
84         List<NeutronQosPolicy> ans = new ArrayList<>();
85         for (NeutronQosPolicy nsg : qosPolicyInterface.getAll()) {
86             if ((queryQosPolicyUUID == null || queryQosPolicyUUID.equals(nsg.getID())) &&
87                 (queryQosPolicyTenantID == null || queryQosPolicyTenantID.equals(nsg.getTenantID())) &&
88                 (queryQosPolicyName == null || queryQosPolicyName.equals(nsg.getQosPolicyName())) &&
89                 (queryQosPolicyIsShared == null || queryQosPolicyIsShared.equals(nsg.getPolicyIsShared()))) {
90                 if (fields.size() > 0) {
91                     ans.add(extractFields(nsg, fields));
92                 } else {
93                     ans.add(nsg);
94                 }
95             }
96         }
97         return Response.status(HttpURLConnection.HTTP_OK).entity(
98             new NeutronQosPolicyRequest(ans)).build();
99     }
100
101     /**
102      * Returns a specific Qos Policy
103      */
104     @Path("{qosPolicyUUID}")
105     @GET
106     @Produces({ MediaType.APPLICATION_JSON })
107     @StatusCodes({
108         @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
109         @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
110         @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
111         @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
112         @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
113     public Response showQosPolicy(
114         @PathParam("qosPolicyUUID") String qosPolicyUUID,
115         @QueryParam("fields") List<String> fields) {
116         return show(qosPolicyUUID, fields);
117     }
118
119     /**
120      * Creates new Qos Policy
121      */
122     @POST
123     @Produces({ MediaType.APPLICATION_JSON })
124     @Consumes({ MediaType.APPLICATION_JSON })
125     @StatusCodes({
126         @ResponseCode(code = HttpURLConnection.HTTP_CREATED, condition = "Created"),
127         @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
128     public Response createQosPolicies(final NeutronQosPolicyRequest input) {
129         return create(input);
130     }
131
132     /**
133      * Updates a Qos Policy
134      */
135     @Path("{qosPolicyUUID}")
136     @PUT
137     @Produces({ MediaType.APPLICATION_JSON })
138     @Consumes({ MediaType.APPLICATION_JSON })
139     @StatusCodes({
140         @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
141         @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
142         @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
143     public Response updateQosPolicy(
144         @PathParam("qosPolicyUUID") String qosPolicyUUID,
145         final NeutronQosPolicyRequest input) {
146         return update(qosPolicyUUID, input);
147     }
148
149     /**
150      * Deletes a Qos Policy
151      */
152     @Path("{qosPolicyUUID}")
153     @DELETE
154     @StatusCodes({
155         @ResponseCode(code = HttpURLConnection.HTTP_NO_CONTENT, condition = "No Content"),
156         @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
157         @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
158     public Response deleteQosPolicy(
159         @PathParam("qosPolicyUUID") String qosPolicyUUID) {
160         return delete(qosPolicyUUID);
161     }
162 }