blob: eb422323d0cf59f0eaae98eaa42156e9c8206b46 [file] [log] [blame]
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present 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;
22import org.onosproject.net.ConnectPoint;
Ray Milkey19ffea32015-01-28 10:03:06 -080023import org.onosproject.net.DeviceId;
HIGUCHI Yuta5bb99a42015-03-19 16:52:15 -070024import org.onosproject.net.ElementId;
Ray Milkey19ffea32015-01-28 10:03:06 -080025import org.onosproject.net.HostId;
HIGUCHI Yuta5bb99a42015-03-19 16:52:15 -070026import org.onosproject.net.PortNumber;
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080027
28import static com.google.common.base.Preconditions.checkNotNull;
HIGUCHI Yuta5bb99a42015-03-19 16:52:15 -070029import static org.onosproject.net.PortNumber.portNumber;
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080030
31/**
32 * Connection point JSON codec.
33 */
Ray Milkey540b2ce2015-02-04 17:50:20 -080034public final class ConnectPointCodec extends JsonCodec<ConnectPoint> {
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080035
HIGUCHI Yuta5bb99a42015-03-19 16:52:15 -070036 // JSON field names
37 private static final String ELEMENT_HOST = "host";
38 private static final String ELEMENT_DEVICE = "device";
39 private static final String PORT = "port";
40
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080041 @Override
42 public ObjectNode encode(ConnectPoint point, CodecContext context) {
43 checkNotNull(point, "Connect point cannot be null");
Ray Milkey19ffea32015-01-28 10:03:06 -080044 ObjectNode root = context.mapper().createObjectNode()
HIGUCHI Yuta5bb99a42015-03-19 16:52:15 -070045 .put(PORT, point.port().toString());
Ray Milkey19ffea32015-01-28 10:03:06 -080046
47 if (point.elementId() instanceof DeviceId) {
HIGUCHI Yuta5bb99a42015-03-19 16:52:15 -070048 root.put(ELEMENT_DEVICE, point.deviceId().toString());
Ray Milkey19ffea32015-01-28 10:03:06 -080049 } else if (point.elementId() instanceof HostId) {
HIGUCHI Yuta5bb99a42015-03-19 16:52:15 -070050 root.put(ELEMENT_HOST, point.hostId().toString());
Ray Milkey19ffea32015-01-28 10:03:06 -080051 }
52
53 return root;
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080054 }
55
HIGUCHI Yuta5bb99a42015-03-19 16:52:15 -070056 @Override
57 public ConnectPoint decode(ObjectNode json, CodecContext context) {
58 if (json == null || !json.isObject()) {
59 return null;
60 }
61
62 ElementId elementId;
63 if (json.has(ELEMENT_DEVICE)) {
64 elementId = DeviceId.deviceId(json.get(ELEMENT_DEVICE).asText());
65 } else if (json.has(ELEMENT_HOST)) {
66 elementId = HostId.hostId(json.get(ELEMENT_HOST).asText());
67 } else {
68 // invalid JSON
69 return null;
70 }
71 PortNumber portNumber = portNumber(json.get(PORT).asText());
72 return new ConnectPoint(elementId, portNumber);
73 }
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080074}