blob: 491d751e16726177cce500540d932746bd7259d8 [file] [log] [blame]
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -08001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 Open Networking Laboratory
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -08003 *
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 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.codec.impl;
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080017
18import com.fasterxml.jackson.databind.node.ObjectNode;
HIGUCHI Yuta5bb99a42015-03-19 16:52:15 -070019
Brian O'Connorabafb502014-12-02 22:26:20 -080020import org.onosproject.codec.CodecContext;
21import org.onosproject.codec.JsonCodec;
HIGUCHI Yuta5bb99a42015-03-19 16:52:15 -070022import org.onosproject.net.Annotations;
Brian O'Connorabafb502014-12-02 22:26:20 -080023import org.onosproject.net.ConnectPoint;
HIGUCHI Yuta5bb99a42015-03-19 16:52:15 -070024import org.onosproject.net.DefaultLink;
Brian O'Connorabafb502014-12-02 22:26:20 -080025import org.onosproject.net.Link;
HIGUCHI Yuta5bb99a42015-03-19 16:52:15 -070026import org.onosproject.net.Link.Type;
27import org.onosproject.net.provider.ProviderId;
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080028
29import static com.google.common.base.Preconditions.checkNotNull;
30
31/**
32 * Link JSON codec.
33 */
Ray Milkey540b2ce2015-02-04 17:50:20 -080034public final class LinkCodec extends AnnotatedCodec<Link> {
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080035
HIGUCHI Yuta5bb99a42015-03-19 16:52:15 -070036 // JSON field names
37 private static final String SRC = "src";
38 private static final String DST = "dst";
39 private static final String TYPE = "type";
Ray Milkey3078fc02015-05-06 16:14:14 -070040 private static final String STATE = "state";
HIGUCHI Yuta5bb99a42015-03-19 16:52:15 -070041
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080042 @Override
43 public ObjectNode encode(Link link, CodecContext context) {
44 checkNotNull(link, "Link cannot be null");
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080045 JsonCodec<ConnectPoint> codec = context.codec(ConnectPoint.class);
46 ObjectNode result = context.mapper().createObjectNode();
HIGUCHI Yuta5bb99a42015-03-19 16:52:15 -070047 result.set(SRC, codec.encode(link.src(), context));
48 result.set(DST, codec.encode(link.dst(), context));
49 result.put(TYPE, link.type().toString());
Ray Milkey3078fc02015-05-06 16:14:14 -070050 if (link.state() != null) {
51 result.put(STATE, link.state().toString());
52 }
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080053 return annotate(result, link, context);
54 }
55
HIGUCHI Yuta5bb99a42015-03-19 16:52:15 -070056
57 /**
58 * {@inheritDoc}
59 *
60 * Note: ProviderId is not part of JSON representation.
61 * Returned object will have random ProviderId set.
62 */
63 @Override
64 public Link decode(ObjectNode json, CodecContext context) {
65 if (json == null || !json.isObject()) {
66 return null;
67 }
68
69 JsonCodec<ConnectPoint> codec = context.codec(ConnectPoint.class);
70 // TODO: add providerId to JSON if we need to recover them.
71 ProviderId pid = new ProviderId("json", "LinkCodec");
72
73 ConnectPoint src = codec.decode((ObjectNode) json.get(SRC), context);
74 ConnectPoint dst = codec.decode((ObjectNode) json.get(DST), context);
75 Type type = Type.valueOf(json.get(TYPE).asText());
76 Annotations annotations = extractAnnotations(json, context);
77
78 return new DefaultLink(pid, src, dst, type, annotations);
79 }
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080080}