blob: 7638ff765792b844aebaf3b5f9153fd07afc65b6 [file] [log] [blame]
musonous95c3ee52016-02-23 10:54:34 +09001/*
2 * Copyright 2016 Open Networking Laboratory
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
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;
23import org.onosproject.openstacknetworking.OpenstackRoutingService;
24import 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
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
128 * @return 200 OK
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
142 return Response.status(Response.Status.OK).build();
143 }
144
145}