jiangrui | 330b0c9 | 2015-11-28 14:09:50 +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 | 330b0c9 | 2015-11-28 14:09:50 +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.Maps; |
| 22 | import com.google.common.collect.Sets; |
jiangrui | 330b0c9 | 2015-11-28 14:09:50 +0800 | [diff] [blame] | 23 | import org.onlab.packet.IpAddress; |
| 24 | import org.onlab.util.ItemNotFoundException; |
| 25 | import org.onosproject.rest.AbstractWebResource; |
| 26 | import org.onosproject.vtnrsc.DefaultRouter; |
| 27 | import org.onosproject.vtnrsc.FixedIp; |
| 28 | import org.onosproject.vtnrsc.Router; |
| 29 | import org.onosproject.vtnrsc.Router.Status; |
| 30 | import org.onosproject.vtnrsc.RouterGateway; |
| 31 | import org.onosproject.vtnrsc.RouterId; |
| 32 | import org.onosproject.vtnrsc.RouterInterface; |
| 33 | import org.onosproject.vtnrsc.SubnetId; |
| 34 | import org.onosproject.vtnrsc.TenantId; |
| 35 | import org.onosproject.vtnrsc.TenantNetworkId; |
| 36 | import org.onosproject.vtnrsc.VirtualPortId; |
| 37 | import org.onosproject.vtnrsc.router.RouterService; |
| 38 | import org.onosproject.vtnrsc.routerinterface.RouterInterfaceService; |
| 39 | import org.onosproject.vtnweb.web.RouterCodec; |
| 40 | import org.slf4j.Logger; |
| 41 | import org.slf4j.LoggerFactory; |
| 42 | |
Jian Li | c2a542b | 2016-05-10 11:48:19 -0700 | [diff] [blame] | 43 | import javax.ws.rs.Consumes; |
| 44 | import javax.ws.rs.DELETE; |
| 45 | import javax.ws.rs.GET; |
| 46 | import javax.ws.rs.POST; |
| 47 | import javax.ws.rs.PUT; |
| 48 | import javax.ws.rs.Path; |
| 49 | import javax.ws.rs.PathParam; |
| 50 | import javax.ws.rs.Produces; |
| 51 | import javax.ws.rs.QueryParam; |
| 52 | import javax.ws.rs.core.MediaType; |
| 53 | import javax.ws.rs.core.Response; |
| 54 | import java.io.IOException; |
| 55 | import java.io.InputStream; |
| 56 | import java.util.ArrayList; |
| 57 | import java.util.Collection; |
| 58 | import java.util.Collections; |
| 59 | import java.util.HashMap; |
| 60 | import java.util.List; |
| 61 | import java.util.Map; |
| 62 | import java.util.Set; |
| 63 | import java.util.concurrent.ConcurrentMap; |
| 64 | |
| 65 | import static com.google.common.base.Preconditions.checkArgument; |
| 66 | import static com.google.common.base.Preconditions.checkNotNull; |
| 67 | import static javax.ws.rs.core.Response.Status.BAD_REQUEST; |
| 68 | import static javax.ws.rs.core.Response.Status.CONFLICT; |
| 69 | import static javax.ws.rs.core.Response.Status.CREATED; |
| 70 | import static javax.ws.rs.core.Response.Status.NOT_FOUND; |
jiangrui | 330b0c9 | 2015-11-28 14:09:50 +0800 | [diff] [blame] | 71 | |
| 72 | @Path("routers") |
| 73 | public class RouterWebResource extends AbstractWebResource { |
| 74 | private final Logger log = LoggerFactory.getLogger(RouterWebResource.class); |
| 75 | public static final String CREATE_FAIL = "Router is failed to create!"; |
| 76 | public static final String UPDATE_FAIL = "Router is failed to update!"; |
| 77 | public static final String GET_FAIL = "Router is failed to get!"; |
| 78 | public static final String NOT_EXIST = "Router does not exist!"; |
| 79 | public static final String DELETE_SUCCESS = "Router delete success!"; |
| 80 | public static final String JSON_NOT_NULL = "JsonNode can not be null"; |
| 81 | public static final String INTFACR_ADD_SUCCESS = "Interface add success"; |
| 82 | public static final String INTFACR_DEL_SUCCESS = "Interface delete success"; |
| 83 | |
| 84 | @GET |
| 85 | @Produces(MediaType.APPLICATION_JSON) |
Wu wenbin | d0b119f | 2016-05-11 18:03:41 +0800 | [diff] [blame] | 86 | @Consumes(MediaType.APPLICATION_JSON) |
jiangrui | 330b0c9 | 2015-11-28 14:09:50 +0800 | [diff] [blame] | 87 | public Response listRouters() { |
| 88 | Collection<Router> routers = get(RouterService.class).getRouters(); |
| 89 | ObjectNode result = new ObjectMapper().createObjectNode(); |
| 90 | result.set("routers", new RouterCodec().encode(routers, this)); |
| 91 | return ok(result.toString()).build(); |
| 92 | } |
| 93 | |
| 94 | @GET |
| 95 | @Path("{routerUUID}") |
| 96 | @Produces(MediaType.APPLICATION_JSON) |
Wu wenbin | d0b119f | 2016-05-11 18:03:41 +0800 | [diff] [blame] | 97 | @Consumes(MediaType.APPLICATION_JSON) |
jiangrui | 330b0c9 | 2015-11-28 14:09:50 +0800 | [diff] [blame] | 98 | public Response getRouter(@PathParam("routerUUID") String id, |
| 99 | @QueryParam("fields") List<String> fields) { |
| 100 | |
| 101 | if (!get(RouterService.class).exists(RouterId.valueOf(id))) { |
| 102 | return Response.status(NOT_FOUND) |
| 103 | .entity("The Router does not exists").build(); |
| 104 | } |
| 105 | Router sub = nullIsNotFound(get(RouterService.class) |
lishuai | d6f0c9e | 2015-12-16 11:40:01 +0800 | [diff] [blame] | 106 | .getRouter(RouterId.valueOf(id)), NOT_EXIST); |
jiangrui | 330b0c9 | 2015-11-28 14:09:50 +0800 | [diff] [blame] | 107 | |
| 108 | ObjectNode result = new ObjectMapper().createObjectNode(); |
Jon Hall | cbd1b39 | 2017-01-18 20:15:44 -0800 | [diff] [blame] | 109 | if (!fields.isEmpty()) { |
jiangrui | 330b0c9 | 2015-11-28 14:09:50 +0800 | [diff] [blame] | 110 | result.set("router", |
| 111 | new RouterCodec().extracFields(sub, this, fields)); |
| 112 | } else { |
| 113 | result.set("router", new RouterCodec().encode(sub, this)); |
| 114 | } |
| 115 | return ok(result.toString()).build(); |
| 116 | } |
| 117 | |
| 118 | @POST |
| 119 | @Produces(MediaType.APPLICATION_JSON) |
| 120 | @Consumes(MediaType.APPLICATION_JSON) |
| 121 | public Response createRouter(final InputStream input) { |
| 122 | try { |
| 123 | ObjectMapper mapper = new ObjectMapper(); |
| 124 | JsonNode subnode = mapper.readTree(input); |
| 125 | Collection<Router> routers = createOrUpdateByInputStream(subnode); |
| 126 | |
| 127 | Boolean result = nullIsNotFound((get(RouterService.class) |
lishuai | d6f0c9e | 2015-12-16 11:40:01 +0800 | [diff] [blame] | 128 | .createRouters(routers)), CREATE_FAIL); |
jiangrui | 330b0c9 | 2015-11-28 14:09:50 +0800 | [diff] [blame] | 129 | if (!result) { |
| 130 | return Response.status(CONFLICT).entity(CREATE_FAIL).build(); |
| 131 | } |
| 132 | return Response.status(CREATED).entity(result.toString()).build(); |
| 133 | |
Ray Milkey | 986a47a | 2018-01-25 11:38:51 -0800 | [diff] [blame^] | 134 | } catch (IllegalArgumentException | IOException e) { |
jiangrui | 330b0c9 | 2015-11-28 14:09:50 +0800 | [diff] [blame] | 135 | return Response.status(BAD_REQUEST).entity(e.getMessage()).build(); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | @PUT |
| 140 | @Path("{routerUUID}") |
| 141 | @Produces(MediaType.APPLICATION_JSON) |
| 142 | @Consumes(MediaType.APPLICATION_JSON) |
| 143 | public Response updateRouter(@PathParam("routerUUID") String id, |
| 144 | final InputStream input) { |
| 145 | try { |
| 146 | ObjectMapper mapper = new ObjectMapper(); |
| 147 | JsonNode subnode = mapper.readTree(input); |
lishuai | d6f0c9e | 2015-12-16 11:40:01 +0800 | [diff] [blame] | 148 | Collection<Router> routers = changeUpdateJsonToSub(subnode, id); |
jiangrui | 330b0c9 | 2015-11-28 14:09:50 +0800 | [diff] [blame] | 149 | Boolean result = nullIsNotFound(get(RouterService.class) |
| 150 | .updateRouters(routers), UPDATE_FAIL); |
| 151 | if (!result) { |
| 152 | return Response.status(CONFLICT).entity(UPDATE_FAIL).build(); |
| 153 | } |
| 154 | return ok(result.toString()).build(); |
| 155 | } catch (Exception e) { |
| 156 | return Response.status(BAD_REQUEST).entity(e.getMessage()).build(); |
| 157 | } |
| 158 | } |
| 159 | |
jiangrui | 330b0c9 | 2015-11-28 14:09:50 +0800 | [diff] [blame] | 160 | @DELETE |
Wu wenbin | b0bd613 | 2016-05-10 19:20:23 +0800 | [diff] [blame] | 161 | @Path("{routerUUID}") |
| 162 | @Consumes(MediaType.APPLICATION_JSON) |
| 163 | @Produces(MediaType.APPLICATION_JSON) |
jiangrui | 330b0c9 | 2015-11-28 14:09:50 +0800 | [diff] [blame] | 164 | public Response deleteSingleRouter(@PathParam("routerUUID") String id) |
| 165 | throws IOException { |
| 166 | try { |
| 167 | RouterId routerId = RouterId.valueOf(id); |
| 168 | Set<RouterId> routerIds = Sets.newHashSet(routerId); |
| 169 | get(RouterService.class).removeRouters(routerIds); |
Jian Li | c2a542b | 2016-05-10 11:48:19 -0700 | [diff] [blame] | 170 | return Response.noContent().entity(DELETE_SUCCESS).build(); |
jiangrui | 330b0c9 | 2015-11-28 14:09:50 +0800 | [diff] [blame] | 171 | } catch (Exception e) { |
| 172 | return Response.status(BAD_REQUEST).entity(e.getMessage()).build(); |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | @PUT |
| 177 | @Path("{routerUUID}/add_router_interface") |
| 178 | @Produces(MediaType.APPLICATION_JSON) |
| 179 | @Consumes(MediaType.APPLICATION_JSON) |
| 180 | public Response addRouterInterface(@PathParam("routerUUID") String id, |
| 181 | final InputStream input) { |
| 182 | if (!get(RouterService.class).exists(RouterId.valueOf(id))) { |
| 183 | return Response.status(NOT_FOUND).entity(NOT_EXIST).build(); |
| 184 | } |
| 185 | try { |
| 186 | ObjectMapper mapper = new ObjectMapper(); |
| 187 | JsonNode subnode = mapper.readTree(input); |
| 188 | if (!subnode.hasNonNull("id")) { |
| 189 | throw new IllegalArgumentException("id should not be null"); |
| 190 | } else if (subnode.get("id").asText().isEmpty()) { |
| 191 | throw new IllegalArgumentException("id should not be empty"); |
| 192 | } |
| 193 | RouterId routerId = RouterId.valueOf(id); |
| 194 | if (!subnode.hasNonNull("subnet_id")) { |
| 195 | throw new IllegalArgumentException("subnet_id should not be null"); |
| 196 | } else if (subnode.get("subnet_id").asText().isEmpty()) { |
| 197 | throw new IllegalArgumentException("subnet_id should not be empty"); |
| 198 | } |
lishuai | d6f0c9e | 2015-12-16 11:40:01 +0800 | [diff] [blame] | 199 | SubnetId subnetId = SubnetId |
| 200 | .subnetId(subnode.get("subnet_id").asText()); |
jiangrui | 330b0c9 | 2015-11-28 14:09:50 +0800 | [diff] [blame] | 201 | if (!subnode.hasNonNull("tenant_id")) { |
| 202 | throw new IllegalArgumentException("tenant_id should not be null"); |
| 203 | } else if (subnode.get("tenant_id").asText().isEmpty()) { |
| 204 | throw new IllegalArgumentException("tenant_id should not be empty"); |
| 205 | } |
lishuai | d6f0c9e | 2015-12-16 11:40:01 +0800 | [diff] [blame] | 206 | TenantId tenentId = TenantId |
| 207 | .tenantId(subnode.get("tenant_id").asText()); |
jiangrui | 330b0c9 | 2015-11-28 14:09:50 +0800 | [diff] [blame] | 208 | if (!subnode.hasNonNull("port_id")) { |
| 209 | throw new IllegalArgumentException("port_id should not be null"); |
| 210 | } else if (subnode.get("port_id").asText().isEmpty()) { |
| 211 | throw new IllegalArgumentException("port_id should not be empty"); |
| 212 | } |
lishuai | d6f0c9e | 2015-12-16 11:40:01 +0800 | [diff] [blame] | 213 | VirtualPortId portId = VirtualPortId |
| 214 | .portId(subnode.get("port_id").asText()); |
jiangrui | 330b0c9 | 2015-11-28 14:09:50 +0800 | [diff] [blame] | 215 | RouterInterface routerInterface = RouterInterface |
| 216 | .routerInterface(subnetId, portId, routerId, tenentId); |
| 217 | get(RouterInterfaceService.class) |
| 218 | .addRouterInterface(routerInterface); |
| 219 | return ok(INTFACR_ADD_SUCCESS).build(); |
| 220 | } catch (Exception e) { |
| 221 | return Response.status(BAD_REQUEST).entity(e.getMessage()).build(); |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | @PUT |
| 226 | @Path("{routerUUID}/remove_router_interface") |
| 227 | @Produces(MediaType.APPLICATION_JSON) |
| 228 | @Consumes(MediaType.APPLICATION_JSON) |
| 229 | public Response removeRouterInterface(@PathParam("routerUUID") String id, |
| 230 | final InputStream input) { |
| 231 | if (!get(RouterService.class).exists(RouterId.valueOf(id))) { |
| 232 | return Response.status(NOT_FOUND).entity(NOT_EXIST).build(); |
| 233 | } |
| 234 | try { |
| 235 | ObjectMapper mapper = new ObjectMapper(); |
| 236 | JsonNode subnode = mapper.readTree(input); |
| 237 | if (!subnode.hasNonNull("id")) { |
| 238 | throw new IllegalArgumentException("id should not be null"); |
| 239 | } else if (subnode.get("id").asText().isEmpty()) { |
| 240 | throw new IllegalArgumentException("id should not be empty"); |
| 241 | } |
| 242 | RouterId routerId = RouterId.valueOf(id); |
| 243 | if (!subnode.hasNonNull("subnet_id")) { |
| 244 | throw new IllegalArgumentException("subnet_id should not be null"); |
| 245 | } else if (subnode.get("subnet_id").asText().isEmpty()) { |
| 246 | throw new IllegalArgumentException("subnet_id should not be empty"); |
| 247 | } |
lishuai | d6f0c9e | 2015-12-16 11:40:01 +0800 | [diff] [blame] | 248 | SubnetId subnetId = SubnetId |
| 249 | .subnetId(subnode.get("subnet_id").asText()); |
jiangrui | 330b0c9 | 2015-11-28 14:09:50 +0800 | [diff] [blame] | 250 | if (!subnode.hasNonNull("port_id")) { |
| 251 | throw new IllegalArgumentException("port_id should not be null"); |
| 252 | } else if (subnode.get("port_id").asText().isEmpty()) { |
| 253 | throw new IllegalArgumentException("port_id should not be empty"); |
| 254 | } |
lishuai | d6f0c9e | 2015-12-16 11:40:01 +0800 | [diff] [blame] | 255 | VirtualPortId portId = VirtualPortId |
| 256 | .portId(subnode.get("port_id").asText()); |
jiangrui | 330b0c9 | 2015-11-28 14:09:50 +0800 | [diff] [blame] | 257 | if (!subnode.hasNonNull("tenant_id")) { |
| 258 | throw new IllegalArgumentException("tenant_id should not be null"); |
| 259 | } else if (subnode.get("tenant_id").asText().isEmpty()) { |
| 260 | throw new IllegalArgumentException("tenant_id should not be empty"); |
| 261 | } |
lishuai | d6f0c9e | 2015-12-16 11:40:01 +0800 | [diff] [blame] | 262 | TenantId tenentId = TenantId |
| 263 | .tenantId(subnode.get("tenant_id").asText()); |
jiangrui | 330b0c9 | 2015-11-28 14:09:50 +0800 | [diff] [blame] | 264 | RouterInterface routerInterface = RouterInterface |
| 265 | .routerInterface(subnetId, portId, routerId, tenentId); |
| 266 | get(RouterInterfaceService.class) |
| 267 | .removeRouterInterface(routerInterface); |
| 268 | return ok(INTFACR_DEL_SUCCESS).build(); |
| 269 | } catch (Exception e) { |
| 270 | return Response.status(BAD_REQUEST).entity(e.getMessage()).build(); |
| 271 | } |
| 272 | } |
| 273 | |
Ray Milkey | 986a47a | 2018-01-25 11:38:51 -0800 | [diff] [blame^] | 274 | private Collection<Router> createOrUpdateByInputStream(JsonNode subnode) { |
jiangrui | 330b0c9 | 2015-11-28 14:09:50 +0800 | [diff] [blame] | 275 | checkNotNull(subnode, JSON_NOT_NULL); |
| 276 | JsonNode routerNode = subnode.get("routers"); |
| 277 | if (routerNode == null) { |
| 278 | routerNode = subnode.get("router"); |
| 279 | } |
| 280 | log.debug("routerNode is {}", routerNode.toString()); |
| 281 | |
| 282 | if (routerNode.isArray()) { |
Ray Milkey | 986a47a | 2018-01-25 11:38:51 -0800 | [diff] [blame^] | 283 | throw new IllegalArgumentException("only singleton requests allowed"); |
jiangrui | 330b0c9 | 2015-11-28 14:09:50 +0800 | [diff] [blame] | 284 | } else { |
| 285 | return changeJsonToSub(routerNode); |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | /** |
| 290 | * Returns a collection of floatingIps from floatingIpNodes. |
| 291 | * |
| 292 | * @param routerNode the router json node |
| 293 | * @return routers a collection of router |
jiangrui | 330b0c9 | 2015-11-28 14:09:50 +0800 | [diff] [blame] | 294 | */ |
Ray Milkey | 986a47a | 2018-01-25 11:38:51 -0800 | [diff] [blame^] | 295 | public Collection<Router> changeJsonToSub(JsonNode routerNode) { |
jiangrui | 330b0c9 | 2015-11-28 14:09:50 +0800 | [diff] [blame] | 296 | checkNotNull(routerNode, JSON_NOT_NULL); |
| 297 | Map<RouterId, Router> subMap = new HashMap<RouterId, Router>(); |
| 298 | if (!routerNode.hasNonNull("id")) { |
Rafal Szalecki | 54b3451 | 2017-01-25 10:39:01 +0100 | [diff] [blame] | 299 | throw new IllegalArgumentException("id should not be null"); |
jiangrui | 330b0c9 | 2015-11-28 14:09:50 +0800 | [diff] [blame] | 300 | } else if (routerNode.get("id").asText().isEmpty()) { |
| 301 | throw new IllegalArgumentException("id should not be empty"); |
| 302 | } |
| 303 | RouterId id = RouterId.valueOf(routerNode.get("id").asText()); |
| 304 | |
| 305 | if (!routerNode.hasNonNull("tenant_id")) { |
| 306 | throw new IllegalArgumentException("tenant_id should not be null"); |
| 307 | } else if (routerNode.get("tenant_id").asText().isEmpty()) { |
| 308 | throw new IllegalArgumentException("tenant_id should not be empty"); |
| 309 | } |
lishuai | d6f0c9e | 2015-12-16 11:40:01 +0800 | [diff] [blame] | 310 | TenantId tenantId = TenantId |
| 311 | .tenantId(routerNode.get("tenant_id").asText()); |
jiangrui | 330b0c9 | 2015-11-28 14:09:50 +0800 | [diff] [blame] | 312 | |
| 313 | VirtualPortId gwPortId = null; |
| 314 | if (routerNode.hasNonNull("gw_port_id")) { |
lishuai | d6f0c9e | 2015-12-16 11:40:01 +0800 | [diff] [blame] | 315 | gwPortId = VirtualPortId |
| 316 | .portId(routerNode.get("gw_port_id").asText()); |
jiangrui | 330b0c9 | 2015-11-28 14:09:50 +0800 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | if (!routerNode.hasNonNull("status")) { |
| 320 | throw new IllegalArgumentException("status should not be null"); |
| 321 | } else if (routerNode.get("status").asText().isEmpty()) { |
| 322 | throw new IllegalArgumentException("status should not be empty"); |
| 323 | } |
| 324 | Status status = Status.valueOf(routerNode.get("status").asText()); |
| 325 | |
| 326 | String routerName = null; |
| 327 | if (routerNode.hasNonNull("name")) { |
| 328 | routerName = routerNode.get("name").asText(); |
| 329 | } |
| 330 | |
| 331 | boolean adminStateUp = true; |
| 332 | checkArgument(routerNode.get("admin_state_up").isBoolean(), |
| 333 | "admin_state_up should be boolean"); |
| 334 | if (routerNode.hasNonNull("admin_state_up")) { |
| 335 | adminStateUp = routerNode.get("admin_state_up").asBoolean(); |
| 336 | } |
| 337 | boolean distributed = false; |
| 338 | if (routerNode.hasNonNull("distributed")) { |
| 339 | distributed = routerNode.get("distributed").asBoolean(); |
| 340 | } |
| 341 | RouterGateway gateway = null; |
| 342 | if (routerNode.hasNonNull("external_gateway_info")) { |
lishuai | d6f0c9e | 2015-12-16 11:40:01 +0800 | [diff] [blame] | 343 | gateway = jsonNodeToGateway(routerNode |
| 344 | .get("external_gateway_info")); |
| 345 | } |
| 346 | List<String> routes = new ArrayList<String>(); |
| 347 | DefaultRouter routerObj = new DefaultRouter(id, routerName, |
| 348 | adminStateUp, status, |
| 349 | distributed, gateway, |
| 350 | gwPortId, tenantId, routes); |
| 351 | subMap.put(id, routerObj); |
| 352 | return Collections.unmodifiableCollection(subMap.values()); |
| 353 | } |
| 354 | |
| 355 | /** |
| 356 | * Returns a collection of floatingIps from floatingIpNodes. |
| 357 | * |
| 358 | * @param subnode the router json node |
| 359 | * @param routerId the router identify |
| 360 | * @return routers a collection of router |
lishuai | d6f0c9e | 2015-12-16 11:40:01 +0800 | [diff] [blame] | 361 | */ |
| 362 | public Collection<Router> changeUpdateJsonToSub(JsonNode subnode, |
Ray Milkey | 986a47a | 2018-01-25 11:38:51 -0800 | [diff] [blame^] | 363 | String routerId) { |
lishuai | d6f0c9e | 2015-12-16 11:40:01 +0800 | [diff] [blame] | 364 | checkNotNull(subnode, JSON_NOT_NULL); |
| 365 | checkNotNull(routerId, "routerId should not be null"); |
| 366 | Map<RouterId, Router> subMap = new HashMap<RouterId, Router>(); |
| 367 | JsonNode routerNode = subnode.get("router"); |
| 368 | RouterId id = RouterId.valueOf(routerId); |
| 369 | Router sub = nullIsNotFound(get(RouterService.class).getRouter(id), |
| 370 | NOT_EXIST); |
| 371 | TenantId tenantId = sub.tenantId(); |
| 372 | |
| 373 | VirtualPortId gwPortId = null; |
| 374 | if (routerNode.hasNonNull("gw_port_id")) { |
| 375 | gwPortId = VirtualPortId |
| 376 | .portId(routerNode.get("gw_port_id").asText()); |
| 377 | } |
| 378 | Status status = sub.status(); |
| 379 | |
| 380 | String routerName = routerNode.get("name").asText(); |
| 381 | |
| 382 | checkArgument(routerNode.get("admin_state_up").isBoolean(), |
| 383 | "admin_state_up should be boolean"); |
| 384 | boolean adminStateUp = routerNode.get("admin_state_up").asBoolean(); |
| 385 | |
| 386 | boolean distributed = sub.distributed(); |
| 387 | if (routerNode.hasNonNull("distributed")) { |
| 388 | distributed = routerNode.get("distributed").asBoolean(); |
| 389 | } |
| 390 | RouterGateway gateway = sub.externalGatewayInfo(); |
| 391 | if (routerNode.hasNonNull("external_gateway_info")) { |
| 392 | gateway = jsonNodeToGateway(routerNode |
| 393 | .get("external_gateway_info")); |
jiangrui | 330b0c9 | 2015-11-28 14:09:50 +0800 | [diff] [blame] | 394 | } |
| 395 | List<String> routes = new ArrayList<String>(); |
| 396 | DefaultRouter routerObj = new DefaultRouter(id, routerName, |
| 397 | adminStateUp, status, |
| 398 | distributed, gateway, |
| 399 | gwPortId, tenantId, routes); |
| 400 | subMap.put(id, routerObj); |
| 401 | return Collections.unmodifiableCollection(subMap.values()); |
| 402 | } |
| 403 | |
| 404 | /** |
| 405 | * Changes JsonNode Gateway to the Gateway. |
| 406 | * |
| 407 | * @param gateway the gateway JsonNode |
| 408 | * @return gateway |
| 409 | */ |
| 410 | private RouterGateway jsonNodeToGateway(JsonNode gateway) { |
| 411 | checkNotNull(gateway, JSON_NOT_NULL); |
| 412 | if (!gateway.hasNonNull("network_id")) { |
| 413 | throw new IllegalArgumentException("network_id should not be null"); |
| 414 | } else if (gateway.get("network_id").asText().isEmpty()) { |
| 415 | throw new IllegalArgumentException("network_id should not be empty"); |
| 416 | } |
lishuai | d6f0c9e | 2015-12-16 11:40:01 +0800 | [diff] [blame] | 417 | TenantNetworkId networkId = TenantNetworkId |
| 418 | .networkId(gateway.get("network_id").asText()); |
jiangrui | 330b0c9 | 2015-11-28 14:09:50 +0800 | [diff] [blame] | 419 | |
| 420 | if (!gateway.hasNonNull("enable_snat")) { |
| 421 | throw new IllegalArgumentException("enable_snat should not be null"); |
| 422 | } else if (gateway.get("enable_snat").asText().isEmpty()) { |
| 423 | throw new IllegalArgumentException("enable_snat should not be empty"); |
| 424 | } |
| 425 | checkArgument(gateway.get("enable_snat").isBoolean(), |
| 426 | "enable_snat should be boolean"); |
| 427 | boolean enableSnat = gateway.get("enable_snat").asBoolean(); |
| 428 | |
| 429 | if (!gateway.hasNonNull("external_fixed_ips")) { |
lishuai | d6f0c9e | 2015-12-16 11:40:01 +0800 | [diff] [blame] | 430 | throw new IllegalArgumentException("external_fixed_ips should not be null"); |
jiangrui | 330b0c9 | 2015-11-28 14:09:50 +0800 | [diff] [blame] | 431 | } else if (gateway.get("external_fixed_ips").isNull()) { |
lishuai | d6f0c9e | 2015-12-16 11:40:01 +0800 | [diff] [blame] | 432 | throw new IllegalArgumentException("external_fixed_ips should not be empty"); |
jiangrui | 330b0c9 | 2015-11-28 14:09:50 +0800 | [diff] [blame] | 433 | } |
yuanyou | e2ed386 | 2016-05-06 13:18:08 +0800 | [diff] [blame] | 434 | Iterable<FixedIp> fixedIpList = jsonNodeToFixedIp(gateway |
jiangrui | 330b0c9 | 2015-11-28 14:09:50 +0800 | [diff] [blame] | 435 | .get("external_fixed_ips")); |
lishuai | d6f0c9e | 2015-12-16 11:40:01 +0800 | [diff] [blame] | 436 | RouterGateway gatewayObj = RouterGateway |
yuanyou | e2ed386 | 2016-05-06 13:18:08 +0800 | [diff] [blame] | 437 | .routerGateway(networkId, enableSnat, Sets.newHashSet(fixedIpList)); |
jiangrui | 330b0c9 | 2015-11-28 14:09:50 +0800 | [diff] [blame] | 438 | return gatewayObj; |
| 439 | } |
| 440 | |
| 441 | /** |
| 442 | * Changes JsonNode fixedIp to a collection of the fixedIp. |
| 443 | * |
| 444 | * @param fixedIp the allocationPools JsonNode |
| 445 | * @return a collection of fixedIp |
| 446 | */ |
yuanyou | e2ed386 | 2016-05-06 13:18:08 +0800 | [diff] [blame] | 447 | private Iterable<FixedIp> jsonNodeToFixedIp(JsonNode fixedIp) { |
jiangrui | 330b0c9 | 2015-11-28 14:09:50 +0800 | [diff] [blame] | 448 | checkNotNull(fixedIp, JSON_NOT_NULL); |
| 449 | ConcurrentMap<Integer, FixedIp> fixedIpMaps = Maps.newConcurrentMap(); |
| 450 | Integer i = 0; |
| 451 | for (JsonNode node : fixedIp) { |
| 452 | if (!node.hasNonNull("subnet_id")) { |
| 453 | throw new IllegalArgumentException("subnet_id should not be null"); |
| 454 | } else if (node.get("subnet_id").asText().isEmpty()) { |
| 455 | throw new IllegalArgumentException("subnet_id should not be empty"); |
| 456 | } |
lishuai | d6f0c9e | 2015-12-16 11:40:01 +0800 | [diff] [blame] | 457 | SubnetId subnetId = SubnetId |
| 458 | .subnetId(node.get("subnet_id").asText()); |
jiangrui | 330b0c9 | 2015-11-28 14:09:50 +0800 | [diff] [blame] | 459 | if (!node.hasNonNull("ip_address")) { |
| 460 | throw new IllegalArgumentException("ip_address should not be null"); |
| 461 | } else if (node.get("ip_address").asText().isEmpty()) { |
| 462 | throw new IllegalArgumentException("ip_address should not be empty"); |
| 463 | } |
lishuai | d6f0c9e | 2015-12-16 11:40:01 +0800 | [diff] [blame] | 464 | IpAddress ipAddress = IpAddress |
| 465 | .valueOf(node.get("ip_address").asText()); |
jiangrui | 330b0c9 | 2015-11-28 14:09:50 +0800 | [diff] [blame] | 466 | FixedIp fixedIpObj = FixedIp.fixedIp(subnetId, ipAddress); |
| 467 | |
| 468 | fixedIpMaps.putIfAbsent(i, fixedIpObj); |
| 469 | i++; |
| 470 | } |
| 471 | return Collections.unmodifiableCollection(fixedIpMaps.values()); |
| 472 | } |
| 473 | |
| 474 | /** |
| 475 | * Returns the specified item if that items is null; otherwise throws not |
| 476 | * found exception. |
| 477 | * |
| 478 | * @param item item to check |
| 479 | * @param <T> item type |
| 480 | * @param message not found message |
| 481 | * @return item if not null |
| 482 | * @throws org.onlab.util.ItemNotFoundException if item is null |
| 483 | */ |
| 484 | protected <T> T nullIsNotFound(T item, String message) { |
| 485 | if (item == null) { |
| 486 | throw new ItemNotFoundException(message); |
| 487 | } |
| 488 | return item; |
| 489 | } |
| 490 | } |