jiangrui | 9d54c26 | 2015-11-28 14:23:39 +0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
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) |
| 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 | |
jiangrui | 9d54c26 | 2015-11-28 14:23:39 +0800 | [diff] [blame] | 151 | @DELETE |
Wu wenbin | b0bd613 | 2016-05-10 19:20:23 +0800 | [diff] [blame] | 152 | @Path("{floatingIpUUID}") |
| 153 | @Consumes(MediaType.APPLICATION_JSON) |
| 154 | @Produces(MediaType.APPLICATION_JSON) |
jiangrui | 9d54c26 | 2015-11-28 14:23:39 +0800 | [diff] [blame] | 155 | 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); |
lishuai | b43dbf7 | 2016-01-06 11:11:35 +0800 | [diff] [blame] | 160 | 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 Li | c2a542b | 2016-05-10 11:48:19 -0700 | [diff] [blame^] | 165 | return Response.noContent().entity(DELETE_SUCCESS).build(); |
jiangrui | 9d54c26 | 2015-11-28 14:23:39 +0800 | [diff] [blame] | 166 | } 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 saraswal | d270b18 | 2015-12-01 01:53:06 +0530 | [diff] [blame] | 194 | * @throws Exception when any argument is illegal |
jiangrui | 9d54c26 | 2015-11-28 14:23:39 +0800 | [diff] [blame] | 195 | */ |
| 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 | } |