blob: c5ddb07555902a6696b8c8c6dabf408ac8bf1fab [file] [log] [blame]
jiangrui9d54c262015-11-28 14:23:39 +08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
jiangrui9d54c262015-11-28 14:23:39 +08003 *
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 */
16package org.onosproject.vtnweb.resources;
17
Jian Lic2a542b2016-05-10 11:48:19 -070018import com.fasterxml.jackson.databind.JsonNode;
19import com.fasterxml.jackson.databind.ObjectMapper;
20import com.fasterxml.jackson.databind.node.ObjectNode;
21import com.google.common.collect.Sets;
22import org.onlab.packet.IpAddress;
23import org.onlab.util.ItemNotFoundException;
24import org.onosproject.rest.AbstractWebResource;
25import org.onosproject.vtnrsc.DefaultFloatingIp;
26import org.onosproject.vtnrsc.FloatingIp;
27import org.onosproject.vtnrsc.FloatingIp.Status;
28import org.onosproject.vtnrsc.FloatingIpId;
29import org.onosproject.vtnrsc.RouterId;
30import org.onosproject.vtnrsc.TenantId;
31import org.onosproject.vtnrsc.TenantNetworkId;
32import org.onosproject.vtnrsc.VirtualPortId;
33import org.onosproject.vtnrsc.floatingip.FloatingIpService;
34import org.onosproject.vtnweb.web.FloatingIpCodec;
35import org.slf4j.Logger;
36import org.slf4j.LoggerFactory;
jiangrui9d54c262015-11-28 14:23:39 +080037
38import javax.ws.rs.Consumes;
39import javax.ws.rs.DELETE;
40import javax.ws.rs.GET;
41import javax.ws.rs.POST;
42import javax.ws.rs.PUT;
43import javax.ws.rs.Path;
44import javax.ws.rs.PathParam;
45import javax.ws.rs.Produces;
46import javax.ws.rs.QueryParam;
47import javax.ws.rs.core.MediaType;
48import javax.ws.rs.core.Response;
Jian Lic2a542b2016-05-10 11:48:19 -070049import java.io.IOException;
50import java.io.InputStream;
51import java.util.Collection;
52import java.util.Collections;
53import java.util.HashMap;
54import java.util.List;
55import java.util.Map;
56import java.util.Set;
jiangrui9d54c262015-11-28 14:23:39 +080057
Jian Lic2a542b2016-05-10 11:48:19 -070058import static com.google.common.base.Preconditions.checkNotNull;
59import static javax.ws.rs.core.Response.Status.BAD_REQUEST;
60import static javax.ws.rs.core.Response.Status.CONFLICT;
61import static javax.ws.rs.core.Response.Status.CREATED;
62import static javax.ws.rs.core.Response.Status.NOT_FOUND;
jiangrui9d54c262015-11-28 14:23:39 +080063
64@Path("floatingips")
65public class FloatingIpWebResource extends AbstractWebResource {
66 private final Logger log = LoggerFactory
67 .getLogger(FloatingIpWebResource.class);
68 public static final String CREATE_FAIL = "Floating IP is failed to create!";
69 public static final String UPDATE_FAIL = "Floating IP is failed to update!";
lishuaib43dbf72016-01-06 11:11:35 +080070 public static final String DELETE_FAIL = "Floating IP is failed to delete!";
jiangrui9d54c262015-11-28 14:23:39 +080071 public static final String GET_FAIL = "Floating IP is failed to get!";
72 public static final String NOT_EXIST = "Floating IP does not exist!";
73 public static final String DELETE_SUCCESS = "Floating IP delete success!";
74 public static final String JSON_NOT_NULL = "JsonNode can not be null";
75
76 @GET
77 @Produces(MediaType.APPLICATION_JSON)
78 public Response listFloatingIps() {
79 Collection<FloatingIp> floatingIps = get(FloatingIpService.class)
80 .getFloatingIps();
81 ObjectNode result = new ObjectMapper().createObjectNode();
82 result.set("floatingips",
83 new FloatingIpCodec().encode(floatingIps, this));
84 return ok(result.toString()).build();
85 }
86
87 @GET
88 @Path("{floatingIpUUID}")
89 @Produces(MediaType.APPLICATION_JSON)
90 public Response getFloatingIp(@PathParam("floatingIpUUID") String id,
91 @QueryParam("fields") List<String> fields) {
92
93 if (!get(FloatingIpService.class).exists(FloatingIpId.of(id))) {
94 return Response.status(NOT_FOUND).entity(NOT_EXIST).build();
95 }
96 FloatingIp sub = nullIsNotFound(get(FloatingIpService.class)
97 .getFloatingIp(FloatingIpId.of(id)), GET_FAIL);
98
99 ObjectNode result = new ObjectMapper().createObjectNode();
100 if (fields.size() > 0) {
101 result.set("floatingip",
102 new FloatingIpCodec().extracFields(sub, this, fields));
103 } else {
104 result.set("floatingip", new FloatingIpCodec().encode(sub, this));
105 }
106 return ok(result.toString()).build();
107 }
108
109 @POST
110 @Produces(MediaType.APPLICATION_JSON)
111 @Consumes(MediaType.APPLICATION_JSON)
112 public Response createFloatingIp(final InputStream input) {
113 try {
114 ObjectMapper mapper = new ObjectMapper();
115 JsonNode subnode = mapper.readTree(input);
116 Collection<FloatingIp> floatingIps = createOrUpdateByInputStream(subnode);
117 Boolean result = nullIsNotFound((get(FloatingIpService.class)
118 .createFloatingIps(floatingIps)),
119 CREATE_FAIL);
120 if (!result) {
121 return Response.status(CONFLICT).entity(CREATE_FAIL).build();
122 }
123 return Response.status(CREATED).entity(result.toString()).build();
124
125 } catch (Exception e) {
126 return Response.status(BAD_REQUEST).entity(e.getMessage()).build();
127 }
128 }
129
130 @PUT
131 @Path("{floatingIpUUID}")
132 @Produces(MediaType.APPLICATION_JSON)
133 @Consumes(MediaType.APPLICATION_JSON)
134 public Response updateFloatingIp(@PathParam("floatingIpUUID") String id,
135 final InputStream input) {
136 try {
137 ObjectMapper mapper = new ObjectMapper();
138 JsonNode subnode = mapper.readTree(input);
139 Collection<FloatingIp> floatingIps = createOrUpdateByInputStream(subnode);
140 Boolean result = nullIsNotFound(get(FloatingIpService.class)
141 .updateFloatingIps(floatingIps), UPDATE_FAIL);
142 if (!result) {
143 return Response.status(CONFLICT).entity(UPDATE_FAIL).build();
144 }
145 return ok(result.toString()).build();
146 } catch (Exception e) {
147 return Response.status(BAD_REQUEST).entity(e.getMessage()).build();
148 }
149 }
150
jiangrui9d54c262015-11-28 14:23:39 +0800151 @DELETE
Wu wenbinb0bd6132016-05-10 19:20:23 +0800152 @Path("{floatingIpUUID}")
153 @Consumes(MediaType.APPLICATION_JSON)
154 @Produces(MediaType.APPLICATION_JSON)
jiangrui9d54c262015-11-28 14:23:39 +0800155 public Response deleteSingleFloatingIp(@PathParam("floatingIpUUID") String id)
156 throws IOException {
157 try {
158 FloatingIpId floatingIpId = FloatingIpId.of(id);
159 Set<FloatingIpId> floatingIpIds = Sets.newHashSet(floatingIpId);
lishuaib43dbf72016-01-06 11:11:35 +0800160 Boolean result = nullIsNotFound(get(FloatingIpService.class)
161 .removeFloatingIps(floatingIpIds), DELETE_FAIL);
162 if (!result) {
163 return Response.status(CONFLICT).entity(DELETE_FAIL).build();
164 }
Jian Lic2a542b2016-05-10 11:48:19 -0700165 return Response.noContent().entity(DELETE_SUCCESS).build();
jiangrui9d54c262015-11-28 14:23:39 +0800166 } catch (Exception e) {
167 return Response.status(NOT_FOUND).entity(e.getMessage()).build();
168 }
169 }
170
171 private Collection<FloatingIp> createOrUpdateByInputStream(JsonNode subnode)
172 throws Exception {
173 checkNotNull(subnode, JSON_NOT_NULL);
174 Collection<FloatingIp> floatingIps = null;
175 JsonNode floatingIpNodes = subnode.get("floatingips");
176 if (floatingIpNodes == null) {
177 floatingIpNodes = subnode.get("floatingip");
178 }
179 log.debug("floatingNodes is {}", floatingIpNodes.toString());
180
181 if (floatingIpNodes.isArray()) {
182 throw new IllegalArgumentException("only singleton requests allowed");
183 } else {
184 floatingIps = changeJsonToSub(floatingIpNodes);
185 }
186 return floatingIps;
187 }
188
189 /**
190 * Returns a collection of floatingIps from floatingIpNodes.
191 *
192 * @param floatingIpNodes the floatingIp json node
193 * @return floatingIps a collection of floatingIp
Bharat saraswald270b182015-12-01 01:53:06 +0530194 * @throws Exception when any argument is illegal
jiangrui9d54c262015-11-28 14:23:39 +0800195 */
196 public Collection<FloatingIp> changeJsonToSub(JsonNode floatingIpNodes)
197 throws Exception {
198 checkNotNull(floatingIpNodes, JSON_NOT_NULL);
199 Map<FloatingIpId, FloatingIp> subMap = new HashMap<FloatingIpId, FloatingIp>();
200 if (!floatingIpNodes.hasNonNull("id")) {
201 throw new IllegalArgumentException("id should not be null");
202 } else if (floatingIpNodes.get("id").asText().isEmpty()) {
203 throw new IllegalArgumentException("id should not be empty");
204 }
205 FloatingIpId id = FloatingIpId.of(floatingIpNodes.get("id")
206 .asText());
207
208 if (!floatingIpNodes.hasNonNull("tenant_id")) {
209 throw new IllegalArgumentException("tenant_id should not be null");
210 } else if (floatingIpNodes.get("tenant_id").asText().isEmpty()) {
211 throw new IllegalArgumentException("tenant_id should not be empty");
212 }
213 TenantId tenantId = TenantId.tenantId(floatingIpNodes.get("tenant_id")
214 .asText());
215
216 if (!floatingIpNodes.hasNonNull("floating_network_id")) {
217 throw new IllegalArgumentException(
218 "floating_network_id should not be null");
219 } else if (floatingIpNodes.get("floating_network_id").asText()
220 .isEmpty()) {
221 throw new IllegalArgumentException(
222 "floating_network_id should not be empty");
223 }
224 TenantNetworkId networkId = TenantNetworkId.networkId(floatingIpNodes
225 .get("floating_network_id").asText());
226
227 VirtualPortId portId = null;
228 if (floatingIpNodes.hasNonNull("port_id")) {
229 portId = VirtualPortId.portId(floatingIpNodes.get("port_id")
230 .asText());
231 }
232
233 RouterId routerId = null;
234 if (floatingIpNodes.hasNonNull("router_id")) {
235 routerId = RouterId.valueOf(floatingIpNodes.get("router_id")
236 .asText());
237 }
238
239 IpAddress fixedIp = null;
240 if (floatingIpNodes.hasNonNull("fixed_ip_address")) {
241 fixedIp = IpAddress.valueOf(floatingIpNodes.get("fixed_ip_address")
242 .asText());
243 }
244
245 if (!floatingIpNodes.hasNonNull("floating_ip_address")) {
246 throw new IllegalArgumentException(
247 "floating_ip_address should not be null");
248 } else if (floatingIpNodes.get("floating_ip_address").asText()
249 .isEmpty()) {
250 throw new IllegalArgumentException(
251 "floating_ip_address should not be empty");
252 }
253 IpAddress floatingIp = IpAddress.valueOf(floatingIpNodes
254 .get("floating_ip_address").asText());
255
256 if (!floatingIpNodes.hasNonNull("status")) {
257 throw new IllegalArgumentException("status should not be null");
258 } else if (floatingIpNodes.get("status").asText().isEmpty()) {
259 throw new IllegalArgumentException("status should not be empty");
260 }
261 Status status = Status.valueOf(floatingIpNodes.get("status").asText());
262
263 DefaultFloatingIp floatingIpObj = new DefaultFloatingIp(id, tenantId,
264 networkId,
265 portId,
266 routerId,
267 floatingIp,
268 fixedIp, status);
269 subMap.put(id, floatingIpObj);
270 return Collections.unmodifiableCollection(subMap.values());
271 }
272
273 /**
274 * Returns the specified item if that items is null; otherwise throws not
275 * found exception.
276 *
277 * @param item item to check
278 * @param <T> item type
279 * @param message not found message
280 * @return item if not null
281 * @throws org.onlab.util.ItemNotFoundException if item is null
282 */
283 protected <T> T nullIsNotFound(T item, String message) {
284 if (item == null) {
285 throw new ItemNotFoundException(message);
286 }
287 return item;
288 }
289}