cb5e57dc5355b5e519fe79fdf6b9308cb17937da
[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.NeutronQosPolicy;
29
30 @Path("/qos/policies")
31 public final class NeutronQosPolicyNorthbound
32         extends AbstractNeutronNorthbound<NeutronQosPolicy, NeutronQosPolicyRequest, INeutronQosPolicyCRUD> {
33
34     private static final String RESOURCE_NAME = "Qos Policy";
35
36     @Override
37     protected String getResourceName() {
38         return RESOURCE_NAME;
39     }
40
41     /**
42      * Returns a list of all Qos Policies.
43      */
44     @GET
45     @Produces({ MediaType.APPLICATION_JSON })
46     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
47             @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
48             @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
49             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
50     public Response listGroups(
51             // return fields
52             @QueryParam("fields") List<String> fields,
53             // OpenStack qos Policy attributes
54             @QueryParam("id") String queryQosPolicyUUID,
55             @QueryParam("tenant_id") String queryQosPolicyTenantID,
56             @QueryParam("name") String queryQosPolicyName,
57             @QueryParam("shared") Boolean queryQosPolicyIsShared,
58             // pagination
59             @QueryParam("limit") String limit,
60             @QueryParam("marker") String marker,
61             @QueryParam("page_reverse") String pageReverse) {
62         INeutronQosPolicyCRUD qosPolicyInterface = getNeutronCRUD();
63         List<NeutronQosPolicy> ans = new ArrayList<>();
64         for (NeutronQosPolicy nsg : qosPolicyInterface.getAll()) {
65             if ((queryQosPolicyUUID == null || queryQosPolicyUUID.equals(nsg.getID()))
66                     && (queryQosPolicyTenantID == null || queryQosPolicyTenantID.equals(nsg.getTenantID()))
67                     && (queryQosPolicyName == null || queryQosPolicyName.equals(nsg.getName()))
68                     && (queryQosPolicyIsShared == null || queryQosPolicyIsShared.equals(nsg.getPolicyIsShared()))) {
69                 if (fields.size() > 0) {
70                     ans.add(nsg.extractFields(fields));
71                 } else {
72                     ans.add(nsg);
73                 }
74             }
75         }
76         return Response.status(HttpURLConnection.HTTP_OK).entity(new NeutronQosPolicyRequest(ans)).build();
77     }
78
79     /**
80      * Returns a specific Qos Policy.
81      */
82     @Path("{qosPolicyUUID}")
83     @GET
84     @Produces({ MediaType.APPLICATION_JSON })
85     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
86             @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
87             @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
88             @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
89             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
90     public Response showQosPolicy(@PathParam("qosPolicyUUID") String qosPolicyUUID,
91             @QueryParam("fields") List<String> fields) {
92         return show(qosPolicyUUID, fields);
93     }
94
95     /**
96      * Creates new Qos Policy.
97      */
98     @POST
99     @Produces({ MediaType.APPLICATION_JSON })
100     @Consumes({ MediaType.APPLICATION_JSON })
101     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_CREATED, condition = "Created"),
102             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
103     public Response createQosPolicies(final NeutronQosPolicyRequest input) {
104         return create(input);
105     }
106
107     /**
108      * Updates a Qos Policy.
109      */
110     @Path("{qosPolicyUUID}")
111     @PUT
112     @Produces({ MediaType.APPLICATION_JSON })
113     @Consumes({ MediaType.APPLICATION_JSON })
114     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
115             @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
116             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
117     public Response updateQosPolicy(@PathParam("qosPolicyUUID") String qosPolicyUUID,
118             final NeutronQosPolicyRequest input) {
119         return update(qosPolicyUUID, input);
120     }
121
122     /**
123      * Deletes a Qos Policy.
124      */
125     @Path("{qosPolicyUUID}")
126     @DELETE
127     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_NO_CONTENT, condition = "No Content"),
128             @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
129             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
130     public Response deleteQosPolicy(@PathParam("qosPolicyUUID") String qosPolicyUUID) {
131         return delete(qosPolicyUUID);
132     }
133 }