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; ...@@ -28,6 +28,7 @@ import org.onosproject.codec.JsonCodec;
28 import org.onosproject.core.Application; 28 import org.onosproject.core.Application;
29 import org.onosproject.incubator.net.virtual.TenantId; 29 import org.onosproject.incubator.net.virtual.TenantId;
30 import org.onosproject.incubator.net.virtual.VirtualDevice; 30 import org.onosproject.incubator.net.virtual.VirtualDevice;
31 +import org.onosproject.incubator.net.virtual.VirtualLink;
31 import org.onosproject.incubator.net.virtual.VirtualNetwork; 32 import org.onosproject.incubator.net.virtual.VirtualNetwork;
32 import org.onosproject.incubator.net.virtual.VirtualPort; 33 import org.onosproject.incubator.net.virtual.VirtualPort;
33 import org.onosproject.net.Annotations; 34 import org.onosproject.net.Annotations;
...@@ -134,6 +135,7 @@ public class CodecManager implements CodecService { ...@@ -134,6 +135,7 @@ public class CodecManager implements CodecService {
134 registerCodec(VirtualNetwork.class, new VirtualNetworkCodec()); 135 registerCodec(VirtualNetwork.class, new VirtualNetworkCodec());
135 registerCodec(VirtualDevice.class, new VirtualDeviceCodec()); 136 registerCodec(VirtualDevice.class, new VirtualDeviceCodec());
136 registerCodec(VirtualPort.class, new VirtualPortCodec()); 137 registerCodec(VirtualPort.class, new VirtualPortCodec());
138 + registerCodec(VirtualLink.class, new VirtualLinkCodec());
137 log.info("Started"); 139 log.info("Started");
138 } 140 }
139 141
......
1 +package org.onosproject.codec.impl;
2 +
3 +import com.fasterxml.jackson.databind.node.ObjectNode;
4 +import org.onosproject.codec.CodecContext;
5 +import org.onosproject.codec.JsonCodec;
6 +import org.onosproject.incubator.net.tunnel.TunnelId;
7 +import org.onosproject.incubator.net.virtual.DefaultVirtualLink;
8 +import org.onosproject.incubator.net.virtual.NetworkId;
9 +import org.onosproject.incubator.net.virtual.VirtualLink;
10 +import org.onosproject.net.Link;
11 +
12 +import static com.google.common.base.Preconditions.checkNotNull;
13 +import static org.onlab.util.Tools.nullIsIllegal;
14 +
15 +/**
16 + * Codec for the VirtualLink class.
17 + */
18 +public class VirtualLinkCodec extends JsonCodec<VirtualLink> {
19 +
20 + // JSON field names
21 + private static final String NETWORK_ID = "networkId";
22 + private static final String TUNNEL_ID = "tunnelId";
23 +
24 + private static final String NULL_OBJECT_MSG = "VirtualLink cannot be null";
25 + private static final String MISSING_MEMBER_MSG = " member is required in VirtualLink";
26 +
27 + @Override
28 + public ObjectNode encode(VirtualLink vLink, CodecContext context) {
29 + checkNotNull(vLink, NULL_OBJECT_MSG);
30 +
31 + JsonCodec<Link> codec = context.codec(Link.class);
32 + ObjectNode result = codec.encode(vLink, context);
33 + result.put(NETWORK_ID, vLink.networkId().toString());
34 + // TODO check if tunnelId needs to be part of VirtualLink interface.
35 + if (vLink instanceof DefaultVirtualLink) {
36 + result.put(TUNNEL_ID, ((DefaultVirtualLink) vLink).tunnelId().toString());
37 + }
38 + return result;
39 + }
40 +
41 + @Override
42 + public VirtualLink decode(ObjectNode json, CodecContext context) {
43 + if (json == null || !json.isObject()) {
44 + return null;
45 + }
46 + JsonCodec<Link> codec = context.codec(Link.class);
47 + Link link = codec.decode(json, context);
48 + NetworkId nId = NetworkId.networkId(Long.parseLong(extractMember(NETWORK_ID, json)));
49 + String tunnelIdStr = json.path(TUNNEL_ID).asText();
50 + TunnelId tunnelId = tunnelIdStr != null ? TunnelId.valueOf(tunnelIdStr)
51 + : TunnelId.valueOf(0);
52 + return new DefaultVirtualLink(nId, link.src(), link.dst(), tunnelId);
53 + }
54 +
55 + /**
56 + * Extract member from JSON ObjectNode.
57 + *
58 + * @param key key for which value is needed
59 + * @param json JSON ObjectNode
60 + * @return member value
61 + */
62 + private String extractMember(String key, ObjectNode json) {
63 + return nullIsIllegal(json.get(key), key + MISSING_MEMBER_MSG).asText();
64 + }
65 +}
...@@ -131,7 +131,7 @@ public class TenantWebResource extends AbstractWebResource { ...@@ -131,7 +131,7 @@ public class TenantWebResource extends AbstractWebResource {
131 * 131 *
132 * @param stream TenantId JSON stream 132 * @param stream TenantId JSON stream
133 * @return TenantId 133 * @return TenantId
134 - * @throws IOException 134 + * @throws IOException if unable to parse the request
135 */ 135 */
136 private TenantId getTenantIdFromJsonStream(InputStream stream) throws IOException { 136 private TenantId getTenantIdFromJsonStream(InputStream stream) throws IOException {
137 ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream); 137 ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream);
...@@ -146,11 +146,11 @@ public class TenantWebResource extends AbstractWebResource { ...@@ -146,11 +146,11 @@ public class TenantWebResource extends AbstractWebResource {
146 /** 146 /**
147 * Get the matching tenant identifier from existing tenant identifiers in system. 147 * Get the matching tenant identifier from existing tenant identifiers in system.
148 * 148 *
149 - * @param vnetAdminSvc 149 + * @param vnetAdminSvc virtual network administration service
150 * @param tidIn tenant identifier 150 * @param tidIn tenant identifier
151 * @return TenantId 151 * @return TenantId
152 */ 152 */
153 - private static TenantId getExistingTenantId(VirtualNetworkAdminService vnetAdminSvc, 153 + static TenantId getExistingTenantId(VirtualNetworkAdminService vnetAdminSvc,
154 TenantId tidIn) { 154 TenantId tidIn) {
155 final TenantId resultTid = vnetAdminSvc 155 final TenantId resultTid = vnetAdminSvc
156 .getTenantIds() 156 .getTenantIds()
......
1 +{
2 + "type": "object",
3 + "title": "vlink",
4 + "required": [
5 + "networkId",
6 + "src",
7 + "dst",
8 + "type",
9 + "state"
10 + ],
11 + "properties": {
12 + "networkId": {
13 + "type": "String",
14 + "example": "Network identifier"
15 + },
16 + "src": {
17 + "type": "object",
18 + "title": "src",
19 + "required": [
20 + "port",
21 + "device"
22 + ],
23 + "properties": {
24 + "port": {
25 + "type": "string",
26 + "example": "3"
27 + },
28 + "device": {
29 + "type": "string",
30 + "example": "of:0000000000000002"
31 + }
32 + }
33 + },
34 + "dst": {
35 + "type": "object",
36 + "title": "dst",
37 + "required": [
38 + "port",
39 + "device"
40 + ],
41 + "properties": {
42 + "port": {
43 + "type": "string",
44 + "example": "2"
45 + },
46 + "device": {
47 + "type": "string",
48 + "example": "of:0000000000000003"
49 + }
50 + }
51 + },
52 + "type": {
53 + "type": "string",
54 + "example": "DIRECT"
55 + },
56 + "state": {
57 + "type": "string",
58 + "example": "ACTIVE"
59 + },
60 + "tunnelId": {
61 + "type": "int64",
62 + "example": "Tunnel identifier"
63 + }
64 + }
65 +}
1 +{
2 + "networkId": "3",
3 + "src": {
4 + "device": "of:devid2",
5 + "port": "22"
6 + },
7 + "dst": {
8 + "device": "of:devid1",
9 + "port": "21"
10 + },
11 + "type": "VIRTUAL",
12 + "state": "ACTIVE",
13 + "tunnelId": "31"
14 +}