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 | |
| 20 | import org.onlab.packet.ChassisId; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 21 | import org.onosproject.codec.CodecContext; |
HIGUCHI Yuta | 5bb99a4 | 2015-03-19 16:52:15 -0700 | [diff] [blame] | 22 | import org.onosproject.net.Annotations; |
| 23 | import org.onosproject.net.DefaultAnnotations; |
Thomas Vachuska | 3afbc7f | 2016-02-01 15:55:38 -0800 | [diff] [blame] | 24 | import org.onosproject.net.DefaultDevice; |
HIGUCHI Yuta | 5bb99a4 | 2015-03-19 16:52:15 -0700 | [diff] [blame] | 25 | import org.onosproject.net.DefaultPort; |
| 26 | import org.onosproject.net.Device; |
| 27 | import org.onosproject.net.DeviceId; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 28 | import org.onosproject.net.Port; |
HIGUCHI Yuta | 5bb99a4 | 2015-03-19 16:52:15 -0700 | [diff] [blame] | 29 | import org.onosproject.net.Port.Type; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 30 | import org.onosproject.net.PortNumber; |
HIGUCHI Yuta | 5bb99a4 | 2015-03-19 16:52:15 -0700 | [diff] [blame] | 31 | import org.onosproject.net.provider.ProviderId; |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 32 | |
| 33 | import static com.google.common.base.Preconditions.checkNotNull; |
| 34 | |
| 35 | /** |
| 36 | * Device port JSON codec. |
| 37 | */ |
Ray Milkey | 540b2ce | 2015-02-04 17:50:20 -0800 | [diff] [blame] | 38 | public final class PortCodec extends AnnotatedCodec<Port> { |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 39 | |
HIGUCHI Yuta | 5bb99a4 | 2015-03-19 16:52:15 -0700 | [diff] [blame] | 40 | // JSON field names |
| 41 | private static final String ELEMENT = "element"; // DeviceId |
| 42 | private static final String PORT_NAME = "port"; |
| 43 | private static final String IS_ENABLED = "isEnabled"; |
| 44 | private static final String TYPE = "type"; |
| 45 | private static final String PORT_SPEED = "portSpeed"; |
| 46 | |
| 47 | // Special port name alias |
| 48 | private static final String PORT_NAME_LOCAL = "local"; |
| 49 | |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 50 | @Override |
| 51 | public ObjectNode encode(Port port, CodecContext context) { |
| 52 | checkNotNull(port, "Port cannot be null"); |
| 53 | ObjectNode result = context.mapper().createObjectNode() |
HIGUCHI Yuta | 5bb99a4 | 2015-03-19 16:52:15 -0700 | [diff] [blame] | 54 | .put(ELEMENT, port.element().id().toString()) |
| 55 | .put(PORT_NAME, portName(port.number())) |
| 56 | .put(IS_ENABLED, port.isEnabled()) |
| 57 | .put(TYPE, port.type().toString().toLowerCase()) |
| 58 | .put(PORT_SPEED, port.portSpeed()); |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 59 | return annotate(result, port, context); |
| 60 | } |
| 61 | |
| 62 | private String portName(PortNumber port) { |
HIGUCHI Yuta | 5bb99a4 | 2015-03-19 16:52:15 -0700 | [diff] [blame] | 63 | return port.equals(PortNumber.LOCAL) ? PORT_NAME_LOCAL : port.toString(); |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 64 | } |
| 65 | |
HIGUCHI Yuta | 5bb99a4 | 2015-03-19 16:52:15 -0700 | [diff] [blame] | 66 | private static PortNumber portNumber(String portName) { |
| 67 | if (portName.equalsIgnoreCase(PORT_NAME_LOCAL)) { |
| 68 | return PortNumber.LOCAL; |
| 69 | } |
| 70 | |
| 71 | return PortNumber.portNumber(portName); |
| 72 | } |
| 73 | |
| 74 | |
| 75 | /** |
| 76 | * {@inheritDoc} |
| 77 | * |
| 78 | * Note: Result of {@link Port#element()} returned Port object, |
| 79 | * is not a full Device object. |
| 80 | * Only it's DeviceId can be used. |
| 81 | */ |
| 82 | @Override |
| 83 | public Port decode(ObjectNode json, CodecContext context) { |
| 84 | if (json == null || !json.isObject()) { |
| 85 | return null; |
| 86 | } |
| 87 | |
| 88 | DeviceId did = DeviceId.deviceId(json.get(ELEMENT).asText()); |
| 89 | Device device = new DummyDevice(did); |
| 90 | PortNumber number = portNumber(json.get(PORT_NAME).asText()); |
| 91 | boolean isEnabled = json.get(IS_ENABLED).asBoolean(); |
| 92 | Type type = Type.valueOf(json.get(TYPE).asText().toUpperCase()); |
| 93 | long portSpeed = json.get(PORT_SPEED).asLong(); |
| 94 | Annotations annotations = extractAnnotations(json, context); |
| 95 | |
| 96 | return new DefaultPort(device, number, isEnabled, type, portSpeed, annotations); |
| 97 | } |
| 98 | |
| 99 | |
| 100 | /** |
| 101 | * Dummy Device which only holds DeviceId. |
| 102 | */ |
Thomas Vachuska | 3afbc7f | 2016-02-01 15:55:38 -0800 | [diff] [blame] | 103 | private static final class DummyDevice extends DefaultDevice { |
HIGUCHI Yuta | 5bb99a4 | 2015-03-19 16:52:15 -0700 | [diff] [blame] | 104 | /** |
| 105 | * Constructs Dummy Device which only holds DeviceId. |
| 106 | * |
| 107 | * @param did device Id |
| 108 | */ |
| 109 | public DummyDevice(DeviceId did) { |
Thomas Vachuska | 3afbc7f | 2016-02-01 15:55:38 -0800 | [diff] [blame] | 110 | super(new ProviderId(did.uri().getScheme(), "PortCodec"), did, |
| 111 | Type.SWITCH, "dummy", "0", "0", "0", new ChassisId(), |
| 112 | DefaultAnnotations.EMPTY); |
HIGUCHI Yuta | 5bb99a4 | 2015-03-19 16:52:15 -0700 | [diff] [blame] | 113 | } |
| 114 | } |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 115 | } |