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