musonous | 95c3ee5 | 2016-02-23 10:54:34 +0900 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
musonous | 95c3ee5 | 2016-02-23 10:54:34 +0900 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package org.onosproject.openstacknetworking.web; |
| 18 | |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 19 | import org.onosproject.openstacknetworking.api.OpenstackRouterAdminService; |
musonous | 95c3ee5 | 2016-02-23 10:54:34 +0900 | [diff] [blame] | 20 | import org.onosproject.rest.AbstractWebResource; |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 21 | import org.openstack4j.openstack.networking.domain.NeutronFloatingIP; |
musonous | 95c3ee5 | 2016-02-23 10:54:34 +0900 | [diff] [blame] | 22 | import org.slf4j.Logger; |
| 23 | import org.slf4j.LoggerFactory; |
| 24 | |
| 25 | import javax.ws.rs.Consumes; |
| 26 | import javax.ws.rs.DELETE; |
| 27 | import javax.ws.rs.POST; |
| 28 | import javax.ws.rs.PUT; |
| 29 | import javax.ws.rs.Path; |
| 30 | import javax.ws.rs.PathParam; |
| 31 | import javax.ws.rs.Produces; |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 32 | import javax.ws.rs.core.Context; |
musonous | 95c3ee5 | 2016-02-23 10:54:34 +0900 | [diff] [blame] | 33 | import javax.ws.rs.core.MediaType; |
| 34 | import javax.ws.rs.core.Response; |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 35 | import javax.ws.rs.core.UriBuilder; |
| 36 | import javax.ws.rs.core.UriInfo; |
musonous | 95c3ee5 | 2016-02-23 10:54:34 +0900 | [diff] [blame] | 37 | import java.io.InputStream; |
| 38 | |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 39 | import static javax.ws.rs.core.Response.created; |
| 40 | import static javax.ws.rs.core.Response.noContent; |
| 41 | import static javax.ws.rs.core.Response.status; |
Jian Li | 091d8d2 | 2018-02-20 10:42:06 +0900 | [diff] [blame] | 42 | import static org.onosproject.openstacknetworking.util.OpenstackUtil.jsonToModelEntity; |
musonous | 95c3ee5 | 2016-02-23 10:54:34 +0900 | [diff] [blame] | 43 | |
| 44 | /** |
| 45 | * Handles REST API call of Neutron L3 plugin. |
| 46 | */ |
| 47 | @Path("floatingips") |
| 48 | public class OpenstackFloatingIpWebResource extends AbstractWebResource { |
| 49 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 50 | |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 51 | private static final String MESSAGE = "Received floating IP %s request"; |
| 52 | private static final String FLOATING_IPS = "floatingips"; |
| 53 | |
| 54 | private final OpenstackRouterAdminService adminService = |
Jian Li | 5c51d90a | 2018-02-20 14:10:25 +0900 | [diff] [blame] | 55 | get(OpenstackRouterAdminService.class); |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 56 | |
| 57 | @Context |
| 58 | private UriInfo uriInfo; |
musonous | 95c3ee5 | 2016-02-23 10:54:34 +0900 | [diff] [blame] | 59 | |
| 60 | /** |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 61 | * Creates a floating IP from the JSON input stream. |
musonous | 95c3ee5 | 2016-02-23 10:54:34 +0900 | [diff] [blame] | 62 | * |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 63 | * @param input floating ip JSON input stream |
| 64 | * @return 201 CREATED if the JSON is correct, 400 BAD_REQUEST if the JSON |
| 65 | * is invalid or duplicated floating ip already exists |
Jian Li | 2077f66 | 2018-02-12 14:01:35 +0900 | [diff] [blame] | 66 | * @onos.rsModel NeutronFloatingIp |
musonous | 95c3ee5 | 2016-02-23 10:54:34 +0900 | [diff] [blame] | 67 | */ |
| 68 | @POST |
| 69 | @Consumes(MediaType.APPLICATION_JSON) |
| 70 | @Produces(MediaType.APPLICATION_JSON) |
| 71 | public Response createFloatingIp(InputStream input) { |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 72 | log.trace(String.format(MESSAGE, "CREATE")); |
musonous | 95c3ee5 | 2016-02-23 10:54:34 +0900 | [diff] [blame] | 73 | |
Jian Li | 091d8d2 | 2018-02-20 10:42:06 +0900 | [diff] [blame] | 74 | final NeutronFloatingIP floatingIp = (NeutronFloatingIP) |
| 75 | jsonToModelEntity(input, NeutronFloatingIP.class); |
| 76 | |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 77 | adminService.createFloatingIp(floatingIp); |
musonous | 95c3ee5 | 2016-02-23 10:54:34 +0900 | [diff] [blame] | 78 | |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 79 | UriBuilder locationBuilder = uriInfo.getBaseUriBuilder() |
| 80 | .path(FLOATING_IPS) |
| 81 | .path(floatingIp.getId()); |
musonous | 95c3ee5 | 2016-02-23 10:54:34 +0900 | [diff] [blame] | 82 | |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 83 | return created(locationBuilder.build()).build(); |
musonous | 95c3ee5 | 2016-02-23 10:54:34 +0900 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | /** |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 87 | * Updates the floating IP with the specified identifier. |
musonous | 95c3ee5 | 2016-02-23 10:54:34 +0900 | [diff] [blame] | 88 | * |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 89 | * @param id floating ip identifier |
| 90 | * @param input floating ip JSON input stream |
| 91 | * @return 200 OK with the updated floating ip, 400 BAD_REQUEST if the requested |
| 92 | * floating ip does not exist |
Jian Li | 2077f66 | 2018-02-12 14:01:35 +0900 | [diff] [blame] | 93 | * @onos.rsModel NeutronFloatingIp |
musonous | 95c3ee5 | 2016-02-23 10:54:34 +0900 | [diff] [blame] | 94 | */ |
| 95 | @PUT |
| 96 | @Path("{id}") |
| 97 | @Consumes(MediaType.APPLICATION_JSON) |
| 98 | @Produces(MediaType.APPLICATION_JSON) |
| 99 | public Response updateFloatingIp(@PathParam("id") String id, InputStream input) { |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 100 | log.trace(String.format(MESSAGE, "UPDATE " + id)); |
musonous | 95c3ee5 | 2016-02-23 10:54:34 +0900 | [diff] [blame] | 101 | |
Jian Li | 091d8d2 | 2018-02-20 10:42:06 +0900 | [diff] [blame] | 102 | final NeutronFloatingIP floatingIp = (NeutronFloatingIP) |
| 103 | jsonToModelEntity(input, NeutronFloatingIP.class); |
| 104 | |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 105 | adminService.updateFloatingIp(floatingIp); |
musonous | 95c3ee5 | 2016-02-23 10:54:34 +0900 | [diff] [blame] | 106 | |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 107 | return status(Response.Status.OK).build(); |
musonous | 95c3ee5 | 2016-02-23 10:54:34 +0900 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | /** |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 111 | * Removes the floating IP. |
musonous | 95c3ee5 | 2016-02-23 10:54:34 +0900 | [diff] [blame] | 112 | * |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 113 | * @param id floating ip identifier |
| 114 | * @return 204 NO_CONTENT, 400 BAD_REQUEST if the floating ip does not exist |
musonous | 95c3ee5 | 2016-02-23 10:54:34 +0900 | [diff] [blame] | 115 | */ |
| 116 | @DELETE |
| 117 | @Path("{id}") |
jskim | 7d88174 | 2016-09-13 09:39:02 +0900 | [diff] [blame] | 118 | @Consumes(MediaType.APPLICATION_JSON) |
musonous | 95c3ee5 | 2016-02-23 10:54:34 +0900 | [diff] [blame] | 119 | public Response deleteFloatingIp(@PathParam("id") String id) { |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 120 | log.trace(String.format(MESSAGE, "DELETE " + id)); |
musonous | 95c3ee5 | 2016-02-23 10:54:34 +0900 | [diff] [blame] | 121 | |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 122 | adminService.removeFloatingIp(id); |
| 123 | return noContent().build(); |
musonous | 95c3ee5 | 2016-02-23 10:54:34 +0900 | [diff] [blame] | 124 | } |
musonous | 95c3ee5 | 2016-02-23 10:54:34 +0900 | [diff] [blame] | 125 | } |