Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 1 | /* |
Ray Milkey | 34c9590 | 2015-04-15 09:47:53 -0700 | [diff] [blame] | 2 | * Copyright 2014-2015 Open Networking Laboratory |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 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 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.codec.impl; |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 17 | |
| 18 | import com.fasterxml.jackson.databind.node.ObjectNode; |
HIGUCHI Yuta | 5bb99a4 | 2015-03-19 16:52:15 -0700 | [diff] [blame] | 19 | |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 20 | import org.onosproject.codec.CodecContext; |
| 21 | import org.onosproject.codec.JsonCodec; |
HIGUCHI Yuta | 5bb99a4 | 2015-03-19 16:52:15 -0700 | [diff] [blame] | 22 | import org.onosproject.net.Annotations; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 23 | import org.onosproject.net.ConnectPoint; |
HIGUCHI Yuta | 5bb99a4 | 2015-03-19 16:52:15 -0700 | [diff] [blame] | 24 | import org.onosproject.net.DefaultLink; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 25 | import org.onosproject.net.Link; |
HIGUCHI Yuta | 5bb99a4 | 2015-03-19 16:52:15 -0700 | [diff] [blame] | 26 | import org.onosproject.net.Link.Type; |
| 27 | import org.onosproject.net.provider.ProviderId; |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 28 | |
| 29 | import static com.google.common.base.Preconditions.checkNotNull; |
| 30 | |
| 31 | /** |
| 32 | * Link JSON codec. |
| 33 | */ |
Ray Milkey | 540b2ce | 2015-02-04 17:50:20 -0800 | [diff] [blame] | 34 | public final class LinkCodec extends AnnotatedCodec<Link> { |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 35 | |
HIGUCHI Yuta | 5bb99a4 | 2015-03-19 16:52:15 -0700 | [diff] [blame] | 36 | // 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 Milkey | 3078fc0 | 2015-05-06 16:14:14 -0700 | [diff] [blame] | 40 | private static final String STATE = "state"; |
HIGUCHI Yuta | 5bb99a4 | 2015-03-19 16:52:15 -0700 | [diff] [blame] | 41 | |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 42 | @Override |
| 43 | public ObjectNode encode(Link link, CodecContext context) { |
| 44 | checkNotNull(link, "Link cannot be null"); |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 45 | JsonCodec<ConnectPoint> codec = context.codec(ConnectPoint.class); |
| 46 | ObjectNode result = context.mapper().createObjectNode(); |
HIGUCHI Yuta | 5bb99a4 | 2015-03-19 16:52:15 -0700 | [diff] [blame] | 47 | 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 Milkey | 3078fc0 | 2015-05-06 16:14:14 -0700 | [diff] [blame] | 50 | if (link.state() != null) { |
| 51 | result.put(STATE, link.state().toString()); |
| 52 | } |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 53 | return annotate(result, link, context); |
| 54 | } |
| 55 | |
HIGUCHI Yuta | 5bb99a4 | 2015-03-19 16:52:15 -0700 | [diff] [blame] | 56 | |
| 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 | |
Ray Milkey | b82c42b | 2015-06-30 09:42:20 -0700 | [diff] [blame] | 73 | ConnectPoint src = codec.decode(get(json, SRC), context); |
| 74 | ConnectPoint dst = codec.decode(get(json, DST), context); |
HIGUCHI Yuta | 5bb99a4 | 2015-03-19 16:52:15 -0700 | [diff] [blame] | 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 Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 80 | } |