Bump upstream versions
[neutron.git] / northbound-api / src / main / java / org / opendaylight / neutron / northbound / api / NeutronRoutersNorthbound.java
1 /*
2  * Copyright (c) 2013, 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
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.GET;
18 import javax.ws.rs.POST;
19 import javax.ws.rs.PUT;
20 import javax.ws.rs.Path;
21 import javax.ws.rs.PathParam;
22 import javax.ws.rs.Produces;
23 import javax.ws.rs.QueryParam;
24 import javax.ws.rs.core.MediaType;
25 import javax.ws.rs.core.Response;
26 import org.codehaus.enunciate.jaxrs.ResponseCode;
27 import org.codehaus.enunciate.jaxrs.StatusCodes;
28 import org.opendaylight.neutron.spi.INeutronRouterCRUD;
29 import org.opendaylight.neutron.spi.NeutronRouter;
30 import org.opendaylight.neutron.spi.NeutronRouter_Interface;
31
32 /**
33  * Neutron Northbound REST APIs.<br>
34  * This class provides REST APIs for managing neutron routers
35  *
36  * <br>
37  * <br>
38  * Authentication scheme : <b>HTTP Basic</b><br>
39  * Authentication realm : <b>opendaylight</b><br>
40  * Transport : <b>HTTP and HTTPS</b><br>
41  * <br>
42  * HTTPS Authentication is disabled by default. Administrator can enable it in
43  * tomcat-server.xml after adding a proper keystore / SSL certificate from a
44  * trusted authority.<br>
45  * More info :
46  * http://tomcat.apache.org/tomcat-7.0-doc/ssl-howto.html#Configuration
47  *
48  */
49
50 @Path("/routers")
51 public final class NeutronRoutersNorthbound
52         extends AbstractNeutronNorthbound<NeutronRouter, NeutronRouterRequest, INeutronRouterCRUD> {
53     static final String ROUTER_INTERFACE_STR = "network:router_interface";
54     static final String ROUTER_GATEWAY_STR = "network:router_gateway";
55     private static final String RESOURCE_NAME = "Router";
56
57     @Override
58     protected String getResourceName() {
59         return RESOURCE_NAME;
60     }
61
62     /**
63      * Returns a list of all Routers */
64
65     @GET
66     @Produces({ MediaType.APPLICATION_JSON })
67     //@TypeHint(OpenStackRouters.class)
68     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
69             @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
70             @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
71             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
72     public Response listRouters(
73             // return fields
74             @QueryParam("fields") List<String> fields,
75             // note: openstack isn't clear about filtering on lists, so we aren't handling them
76             @QueryParam("id") String queryID,
77             @QueryParam("name") String queryName,
78             @QueryParam("admin_state_up") Boolean queryAdminStateUp,
79             @QueryParam("status") String queryStatus,
80             @QueryParam("tenant_id") String queryTenantID,
81             @QueryParam("external_gateway_info") String queryExternalGatewayInfo,
82             // pagination
83             @QueryParam("limit") String limit,
84             @QueryParam("marker") String marker,
85             @QueryParam("page_reverse") String pageReverse
86     // sorting not supported
87     ) {
88         INeutronRouterCRUD routerInterface = getNeutronCRUD();
89         if (routerInterface == null) {
90             throw new ServiceUnavailableException(serviceUnavailable());
91         }
92         List<NeutronRouter> allRouters = routerInterface.getAll();
93         List<NeutronRouter> ans = new ArrayList<>();
94         Iterator<NeutronRouter> i = allRouters.iterator();
95         while (i.hasNext()) {
96             NeutronRouter oSS = i.next();
97             if ((queryID == null || queryID.equals(oSS.getID()))
98                     && (queryName == null || queryName.equals(oSS.getName()))
99                     && (queryAdminStateUp == null || queryAdminStateUp.equals(oSS.getAdminStateUp()))
100                     && (queryStatus == null || queryStatus.equals(oSS.getStatus()))
101                     && (queryExternalGatewayInfo == null
102                             || queryExternalGatewayInfo.equals(oSS.getExternalGatewayInfo()))
103                     && (queryTenantID == null || queryTenantID.equals(oSS.getTenantID()))) {
104                 if (fields.size() > 0) {
105                     ans.add(oSS.extractFields(fields));
106                 } else {
107                     ans.add(oSS);
108                 }
109             }
110         }
111         //TODO: apply pagination to results
112         return Response.status(HttpURLConnection.HTTP_OK).entity(new NeutronRouterRequest(ans)).build();
113     }
114
115     /**
116      * Returns a specific Router */
117
118     @Path("{routerUUID}")
119     @GET
120     @Produces({ MediaType.APPLICATION_JSON })
121     //@TypeHint(OpenStackRouters.class)
122     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
123             @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
124             @ResponseCode(code = HttpURLConnection.HTTP_FORBIDDEN, condition = "Forbidden"),
125             @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
126             @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
127             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
128     public Response showRouter(@PathParam("routerUUID") String routerUUID,
129             // return fields
130             @QueryParam("fields") List<String> fields) {
131         return show(routerUUID, fields);
132     }
133
134     /**
135      * Creates new Routers */
136
137     @POST
138     @Produces({ MediaType.APPLICATION_JSON })
139     @Consumes({ MediaType.APPLICATION_JSON })
140     //@TypeHint(OpenStackRouters.class)
141     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_CREATED, condition = "Created"),
142             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
143     public Response createRouters(final NeutronRouterRequest input) {
144         return create(input);
145     }
146
147     @Override
148     protected void updateDelta(String uuid, NeutronRouter delta, NeutronRouter original) {
149         delta.setID(uuid);
150         delta.setTenantID(original.getTenantID());
151     }
152
153     /**
154      * Updates a Router */
155
156     @Path("{routerUUID}")
157     @PUT
158     @Produces({ MediaType.APPLICATION_JSON })
159     @Consumes({ MediaType.APPLICATION_JSON })
160     //@TypeHint(OpenStackRouters.class)
161     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
162             @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
163             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
164     public Response updateRouter(@PathParam("routerUUID") String routerUUID, NeutronRouterRequest input) {
165         return update(routerUUID, input);
166     }
167
168     /**
169      * Deletes a Router */
170
171     @Path("{routerUUID}")
172     @DELETE
173     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_NO_CONTENT, condition = "No Content"),
174             @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
175             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
176     public Response deleteRouter(@PathParam("routerUUID") String routerUUID) {
177         return delete(routerUUID);
178     }
179
180     /**
181      * Adds an interface to a router */
182
183     @Path("{routerUUID}/add_router_interface")
184     @PUT
185     @Produces({ MediaType.APPLICATION_JSON })
186     @Consumes({ MediaType.APPLICATION_JSON })
187     //@TypeHint(OpenStackRouterInterfaces.class)
188     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful") })
189     public Response addRouterInterface(@PathParam("routerUUID") String routerUUID, NeutronRouter_Interface input) {
190         // Do nothing. Keep this interface for compatibility
191         return Response.status(HttpURLConnection.HTTP_OK).entity(input).build();
192     }
193
194     @Path("{routerUUID}/remove_router_interface")
195     @PUT
196     @Produces({ MediaType.APPLICATION_JSON })
197     @Consumes({ MediaType.APPLICATION_JSON })
198     //@TypeHint(OpenStackRouterInterfaces.class)
199     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful") })
200     public Response removeRouterInterface(@PathParam("routerUUID") String routerUUID, NeutronRouter_Interface input) {
201         // Do nothing. Keep this interface for compatibility
202         return Response.status(HttpURLConnection.HTTP_OK).entity(input).build();
203     }
204 }