blob: 15d56cf834c6f1a50cd8862ca7c1bbfd4ae4532f [file] [log] [blame]
jiangrui330b0c92015-11-28 14:09:50 +08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
jiangrui330b0c92015-11-28 14:09:50 +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.Maps;
22import com.google.common.collect.Sets;
jiangrui330b0c92015-11-28 14:09:50 +080023import org.onlab.packet.IpAddress;
24import org.onlab.util.ItemNotFoundException;
25import org.onosproject.rest.AbstractWebResource;
26import org.onosproject.vtnrsc.DefaultRouter;
27import org.onosproject.vtnrsc.FixedIp;
28import org.onosproject.vtnrsc.Router;
29import org.onosproject.vtnrsc.Router.Status;
30import org.onosproject.vtnrsc.RouterGateway;
31import org.onosproject.vtnrsc.RouterId;
32import org.onosproject.vtnrsc.RouterInterface;
33import org.onosproject.vtnrsc.SubnetId;
34import org.onosproject.vtnrsc.TenantId;
35import org.onosproject.vtnrsc.TenantNetworkId;
36import org.onosproject.vtnrsc.VirtualPortId;
37import org.onosproject.vtnrsc.router.RouterService;
38import org.onosproject.vtnrsc.routerinterface.RouterInterfaceService;
39import org.onosproject.vtnweb.web.RouterCodec;
40import org.slf4j.Logger;
41import org.slf4j.LoggerFactory;
42
Jian Lic2a542b2016-05-10 11:48:19 -070043import javax.ws.rs.Consumes;
44import javax.ws.rs.DELETE;
45import javax.ws.rs.GET;
46import javax.ws.rs.POST;
47import javax.ws.rs.PUT;
48import javax.ws.rs.Path;
49import javax.ws.rs.PathParam;
50import javax.ws.rs.Produces;
51import javax.ws.rs.QueryParam;
52import javax.ws.rs.core.MediaType;
53import javax.ws.rs.core.Response;
54import java.io.IOException;
55import java.io.InputStream;
56import java.util.ArrayList;
57import java.util.Collection;
58import java.util.Collections;
59import java.util.HashMap;
60import java.util.List;
61import java.util.Map;
62import java.util.Set;
63import java.util.concurrent.ConcurrentMap;
64
65import static com.google.common.base.Preconditions.checkArgument;
66import static com.google.common.base.Preconditions.checkNotNull;
67import static javax.ws.rs.core.Response.Status.BAD_REQUEST;
68import static javax.ws.rs.core.Response.Status.CONFLICT;
69import static javax.ws.rs.core.Response.Status.CREATED;
70import static javax.ws.rs.core.Response.Status.NOT_FOUND;
Ray Milkey86ee5e82018-04-02 15:33:07 -070071import static org.onlab.util.Tools.readTreeFromStream;
jiangrui330b0c92015-11-28 14:09:50 +080072
73@Path("routers")
74public class RouterWebResource extends AbstractWebResource {
75 private final Logger log = LoggerFactory.getLogger(RouterWebResource.class);
76 public static final String CREATE_FAIL = "Router is failed to create!";
77 public static final String UPDATE_FAIL = "Router is failed to update!";
78 public static final String GET_FAIL = "Router is failed to get!";
79 public static final String NOT_EXIST = "Router does not exist!";
80 public static final String DELETE_SUCCESS = "Router delete success!";
81 public static final String JSON_NOT_NULL = "JsonNode can not be null";
82 public static final String INTFACR_ADD_SUCCESS = "Interface add success";
83 public static final String INTFACR_DEL_SUCCESS = "Interface delete success";
84
85 @GET
86 @Produces(MediaType.APPLICATION_JSON)
Wu wenbind0b119f2016-05-11 18:03:41 +080087 @Consumes(MediaType.APPLICATION_JSON)
jiangrui330b0c92015-11-28 14:09:50 +080088 public Response listRouters() {
89 Collection<Router> routers = get(RouterService.class).getRouters();
90 ObjectNode result = new ObjectMapper().createObjectNode();
91 result.set("routers", new RouterCodec().encode(routers, this));
92 return ok(result.toString()).build();
93 }
94
95 @GET
96 @Path("{routerUUID}")
97 @Produces(MediaType.APPLICATION_JSON)
Wu wenbind0b119f2016-05-11 18:03:41 +080098 @Consumes(MediaType.APPLICATION_JSON)
jiangrui330b0c92015-11-28 14:09:50 +080099 public Response getRouter(@PathParam("routerUUID") String id,
100 @QueryParam("fields") List<String> fields) {
101
102 if (!get(RouterService.class).exists(RouterId.valueOf(id))) {
103 return Response.status(NOT_FOUND)
104 .entity("The Router does not exists").build();
105 }
106 Router sub = nullIsNotFound(get(RouterService.class)
lishuaid6f0c9e2015-12-16 11:40:01 +0800107 .getRouter(RouterId.valueOf(id)), NOT_EXIST);
jiangrui330b0c92015-11-28 14:09:50 +0800108
109 ObjectNode result = new ObjectMapper().createObjectNode();
Jon Hallcbd1b392017-01-18 20:15:44 -0800110 if (!fields.isEmpty()) {
jiangrui330b0c92015-11-28 14:09:50 +0800111 result.set("router",
112 new RouterCodec().extracFields(sub, this, fields));
113 } else {
114 result.set("router", new RouterCodec().encode(sub, this));
115 }
116 return ok(result.toString()).build();
117 }
118
119 @POST
120 @Produces(MediaType.APPLICATION_JSON)
121 @Consumes(MediaType.APPLICATION_JSON)
122 public Response createRouter(final InputStream input) {
123 try {
124 ObjectMapper mapper = new ObjectMapper();
Ray Milkey86ee5e82018-04-02 15:33:07 -0700125 JsonNode subnode = readTreeFromStream(mapper, input);
jiangrui330b0c92015-11-28 14:09:50 +0800126 Collection<Router> routers = createOrUpdateByInputStream(subnode);
127
128 Boolean result = nullIsNotFound((get(RouterService.class)
lishuaid6f0c9e2015-12-16 11:40:01 +0800129 .createRouters(routers)), CREATE_FAIL);
jiangrui330b0c92015-11-28 14:09:50 +0800130 if (!result) {
131 return Response.status(CONFLICT).entity(CREATE_FAIL).build();
132 }
133 return Response.status(CREATED).entity(result.toString()).build();
134
Ray Milkey986a47a2018-01-25 11:38:51 -0800135 } catch (IllegalArgumentException | IOException e) {
jiangrui330b0c92015-11-28 14:09:50 +0800136 return Response.status(BAD_REQUEST).entity(e.getMessage()).build();
137 }
138 }
139
140 @PUT
141 @Path("{routerUUID}")
142 @Produces(MediaType.APPLICATION_JSON)
143 @Consumes(MediaType.APPLICATION_JSON)
144 public Response updateRouter(@PathParam("routerUUID") String id,
145 final InputStream input) {
146 try {
147 ObjectMapper mapper = new ObjectMapper();
Ray Milkey86ee5e82018-04-02 15:33:07 -0700148 JsonNode subnode = readTreeFromStream(mapper, input);
lishuaid6f0c9e2015-12-16 11:40:01 +0800149 Collection<Router> routers = changeUpdateJsonToSub(subnode, id);
jiangrui330b0c92015-11-28 14:09:50 +0800150 Boolean result = nullIsNotFound(get(RouterService.class)
151 .updateRouters(routers), UPDATE_FAIL);
152 if (!result) {
153 return Response.status(CONFLICT).entity(UPDATE_FAIL).build();
154 }
155 return ok(result.toString()).build();
156 } catch (Exception e) {
157 return Response.status(BAD_REQUEST).entity(e.getMessage()).build();
158 }
159 }
160
jiangrui330b0c92015-11-28 14:09:50 +0800161 @DELETE
Wu wenbinb0bd6132016-05-10 19:20:23 +0800162 @Path("{routerUUID}")
163 @Consumes(MediaType.APPLICATION_JSON)
164 @Produces(MediaType.APPLICATION_JSON)
jiangrui330b0c92015-11-28 14:09:50 +0800165 public Response deleteSingleRouter(@PathParam("routerUUID") String id)
166 throws IOException {
167 try {
168 RouterId routerId = RouterId.valueOf(id);
169 Set<RouterId> routerIds = Sets.newHashSet(routerId);
170 get(RouterService.class).removeRouters(routerIds);
Jian Lic2a542b2016-05-10 11:48:19 -0700171 return Response.noContent().entity(DELETE_SUCCESS).build();
jiangrui330b0c92015-11-28 14:09:50 +0800172 } catch (Exception e) {
173 return Response.status(BAD_REQUEST).entity(e.getMessage()).build();
174 }
175 }
176
177 @PUT
178 @Path("{routerUUID}/add_router_interface")
179 @Produces(MediaType.APPLICATION_JSON)
180 @Consumes(MediaType.APPLICATION_JSON)
181 public Response addRouterInterface(@PathParam("routerUUID") String id,
182 final InputStream input) {
183 if (!get(RouterService.class).exists(RouterId.valueOf(id))) {
184 return Response.status(NOT_FOUND).entity(NOT_EXIST).build();
185 }
186 try {
187 ObjectMapper mapper = new ObjectMapper();
Ray Milkey86ee5e82018-04-02 15:33:07 -0700188 JsonNode subnode = readTreeFromStream(mapper, input);
jiangrui330b0c92015-11-28 14:09:50 +0800189 if (!subnode.hasNonNull("id")) {
190 throw new IllegalArgumentException("id should not be null");
191 } else if (subnode.get("id").asText().isEmpty()) {
192 throw new IllegalArgumentException("id should not be empty");
193 }
194 RouterId routerId = RouterId.valueOf(id);
195 if (!subnode.hasNonNull("subnet_id")) {
196 throw new IllegalArgumentException("subnet_id should not be null");
197 } else if (subnode.get("subnet_id").asText().isEmpty()) {
198 throw new IllegalArgumentException("subnet_id should not be empty");
199 }
lishuaid6f0c9e2015-12-16 11:40:01 +0800200 SubnetId subnetId = SubnetId
201 .subnetId(subnode.get("subnet_id").asText());
jiangrui330b0c92015-11-28 14:09:50 +0800202 if (!subnode.hasNonNull("tenant_id")) {
203 throw new IllegalArgumentException("tenant_id should not be null");
204 } else if (subnode.get("tenant_id").asText().isEmpty()) {
205 throw new IllegalArgumentException("tenant_id should not be empty");
206 }
Sangyeok Sim9c2db2b2018-09-27 20:15:35 +0900207 TenantId tenantId = TenantId
lishuaid6f0c9e2015-12-16 11:40:01 +0800208 .tenantId(subnode.get("tenant_id").asText());
jiangrui330b0c92015-11-28 14:09:50 +0800209 if (!subnode.hasNonNull("port_id")) {
210 throw new IllegalArgumentException("port_id should not be null");
211 } else if (subnode.get("port_id").asText().isEmpty()) {
212 throw new IllegalArgumentException("port_id should not be empty");
213 }
lishuaid6f0c9e2015-12-16 11:40:01 +0800214 VirtualPortId portId = VirtualPortId
215 .portId(subnode.get("port_id").asText());
jiangrui330b0c92015-11-28 14:09:50 +0800216 RouterInterface routerInterface = RouterInterface
Sangyeok Sim9c2db2b2018-09-27 20:15:35 +0900217 .routerInterface(subnetId, portId, routerId, tenantId);
jiangrui330b0c92015-11-28 14:09:50 +0800218 get(RouterInterfaceService.class)
219 .addRouterInterface(routerInterface);
220 return ok(INTFACR_ADD_SUCCESS).build();
221 } catch (Exception e) {
222 return Response.status(BAD_REQUEST).entity(e.getMessage()).build();
223 }
224 }
225
226 @PUT
227 @Path("{routerUUID}/remove_router_interface")
228 @Produces(MediaType.APPLICATION_JSON)
229 @Consumes(MediaType.APPLICATION_JSON)
230 public Response removeRouterInterface(@PathParam("routerUUID") String id,
231 final InputStream input) {
232 if (!get(RouterService.class).exists(RouterId.valueOf(id))) {
233 return Response.status(NOT_FOUND).entity(NOT_EXIST).build();
234 }
235 try {
236 ObjectMapper mapper = new ObjectMapper();
Ray Milkey86ee5e82018-04-02 15:33:07 -0700237 JsonNode subnode = readTreeFromStream(mapper, input);
jiangrui330b0c92015-11-28 14:09:50 +0800238 if (!subnode.hasNonNull("id")) {
239 throw new IllegalArgumentException("id should not be null");
240 } else if (subnode.get("id").asText().isEmpty()) {
241 throw new IllegalArgumentException("id should not be empty");
242 }
243 RouterId routerId = RouterId.valueOf(id);
244 if (!subnode.hasNonNull("subnet_id")) {
245 throw new IllegalArgumentException("subnet_id should not be null");
246 } else if (subnode.get("subnet_id").asText().isEmpty()) {
247 throw new IllegalArgumentException("subnet_id should not be empty");
248 }
lishuaid6f0c9e2015-12-16 11:40:01 +0800249 SubnetId subnetId = SubnetId
250 .subnetId(subnode.get("subnet_id").asText());
jiangrui330b0c92015-11-28 14:09:50 +0800251 if (!subnode.hasNonNull("port_id")) {
252 throw new IllegalArgumentException("port_id should not be null");
253 } else if (subnode.get("port_id").asText().isEmpty()) {
254 throw new IllegalArgumentException("port_id should not be empty");
255 }
lishuaid6f0c9e2015-12-16 11:40:01 +0800256 VirtualPortId portId = VirtualPortId
257 .portId(subnode.get("port_id").asText());
jiangrui330b0c92015-11-28 14:09:50 +0800258 if (!subnode.hasNonNull("tenant_id")) {
259 throw new IllegalArgumentException("tenant_id should not be null");
260 } else if (subnode.get("tenant_id").asText().isEmpty()) {
261 throw new IllegalArgumentException("tenant_id should not be empty");
262 }
Sangyeok Sim9c2db2b2018-09-27 20:15:35 +0900263 TenantId tenantId = TenantId
lishuaid6f0c9e2015-12-16 11:40:01 +0800264 .tenantId(subnode.get("tenant_id").asText());
jiangrui330b0c92015-11-28 14:09:50 +0800265 RouterInterface routerInterface = RouterInterface
Sangyeok Sim9c2db2b2018-09-27 20:15:35 +0900266 .routerInterface(subnetId, portId, routerId, tenantId);
jiangrui330b0c92015-11-28 14:09:50 +0800267 get(RouterInterfaceService.class)
268 .removeRouterInterface(routerInterface);
269 return ok(INTFACR_DEL_SUCCESS).build();
270 } catch (Exception e) {
271 return Response.status(BAD_REQUEST).entity(e.getMessage()).build();
272 }
273 }
274
Ray Milkey986a47a2018-01-25 11:38:51 -0800275 private Collection<Router> createOrUpdateByInputStream(JsonNode subnode) {
jiangrui330b0c92015-11-28 14:09:50 +0800276 checkNotNull(subnode, JSON_NOT_NULL);
277 JsonNode routerNode = subnode.get("routers");
278 if (routerNode == null) {
279 routerNode = subnode.get("router");
280 }
281 log.debug("routerNode is {}", routerNode.toString());
282
283 if (routerNode.isArray()) {
Ray Milkey986a47a2018-01-25 11:38:51 -0800284 throw new IllegalArgumentException("only singleton requests allowed");
jiangrui330b0c92015-11-28 14:09:50 +0800285 } else {
286 return changeJsonToSub(routerNode);
287 }
288 }
289
290 /**
291 * Returns a collection of floatingIps from floatingIpNodes.
292 *
293 * @param routerNode the router json node
294 * @return routers a collection of router
jiangrui330b0c92015-11-28 14:09:50 +0800295 */
Ray Milkey986a47a2018-01-25 11:38:51 -0800296 public Collection<Router> changeJsonToSub(JsonNode routerNode) {
jiangrui330b0c92015-11-28 14:09:50 +0800297 checkNotNull(routerNode, JSON_NOT_NULL);
298 Map<RouterId, Router> subMap = new HashMap<RouterId, Router>();
299 if (!routerNode.hasNonNull("id")) {
Rafal Szalecki54b34512017-01-25 10:39:01 +0100300 throw new IllegalArgumentException("id should not be null");
jiangrui330b0c92015-11-28 14:09:50 +0800301 } else if (routerNode.get("id").asText().isEmpty()) {
302 throw new IllegalArgumentException("id should not be empty");
303 }
304 RouterId id = RouterId.valueOf(routerNode.get("id").asText());
305
306 if (!routerNode.hasNonNull("tenant_id")) {
307 throw new IllegalArgumentException("tenant_id should not be null");
308 } else if (routerNode.get("tenant_id").asText().isEmpty()) {
309 throw new IllegalArgumentException("tenant_id should not be empty");
310 }
lishuaid6f0c9e2015-12-16 11:40:01 +0800311 TenantId tenantId = TenantId
312 .tenantId(routerNode.get("tenant_id").asText());
jiangrui330b0c92015-11-28 14:09:50 +0800313
314 VirtualPortId gwPortId = null;
315 if (routerNode.hasNonNull("gw_port_id")) {
lishuaid6f0c9e2015-12-16 11:40:01 +0800316 gwPortId = VirtualPortId
317 .portId(routerNode.get("gw_port_id").asText());
jiangrui330b0c92015-11-28 14:09:50 +0800318 }
319
320 if (!routerNode.hasNonNull("status")) {
321 throw new IllegalArgumentException("status should not be null");
322 } else if (routerNode.get("status").asText().isEmpty()) {
323 throw new IllegalArgumentException("status should not be empty");
324 }
325 Status status = Status.valueOf(routerNode.get("status").asText());
326
327 String routerName = null;
328 if (routerNode.hasNonNull("name")) {
329 routerName = routerNode.get("name").asText();
330 }
331
332 boolean adminStateUp = true;
333 checkArgument(routerNode.get("admin_state_up").isBoolean(),
334 "admin_state_up should be boolean");
335 if (routerNode.hasNonNull("admin_state_up")) {
336 adminStateUp = routerNode.get("admin_state_up").asBoolean();
337 }
338 boolean distributed = false;
339 if (routerNode.hasNonNull("distributed")) {
340 distributed = routerNode.get("distributed").asBoolean();
341 }
342 RouterGateway gateway = null;
343 if (routerNode.hasNonNull("external_gateway_info")) {
lishuaid6f0c9e2015-12-16 11:40:01 +0800344 gateway = jsonNodeToGateway(routerNode
345 .get("external_gateway_info"));
346 }
347 List<String> routes = new ArrayList<String>();
348 DefaultRouter routerObj = new DefaultRouter(id, routerName,
349 adminStateUp, status,
350 distributed, gateway,
351 gwPortId, tenantId, routes);
352 subMap.put(id, routerObj);
353 return Collections.unmodifiableCollection(subMap.values());
354 }
355
356 /**
357 * Returns a collection of floatingIps from floatingIpNodes.
358 *
359 * @param subnode the router json node
360 * @param routerId the router identify
361 * @return routers a collection of router
lishuaid6f0c9e2015-12-16 11:40:01 +0800362 */
363 public Collection<Router> changeUpdateJsonToSub(JsonNode subnode,
Ray Milkey986a47a2018-01-25 11:38:51 -0800364 String routerId) {
lishuaid6f0c9e2015-12-16 11:40:01 +0800365 checkNotNull(subnode, JSON_NOT_NULL);
366 checkNotNull(routerId, "routerId should not be null");
367 Map<RouterId, Router> subMap = new HashMap<RouterId, Router>();
368 JsonNode routerNode = subnode.get("router");
369 RouterId id = RouterId.valueOf(routerId);
370 Router sub = nullIsNotFound(get(RouterService.class).getRouter(id),
371 NOT_EXIST);
372 TenantId tenantId = sub.tenantId();
373
374 VirtualPortId gwPortId = null;
375 if (routerNode.hasNonNull("gw_port_id")) {
376 gwPortId = VirtualPortId
377 .portId(routerNode.get("gw_port_id").asText());
378 }
379 Status status = sub.status();
380
381 String routerName = routerNode.get("name").asText();
382
383 checkArgument(routerNode.get("admin_state_up").isBoolean(),
384 "admin_state_up should be boolean");
385 boolean adminStateUp = routerNode.get("admin_state_up").asBoolean();
386
387 boolean distributed = sub.distributed();
388 if (routerNode.hasNonNull("distributed")) {
389 distributed = routerNode.get("distributed").asBoolean();
390 }
391 RouterGateway gateway = sub.externalGatewayInfo();
392 if (routerNode.hasNonNull("external_gateway_info")) {
393 gateway = jsonNodeToGateway(routerNode
394 .get("external_gateway_info"));
jiangrui330b0c92015-11-28 14:09:50 +0800395 }
396 List<String> routes = new ArrayList<String>();
397 DefaultRouter routerObj = new DefaultRouter(id, routerName,
398 adminStateUp, status,
399 distributed, gateway,
400 gwPortId, tenantId, routes);
401 subMap.put(id, routerObj);
402 return Collections.unmodifiableCollection(subMap.values());
403 }
404
405 /**
406 * Changes JsonNode Gateway to the Gateway.
407 *
408 * @param gateway the gateway JsonNode
409 * @return gateway
410 */
411 private RouterGateway jsonNodeToGateway(JsonNode gateway) {
412 checkNotNull(gateway, JSON_NOT_NULL);
413 if (!gateway.hasNonNull("network_id")) {
414 throw new IllegalArgumentException("network_id should not be null");
415 } else if (gateway.get("network_id").asText().isEmpty()) {
416 throw new IllegalArgumentException("network_id should not be empty");
417 }
lishuaid6f0c9e2015-12-16 11:40:01 +0800418 TenantNetworkId networkId = TenantNetworkId
419 .networkId(gateway.get("network_id").asText());
jiangrui330b0c92015-11-28 14:09:50 +0800420
421 if (!gateway.hasNonNull("enable_snat")) {
422 throw new IllegalArgumentException("enable_snat should not be null");
423 } else if (gateway.get("enable_snat").asText().isEmpty()) {
424 throw new IllegalArgumentException("enable_snat should not be empty");
425 }
426 checkArgument(gateway.get("enable_snat").isBoolean(),
427 "enable_snat should be boolean");
428 boolean enableSnat = gateway.get("enable_snat").asBoolean();
429
430 if (!gateway.hasNonNull("external_fixed_ips")) {
lishuaid6f0c9e2015-12-16 11:40:01 +0800431 throw new IllegalArgumentException("external_fixed_ips should not be null");
jiangrui330b0c92015-11-28 14:09:50 +0800432 } else if (gateway.get("external_fixed_ips").isNull()) {
lishuaid6f0c9e2015-12-16 11:40:01 +0800433 throw new IllegalArgumentException("external_fixed_ips should not be empty");
jiangrui330b0c92015-11-28 14:09:50 +0800434 }
yuanyoue2ed3862016-05-06 13:18:08 +0800435 Iterable<FixedIp> fixedIpList = jsonNodeToFixedIp(gateway
jiangrui330b0c92015-11-28 14:09:50 +0800436 .get("external_fixed_ips"));
lishuaid6f0c9e2015-12-16 11:40:01 +0800437 RouterGateway gatewayObj = RouterGateway
yuanyoue2ed3862016-05-06 13:18:08 +0800438 .routerGateway(networkId, enableSnat, Sets.newHashSet(fixedIpList));
jiangrui330b0c92015-11-28 14:09:50 +0800439 return gatewayObj;
440 }
441
442 /**
443 * Changes JsonNode fixedIp to a collection of the fixedIp.
444 *
445 * @param fixedIp the allocationPools JsonNode
446 * @return a collection of fixedIp
447 */
yuanyoue2ed3862016-05-06 13:18:08 +0800448 private Iterable<FixedIp> jsonNodeToFixedIp(JsonNode fixedIp) {
jiangrui330b0c92015-11-28 14:09:50 +0800449 checkNotNull(fixedIp, JSON_NOT_NULL);
450 ConcurrentMap<Integer, FixedIp> fixedIpMaps = Maps.newConcurrentMap();
451 Integer i = 0;
452 for (JsonNode node : fixedIp) {
453 if (!node.hasNonNull("subnet_id")) {
454 throw new IllegalArgumentException("subnet_id should not be null");
455 } else if (node.get("subnet_id").asText().isEmpty()) {
456 throw new IllegalArgumentException("subnet_id should not be empty");
457 }
lishuaid6f0c9e2015-12-16 11:40:01 +0800458 SubnetId subnetId = SubnetId
459 .subnetId(node.get("subnet_id").asText());
jiangrui330b0c92015-11-28 14:09:50 +0800460 if (!node.hasNonNull("ip_address")) {
461 throw new IllegalArgumentException("ip_address should not be null");
462 } else if (node.get("ip_address").asText().isEmpty()) {
463 throw new IllegalArgumentException("ip_address should not be empty");
464 }
lishuaid6f0c9e2015-12-16 11:40:01 +0800465 IpAddress ipAddress = IpAddress
466 .valueOf(node.get("ip_address").asText());
jiangrui330b0c92015-11-28 14:09:50 +0800467 FixedIp fixedIpObj = FixedIp.fixedIp(subnetId, ipAddress);
468
469 fixedIpMaps.putIfAbsent(i, fixedIpObj);
470 i++;
471 }
472 return Collections.unmodifiableCollection(fixedIpMaps.values());
473 }
474
475 /**
476 * Returns the specified item if that items is null; otherwise throws not
477 * found exception.
478 *
479 * @param item item to check
480 * @param <T> item type
481 * @param message not found message
482 * @return item if not null
483 * @throws org.onlab.util.ItemNotFoundException if item is null
484 */
485 protected <T> T nullIsNotFound(T item, String message) {
486 if (item == null) {
487 throw new ItemNotFoundException(message);
488 }
489 return item;
490 }
491}