blob: 029549e1663b3a416f6de80a62e86b733c12c9ec [file] [log] [blame]
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
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.DefaultDevice;
Brian O'Connorabafb502014-12-02 22:26:20 -080024import org.onosproject.net.Device;
HIGUCHI Yuta5bb99a42015-03-19 16:52:15 -070025import org.onosproject.net.Device.Type;
26import org.onosproject.net.DeviceId;
Brian O'Connorabafb502014-12-02 22:26:20 -080027import org.onosproject.net.device.DeviceService;
kalagesa42019542017-03-14 18:00:47 +053028import org.onosproject.net.driver.DriverService;
HIGUCHI Yuta5bb99a42015-03-19 16:52:15 -070029import org.onosproject.net.provider.ProviderId;
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080030
31import static com.google.common.base.Preconditions.checkNotNull;
HIGUCHI Yuta5bb99a42015-03-19 16:52:15 -070032import static org.onosproject.net.DeviceId.deviceId;
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080033
34/**
35 * Device JSON codec.
36 */
Ray Milkey540b2ce2015-02-04 17:50:20 -080037public final class DeviceCodec extends AnnotatedCodec<Device> {
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080038
HIGUCHI Yuta5bb99a42015-03-19 16:52:15 -070039 // JSON fieldNames
40 private static final String ID = "id";
41 private static final String TYPE = "type";
kalagesa42019542017-03-14 18:00:47 +053042 private static final String AVAILABLE = "available";
43 private static final String ROLE = "role";
HIGUCHI Yuta5bb99a42015-03-19 16:52:15 -070044 private static final String MFR = "mfr";
45 private static final String HW = "hw";
46 private static final String SW = "sw";
47 private static final String SERIAL = "serial";
48 private static final String CHASSIS_ID = "chassisId";
kalagesa42019542017-03-14 18:00:47 +053049 private static final String DRIVER = "driver";
HIGUCHI Yuta5bb99a42015-03-19 16:52:15 -070050
51
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080052 @Override
53 public ObjectNode encode(Device device, CodecContext context) {
54 checkNotNull(device, "Device cannot be null");
Ray Milkey3078fc02015-05-06 16:14:14 -070055 DeviceService service = context.getService(DeviceService.class);
kalagesa42019542017-03-14 18:00:47 +053056 DriverService driveService = context.getService(DriverService.class);
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080057 ObjectNode result = context.mapper().createObjectNode()
HIGUCHI Yuta5bb99a42015-03-19 16:52:15 -070058 .put(ID, device.id().toString())
59 .put(TYPE, device.type().name())
kalagesa42019542017-03-14 18:00:47 +053060 .put(AVAILABLE, service.isAvailable(device.id()))
61 .put(ROLE, service.getRole(device.id()).toString())
HIGUCHI Yuta5bb99a42015-03-19 16:52:15 -070062 .put(MFR, device.manufacturer())
63 .put(HW, device.hwVersion())
64 .put(SW, device.swVersion())
65 .put(SERIAL, device.serialNumber())
kalagesa42019542017-03-14 18:00:47 +053066 .put(DRIVER, driveService.getDriver(device.id()).name())
HIGUCHI Yuta5bb99a42015-03-19 16:52:15 -070067 .put(CHASSIS_ID, device.chassisId().toString());
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080068 return annotate(result, device, context);
69 }
70
HIGUCHI Yuta5bb99a42015-03-19 16:52:15 -070071
72 /**
73 * {@inheritDoc}
74 *
75 * Note: ProviderId is not part of JSON representation.
76 * Returned object will have random ProviderId set.
77 */
78 @Override
79 public Device decode(ObjectNode json, CodecContext context) {
80 if (json == null || !json.isObject()) {
81 return null;
82 }
83
84 DeviceId id = deviceId(json.get(ID).asText());
85 // TODO: add providerId to JSON if we need to recover them.
86 ProviderId pid = new ProviderId(id.uri().getScheme(), "DeviceCodec");
87
88 Type type = Type.valueOf(json.get(TYPE).asText());
89 String mfr = json.get(MFR).asText();
90 String hw = json.get(HW).asText();
91 String sw = json.get(SW).asText();
92 String serial = json.get(SERIAL).asText();
93 ChassisId chassisId = new ChassisId(json.get(CHASSIS_ID).asText());
94 Annotations annotations = extractAnnotations(json, context);
95
96 return new DefaultDevice(pid, id, type, mfr, hw, sw, serial,
97 chassisId, annotations);
98 }
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080099}