blob: a6880fdf27bab3cda720c4e6d957ab4e17048277 [file] [log] [blame]
musonous95c3ee52016-02-23 10:54:34 +09001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
musonous95c3ee52016-02-23 10:54:34 +09003 *
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
17package org.onosproject.openstacknetworking.web;
18
19import com.fasterxml.jackson.databind.ObjectMapper;
20import com.fasterxml.jackson.databind.node.ObjectNode;
21import org.onosproject.openstackinterface.OpenstackFloatingIP;
22import org.onosproject.openstackinterface.web.OpenstackFloatingIpCodec;
Hyunsun Moon05400872017-02-07 17:11:25 +090023import org.onosproject.openstacknetworking.api.OpenstackFloatingIpService;
musonous95c3ee52016-02-23 10:54:34 +090024import org.onosproject.rest.AbstractWebResource;
25import org.slf4j.Logger;
26import org.slf4j.LoggerFactory;
27
28import javax.ws.rs.Consumes;
29import javax.ws.rs.DELETE;
30import javax.ws.rs.POST;
31import javax.ws.rs.PUT;
32import javax.ws.rs.Path;
33import javax.ws.rs.PathParam;
34import javax.ws.rs.Produces;
35import javax.ws.rs.core.MediaType;
36import javax.ws.rs.core.Response;
37import java.io.InputStream;
38
39import static com.google.common.base.Preconditions.checkNotNull;
40
41/**
42 * Handles REST API call of Neutron L3 plugin.
43 */
44@Path("floatingips")
45public class OpenstackFloatingIpWebResource extends AbstractWebResource {
46 private final Logger log = LoggerFactory.getLogger(getClass());
47
Hyunsun Moonb3eb84d2016-07-27 19:10:52 -070048 private static final OpenstackFloatingIpCodec FLOATING_IP_CODEC = new OpenstackFloatingIpCodec();
musonous95c3ee52016-02-23 10:54:34 +090049
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 Moonb3eb84d2016-07-27 19:10:52 -070066 OpenstackFloatingIP osFloatingIp = FLOATING_IP_CODEC.decode(floatingIpNode, this);
67 OpenstackFloatingIpService floatingIpService =
68 getService(OpenstackFloatingIpService.class);
69 floatingIpService.createFloatingIp(osFloatingIp);
musonous95c3ee52016-02-23 10:54:34 +090070
71 log.debug("REST API CREATE floatingip called");
musonous95c3ee52016-02-23 10:54:34 +090072 return Response.status(Response.Status.OK).build();
73 } catch (Exception e) {
74 log.error("createFloatingIp failed with {}", e.toString());
musonous95c3ee52016-02-23 10:54:34 +090075 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 Moonb3eb84d2016-07-27 19:10:52 -070099 OpenstackFloatingIP osFloatingIp = FLOATING_IP_CODEC.decode(floatingIpNode, this);
100 OpenstackFloatingIpService floatingIpService =
101 getService(OpenstackFloatingIpService.class);
102 floatingIpService.updateFloatingIp(osFloatingIp);
musonous95c3ee52016-02-23 10:54:34 +0900103
104 log.debug("REST API UPDATE floatingip called {}", id);
musonous95c3ee52016-02-23 10:54:34 +0900105 return Response.status(Response.Status.OK).build();
106 } catch (Exception e) {
107 log.error("updateFloatingIp failed with {}", e.toString());
musonous95c3ee52016-02-23 10:54:34 +0900108 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 Lic2a542b2016-05-10 11:48:19 -0700117 * @return 204 OK
musonous95c3ee52016-02-23 10:54:34 +0900118 */
119 @DELETE
120 @Path("{id}")
jskim7d881742016-09-13 09:39:02 +0900121 @Consumes(MediaType.APPLICATION_JSON)
musonous95c3ee52016-02-23 10:54:34 +0900122 public Response deleteFloatingIp(@PathParam("id") String id) {
123 checkNotNull(id);
124
Hyunsun Moonb3eb84d2016-07-27 19:10:52 -0700125 OpenstackFloatingIpService floatingIpService =
126 getService(OpenstackFloatingIpService.class);
127 floatingIpService.deleteFloatingIp(id);
musonous95c3ee52016-02-23 10:54:34 +0900128
129 log.debug("REST API DELETE floatingip is called {}", id);
Jian Lic2a542b2016-05-10 11:48:19 -0700130 return Response.noContent().build();
musonous95c3ee52016-02-23 10:54:34 +0900131 }
132
133}