blob: 7f78756340ce401020dd146c682f2393d8d99fcc [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.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;
HIGUCHI Yuta5bb99a42015-03-19 16:52:15 -070028import org.onosproject.net.provider.ProviderId;
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080029
30import static com.google.common.base.Preconditions.checkNotNull;
HIGUCHI Yuta5bb99a42015-03-19 16:52:15 -070031import static org.onosproject.net.DeviceId.deviceId;
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080032
33/**
34 * Device JSON codec.
35 */
Ray Milkey540b2ce2015-02-04 17:50:20 -080036public final class DeviceCodec extends AnnotatedCodec<Device> {
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080037
HIGUCHI Yuta5bb99a42015-03-19 16:52:15 -070038 // JSON fieldNames
39 private static final String ID = "id";
40 private static final String TYPE = "type";
41 private static final String MFR = "mfr";
42 private static final String HW = "hw";
43 private static final String SW = "sw";
44 private static final String SERIAL = "serial";
45 private static final String CHASSIS_ID = "chassisId";
46
47
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080048 @Override
49 public ObjectNode encode(Device device, CodecContext context) {
50 checkNotNull(device, "Device cannot be null");
51 DeviceService service = context.get(DeviceService.class);
52 ObjectNode result = context.mapper().createObjectNode()
HIGUCHI Yuta5bb99a42015-03-19 16:52:15 -070053 .put(ID, device.id().toString())
54 .put(TYPE, device.type().name())
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080055 .put("available", service.isAvailable(device.id()))
56 .put("role", service.getRole(device.id()).toString())
HIGUCHI Yuta5bb99a42015-03-19 16:52:15 -070057 .put(MFR, device.manufacturer())
58 .put(HW, device.hwVersion())
59 .put(SW, device.swVersion())
60 .put(SERIAL, device.serialNumber())
61 .put(CHASSIS_ID, device.chassisId().toString());
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080062 return annotate(result, device, context);
63 }
64
HIGUCHI Yuta5bb99a42015-03-19 16:52:15 -070065
66 /**
67 * {@inheritDoc}
68 *
69 * Note: ProviderId is not part of JSON representation.
70 * Returned object will have random ProviderId set.
71 */
72 @Override
73 public Device decode(ObjectNode json, CodecContext context) {
74 if (json == null || !json.isObject()) {
75 return null;
76 }
77
78 DeviceId id = deviceId(json.get(ID).asText());
79 // TODO: add providerId to JSON if we need to recover them.
80 ProviderId pid = new ProviderId(id.uri().getScheme(), "DeviceCodec");
81
82 Type type = Type.valueOf(json.get(TYPE).asText());
83 String mfr = json.get(MFR).asText();
84 String hw = json.get(HW).asText();
85 String sw = json.get(SW).asText();
86 String serial = json.get(SERIAL).asText();
87 ChassisId chassisId = new ChassisId(json.get(CHASSIS_ID).asText());
88 Annotations annotations = extractAnnotations(json, context);
89
90 return new DefaultDevice(pid, id, type, mfr, hw, sw, serial,
91 chassisId, annotations);
92 }
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080093}