northbound: code clean up
[neutron.git] / northbound-api / src / main / java / org / opendaylight / neutron / northbound / api / NeutronBgpvpnsNorthbound.java
1 /*
2  * Copyright (c) 2015 Ericsson India Global Services Pvt Ltd. 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.DefaultValue;
18 import javax.ws.rs.GET;
19 import javax.ws.rs.POST;
20 import javax.ws.rs.PUT;
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 import org.codehaus.enunciate.jaxrs.ResponseCode;
30 import org.codehaus.enunciate.jaxrs.StatusCodes;
31 import org.codehaus.enunciate.jaxrs.TypeHint;
32 import org.opendaylight.neutron.spi.INeutronBgpvpnCRUD;
33 import org.opendaylight.neutron.spi.NeutronBgpvpn;
34
35 /**
36  * Neutron Northbound REST APIs for Bgpvpn.<br>
37  * This class provides REST APIs for managing neutron Bgpvpns
38  *
39  * <br>
40  * <br>
41  * Authentication scheme : <b>HTTP Basic</b><br>
42  * Authentication realm : <b>opendaylight</b><br>
43  * Transport : <b>HTTP and HTTPS</b><br>
44  * <br>
45  * HTTPS Authentication is disabled by default. Administrator can enable it in
46  * tomcat-server.xml after adding a proper keystore / SSL certificate from a
47  * trusted authority.<br>
48  * More info :
49  * http://tomcat.apache.org/tomcat-7.0-doc/ssl-howto.html#Configuration
50  *
51  */
52
53 @Path("/bgpvpns")
54 public final class NeutronBgpvpnsNorthbound
55         extends AbstractNeutronNorthbound<NeutronBgpvpn, NeutronBgpvpnRequest, INeutronBgpvpnCRUD> {
56
57     @Context
58     UriInfo uriInfo;
59
60     private static final String RESOURCE_NAME = "Bgpvpn";
61
62     @Override
63     protected String getResourceName() {
64         return RESOURCE_NAME;
65     }
66
67     /**
68      * Returns a list of all Bgpvpns */
69
70     @GET
71     @Produces({ MediaType.APPLICATION_JSON })
72     //@TypeHint(OpenStackBgpvpns.class)
73     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
74             @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
75             @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
76             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
77     public Response listBgpvpns(
78             // return fields
79             @QueryParam("fields") List<String> fields,
80             // note: openstack isn't clear about filtering on lists, so we aren't handling them
81             @QueryParam("id") String queryID,
82             @QueryParam("name") String queryName,
83             @QueryParam("admin_state_up") String queryAdminStateUp,
84             @QueryParam("status") String queryStatus,
85             @QueryParam("tenant_id") String queryTenantID,
86             @QueryParam("type") String queryType,
87             @QueryParam("auto_aggregate") String queryAutoAggregate,
88             // pagination
89             @QueryParam("limit") Integer limit,
90             @QueryParam("marker") String marker,
91             @DefaultValue("false") @QueryParam("page_reverse") Boolean pageReverse
92     // sorting not supported
93     ) {
94         INeutronBgpvpnCRUD bgpvpnInterface = getNeutronCRUD();
95         List<NeutronBgpvpn> allBgpvpns = bgpvpnInterface.getAll();
96         List<NeutronBgpvpn> ans = new ArrayList<>();
97         Iterator<NeutronBgpvpn> i = allBgpvpns.iterator();
98         while (i.hasNext()) {
99             NeutronBgpvpn oSN = i.next();
100             //match filters: TODO provider extension
101             Boolean bAdminStateUp = null;
102             Boolean bAutoAggregate = null;
103             if (queryAdminStateUp != null) {
104                 bAdminStateUp = Boolean.valueOf(queryAdminStateUp);
105             }
106             if (queryAutoAggregate != null) {
107                 bAutoAggregate = Boolean.valueOf(queryAutoAggregate);
108             }
109             if ((queryID == null || queryID.equals(oSN.getID()))
110                     && (queryName == null || queryName.equals(oSN.getBgpvpnName()))
111                     && (bAdminStateUp == null || bAdminStateUp.booleanValue() == oSN.isAdminStateUp())
112                     && (queryStatus == null || queryStatus.equals(oSN.getStatus()))
113                     && (bAutoAggregate == null || bAutoAggregate.booleanValue() == oSN.isAutoAggregate())
114                     && (queryTenantID == null || queryTenantID.equals(oSN.getTenantID()))) {
115                 if (fields.size() > 0) {
116                     ans.add(oSN.extractFields(fields));
117                 } else {
118                     ans.add(oSN);
119                 }
120             }
121         }
122
123         if (limit != null && ans.size() > 1) {
124             // Return a paginated request
125             NeutronBgpvpnRequest request = (NeutronBgpvpnRequest) PaginatedRequestFactory.createRequest(limit, marker,
126                     pageReverse, uriInfo, ans, NeutronBgpvpn.class);
127             return Response.status(HttpURLConnection.HTTP_OK).entity(request).build();
128         }
129
130         return Response.status(HttpURLConnection.HTTP_OK).entity(new NeutronBgpvpnRequest(ans)).build();
131
132     }
133
134     /**
135      * Returns a specific Bgpvpn */
136
137     @Path("{bgpvpnUUID}")
138     @GET
139     @Produces({ MediaType.APPLICATION_JSON })
140     //@TypeHint(OpenStackBgpvpns.class)
141     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
142             @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
143             @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
144             @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
145             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
146     public Response showBgpvpn(@PathParam("bgpvpnUUID") String bgpvpnUUID,
147             // return fields
148             @QueryParam("fields") List<String> fields) {
149         return show(bgpvpnUUID, fields);
150     }
151
152     /**
153      * Creates new Bgpvpns */
154     @POST
155     @Produces({ MediaType.APPLICATION_JSON })
156     @Consumes({ MediaType.APPLICATION_JSON })
157     @TypeHint(NeutronBgpvpn.class)
158     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_CREATED, condition = "Created"),
159             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
160     public Response createBgpvpns(final NeutronBgpvpnRequest input) {
161         return create(input);
162     }
163
164     /**
165      * Updates a Bgpvpn */
166     @Override
167     protected void updateDelta(String uuid, NeutronBgpvpn delta, NeutronBgpvpn original) {
168         //Fill in defaults if they're missing in update
169         delta.initDefaults();
170         delta.setID(uuid);
171         delta.setTenantID(original.getTenantID());
172     }
173
174     @Path("{bgpvpnUUID}")
175     @PUT
176     @Produces({ MediaType.APPLICATION_JSON })
177     @Consumes({ MediaType.APPLICATION_JSON })
178     //@TypeHint(OpenStackBgpvpns.class)
179     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
180             @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
181             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
182     public Response updateBgpvpn(@PathParam("bgpvpnUUID") String bgpvpnUUID, final NeutronBgpvpnRequest input) {
183         return update(bgpvpnUUID, input);
184     }
185
186     /**
187      * Deletes a Bgpvpn */
188
189     @Path("{bgpvpnUUID}")
190     @DELETE
191     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_NO_CONTENT, condition = "No Content"),
192             @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
193             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
194     public Response deleteBgpvpn(@PathParam("bgpvpnUUID") String bgpvpnUUID) {
195         return delete(bgpvpnUUID);
196     }
197 }