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 | |
Jian Li | 7b8c368 | 2019-05-12 13:57:15 +0900 | [diff] [blame^] | 19 | import org.apache.commons.io.IOUtils; |
| 20 | import org.onosproject.openstacknetworking.api.OpenstackHaService; |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 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.openstack.networking.domain.NeutronFloatingIP; |
musonous | 95c3ee5 | 2016-02-23 10:54:34 +0900 | [diff] [blame] | 24 | import org.slf4j.Logger; |
| 25 | import org.slf4j.LoggerFactory; |
| 26 | |
| 27 | import javax.ws.rs.Consumes; |
| 28 | import javax.ws.rs.DELETE; |
| 29 | import javax.ws.rs.POST; |
| 30 | import javax.ws.rs.PUT; |
| 31 | import javax.ws.rs.Path; |
| 32 | import javax.ws.rs.PathParam; |
| 33 | import javax.ws.rs.Produces; |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 34 | import javax.ws.rs.core.Context; |
musonous | 95c3ee5 | 2016-02-23 10:54:34 +0900 | [diff] [blame] | 35 | import javax.ws.rs.core.MediaType; |
| 36 | import javax.ws.rs.core.Response; |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 37 | import javax.ws.rs.core.UriBuilder; |
| 38 | import javax.ws.rs.core.UriInfo; |
Jian Li | 7b8c368 | 2019-05-12 13:57:15 +0900 | [diff] [blame^] | 39 | import java.io.IOException; |
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 javax.ws.rs.core.Response.created; |
| 43 | import static javax.ws.rs.core.Response.noContent; |
| 44 | import static javax.ws.rs.core.Response.status; |
Jian Li | 7b8c368 | 2019-05-12 13:57:15 +0900 | [diff] [blame^] | 45 | import static org.onosproject.openstacknetworking.api.Constants.REST_UTF8; |
Jian Li | dea0fdb | 2018-04-02 19:02:48 +0900 | [diff] [blame] | 46 | import static org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.jsonToModelEntity; |
Jian Li | 7b8c368 | 2019-05-12 13:57:15 +0900 | [diff] [blame^] | 47 | import static org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.syncDelete; |
| 48 | import static org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.syncPost; |
| 49 | import static org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.syncPut; |
musonous | 95c3ee5 | 2016-02-23 10:54:34 +0900 | [diff] [blame] | 50 | |
| 51 | /** |
Jian Li | 68efab4 | 2019-03-31 19:43:18 +0900 | [diff] [blame] | 52 | * Handles floating IP REST API call of Neutron L3 plugin. |
musonous | 95c3ee5 | 2016-02-23 10:54:34 +0900 | [diff] [blame] | 53 | */ |
| 54 | @Path("floatingips") |
| 55 | public class OpenstackFloatingIpWebResource extends AbstractWebResource { |
| 56 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 57 | |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 58 | private static final String MESSAGE = "Received floating IP %s request"; |
| 59 | private static final String FLOATING_IPS = "floatingips"; |
| 60 | |
| 61 | private final OpenstackRouterAdminService adminService = |
Jian Li | 5c51d90a | 2018-02-20 14:10:25 +0900 | [diff] [blame] | 62 | get(OpenstackRouterAdminService.class); |
Jian Li | 7b8c368 | 2019-05-12 13:57:15 +0900 | [diff] [blame^] | 63 | private final OpenstackHaService haService = get(OpenstackHaService.class); |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 64 | |
| 65 | @Context |
| 66 | private UriInfo uriInfo; |
musonous | 95c3ee5 | 2016-02-23 10:54:34 +0900 | [diff] [blame] | 67 | |
| 68 | /** |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 69 | * Creates a floating IP from the JSON input stream. |
musonous | 95c3ee5 | 2016-02-23 10:54:34 +0900 | [diff] [blame] | 70 | * |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 71 | * @param input floating ip JSON input stream |
| 72 | * @return 201 CREATED if the JSON is correct, 400 BAD_REQUEST if the JSON |
| 73 | * is invalid or duplicated floating ip already exists |
Jian Li | 7b8c368 | 2019-05-12 13:57:15 +0900 | [diff] [blame^] | 74 | * @throws IOException exception |
Jian Li | 2077f66 | 2018-02-12 14:01:35 +0900 | [diff] [blame] | 75 | * @onos.rsModel NeutronFloatingIp |
musonous | 95c3ee5 | 2016-02-23 10:54:34 +0900 | [diff] [blame] | 76 | */ |
| 77 | @POST |
| 78 | @Consumes(MediaType.APPLICATION_JSON) |
| 79 | @Produces(MediaType.APPLICATION_JSON) |
Jian Li | 7b8c368 | 2019-05-12 13:57:15 +0900 | [diff] [blame^] | 80 | public Response createFloatingIp(InputStream input) throws IOException { |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 81 | log.trace(String.format(MESSAGE, "CREATE")); |
musonous | 95c3ee5 | 2016-02-23 10:54:34 +0900 | [diff] [blame] | 82 | |
Jian Li | 7b8c368 | 2019-05-12 13:57:15 +0900 | [diff] [blame^] | 83 | String inputStr = IOUtils.toString(input, REST_UTF8); |
| 84 | |
| 85 | if (!haService.isActive()) { |
| 86 | return syncPost(haService, FLOATING_IPS, inputStr); |
| 87 | } |
| 88 | |
Jian Li | 091d8d2 | 2018-02-20 10:42:06 +0900 | [diff] [blame] | 89 | final NeutronFloatingIP floatingIp = (NeutronFloatingIP) |
Jian Li | 7b8c368 | 2019-05-12 13:57:15 +0900 | [diff] [blame^] | 90 | jsonToModelEntity(inputStr, NeutronFloatingIP.class); |
Jian Li | 091d8d2 | 2018-02-20 10:42:06 +0900 | [diff] [blame] | 91 | |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 92 | adminService.createFloatingIp(floatingIp); |
musonous | 95c3ee5 | 2016-02-23 10:54:34 +0900 | [diff] [blame] | 93 | |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 94 | UriBuilder locationBuilder = uriInfo.getBaseUriBuilder() |
| 95 | .path(FLOATING_IPS) |
| 96 | .path(floatingIp.getId()); |
musonous | 95c3ee5 | 2016-02-23 10:54:34 +0900 | [diff] [blame] | 97 | |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 98 | return created(locationBuilder.build()).build(); |
musonous | 95c3ee5 | 2016-02-23 10:54:34 +0900 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | /** |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 102 | * Updates the floating IP with the specified identifier. |
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 | * @param id floating ip identifier |
| 105 | * @param input floating ip JSON input stream |
| 106 | * @return 200 OK with the updated floating ip, 400 BAD_REQUEST if the requested |
| 107 | * floating ip does not exist |
Jian Li | 7b8c368 | 2019-05-12 13:57:15 +0900 | [diff] [blame^] | 108 | * @throws IOException exception |
Jian Li | 2077f66 | 2018-02-12 14:01:35 +0900 | [diff] [blame] | 109 | * @onos.rsModel NeutronFloatingIp |
musonous | 95c3ee5 | 2016-02-23 10:54:34 +0900 | [diff] [blame] | 110 | */ |
| 111 | @PUT |
| 112 | @Path("{id}") |
| 113 | @Consumes(MediaType.APPLICATION_JSON) |
| 114 | @Produces(MediaType.APPLICATION_JSON) |
Jian Li | 7b8c368 | 2019-05-12 13:57:15 +0900 | [diff] [blame^] | 115 | public Response updateFloatingIp(@PathParam("id") String id, InputStream input) throws IOException { |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 116 | log.trace(String.format(MESSAGE, "UPDATE " + id)); |
musonous | 95c3ee5 | 2016-02-23 10:54:34 +0900 | [diff] [blame] | 117 | |
Jian Li | 7b8c368 | 2019-05-12 13:57:15 +0900 | [diff] [blame^] | 118 | String inputStr = IOUtils.toString(input, REST_UTF8); |
| 119 | |
| 120 | if (!haService.isActive()) { |
| 121 | return syncPut(haService, FLOATING_IPS, id, inputStr); |
| 122 | } |
| 123 | |
Jian Li | 091d8d2 | 2018-02-20 10:42:06 +0900 | [diff] [blame] | 124 | final NeutronFloatingIP floatingIp = (NeutronFloatingIP) |
Jian Li | 7b8c368 | 2019-05-12 13:57:15 +0900 | [diff] [blame^] | 125 | jsonToModelEntity(inputStr, NeutronFloatingIP.class); |
Jian Li | 091d8d2 | 2018-02-20 10:42:06 +0900 | [diff] [blame] | 126 | |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 127 | adminService.updateFloatingIp(floatingIp); |
musonous | 95c3ee5 | 2016-02-23 10:54:34 +0900 | [diff] [blame] | 128 | |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 129 | return status(Response.Status.OK).build(); |
musonous | 95c3ee5 | 2016-02-23 10:54:34 +0900 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | /** |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 133 | * Removes the floating IP. |
musonous | 95c3ee5 | 2016-02-23 10:54:34 +0900 | [diff] [blame] | 134 | * |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 135 | * @param id floating ip identifier |
| 136 | * @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] | 137 | */ |
| 138 | @DELETE |
| 139 | @Path("{id}") |
jskim | 7d88174 | 2016-09-13 09:39:02 +0900 | [diff] [blame] | 140 | @Consumes(MediaType.APPLICATION_JSON) |
musonous | 95c3ee5 | 2016-02-23 10:54:34 +0900 | [diff] [blame] | 141 | public Response deleteFloatingIp(@PathParam("id") String id) { |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 142 | log.trace(String.format(MESSAGE, "DELETE " + id)); |
musonous | 95c3ee5 | 2016-02-23 10:54:34 +0900 | [diff] [blame] | 143 | |
Jian Li | 7b8c368 | 2019-05-12 13:57:15 +0900 | [diff] [blame^] | 144 | if (!haService.isActive()) { |
| 145 | return syncDelete(haService, FLOATING_IPS, id); |
| 146 | } |
| 147 | |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 148 | adminService.removeFloatingIp(id); |
| 149 | return noContent().build(); |
musonous | 95c3ee5 | 2016-02-23 10:54:34 +0900 | [diff] [blame] | 150 | } |
musonous | 95c3ee5 | 2016-02-23 10:54:34 +0900 | [diff] [blame] | 151 | } |