blob: c6f2ac76554ae417c520217c75ee488cdac899fe [file] [log] [blame]
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -08001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 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;
24import org.onosproject.net.DefaultPort;
25import org.onosproject.net.Device;
26import org.onosproject.net.DeviceId;
Brian O'Connorabafb502014-12-02 22:26:20 -080027import org.onosproject.net.Port;
HIGUCHI Yuta5bb99a42015-03-19 16:52:15 -070028import org.onosproject.net.Port.Type;
Brian O'Connorabafb502014-12-02 22:26:20 -080029import org.onosproject.net.PortNumber;
HIGUCHI Yuta5bb99a42015-03-19 16:52:15 -070030import org.onosproject.net.provider.ProviderId;
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080031
32import static com.google.common.base.Preconditions.checkNotNull;
33
34/**
35 * Device port JSON codec.
36 */
Ray Milkey540b2ce2015-02-04 17:50:20 -080037public final class PortCodec extends AnnotatedCodec<Port> {
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080038
HIGUCHI Yuta5bb99a42015-03-19 16:52:15 -070039 // 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 Vachuskaca60f2b2014-11-06 01:34:28 -080049 @Override
50 public ObjectNode encode(Port port, CodecContext context) {
51 checkNotNull(port, "Port cannot be null");
52 ObjectNode result = context.mapper().createObjectNode()
HIGUCHI Yuta5bb99a42015-03-19 16:52:15 -070053 .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 Vachuskaca60f2b2014-11-06 01:34:28 -080058 return annotate(result, port, context);
59 }
60
61 private String portName(PortNumber port) {
HIGUCHI Yuta5bb99a42015-03-19 16:52:15 -070062 return port.equals(PortNumber.LOCAL) ? PORT_NAME_LOCAL : port.toString();
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080063 }
64
HIGUCHI Yuta5bb99a42015-03-19 16:52:15 -070065 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 Vachuskaca60f2b2014-11-06 01:34:28 -0800160}