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 com.fasterxml.jackson.databind.JsonNode; |
| 20 | import org.onlab.osgi.DefaultServiceDirectory; |
| 21 | import org.onosproject.openstacknetworking.api.OpenstackRouterAdminService; |
musonous | 95c3ee5 | 2016-02-23 10:54:34 +0900 | [diff] [blame] | 22 | import org.onosproject.rest.AbstractWebResource; |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 23 | import org.openstack4j.core.transport.ObjectMapperSingleton; |
| 24 | import org.openstack4j.openstack.networking.domain.NeutronFloatingIP; |
musonous | 95c3ee5 | 2016-02-23 10:54:34 +0900 | [diff] [blame] | 25 | import org.slf4j.Logger; |
| 26 | import org.slf4j.LoggerFactory; |
| 27 | |
| 28 | import javax.ws.rs.Consumes; |
| 29 | import javax.ws.rs.DELETE; |
| 30 | import javax.ws.rs.POST; |
| 31 | import javax.ws.rs.PUT; |
| 32 | import javax.ws.rs.Path; |
| 33 | import javax.ws.rs.PathParam; |
| 34 | import javax.ws.rs.Produces; |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 35 | import javax.ws.rs.core.Context; |
musonous | 95c3ee5 | 2016-02-23 10:54:34 +0900 | [diff] [blame] | 36 | import javax.ws.rs.core.MediaType; |
| 37 | import javax.ws.rs.core.Response; |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 38 | import javax.ws.rs.core.UriBuilder; |
| 39 | import javax.ws.rs.core.UriInfo; |
musonous | 95c3ee5 | 2016-02-23 10:54:34 +0900 | [diff] [blame] | 40 | import java.io.InputStream; |
| 41 | |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 42 | import static com.fasterxml.jackson.databind.SerializationFeature.INDENT_OUTPUT; |
| 43 | import static javax.ws.rs.core.Response.created; |
| 44 | import static javax.ws.rs.core.Response.noContent; |
| 45 | import static javax.ws.rs.core.Response.status; |
musonous | 95c3ee5 | 2016-02-23 10:54:34 +0900 | [diff] [blame] | 46 | |
| 47 | /** |
| 48 | * Handles REST API call of Neutron L3 plugin. |
| 49 | */ |
| 50 | @Path("floatingips") |
| 51 | public class OpenstackFloatingIpWebResource extends AbstractWebResource { |
| 52 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 53 | |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 54 | private static final String MESSAGE = "Received floating IP %s request"; |
| 55 | private static final String FLOATING_IPS = "floatingips"; |
| 56 | |
| 57 | private final OpenstackRouterAdminService adminService = |
| 58 | DefaultServiceDirectory.getService(OpenstackRouterAdminService.class); |
| 59 | |
| 60 | @Context |
| 61 | private UriInfo uriInfo; |
musonous | 95c3ee5 | 2016-02-23 10:54:34 +0900 | [diff] [blame] | 62 | |
| 63 | /** |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 64 | * Creates a floating IP from the JSON input stream. |
musonous | 95c3ee5 | 2016-02-23 10:54:34 +0900 | [diff] [blame] | 65 | * |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 66 | * @param input floating ip JSON input stream |
| 67 | * @return 201 CREATED if the JSON is correct, 400 BAD_REQUEST if the JSON |
| 68 | * is invalid or duplicated floating ip already exists |
musonous | 95c3ee5 | 2016-02-23 10:54:34 +0900 | [diff] [blame] | 69 | */ |
| 70 | @POST |
| 71 | @Consumes(MediaType.APPLICATION_JSON) |
| 72 | @Produces(MediaType.APPLICATION_JSON) |
| 73 | public Response createFloatingIp(InputStream input) { |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 74 | log.trace(String.format(MESSAGE, "CREATE")); |
musonous | 95c3ee5 | 2016-02-23 10:54:34 +0900 | [diff] [blame] | 75 | |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 76 | final NeutronFloatingIP floatingIp = readFloatingIp(input); |
| 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 |
musonous | 95c3ee5 | 2016-02-23 10:54:34 +0900 | [diff] [blame] | 93 | */ |
| 94 | @PUT |
| 95 | @Path("{id}") |
| 96 | @Consumes(MediaType.APPLICATION_JSON) |
| 97 | @Produces(MediaType.APPLICATION_JSON) |
| 98 | public Response updateFloatingIp(@PathParam("id") String id, InputStream input) { |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 99 | log.trace(String.format(MESSAGE, "UPDATE " + id)); |
musonous | 95c3ee5 | 2016-02-23 10:54:34 +0900 | [diff] [blame] | 100 | |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 101 | final NeutronFloatingIP floatingIp = readFloatingIp(input); |
| 102 | adminService.updateFloatingIp(floatingIp); |
musonous | 95c3ee5 | 2016-02-23 10:54:34 +0900 | [diff] [blame] | 103 | |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 104 | return status(Response.Status.OK).build(); |
musonous | 95c3ee5 | 2016-02-23 10:54:34 +0900 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | /** |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 108 | * Removes the floating IP. |
musonous | 95c3ee5 | 2016-02-23 10:54:34 +0900 | [diff] [blame] | 109 | * |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 110 | * @param id floating ip identifier |
| 111 | * @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] | 112 | */ |
| 113 | @DELETE |
| 114 | @Path("{id}") |
jskim | 7d88174 | 2016-09-13 09:39:02 +0900 | [diff] [blame] | 115 | @Consumes(MediaType.APPLICATION_JSON) |
musonous | 95c3ee5 | 2016-02-23 10:54:34 +0900 | [diff] [blame] | 116 | public Response deleteFloatingIp(@PathParam("id") String id) { |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 117 | log.trace(String.format(MESSAGE, "DELETE " + id)); |
musonous | 95c3ee5 | 2016-02-23 10:54:34 +0900 | [diff] [blame] | 118 | |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 119 | adminService.removeFloatingIp(id); |
| 120 | return noContent().build(); |
musonous | 95c3ee5 | 2016-02-23 10:54:34 +0900 | [diff] [blame] | 121 | } |
| 122 | |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 123 | private NeutronFloatingIP readFloatingIp(InputStream input) { |
| 124 | try { |
| 125 | JsonNode jsonTree = mapper().enable(INDENT_OUTPUT).readTree(input); |
| 126 | log.trace(mapper().writeValueAsString(jsonTree)); |
| 127 | return ObjectMapperSingleton.getContext(NeutronFloatingIP.class) |
| 128 | .readerFor(NeutronFloatingIP.class) |
| 129 | .readValue(jsonTree); |
| 130 | } catch (Exception e) { |
| 131 | throw new IllegalArgumentException(); |
| 132 | } |
| 133 | } |
musonous | 95c3ee5 | 2016-02-23 10:54:34 +0900 | [diff] [blame] | 134 | } |