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