CNlucius | 5b2fff1 | 2015-08-20 14:13:46 +0800 | [diff] [blame] | 1 | /*
|
| 2 | * Copyright 2015 Open Networking Laboratory
|
| 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 |
|
jiangrui | f4da26c | 2015-08-26 13:03:40 +0800 | [diff] [blame^] | 18 | import static com.google.common.base.Preconditions.checkArgument;
|
CNlucius | 5b2fff1 | 2015-08-20 14:13:46 +0800 | [diff] [blame] | 19 | import static com.google.common.base.Preconditions.checkNotNull;
|
| 20 | import static javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR;
|
| 21 | import static javax.ws.rs.core.Response.Status.OK;
|
| 22 |
|
| 23 | import java.io.InputStream;
|
| 24 | import java.util.Collection;
|
| 25 | import java.util.Collections;
|
| 26 | import java.util.HashMap;
|
| 27 | import java.util.HashSet;
|
| 28 | import java.util.Map;
|
| 29 | import java.util.Set;
|
| 30 | import java.util.concurrent.ConcurrentMap;
|
| 31 |
|
| 32 | import javax.ws.rs.Consumes;
|
| 33 | import javax.ws.rs.DELETE;
|
| 34 | import javax.ws.rs.GET;
|
| 35 | import javax.ws.rs.POST;
|
| 36 | import javax.ws.rs.PUT;
|
| 37 | import javax.ws.rs.Path;
|
| 38 | import javax.ws.rs.PathParam;
|
| 39 | import javax.ws.rs.Produces;
|
| 40 | import javax.ws.rs.core.MediaType;
|
| 41 | import javax.ws.rs.core.Response;
|
| 42 |
|
| 43 | import org.onlab.packet.IpAddress;
|
| 44 | import org.onlab.packet.MacAddress;
|
| 45 | import org.onlab.util.ItemNotFoundException;
|
| 46 | import org.onosproject.net.DeviceId;
|
| 47 | import org.onosproject.rest.AbstractWebResource;
|
| 48 | import org.onosproject.vtnrsc.AllowedAddressPair;
|
| 49 | import org.onosproject.vtnrsc.BindingHostId;
|
| 50 | import org.onosproject.vtnrsc.DefaultVirtualPort;
|
| 51 | import org.onosproject.vtnrsc.FixedIp;
|
| 52 | import org.onosproject.vtnrsc.SecurityGroup;
|
| 53 | import org.onosproject.vtnrsc.SubnetId;
|
| 54 | import org.onosproject.vtnrsc.TenantId;
|
| 55 | import org.onosproject.vtnrsc.TenantNetworkId;
|
| 56 | import org.onosproject.vtnrsc.VirtualPort;
|
| 57 | import org.onosproject.vtnrsc.VirtualPortId;
|
| 58 | import org.onosproject.vtnrsc.VirtualPort.State;
|
| 59 | import org.onosproject.vtnrsc.virtualport.VirtualPortService;
|
| 60 | import org.onosproject.vtnrsc.web.VirtualPortCodec;
|
| 61 | import org.slf4j.Logger;
|
| 62 | import org.slf4j.LoggerFactory;
|
| 63 |
|
| 64 | import com.fasterxml.jackson.databind.JsonNode;
|
| 65 | import com.fasterxml.jackson.databind.ObjectMapper;
|
| 66 | import com.fasterxml.jackson.databind.node.ObjectNode;
|
| 67 | import com.google.common.collect.Maps;
|
| 68 |
|
| 69 | /**
|
| 70 | * REST resource for interacting with the inventory of infrastructure
|
| 71 | * virtualPort.
|
| 72 | */
|
| 73 | @Path("ports")
|
| 74 | public class VirtualPortWebResource extends AbstractWebResource {
|
| 75 | public static final String VPORT_NOT_FOUND = "VirtualPort is not found";
|
| 76 | public static final String VPORT_ID_EXIST = "VirtualPort id is exist";
|
| 77 | public static final String VPORT_ID_NOT_EXIST = "VirtualPort id is not exist";
|
| 78 | public static final String JSON_NOT_NULL = "JsonNode can not be null";
|
| 79 | protected static final Logger log = LoggerFactory
|
| 80 | .getLogger(VirtualPortService.class);
|
| 81 |
|
| 82 | @GET
|
| 83 | @Produces({ MediaType.APPLICATION_JSON })
|
| 84 | public Response getPorts() {
|
| 85 | Iterable<VirtualPort> virtualPorts = get(VirtualPortService.class)
|
| 86 | .getPorts();
|
| 87 | ObjectNode result = new ObjectMapper().createObjectNode();
|
| 88 | result.set("ports", new VirtualPortCodec().encode(virtualPorts, this));
|
| 89 | return ok(result.toString()).build();
|
| 90 | }
|
| 91 |
|
| 92 | @GET
|
| 93 | @Path("{id}")
|
| 94 | @Produces({ MediaType.APPLICATION_JSON })
|
| 95 | public Response getportsById(@PathParam("id") String id) {
|
| 96 |
|
| 97 | if (!get(VirtualPortService.class).exists(VirtualPortId.portId(id))) {
|
jiangrui | bac6283 | 2015-08-25 20:30:16 +0800 | [diff] [blame] | 98 | return ok("The virtualPort does not exists").build();
|
CNlucius | 5b2fff1 | 2015-08-20 14:13:46 +0800 | [diff] [blame] | 99 | }
|
| 100 | VirtualPort virtualPort = nullIsNotFound(get(VirtualPortService.class)
|
| 101 | .getPort(VirtualPortId.portId(id)), VPORT_NOT_FOUND);
|
| 102 | ObjectNode result = new ObjectMapper().createObjectNode();
|
jiangrui | c983e73 | 2015-08-25 15:53:31 +0800 | [diff] [blame] | 103 | result.set("port", new VirtualPortCodec().encode(virtualPort, this));
|
CNlucius | 5b2fff1 | 2015-08-20 14:13:46 +0800 | [diff] [blame] | 104 | return ok(result.toString()).build();
|
| 105 | }
|
| 106 |
|
| 107 | @POST
|
| 108 | @Consumes(MediaType.APPLICATION_JSON)
|
| 109 | @Produces(MediaType.APPLICATION_JSON)
|
| 110 | public Response createPorts(InputStream input) {
|
| 111 | try {
|
| 112 | ObjectMapper mapper = new ObjectMapper();
|
| 113 | JsonNode cfg = mapper.readTree(input);
|
| 114 | Iterable<VirtualPort> vPorts = createOrUpdateByInputStream(cfg);
|
| 115 | Boolean issuccess = nullIsNotFound(get(VirtualPortService.class)
|
| 116 | .createPorts(vPorts), VPORT_NOT_FOUND);
|
| 117 | if (!issuccess) {
|
| 118 | return Response.status(INTERNAL_SERVER_ERROR)
|
| 119 | .entity(VPORT_ID_NOT_EXIST).build();
|
| 120 | }
|
| 121 | return Response.status(OK).entity(issuccess.toString()).build();
|
| 122 | } catch (Exception e) {
|
| 123 | log.error("Creates VirtualPort failed because of exception {}",
|
| 124 | e.toString());
|
| 125 | return Response.status(INTERNAL_SERVER_ERROR).entity(e.toString())
|
| 126 | .build();
|
| 127 | }
|
| 128 | }
|
| 129 |
|
| 130 | @Path("{portUUID}")
|
| 131 | @DELETE
|
| 132 | public Response deletePorts(@PathParam("portUUID") String id) {
|
| 133 | Set<VirtualPortId> vPortIds = new HashSet<VirtualPortId>();
|
| 134 | try {
|
| 135 | if (id != null) {
|
| 136 | vPortIds.add(VirtualPortId.portId(id));
|
| 137 | }
|
| 138 | Boolean issuccess = nullIsNotFound(get(VirtualPortService.class)
|
| 139 | .removePorts(vPortIds), VPORT_NOT_FOUND);
|
| 140 | if (!issuccess) {
|
| 141 | return Response.status(INTERNAL_SERVER_ERROR)
|
| 142 | .entity(VPORT_ID_NOT_EXIST).build();
|
| 143 | }
|
| 144 | return Response.status(OK).entity(issuccess.toString()).build();
|
| 145 | } catch (Exception e) {
|
| 146 | log.error("Deletes VirtualPort failed because of exception {}",
|
| 147 | e.toString());
|
| 148 | return Response.status(INTERNAL_SERVER_ERROR).entity(e.toString())
|
| 149 | .build();
|
| 150 | }
|
| 151 | }
|
| 152 |
|
| 153 | @PUT
|
| 154 | @Path("{id}")
|
| 155 | @Consumes(MediaType.APPLICATION_JSON)
|
| 156 | @Produces(MediaType.APPLICATION_JSON)
|
| 157 | public Response updatePorts(@PathParam("id") String id, InputStream input) {
|
| 158 | try {
|
| 159 | ObjectMapper mapper = new ObjectMapper();
|
| 160 | JsonNode cfg = mapper.readTree(input);
|
| 161 | Iterable<VirtualPort> vPorts = createOrUpdateByInputStream(cfg);
|
| 162 | Boolean issuccess = nullIsNotFound(get(VirtualPortService.class)
|
| 163 | .updatePorts(vPorts), VPORT_NOT_FOUND);
|
| 164 | if (!issuccess) {
|
| 165 | return Response.status(INTERNAL_SERVER_ERROR)
|
| 166 | .entity(VPORT_ID_NOT_EXIST).build();
|
| 167 | }
|
| 168 | return Response.status(OK).entity(issuccess.toString()).build();
|
| 169 | } catch (Exception e) {
|
| 170 | log.error("Updates failed because of exception {}", e.toString());
|
| 171 | return Response.status(INTERNAL_SERVER_ERROR).entity(e.toString())
|
| 172 | .build();
|
| 173 | }
|
| 174 | }
|
| 175 |
|
| 176 | /**
|
| 177 | * Returns a Object of the currently known infrastructure virtualPort.
|
| 178 | *
|
| 179 | * @param vPortNode the virtualPort json node
|
| 180 | * @return a collection of virtualPorts
|
| 181 | */
|
| 182 | public Iterable<VirtualPort> createOrUpdateByInputStream(JsonNode vPortNode) {
|
| 183 | checkNotNull(vPortNode, JSON_NOT_NULL);
|
| 184 | JsonNode vPortNodes = vPortNode.get("ports");
|
| 185 | if (vPortNodes == null) {
|
| 186 | vPortNodes = vPortNode.get("port");
|
| 187 | }
|
| 188 | if (vPortNodes.isArray()) {
|
| 189 | return changeJsonToPorts(vPortNodes);
|
| 190 | } else {
|
| 191 | return changeJsonToPort(vPortNodes);
|
| 192 | }
|
| 193 | }
|
| 194 |
|
| 195 | /**
|
| 196 | * Returns the iterable collection of virtualports from subnetNodes.
|
| 197 | *
|
| 198 | * @param vPortNodes the virtualPort json node
|
| 199 | * @return virtualPorts a collection of virtualPorts
|
| 200 | */
|
| 201 | public Iterable<VirtualPort> changeJsonToPorts(JsonNode vPortNodes) {
|
| 202 | checkNotNull(vPortNodes, JSON_NOT_NULL);
|
| 203 | Map<VirtualPortId, VirtualPort> portMap = new HashMap<VirtualPortId, VirtualPort>();
|
| 204 | Map<String, String> strMap = new HashMap<String, String>();
|
| 205 | for (JsonNode vPortnode : vPortNodes) {
|
| 206 | VirtualPortId id = VirtualPortId.portId(vPortnode.get("id")
|
| 207 | .asText());
|
| 208 | String name = vPortnode.get("name").asText();
|
| 209 | TenantId tenantId = TenantId.tenantId(vPortnode.get("tenant_id")
|
| 210 | .asText());
|
| 211 | TenantNetworkId networkId = TenantNetworkId.networkId(vPortnode
|
| 212 | .get("network_id").asText());
|
jiangrui | f4da26c | 2015-08-26 13:03:40 +0800 | [diff] [blame^] | 213 | checkArgument(vPortnode.get("admin_state_up").isBoolean(), "admin_state_up should be boolean");
|
CNlucius | 5b2fff1 | 2015-08-20 14:13:46 +0800 | [diff] [blame] | 214 | Boolean adminStateUp = vPortnode.get("admin_state_up").asBoolean();
|
| 215 | String state = vPortnode.get("status").asText();
|
| 216 | MacAddress macAddress = MacAddress.valueOf(vPortnode
|
| 217 | .get("mac_address").asText());
|
| 218 | DeviceId deviceId = DeviceId.deviceId(vPortnode.get("device_id")
|
| 219 | .asText());
|
| 220 | String deviceOwner = vPortnode.get("device_owner").asText();
|
| 221 | JsonNode fixedIpNodes = vPortNodes.get("fixed_ips");
|
| 222 | Set<FixedIp> fixedIps = new HashSet<FixedIp>();
|
| 223 | for (JsonNode fixedIpNode : fixedIpNodes) {
|
| 224 | FixedIp fixedIp = jsonNodeToFixedIps(fixedIpNode);
|
| 225 | fixedIps.add(fixedIp);
|
| 226 | }
|
| 227 |
|
| 228 | BindingHostId bindingHostId = BindingHostId
|
| 229 | .bindingHostId(vPortnode.get("binding:host_id").asText());
|
| 230 | String bindingVnicType = vPortnode.get("binding:vnic_type")
|
| 231 | .asText();
|
| 232 | String bindingVifType = vPortnode.get("binding:vif_type").asText();
|
| 233 | String bindingVifDetails = vPortnode.get("binding:vif_details")
|
| 234 | .asText();
|
| 235 | JsonNode allowedAddressPairJsonNode = vPortnode
|
| 236 | .get("allowed_address_pairs");
|
| 237 | Collection<AllowedAddressPair> allowedAddressPairs =
|
| 238 | jsonNodeToAllowedAddressPair(allowedAddressPairJsonNode);
|
| 239 | JsonNode securityGroupNode = vPortnode.get("security_groups");
|
| 240 | Collection<SecurityGroup> securityGroups = jsonNodeToSecurityGroup(securityGroupNode);
|
| 241 | strMap.put("name", name);
|
| 242 | strMap.put("deviceOwner", deviceOwner);
|
| 243 | strMap.put("bindingVnicType", bindingVnicType);
|
| 244 | strMap.put("bindingVifType", bindingVifType);
|
| 245 | strMap.put("bindingVifDetails", bindingVifDetails);
|
| 246 | VirtualPort vPort = new DefaultVirtualPort(id, networkId,
|
| 247 | adminStateUp, strMap,
|
| 248 | isState(state),
|
| 249 | macAddress, tenantId,
|
| 250 | deviceId, fixedIps,
|
| 251 | bindingHostId,
|
| 252 | allowedAddressPairs,
|
| 253 | securityGroups);
|
| 254 | portMap.put(id, vPort);
|
| 255 | }
|
| 256 | return Collections.unmodifiableCollection(portMap.values());
|
| 257 | }
|
| 258 |
|
| 259 | /**
|
| 260 | * Returns a collection of virtualPorts from subnetNodes.
|
| 261 | *
|
| 262 | * @param vPortNodes the virtualPort json node
|
| 263 | * @return virtualPorts a collection of virtualPorts
|
| 264 | */
|
| 265 | public Iterable<VirtualPort> changeJsonToPort(JsonNode vPortNodes) {
|
| 266 | checkNotNull(vPortNodes, JSON_NOT_NULL);
|
| 267 | Map<VirtualPortId, VirtualPort> vportMap = new HashMap<VirtualPortId, VirtualPort>();
|
| 268 | Map<String, String> strMap = new HashMap<String, String>();
|
| 269 | VirtualPortId id = VirtualPortId.portId(vPortNodes.get("id").asText());
|
| 270 | String name = vPortNodes.get("name").asText();
|
| 271 | TenantId tenantId = TenantId.tenantId(vPortNodes.get("tenant_id")
|
| 272 | .asText());
|
| 273 | TenantNetworkId networkId = TenantNetworkId.networkId(vPortNodes
|
| 274 | .get("network_id").asText());
|
| 275 | Boolean adminStateUp = vPortNodes.get("admin_state_up").asBoolean();
|
| 276 | String state = vPortNodes.get("status").asText();
|
| 277 | MacAddress macAddress = MacAddress.valueOf(vPortNodes
|
| 278 | .get("mac_address").asText());
|
| 279 | DeviceId deviceId = DeviceId.deviceId(vPortNodes.get("device_id")
|
| 280 | .asText());
|
| 281 | String deviceOwner = vPortNodes.get("device_owner").asText();
|
| 282 | JsonNode fixedIpNodes = vPortNodes.get("fixed_ips");
|
| 283 | Set<FixedIp> fixedIps = new HashSet<FixedIp>();
|
| 284 | for (JsonNode fixedIpNode : fixedIpNodes) {
|
| 285 | FixedIp fixedIp = jsonNodeToFixedIps(fixedIpNode);
|
| 286 | fixedIps.add(fixedIp);
|
| 287 | }
|
| 288 |
|
| 289 | BindingHostId bindingHostId = BindingHostId
|
| 290 | .bindingHostId(vPortNodes.get("binding:host_id").asText());
|
| 291 | String bindingVnicType = vPortNodes.get("binding:vnic_type").asText();
|
| 292 | String bindingVifType = vPortNodes.get("binding:vif_type").asText();
|
| 293 | String bindingVifDetails = vPortNodes.get("binding:vif_details")
|
| 294 | .asText();
|
| 295 | JsonNode allowedAddressPairJsonNode = vPortNodes
|
| 296 | .get("allowed_address_pairs");
|
| 297 | Collection<AllowedAddressPair> allowedAddressPairs =
|
| 298 | jsonNodeToAllowedAddressPair(allowedAddressPairJsonNode);
|
| 299 | JsonNode securityGroupNode = vPortNodes.get("security_groups");
|
| 300 | Collection<SecurityGroup> securityGroups = jsonNodeToSecurityGroup(securityGroupNode);
|
| 301 | strMap.put("name", name);
|
| 302 | strMap.put("deviceOwner", deviceOwner);
|
| 303 | strMap.put("bindingVnicType", bindingVnicType);
|
| 304 | strMap.put("bindingVifType", bindingVifType);
|
| 305 | strMap.put("bindingVifDetails", bindingVifDetails);
|
| 306 | VirtualPort vPort = new DefaultVirtualPort(id, networkId, adminStateUp,
|
| 307 | strMap, isState(state),
|
| 308 | macAddress, tenantId,
|
| 309 | deviceId, fixedIps,
|
| 310 | bindingHostId,
|
| 311 | allowedAddressPairs,
|
| 312 | securityGroups);
|
| 313 | vportMap.put(id, vPort);
|
| 314 |
|
| 315 | return Collections.unmodifiableCollection(vportMap.values());
|
| 316 | }
|
| 317 |
|
| 318 | /**
|
| 319 | * Returns a Object of the currently known infrastructure virtualPort.
|
| 320 | *
|
| 321 | * @param allowedAddressPairs the allowedAddressPairs json node
|
| 322 | * @return a collection of allowedAddressPair
|
| 323 | */
|
| 324 | public Collection<AllowedAddressPair> jsonNodeToAllowedAddressPair(JsonNode allowedAddressPairs) {
|
| 325 | checkNotNull(allowedAddressPairs, JSON_NOT_NULL);
|
| 326 | ConcurrentMap<Integer, AllowedAddressPair> allowMaps = Maps
|
| 327 | .newConcurrentMap();
|
| 328 | int i = 0;
|
| 329 | for (JsonNode node : allowedAddressPairs) {
|
| 330 | IpAddress ip = IpAddress.valueOf(node.get("ip_address").asText());
|
| 331 | MacAddress mac = MacAddress.valueOf(node.get("mac_address")
|
| 332 | .asText());
|
| 333 | AllowedAddressPair allows = AllowedAddressPair
|
| 334 | .allowedAddressPair(ip, mac);
|
| 335 | allowMaps.put(i, allows);
|
| 336 | i++;
|
| 337 | }
|
| 338 | log.debug("The jsonNode of allowedAddressPairallow is {}"
|
| 339 | + allowedAddressPairs.toString());
|
| 340 | return Collections.unmodifiableCollection(allowMaps.values());
|
| 341 | }
|
| 342 |
|
| 343 | /**
|
| 344 | * Returns a collection of virtualPorts.
|
| 345 | *
|
| 346 | * @param securityGroups the virtualPort jsonnode
|
| 347 | * @return a collection of securityGroups
|
| 348 | */
|
| 349 | public Collection<SecurityGroup> jsonNodeToSecurityGroup(JsonNode securityGroups) {
|
| 350 | checkNotNull(securityGroups, JSON_NOT_NULL);
|
| 351 | ConcurrentMap<Integer, SecurityGroup> securMaps = Maps
|
| 352 | .newConcurrentMap();
|
| 353 | int i = 0;
|
| 354 | for (JsonNode node : securityGroups) {
|
| 355 | SecurityGroup securityGroup = SecurityGroup
|
| 356 | .securityGroup(node.asText());
|
| 357 | securMaps.put(i, securityGroup);
|
| 358 | i++;
|
| 359 | }
|
| 360 | return Collections.unmodifiableCollection(securMaps.values());
|
| 361 | }
|
| 362 |
|
| 363 | /**
|
| 364 | * Returns a collection of fixedIps.
|
| 365 | *
|
| 366 | * @param fixedIpNode the fixedIp jsonnode
|
| 367 | * @return a collection of SecurityGroup
|
| 368 | */
|
| 369 | public FixedIp jsonNodeToFixedIps(JsonNode fixedIpNode) {
|
| 370 | SubnetId subnetId = SubnetId.subnetId(fixedIpNode.get("subnet_id")
|
| 371 | .asText());
|
| 372 | IpAddress ipAddress = IpAddress.valueOf(fixedIpNode.get("ip_address")
|
| 373 | .asText());
|
| 374 | FixedIp fixedIps = FixedIp.fixedIp(subnetId, ipAddress);
|
| 375 | return fixedIps;
|
| 376 | }
|
| 377 |
|
| 378 | /**
|
| 379 | * Returns VirtualPort State.
|
| 380 | *
|
| 381 | * @param state the virtualport state
|
| 382 | * @return the virtualPort state
|
| 383 | */
|
| 384 | private State isState(String state) {
|
| 385 | if (state.equals("ACTIVE")) {
|
| 386 | return VirtualPort.State.ACTIVE;
|
| 387 | } else {
|
| 388 | return VirtualPort.State.DOWN;
|
| 389 | }
|
| 390 |
|
| 391 | }
|
| 392 |
|
| 393 | /**
|
| 394 | * Returns the specified item if that items is null; otherwise throws not
|
| 395 | * found exception.
|
| 396 | *
|
| 397 | * @param item item to check
|
| 398 | * @param <T> item type
|
| 399 | * @param message not found message
|
| 400 | * @return item if not null
|
| 401 | * @throws org.onlab.util.ItemNotFoundException if item is null
|
| 402 | */
|
| 403 | protected <T> T nullIsNotFound(T item, String message) {
|
| 404 | if (item == null) {
|
| 405 | throw new ItemNotFoundException(message);
|
| 406 | }
|
| 407 | return item;
|
| 408 | }
|
| 409 | }
|