blob: 9f6b83a334ebfe8c39a0ec69a8418e1628bad155 [file] [log] [blame]
Brian Stanke9a108972016-04-11 15:25:17 -04001/*
2 * Copyright 2016-present 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
Claudine Chiuf6bf8d52016-04-08 01:31:54 +000017package org.onosproject.codec.impl;
18
19import com.fasterxml.jackson.databind.node.ObjectNode;
20import org.onosproject.codec.CodecContext;
21import org.onosproject.codec.JsonCodec;
Claudine Chiuf6bf8d52016-04-08 01:31:54 +000022import org.onosproject.incubator.net.virtual.DefaultVirtualLink;
23import org.onosproject.incubator.net.virtual.NetworkId;
24import org.onosproject.incubator.net.virtual.VirtualLink;
25import org.onosproject.net.Link;
26
27import static com.google.common.base.Preconditions.checkNotNull;
28import static org.onlab.util.Tools.nullIsIllegal;
29
30/**
31 * Codec for the VirtualLink class.
32 */
33public class VirtualLinkCodec extends JsonCodec<VirtualLink> {
34
35 // JSON field names
36 private static final String NETWORK_ID = "networkId";
Claudine Chiuf6bf8d52016-04-08 01:31:54 +000037
38 private static final String NULL_OBJECT_MSG = "VirtualLink cannot be null";
39 private static final String MISSING_MEMBER_MSG = " member is required in VirtualLink";
40
41 @Override
42 public ObjectNode encode(VirtualLink vLink, CodecContext context) {
43 checkNotNull(vLink, NULL_OBJECT_MSG);
44
45 JsonCodec<Link> codec = context.codec(Link.class);
46 ObjectNode result = codec.encode(vLink, context);
47 result.put(NETWORK_ID, vLink.networkId().toString());
Claudine Chiuf6bf8d52016-04-08 01:31:54 +000048 return result;
49 }
50
51 @Override
52 public VirtualLink decode(ObjectNode json, CodecContext context) {
53 if (json == null || !json.isObject()) {
54 return null;
55 }
56 JsonCodec<Link> codec = context.codec(Link.class);
57 Link link = codec.decode(json, context);
58 NetworkId nId = NetworkId.networkId(Long.parseLong(extractMember(NETWORK_ID, json)));
Brian Stanke9a108972016-04-11 15:25:17 -040059 return DefaultVirtualLink.builder()
60 .networkId(nId)
61 .src(link.src())
62 .dst(link.dst())
63 .build();
Claudine Chiuf6bf8d52016-04-08 01:31:54 +000064 }
65
66 /**
67 * Extract member from JSON ObjectNode.
68 *
Brian Stanke9a108972016-04-11 15:25:17 -040069 * @param key key for which value is needed
Claudine Chiuf6bf8d52016-04-08 01:31:54 +000070 * @param json JSON ObjectNode
71 * @return member value
72 */
73 private String extractMember(String key, ObjectNode json) {
74 return nullIsIllegal(json.get(key), key + MISSING_MEMBER_MSG).asText();
75 }
76}