Claudine Chiu
Committed by Gerrit Code Review

ONOS-4077: REST API's for virtual links.

Change-Id: Idc838f24735e75ad2729393a03dcac4d256239bb
......@@ -28,6 +28,7 @@ import org.onosproject.codec.JsonCodec;
import org.onosproject.core.Application;
import org.onosproject.incubator.net.virtual.TenantId;
import org.onosproject.incubator.net.virtual.VirtualDevice;
import org.onosproject.incubator.net.virtual.VirtualLink;
import org.onosproject.incubator.net.virtual.VirtualNetwork;
import org.onosproject.incubator.net.virtual.VirtualPort;
import org.onosproject.net.Annotations;
......@@ -134,6 +135,7 @@ public class CodecManager implements CodecService {
registerCodec(VirtualNetwork.class, new VirtualNetworkCodec());
registerCodec(VirtualDevice.class, new VirtualDeviceCodec());
registerCodec(VirtualPort.class, new VirtualPortCodec());
registerCodec(VirtualLink.class, new VirtualLinkCodec());
log.info("Started");
}
......
package org.onosproject.codec.impl;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.onosproject.codec.CodecContext;
import org.onosproject.codec.JsonCodec;
import org.onosproject.incubator.net.tunnel.TunnelId;
import org.onosproject.incubator.net.virtual.DefaultVirtualLink;
import org.onosproject.incubator.net.virtual.NetworkId;
import org.onosproject.incubator.net.virtual.VirtualLink;
import org.onosproject.net.Link;
import static com.google.common.base.Preconditions.checkNotNull;
import static org.onlab.util.Tools.nullIsIllegal;
/**
* Codec for the VirtualLink class.
*/
public class VirtualLinkCodec extends JsonCodec<VirtualLink> {
// JSON field names
private static final String NETWORK_ID = "networkId";
private static final String TUNNEL_ID = "tunnelId";
private static final String NULL_OBJECT_MSG = "VirtualLink cannot be null";
private static final String MISSING_MEMBER_MSG = " member is required in VirtualLink";
@Override
public ObjectNode encode(VirtualLink vLink, CodecContext context) {
checkNotNull(vLink, NULL_OBJECT_MSG);
JsonCodec<Link> codec = context.codec(Link.class);
ObjectNode result = codec.encode(vLink, context);
result.put(NETWORK_ID, vLink.networkId().toString());
// TODO check if tunnelId needs to be part of VirtualLink interface.
if (vLink instanceof DefaultVirtualLink) {
result.put(TUNNEL_ID, ((DefaultVirtualLink) vLink).tunnelId().toString());
}
return result;
}
@Override
public VirtualLink decode(ObjectNode json, CodecContext context) {
if (json == null || !json.isObject()) {
return null;
}
JsonCodec<Link> codec = context.codec(Link.class);
Link link = codec.decode(json, context);
NetworkId nId = NetworkId.networkId(Long.parseLong(extractMember(NETWORK_ID, json)));
String tunnelIdStr = json.path(TUNNEL_ID).asText();
TunnelId tunnelId = tunnelIdStr != null ? TunnelId.valueOf(tunnelIdStr)
: TunnelId.valueOf(0);
return new DefaultVirtualLink(nId, link.src(), link.dst(), tunnelId);
}
/**
* Extract member from JSON ObjectNode.
*
* @param key key for which value is needed
* @param json JSON ObjectNode
* @return member value
*/
private String extractMember(String key, ObjectNode json) {
return nullIsIllegal(json.get(key), key + MISSING_MEMBER_MSG).asText();
}
}
......@@ -131,7 +131,7 @@ public class TenantWebResource extends AbstractWebResource {
*
* @param stream TenantId JSON stream
* @return TenantId
* @throws IOException
* @throws IOException if unable to parse the request
*/
private TenantId getTenantIdFromJsonStream(InputStream stream) throws IOException {
ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream);
......@@ -146,11 +146,11 @@ public class TenantWebResource extends AbstractWebResource {
/**
* Get the matching tenant identifier from existing tenant identifiers in system.
*
* @param vnetAdminSvc
* @param vnetAdminSvc virtual network administration service
* @param tidIn tenant identifier
* @return TenantId
*/
private static TenantId getExistingTenantId(VirtualNetworkAdminService vnetAdminSvc,
static TenantId getExistingTenantId(VirtualNetworkAdminService vnetAdminSvc,
TenantId tidIn) {
final TenantId resultTid = vnetAdminSvc
.getTenantIds()
......
{
"type": "object",
"title": "vlink",
"required": [
"networkId",
"src",
"dst",
"type",
"state"
],
"properties": {
"networkId": {
"type": "String",
"example": "Network identifier"
},
"src": {
"type": "object",
"title": "src",
"required": [
"port",
"device"
],
"properties": {
"port": {
"type": "string",
"example": "3"
},
"device": {
"type": "string",
"example": "of:0000000000000002"
}
}
},
"dst": {
"type": "object",
"title": "dst",
"required": [
"port",
"device"
],
"properties": {
"port": {
"type": "string",
"example": "2"
},
"device": {
"type": "string",
"example": "of:0000000000000003"
}
}
},
"type": {
"type": "string",
"example": "DIRECT"
},
"state": {
"type": "string",
"example": "ACTIVE"
},
"tunnelId": {
"type": "int64",
"example": "Tunnel identifier"
}
}
}
{
"networkId": "3",
"src": {
"device": "of:devid2",
"port": "22"
},
"dst": {
"device": "of:devid1",
"port": "21"
},
"type": "VIRTUAL",
"state": "ACTIVE",
"tunnelId": "31"
}