Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present 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; |
| 22 | import org.onosproject.net.ConnectPoint; |
Ray Milkey | 19ffea3 | 2015-01-28 10:03:06 -0800 | [diff] [blame] | 23 | import org.onosproject.net.DeviceId; |
HIGUCHI Yuta | 5bb99a4 | 2015-03-19 16:52:15 -0700 | [diff] [blame] | 24 | import org.onosproject.net.ElementId; |
Ray Milkey | 19ffea3 | 2015-01-28 10:03:06 -0800 | [diff] [blame] | 25 | import org.onosproject.net.HostId; |
HIGUCHI Yuta | 5bb99a4 | 2015-03-19 16:52:15 -0700 | [diff] [blame] | 26 | import org.onosproject.net.PortNumber; |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 27 | |
| 28 | import static com.google.common.base.Preconditions.checkNotNull; |
HIGUCHI Yuta | 5bb99a4 | 2015-03-19 16:52:15 -0700 | [diff] [blame] | 29 | import static org.onosproject.net.PortNumber.portNumber; |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 30 | |
| 31 | /** |
| 32 | * Connection point JSON codec. |
| 33 | */ |
Ray Milkey | 540b2ce | 2015-02-04 17:50:20 -0800 | [diff] [blame] | 34 | public final class ConnectPointCodec extends JsonCodec<ConnectPoint> { |
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 ELEMENT_HOST = "host"; |
| 38 | private static final String ELEMENT_DEVICE = "device"; |
| 39 | private static final String PORT = "port"; |
| 40 | |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 41 | @Override |
| 42 | public ObjectNode encode(ConnectPoint point, CodecContext context) { |
| 43 | checkNotNull(point, "Connect point cannot be null"); |
Ray Milkey | 19ffea3 | 2015-01-28 10:03:06 -0800 | [diff] [blame] | 44 | ObjectNode root = context.mapper().createObjectNode() |
HIGUCHI Yuta | 5bb99a4 | 2015-03-19 16:52:15 -0700 | [diff] [blame] | 45 | .put(PORT, point.port().toString()); |
Ray Milkey | 19ffea3 | 2015-01-28 10:03:06 -0800 | [diff] [blame] | 46 | |
| 47 | if (point.elementId() instanceof DeviceId) { |
HIGUCHI Yuta | 5bb99a4 | 2015-03-19 16:52:15 -0700 | [diff] [blame] | 48 | root.put(ELEMENT_DEVICE, point.deviceId().toString()); |
Ray Milkey | 19ffea3 | 2015-01-28 10:03:06 -0800 | [diff] [blame] | 49 | } else if (point.elementId() instanceof HostId) { |
HIGUCHI Yuta | 5bb99a4 | 2015-03-19 16:52:15 -0700 | [diff] [blame] | 50 | root.put(ELEMENT_HOST, point.hostId().toString()); |
Ray Milkey | 19ffea3 | 2015-01-28 10:03:06 -0800 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | return root; |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 54 | } |
| 55 | |
HIGUCHI Yuta | 5bb99a4 | 2015-03-19 16:52:15 -0700 | [diff] [blame] | 56 | @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 Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 74 | } |