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