blob: 106a1b09f774e49d173c49ce11e3b346fe056b3e [file] [log] [blame]
Brian Stanke9a108972016-04-11 15:25:17 -04001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Brian Stanke9a108972016-04-11 15:25:17 -04003 *
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
Claudine Chiu1decd532016-04-19 18:30:01 +000045 ObjectNode result = context.mapper().createObjectNode()
46 .put(NETWORK_ID, vLink.networkId().toString());
Claudine Chiuf6bf8d52016-04-08 01:31:54 +000047 JsonCodec<Link> codec = context.codec(Link.class);
Claudine Chiu1decd532016-04-19 18:30:01 +000048 ObjectNode linkResult = codec.encode(vLink, context);
49 result.setAll(linkResult);
Claudine Chiuf6bf8d52016-04-08 01:31:54 +000050 return result;
51 }
52
53 @Override
54 public VirtualLink decode(ObjectNode json, CodecContext context) {
55 if (json == null || !json.isObject()) {
56 return null;
57 }
58 JsonCodec<Link> codec = context.codec(Link.class);
59 Link link = codec.decode(json, context);
60 NetworkId nId = NetworkId.networkId(Long.parseLong(extractMember(NETWORK_ID, json)));
Brian Stanke9a108972016-04-11 15:25:17 -040061 return DefaultVirtualLink.builder()
62 .networkId(nId)
63 .src(link.src())
64 .dst(link.dst())
65 .build();
Claudine Chiuf6bf8d52016-04-08 01:31:54 +000066 }
67
68 /**
69 * Extract member from JSON ObjectNode.
70 *
Brian Stanke9a108972016-04-11 15:25:17 -040071 * @param key key for which value is needed
Claudine Chiuf6bf8d52016-04-08 01:31:54 +000072 * @param json JSON ObjectNode
73 * @return member value
74 */
75 private String extractMember(String key, ObjectNode json) {
76 return nullIsIllegal(json.get(key), key + MISSING_MEMBER_MSG).asText();
77 }
78}