66b68253c378a92b6d07eb92289c0bf7170d5ff4
[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 package org.opendaylight.neutron.northbound.api;
9
10 import java.net.HttpURLConnection;
11 import java.util.ArrayList;
12 import java.util.List;
13 import javax.inject.Inject;
14 import javax.inject.Singleton;
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.NeutronRouterInterface;
31 import org.ops4j.pax.cdi.api.OsgiService;
32
33 /**
34  * Neutron Northbound REST APIs for managing neutron routers.
35  */
36 @Singleton
37 @Path("/routers")
38 public final class NeutronRoutersNorthbound
39         extends AbstractNeutronNorthbound<NeutronRouter, NeutronRouterRequest, INeutronRouterCRUD> {
40
41     private static final String RESOURCE_NAME = "Router";
42
43     @Inject
44     public NeutronRoutersNorthbound(@OsgiService INeutronRouterCRUD neutronCRUD) {
45         super(neutronCRUD);
46     }
47
48     @Override
49     protected String getResourceName() {
50         return RESOURCE_NAME;
51     }
52
53     /**
54      * Returns a list of all Routers.
55      */
56     @GET
57     @Produces({ MediaType.APPLICATION_JSON })
58     //@TypeHint(OpenStackRouters.class)
59     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
60             @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
61             @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
62             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
63     public Response listRouters(
64             // return fields
65             @QueryParam("fields") List<String> fields,
66             // note: openstack isn't clear about filtering on lists, so we aren't handling them
67             @QueryParam("id") String queryID,
68             @QueryParam("name") String queryName,
69             @QueryParam("admin_state_up") Boolean queryAdminStateUp,
70             @QueryParam("status") String queryStatus,
71             @QueryParam("tenant_id") String queryTenantID,
72             @QueryParam("external_gateway_info") String queryExternalGatewayInfo,
73             // pagination
74             @QueryParam("limit") String limit,
75             @QueryParam("marker") String marker,
76             @QueryParam("page_reverse") String pageReverse
77     // sorting not supported
78     ) {
79         INeutronRouterCRUD routerInterface = getNeutronCRUD();
80         if (routerInterface == null) {
81             throw new ServiceUnavailableException(serviceUnavailable());
82         }
83         List<NeutronRouter> allRouters = routerInterface.getAll();
84         List<NeutronRouter> ans = new ArrayList<>();
85         for (NeutronRouter router : allRouters) {
86             if ((queryID == null || queryID.equals(router.getID()))
87                     && (queryName == null || queryName.equals(router.getName()))
88                     && (queryAdminStateUp == null || queryAdminStateUp.equals(router.getAdminStateUp()))
89                     && (queryStatus == null || queryStatus.equals(router.getStatus()))
90                     && (queryTenantID == null || queryTenantID.equals(router.getTenantID()))) {
91                 if (fields.size() > 0) {
92                     ans.add(router.extractFields(fields));
93                 } else {
94                     ans.add(router);
95                 }
96             }
97         }
98         //TODO: apply pagination to results
99         return Response.status(HttpURLConnection.HTTP_OK).entity(new NeutronRouterRequest(ans)).build();
100     }
101
102     /**
103      * Returns a specific Router.
104      */
105     @Path("{routerUUID}")
106     @GET
107     @Produces({ MediaType.APPLICATION_JSON })
108     //@TypeHint(OpenStackRouters.class)
109     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
110             @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
111             @ResponseCode(code = HttpURLConnection.HTTP_FORBIDDEN, condition = "Forbidden"),
112             @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
113             @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
114             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
115     public Response showRouter(@PathParam("routerUUID") String routerUUID,
116             // return fields
117             @QueryParam("fields") List<String> fields) {
118         return show(routerUUID, fields);
119     }
120
121     /**
122      * Creates new Routers.
123      */
124     @POST
125     @Produces({ MediaType.APPLICATION_JSON })
126     @Consumes({ MediaType.APPLICATION_JSON })
127     //@TypeHint(OpenStackRouters.class)
128     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_CREATED, condition = "Created"),
129             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
130     public Response createRouters(final NeutronRouterRequest input) {
131         return create(input);
132     }
133
134     @Override
135     protected void updateDelta(String uuid, NeutronRouter delta, NeutronRouter original) {
136         delta.setID(uuid);
137         delta.setTenantID(original.getTenantID());
138     }
139
140     /**
141      * Updates a Router.
142      */
143     @Path("{routerUUID}")
144     @PUT
145     @Produces({ MediaType.APPLICATION_JSON })
146     @Consumes({ MediaType.APPLICATION_JSON })
147     //@TypeHint(OpenStackRouters.class)
148     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
149             @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
150             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
151     public Response updateRouter(@PathParam("routerUUID") String routerUUID, NeutronRouterRequest input) {
152         return update(routerUUID, input);
153     }
154
155     /**
156      * Deletes a Router.
157      */
158     @Path("{routerUUID}")
159     @DELETE
160     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_NO_CONTENT, condition = "No Content"),
161             @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
162             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
163     public Response deleteRouter(@PathParam("routerUUID") String routerUUID) {
164         return delete(routerUUID);
165     }
166
167     /**
168      * Adds an interface to a router.
169      */
170     @Path("{routerUUID}/add_router_interface")
171     @PUT
172     @Produces({ MediaType.APPLICATION_JSON })
173     @Consumes({ MediaType.APPLICATION_JSON })
174     //@TypeHint(OpenStackRouterInterfaces.class)
175     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful") })
176     public Response addRouterInterface(@PathParam("routerUUID") String routerUUID, NeutronRouterInterface input) {
177         // Do nothing. Keep this interface for compatibility
178         return Response.status(HttpURLConnection.HTTP_OK).entity(input).build();
179     }
180
181     @Path("{routerUUID}/remove_router_interface")
182     @PUT
183     @Produces({ MediaType.APPLICATION_JSON })
184     @Consumes({ MediaType.APPLICATION_JSON })
185     //@TypeHint(OpenStackRouterInterfaces.class)
186     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful") })
187     public Response removeRouterInterface(@PathParam("routerUUID") String routerUUID, NeutronRouterInterface input) {
188         // Do nothing. Keep this interface for compatibility
189         return Response.status(HttpURLConnection.HTTP_OK).entity(input).build();
190     }
191 }