blob: 61f7d83ba00b2e62fac189f1d6e8ee92eb5167d1 [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
20import org.onlab.packet.ChassisId;
Brian O'Connorabafb502014-12-02 22:26:20 -080021import org.onosproject.codec.CodecContext;
HIGUCHI Yuta5bb99a42015-03-19 16:52:15 -070022import org.onosproject.net.Annotations;
23import org.onosproject.net.DefaultAnnotations;
Thomas Vachuska3afbc7f2016-02-01 15:55:38 -080024import org.onosproject.net.DefaultDevice;
HIGUCHI Yuta5bb99a42015-03-19 16:52:15 -070025import org.onosproject.net.DefaultPort;
26import org.onosproject.net.Device;
27import org.onosproject.net.DeviceId;
Brian O'Connorabafb502014-12-02 22:26:20 -080028import org.onosproject.net.Port;
HIGUCHI Yuta5bb99a42015-03-19 16:52:15 -070029import org.onosproject.net.Port.Type;
Brian O'Connorabafb502014-12-02 22:26:20 -080030import org.onosproject.net.PortNumber;
HIGUCHI Yuta5bb99a42015-03-19 16:52:15 -070031import org.onosproject.net.provider.ProviderId;
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080032
33import static com.google.common.base.Preconditions.checkNotNull;
34
35/**
36 * Device port JSON codec.
37 */
Ray Milkey540b2ce2015-02-04 17:50:20 -080038public final class PortCodec extends AnnotatedCodec<Port> {
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080039
HIGUCHI Yuta5bb99a42015-03-19 16:52:15 -070040 // 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 Vachuskaca60f2b2014-11-06 01:34:28 -080050 @Override
51 public ObjectNode encode(Port port, CodecContext context) {
52 checkNotNull(port, "Port cannot be null");
53 ObjectNode result = context.mapper().createObjectNode()
HIGUCHI Yuta5bb99a42015-03-19 16:52:15 -070054 .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 Vachuskaca60f2b2014-11-06 01:34:28 -080059 return annotate(result, port, context);
60 }
61
62 private String portName(PortNumber port) {
HIGUCHI Yuta5bb99a42015-03-19 16:52:15 -070063 return port.equals(PortNumber.LOCAL) ? PORT_NAME_LOCAL : port.toString();
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080064 }
65
HIGUCHI Yuta5bb99a42015-03-19 16:52:15 -070066 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 Vachuska3afbc7f2016-02-01 15:55:38 -0800103 private static final class DummyDevice extends DefaultDevice {
HIGUCHI Yuta5bb99a42015-03-19 16:52:15 -0700104 /**
105 * Constructs Dummy Device which only holds DeviceId.
106 *
107 * @param did device Id
108 */
109 public DummyDevice(DeviceId did) {
Thomas Vachuska3afbc7f2016-02-01 15:55:38 -0800110 super(new ProviderId(did.uri().getScheme(), "PortCodec"), did,
111 Type.SWITCH, "dummy", "0", "0", "0", new ChassisId(),
112 DefaultAnnotations.EMPTY);
HIGUCHI Yuta5bb99a42015-03-19 16:52:15 -0700113 }
114 }
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -0800115}