Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [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 org.onlab.util.ItemNotFoundException; |
| 23 | import org.onosproject.rest.AbstractWebResource; |
| 24 | import org.onosproject.vtnrsc.DefaultTenantNetwork; |
| 25 | import org.onosproject.vtnrsc.PhysicalNetwork; |
| 26 | import org.onosproject.vtnrsc.SegmentationId; |
| 27 | import org.onosproject.vtnrsc.TenantId; |
| 28 | import org.onosproject.vtnrsc.TenantNetwork; |
| 29 | import org.onosproject.vtnrsc.TenantNetwork.State; |
| 30 | import org.onosproject.vtnrsc.TenantNetwork.Type; |
| 31 | import org.onosproject.vtnrsc.TenantNetworkId; |
| 32 | import org.onosproject.vtnrsc.tenantnetwork.TenantNetworkService; |
| 33 | import org.onosproject.vtnweb.web.TenantNetworkCodec; |
| 34 | import org.slf4j.Logger; |
| 35 | import org.slf4j.LoggerFactory; |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 36 | |
| 37 | import javax.ws.rs.Consumes; |
| 38 | import javax.ws.rs.DELETE; |
| 39 | import javax.ws.rs.GET; |
| 40 | import javax.ws.rs.POST; |
| 41 | import javax.ws.rs.PUT; |
| 42 | import javax.ws.rs.Path; |
| 43 | import javax.ws.rs.PathParam; |
| 44 | import javax.ws.rs.Produces; |
| 45 | import javax.ws.rs.QueryParam; |
| 46 | import javax.ws.rs.core.MediaType; |
| 47 | import javax.ws.rs.core.Response; |
Jian Li | c2a542b | 2016-05-10 11:48:19 -0700 | [diff] [blame^] | 48 | import java.io.InputStream; |
| 49 | import java.util.Collections; |
| 50 | import java.util.HashSet; |
| 51 | import java.util.Iterator; |
| 52 | import java.util.Set; |
| 53 | import java.util.concurrent.ConcurrentMap; |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 54 | |
Jian Li | c2a542b | 2016-05-10 11:48:19 -0700 | [diff] [blame^] | 55 | import static com.google.common.base.Preconditions.checkArgument; |
| 56 | import static com.google.common.base.Preconditions.checkNotNull; |
| 57 | import static javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR; |
| 58 | import static javax.ws.rs.core.Response.Status.NOT_FOUND; |
| 59 | import static javax.ws.rs.core.Response.Status.OK; |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 60 | |
| 61 | /** |
| 62 | * REST resource for interacting with the inventory of networks. |
| 63 | */ |
| 64 | @Path("networks") |
| 65 | public class TenantNetworkWebResource extends AbstractWebResource { |
| 66 | public static final String NETWORK_NOT_FOUND = "Network is not found"; |
| 67 | public static final String NETWORK_ID_EXIST = "Network id is existed"; |
| 68 | public static final String NETWORK_ID_NOT_EXIST = "Network id is not existed"; |
| 69 | public static final String CREATE_NETWORK = "create network"; |
| 70 | public static final String UPDATE_NETWORK = "update network"; |
| 71 | public static final String DELETE_NETWORK = "delete network"; |
| 72 | public static final String JSON_NOT_NULL = "JsonNode can not be null"; |
| 73 | |
| 74 | protected static final Logger log = LoggerFactory |
| 75 | .getLogger(TenantNetworkWebResource.class); |
| 76 | private final ConcurrentMap<TenantNetworkId, TenantNetwork> networksMap = Maps |
| 77 | .newConcurrentMap(); |
| 78 | |
| 79 | @GET |
| 80 | @Produces({ MediaType.APPLICATION_JSON }) |
| 81 | public Response getNetworks(@QueryParam("id") String queryId, |
| 82 | @QueryParam("name") String queryName, |
| 83 | @QueryParam("admin_state_up") String queryadminStateUp, |
| 84 | @QueryParam("status") String querystate, |
| 85 | @QueryParam("shared") String queryshared, |
| 86 | @QueryParam("tenant_id") String querytenantId, |
| 87 | @QueryParam("router:external") String routerExternal, |
| 88 | @QueryParam("provider:network_type") String type, |
| 89 | @QueryParam("provider:physical_network") String physicalNetwork, |
| 90 | @QueryParam("provider:segmentation_id") String segmentationId) { |
| 91 | Iterable<TenantNetwork> networks = get(TenantNetworkService.class) |
| 92 | .getNetworks(); |
| 93 | Iterator<TenantNetwork> networkors = networks.iterator(); |
| 94 | while (networkors.hasNext()) { |
| 95 | TenantNetwork network = networkors.next(); |
| 96 | if ((queryId == null || queryId.equals(network.id().toString())) |
| 97 | && (queryName == null || queryName.equals(network.name())) |
| 98 | && (queryadminStateUp == null || queryadminStateUp |
Satish K | 3203557 | 2015-11-22 14:49:14 +0530 | [diff] [blame] | 99 | .equals(Boolean.toString(network.adminStateUp()))) |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 100 | && (querystate == null || querystate.equals(network.state() |
| 101 | .toString())) |
Satish K | 3203557 | 2015-11-22 14:49:14 +0530 | [diff] [blame] | 102 | && (queryshared == null || queryshared.equals(Boolean.toString(network |
| 103 | .shared()))) |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 104 | && (querytenantId == null || querytenantId.equals(network |
| 105 | .tenantId().toString())) |
Satish K | 3203557 | 2015-11-22 14:49:14 +0530 | [diff] [blame] | 106 | && (routerExternal == null || routerExternal.equals(Boolean.toString(network |
| 107 | .routerExternal()))) |
| 108 | && (type == null || type.equals(network.type().toString())) |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 109 | && (physicalNetwork == null || physicalNetwork |
Satish K | 3203557 | 2015-11-22 14:49:14 +0530 | [diff] [blame] | 110 | .equals(network.physicalNetwork().toString())) |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 111 | && (segmentationId == null || segmentationId.equals(network |
Satish K | 3203557 | 2015-11-22 14:49:14 +0530 | [diff] [blame] | 112 | .segmentationId().toString()))) { |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 113 | networksMap.putIfAbsent(network.id(), network); |
| 114 | } |
| 115 | } |
| 116 | networks = Collections.unmodifiableCollection(networksMap.values()); |
| 117 | ObjectNode result = new ObjectMapper().createObjectNode(); |
| 118 | result.set("networks", new TenantNetworkCodec().encode(networks, this)); |
| 119 | |
| 120 | return ok(result.toString()).build(); |
| 121 | } |
| 122 | |
| 123 | private State isState(String state) { |
| 124 | if (state.equals("ACTIVE")) { |
| 125 | return TenantNetwork.State.ACTIVE; |
| 126 | } else if (state.equals("BUILD")) { |
| 127 | return TenantNetwork.State.BUILD; |
| 128 | } else if (state.equals("DOWN")) { |
| 129 | return TenantNetwork.State.DOWN; |
| 130 | } else if (state.equals("ERROR")) { |
| 131 | return TenantNetwork.State.ERROR; |
| 132 | } else { |
| 133 | return null; |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | private Type isType(String type) { |
| 138 | if (type.equals("LOCAL")) { |
| 139 | return TenantNetwork.Type.LOCAL; |
| 140 | } else { |
| 141 | return null; |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | @GET |
| 146 | @Path("{id}") |
| 147 | @Produces({ MediaType.APPLICATION_JSON }) |
| 148 | public Response getNetwork(@PathParam("id") String id) { |
| 149 | |
| 150 | if (!get(TenantNetworkService.class).exists(TenantNetworkId |
| 151 | .networkId(id))) { |
lishuai | 0f47f34 | 2015-09-16 11:38:48 +0800 | [diff] [blame] | 152 | return Response.status(NOT_FOUND) |
| 153 | .entity(NETWORK_NOT_FOUND).build(); |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 154 | } |
| 155 | TenantNetwork network = nullIsNotFound(get(TenantNetworkService.class) |
| 156 | .getNetwork(TenantNetworkId.networkId(id)), NETWORK_NOT_FOUND); |
| 157 | ObjectNode result = new ObjectMapper().createObjectNode(); |
| 158 | result.set("network", new TenantNetworkCodec().encode(network, this)); |
| 159 | |
| 160 | return ok(result.toString()).build(); |
| 161 | |
| 162 | } |
| 163 | |
| 164 | @POST |
| 165 | @Produces(MediaType.APPLICATION_JSON) |
| 166 | @Consumes(MediaType.APPLICATION_JSON) |
| 167 | public Response createNetworks(InputStream input) { |
| 168 | try { |
| 169 | ObjectMapper mapper = new ObjectMapper(); |
| 170 | JsonNode cfg = mapper.readTree(input); |
| 171 | JsonNode nodes = null; |
| 172 | Iterable<TenantNetwork> networks = null; |
| 173 | if (cfg.get("network") != null) { |
| 174 | nodes = cfg.get("network"); |
| 175 | if (nodes.isArray()) { |
| 176 | networks = changeJson2objs(nodes); |
| 177 | } else { |
| 178 | networks = changeJson2obj(CREATE_NETWORK, null, nodes); |
| 179 | } |
| 180 | } else if (cfg.get("networks") != null) { |
| 181 | nodes = cfg.get("networks"); |
| 182 | networks = changeJson2objs(nodes); |
| 183 | } |
| 184 | Boolean issuccess = nullIsNotFound((get(TenantNetworkService.class) |
| 185 | .createNetworks(networks)), |
| 186 | NETWORK_NOT_FOUND); |
| 187 | |
| 188 | if (!issuccess) { |
| 189 | return Response.status(INTERNAL_SERVER_ERROR) |
| 190 | .entity(NETWORK_ID_EXIST).build(); |
| 191 | } |
| 192 | return Response.status(OK).entity(issuccess.toString()).build(); |
| 193 | } catch (Exception e) { |
| 194 | log.error("Creates tenantNetwork exception {}.", e.toString()); |
| 195 | return Response.status(INTERNAL_SERVER_ERROR).entity(e.toString()) |
| 196 | .build(); |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | @PUT |
| 201 | @Path("{id}") |
| 202 | @Produces(MediaType.APPLICATION_JSON) |
| 203 | @Consumes(MediaType.APPLICATION_JSON) |
| 204 | public Response updateNetworks(@PathParam("id") String id, InputStream input) { |
| 205 | try { |
| 206 | ObjectMapper mapper = new ObjectMapper(); |
| 207 | JsonNode cfg = mapper.readTree(input); |
| 208 | JsonNode nodes = null; |
| 209 | Iterable<TenantNetwork> networks = null; |
| 210 | if (cfg.get("network") != null) { |
| 211 | nodes = cfg.get("network"); |
| 212 | if (nodes.isArray()) { |
| 213 | networks = changeJson2objs(nodes); |
| 214 | } else { |
| 215 | networks = changeJson2obj(UPDATE_NETWORK, |
| 216 | TenantNetworkId.networkId(id), |
| 217 | nodes); |
| 218 | } |
| 219 | } else if (cfg.get("networks") != null) { |
| 220 | nodes = cfg.get("networks"); |
| 221 | networks = changeJson2objs(nodes); |
| 222 | } |
| 223 | Boolean issuccess = nullIsNotFound((get(TenantNetworkService.class) |
| 224 | .updateNetworks(networks)), |
| 225 | NETWORK_NOT_FOUND); |
| 226 | if (!issuccess) { |
| 227 | return Response.status(INTERNAL_SERVER_ERROR) |
| 228 | .entity(NETWORK_ID_NOT_EXIST).build(); |
| 229 | } |
| 230 | return Response.status(OK).entity(issuccess.toString()).build(); |
| 231 | } catch (Exception e) { |
| 232 | log.error("Updates tenantNetwork failed because of exception {}.", |
| 233 | e.toString()); |
| 234 | return Response.status(INTERNAL_SERVER_ERROR).entity(e.toString()) |
| 235 | .build(); |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | @DELETE |
| 240 | @Path("{id}") |
Wu wenbin | b0bd613 | 2016-05-10 19:20:23 +0800 | [diff] [blame] | 241 | @Consumes(MediaType.APPLICATION_JSON) |
| 242 | @Produces(MediaType.APPLICATION_JSON) |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 243 | public Response deleteNetworks(@PathParam("id") String id) { |
| 244 | log.debug("Deletes network by identifier {}.", id); |
| 245 | Set<TenantNetworkId> networkSet = new HashSet<>(); |
| 246 | networkSet.add(TenantNetworkId.networkId(id)); |
| 247 | Boolean issuccess = nullIsNotFound(get(TenantNetworkService.class) |
| 248 | .removeNetworks(networkSet), NETWORK_NOT_FOUND); |
| 249 | if (!issuccess) { |
| 250 | log.debug("Network identifier {} is not existed", id); |
| 251 | return Response.status(INTERNAL_SERVER_ERROR) |
| 252 | .entity(NETWORK_ID_NOT_EXIST).build(); |
| 253 | } |
Jian Li | c2a542b | 2016-05-10 11:48:19 -0700 | [diff] [blame^] | 254 | return ok(issuccess.toString()).build(); |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | /** |
| 258 | * Returns a collection of tenantNetworks. |
| 259 | * |
| 260 | * @param flag the flag |
| 261 | * @param networkId network identifier |
| 262 | * @param node the network json node |
| 263 | * @return a collection of tenantNetworks |
| 264 | */ |
| 265 | public Iterable<TenantNetwork> changeJson2obj(String flag, |
| 266 | TenantNetworkId networkId, |
| 267 | JsonNode node) { |
| 268 | checkNotNull(node, JSON_NOT_NULL); |
| 269 | TenantNetwork network = null; |
| 270 | ConcurrentMap<TenantNetworkId, TenantNetwork> networksMap = Maps |
| 271 | .newConcurrentMap(); |
Satish K | ab54ffa | 2015-11-27 14:19:42 +0530 | [diff] [blame] | 272 | checkArgument(node.get("admin_state_up").isBoolean(), "admin_state_up should be boolean"); |
| 273 | checkArgument(node.get("shared").isBoolean(), "shared should be boolean"); |
| 274 | checkArgument(node.get("router:external").isBoolean(), "router:external should be boolean"); |
| 275 | String name = node.get("name").asText(); |
| 276 | boolean adminStateUp = node.get("admin_state_up").asBoolean(); |
| 277 | String state = node.get("status").asText(); |
| 278 | boolean shared = node.get("shared").asBoolean(); |
| 279 | String tenantId = node.get("tenant_id").asText(); |
| 280 | boolean routerExternal = node.get("router:external").asBoolean(); |
| 281 | String type = node.get("provider:network_type").asText(); |
| 282 | String physicalNetwork = node.get("provider:physical_network").asText(); |
| 283 | String segmentationId = node.get("provider:segmentation_id").asText(); |
| 284 | TenantNetworkId id = null; |
| 285 | if (flag.equals(CREATE_NETWORK)) { |
| 286 | id = TenantNetworkId.networkId(node.get("id").asText()); |
| 287 | } else if (flag.equals(UPDATE_NETWORK)) { |
| 288 | id = networkId; |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 289 | } |
Satish K | ab54ffa | 2015-11-27 14:19:42 +0530 | [diff] [blame] | 290 | network = new DefaultTenantNetwork( |
| 291 | id, |
| 292 | name, |
| 293 | adminStateUp, |
| 294 | isState(state), |
| 295 | shared, |
| 296 | TenantId.tenantId(tenantId), |
| 297 | routerExternal, |
| 298 | isType(type), |
| 299 | PhysicalNetwork |
| 300 | .physicalNetwork(physicalNetwork), |
| 301 | SegmentationId |
| 302 | .segmentationId(segmentationId)); |
| 303 | networksMap.putIfAbsent(id, network); |
| 304 | |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 305 | return Collections.unmodifiableCollection(networksMap.values()); |
| 306 | } |
| 307 | |
| 308 | /** |
| 309 | * Returns a collection of tenantNetworks. |
| 310 | * |
| 311 | * @param nodes the network jsonnodes |
| 312 | * @return a collection of tenantNetworks |
| 313 | */ |
| 314 | public Iterable<TenantNetwork> changeJson2objs(JsonNode nodes) { |
| 315 | checkNotNull(nodes, JSON_NOT_NULL); |
| 316 | TenantNetwork network = null; |
| 317 | ConcurrentMap<TenantNetworkId, TenantNetwork> networksMap = Maps |
| 318 | .newConcurrentMap(); |
Satish K | ab54ffa | 2015-11-27 14:19:42 +0530 | [diff] [blame] | 319 | for (JsonNode node : nodes) { |
| 320 | String id = node.get("id").asText(); |
| 321 | String name = node.get("name").asText(); |
| 322 | boolean adminStateUp = node.get("admin_state_up").asBoolean(); |
| 323 | String state = node.get("status").asText(); |
| 324 | boolean shared = node.get("shared").asBoolean(); |
| 325 | String tenantId = node.get("tenant_id").asText(); |
| 326 | boolean routerExternal = node.get("router:external") |
| 327 | .asBoolean(); |
| 328 | String type = node.get("provider:network_type").asText(); |
| 329 | String physicalNetwork = node.get("provider:physical_network").asText(); |
| 330 | String segmentationId = node.get("provider:segmentation_id").asText(); |
| 331 | network = new DefaultTenantNetwork( |
| 332 | TenantNetworkId.networkId(id), |
| 333 | name, |
| 334 | adminStateUp, |
| 335 | isState(state), |
| 336 | shared, |
| 337 | TenantId.tenantId(tenantId), |
| 338 | routerExternal, |
| 339 | isType(type), |
| 340 | PhysicalNetwork.physicalNetwork(physicalNetwork), |
| 341 | SegmentationId.segmentationId(segmentationId)); |
| 342 | networksMap.putIfAbsent(TenantNetworkId.networkId(id), network); |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 343 | } |
Satish K | ab54ffa | 2015-11-27 14:19:42 +0530 | [diff] [blame] | 344 | |
Madan Jampani | 38a8821 | 2015-09-15 11:21:27 -0700 | [diff] [blame] | 345 | return Collections.unmodifiableCollection(networksMap.values()); |
| 346 | } |
| 347 | |
| 348 | /** |
| 349 | * Returns the specified item if that items is null; otherwise throws not |
| 350 | * found exception. |
| 351 | * |
| 352 | * @param item item to check |
| 353 | * @param <T> item type |
| 354 | * @param message not found message |
| 355 | * @return item if not null |
| 356 | * @throws org.onlab.util.ItemNotFoundException if item is null |
| 357 | */ |
| 358 | protected <T> T nullIsNotFound(T item, String message) { |
| 359 | if (item == null) { |
| 360 | throw new ItemNotFoundException(message); |
| 361 | } |
| 362 | return item; |
| 363 | } |
| 364 | } |