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; |
| 23 | import org.onosproject.openstacknetworking.OpenstackRoutingService; |
| 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 | |
| 48 | private static final OpenstackFloatingIpCodec FLOATING_IP_CODEC |
| 49 | = new OpenstackFloatingIpCodec(); |
| 50 | |
| 51 | /** |
| 52 | * Create FloatingIP. |
| 53 | * |
| 54 | * @param input JSON data describing FloatingIP |
| 55 | * @return 200 OK |
| 56 | */ |
| 57 | @POST |
| 58 | @Consumes(MediaType.APPLICATION_JSON) |
| 59 | @Produces(MediaType.APPLICATION_JSON) |
| 60 | public Response createFloatingIp(InputStream input) { |
| 61 | checkNotNull(input); |
| 62 | |
| 63 | try { |
| 64 | ObjectMapper mapper = new ObjectMapper(); |
| 65 | ObjectNode floatingIpNode = (ObjectNode) mapper.readTree(input); |
| 66 | |
| 67 | OpenstackFloatingIP osFloatingIp = |
| 68 | FLOATING_IP_CODEC.decode(floatingIpNode, this); |
| 69 | |
| 70 | OpenstackRoutingService routingService = |
| 71 | getService(OpenstackRoutingService.class); |
| 72 | |
| 73 | routingService.createFloatingIP(osFloatingIp); |
| 74 | |
| 75 | log.debug("REST API CREATE floatingip called"); |
| 76 | |
| 77 | return Response.status(Response.Status.OK).build(); |
| 78 | } catch (Exception e) { |
| 79 | log.error("createFloatingIp failed with {}", e.toString()); |
| 80 | |
| 81 | return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(e.toString()) |
| 82 | .build(); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Update FloatingIP. |
| 88 | * |
| 89 | * @param id FloatingIP identifier |
| 90 | * @param input JSON data describing FloatingIP |
| 91 | * @return 200 OK |
| 92 | */ |
| 93 | @PUT |
| 94 | @Path("{id}") |
| 95 | @Consumes(MediaType.APPLICATION_JSON) |
| 96 | @Produces(MediaType.APPLICATION_JSON) |
| 97 | public Response updateFloatingIp(@PathParam("id") String id, InputStream input) { |
| 98 | checkNotNull(id); |
| 99 | checkNotNull(input); |
| 100 | |
| 101 | try { |
| 102 | ObjectMapper mapper = new ObjectMapper(); |
| 103 | ObjectNode floatingIpNode = (ObjectNode) mapper.readTree(input); |
| 104 | |
| 105 | OpenstackFloatingIP osFloatingIp = |
| 106 | FLOATING_IP_CODEC.decode(floatingIpNode, this); |
| 107 | |
| 108 | OpenstackRoutingService routingService = |
| 109 | getService(OpenstackRoutingService.class); |
| 110 | |
| 111 | routingService.updateFloatingIP(osFloatingIp); |
| 112 | |
| 113 | log.debug("REST API UPDATE floatingip called {}", id); |
| 114 | |
| 115 | return Response.status(Response.Status.OK).build(); |
| 116 | } catch (Exception e) { |
| 117 | log.error("updateFloatingIp failed with {}", e.toString()); |
| 118 | |
| 119 | return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(e.toString()) |
| 120 | .build(); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Delete FloatingIP. |
| 126 | * |
| 127 | * @param id FloatingIP identifier |
Jian Li | c2a542b | 2016-05-10 11:48:19 -0700 | [diff] [blame^] | 128 | * @return 204 OK |
musonous | 95c3ee5 | 2016-02-23 10:54:34 +0900 | [diff] [blame] | 129 | */ |
| 130 | @DELETE |
| 131 | @Path("{id}") |
| 132 | @Produces(MediaType.APPLICATION_JSON) |
| 133 | public Response deleteFloatingIp(@PathParam("id") String id) { |
| 134 | checkNotNull(id); |
| 135 | |
| 136 | OpenstackRoutingService routingService = |
| 137 | getService(OpenstackRoutingService.class); |
| 138 | routingService.deleteFloatingIP(id); |
| 139 | |
| 140 | log.debug("REST API DELETE floatingip is called {}", id); |
| 141 | |
Jian Li | c2a542b | 2016-05-10 11:48:19 -0700 | [diff] [blame^] | 142 | return Response.noContent().build(); |
musonous | 95c3ee5 | 2016-02-23 10:54:34 +0900 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | } |