musonous | 95c3ee5 | 2016-02-23 10:54:34 +0900 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
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 | |
| 19 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 20 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 21 | import org.onosproject.openstackinterface.OpenstackFloatingIP; |
| 22 | import org.onosproject.openstackinterface.web.OpenstackFloatingIpCodec; |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 23 | import org.onosproject.openstacknetworking.OpenstackFloatingIpService; |
musonous | 95c3ee5 | 2016-02-23 10:54:34 +0900 | [diff] [blame] | 24 | import org.onosproject.rest.AbstractWebResource; |
| 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; |
| 35 | import javax.ws.rs.core.MediaType; |
| 36 | import javax.ws.rs.core.Response; |
| 37 | import java.io.InputStream; |
| 38 | |
| 39 | import static com.google.common.base.Preconditions.checkNotNull; |
| 40 | |
| 41 | /** |
| 42 | * Handles REST API call of Neutron L3 plugin. |
| 43 | */ |
| 44 | @Path("floatingips") |
| 45 | public class OpenstackFloatingIpWebResource extends AbstractWebResource { |
| 46 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 47 | |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 48 | private static final OpenstackFloatingIpCodec FLOATING_IP_CODEC = new OpenstackFloatingIpCodec(); |
musonous | 95c3ee5 | 2016-02-23 10:54:34 +0900 | [diff] [blame] | 49 | |
| 50 | /** |
| 51 | * Create FloatingIP. |
| 52 | * |
| 53 | * @param input JSON data describing FloatingIP |
| 54 | * @return 200 OK |
| 55 | */ |
| 56 | @POST |
| 57 | @Consumes(MediaType.APPLICATION_JSON) |
| 58 | @Produces(MediaType.APPLICATION_JSON) |
| 59 | public Response createFloatingIp(InputStream input) { |
| 60 | checkNotNull(input); |
| 61 | |
| 62 | try { |
| 63 | ObjectMapper mapper = new ObjectMapper(); |
| 64 | ObjectNode floatingIpNode = (ObjectNode) mapper.readTree(input); |
| 65 | |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 66 | OpenstackFloatingIP osFloatingIp = FLOATING_IP_CODEC.decode(floatingIpNode, this); |
| 67 | OpenstackFloatingIpService floatingIpService = |
| 68 | getService(OpenstackFloatingIpService.class); |
| 69 | floatingIpService.createFloatingIp(osFloatingIp); |
musonous | 95c3ee5 | 2016-02-23 10:54:34 +0900 | [diff] [blame] | 70 | |
| 71 | log.debug("REST API CREATE floatingip called"); |
musonous | 95c3ee5 | 2016-02-23 10:54:34 +0900 | [diff] [blame] | 72 | return Response.status(Response.Status.OK).build(); |
| 73 | } catch (Exception e) { |
| 74 | log.error("createFloatingIp failed with {}", e.toString()); |
musonous | 95c3ee5 | 2016-02-23 10:54:34 +0900 | [diff] [blame] | 75 | return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(e.toString()) |
| 76 | .build(); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Update FloatingIP. |
| 82 | * |
| 83 | * @param id FloatingIP identifier |
| 84 | * @param input JSON data describing FloatingIP |
| 85 | * @return 200 OK |
| 86 | */ |
| 87 | @PUT |
| 88 | @Path("{id}") |
| 89 | @Consumes(MediaType.APPLICATION_JSON) |
| 90 | @Produces(MediaType.APPLICATION_JSON) |
| 91 | public Response updateFloatingIp(@PathParam("id") String id, InputStream input) { |
| 92 | checkNotNull(id); |
| 93 | checkNotNull(input); |
| 94 | |
| 95 | try { |
| 96 | ObjectMapper mapper = new ObjectMapper(); |
| 97 | ObjectNode floatingIpNode = (ObjectNode) mapper.readTree(input); |
| 98 | |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 99 | OpenstackFloatingIP osFloatingIp = FLOATING_IP_CODEC.decode(floatingIpNode, this); |
| 100 | OpenstackFloatingIpService floatingIpService = |
| 101 | getService(OpenstackFloatingIpService.class); |
| 102 | floatingIpService.updateFloatingIp(osFloatingIp); |
musonous | 95c3ee5 | 2016-02-23 10:54:34 +0900 | [diff] [blame] | 103 | |
| 104 | log.debug("REST API UPDATE floatingip called {}", id); |
musonous | 95c3ee5 | 2016-02-23 10:54:34 +0900 | [diff] [blame] | 105 | return Response.status(Response.Status.OK).build(); |
| 106 | } catch (Exception e) { |
| 107 | log.error("updateFloatingIp failed with {}", e.toString()); |
musonous | 95c3ee5 | 2016-02-23 10:54:34 +0900 | [diff] [blame] | 108 | return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(e.toString()) |
| 109 | .build(); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Delete FloatingIP. |
| 115 | * |
| 116 | * @param id FloatingIP identifier |
Jian Li | c2a542b | 2016-05-10 11:48:19 -0700 | [diff] [blame] | 117 | * @return 204 OK |
musonous | 95c3ee5 | 2016-02-23 10:54:34 +0900 | [diff] [blame] | 118 | */ |
| 119 | @DELETE |
| 120 | @Path("{id}") |
| 121 | @Produces(MediaType.APPLICATION_JSON) |
| 122 | public Response deleteFloatingIp(@PathParam("id") String id) { |
| 123 | checkNotNull(id); |
| 124 | |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 125 | OpenstackFloatingIpService floatingIpService = |
| 126 | getService(OpenstackFloatingIpService.class); |
| 127 | floatingIpService.deleteFloatingIp(id); |
musonous | 95c3ee5 | 2016-02-23 10:54:34 +0900 | [diff] [blame] | 128 | |
| 129 | log.debug("REST API DELETE floatingip is called {}", id); |
Jian Li | c2a542b | 2016-05-10 11:48:19 -0700 | [diff] [blame] | 130 | return Response.noContent().build(); |
musonous | 95c3ee5 | 2016-02-23 10:54:34 +0900 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | } |