Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
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; |
Seyeon Jeong | 357bcec | 2020-02-28 01:17:34 -0800 | [diff] [blame] | 23 | import org.onosproject.net.DefaultAnnotations; |
HIGUCHI Yuta | 5bb99a4 | 2015-03-19 16:52:15 -0700 | [diff] [blame] | 24 | import org.onosproject.net.DefaultDevice; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 25 | import org.onosproject.net.Device; |
HIGUCHI Yuta | 5bb99a4 | 2015-03-19 16:52:15 -0700 | [diff] [blame] | 26 | import org.onosproject.net.Device.Type; |
| 27 | import org.onosproject.net.DeviceId; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 28 | import org.onosproject.net.device.DeviceService; |
kalagesa | 4201954 | 2017-03-14 18:00:47 +0530 | [diff] [blame] | 29 | import org.onosproject.net.driver.DriverService; |
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; |
HIGUCHI Yuta | 5bb99a4 | 2015-03-19 16:52:15 -0700 | [diff] [blame] | 33 | import static org.onosproject.net.DeviceId.deviceId; |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 34 | |
| 35 | /** |
| 36 | * Device JSON codec. |
| 37 | */ |
Ray Milkey | 540b2ce | 2015-02-04 17:50:20 -0800 | [diff] [blame] | 38 | public final class DeviceCodec extends AnnotatedCodec<Device> { |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 39 | |
HIGUCHI Yuta | 5bb99a4 | 2015-03-19 16:52:15 -0700 | [diff] [blame] | 40 | // JSON fieldNames |
| 41 | private static final String ID = "id"; |
| 42 | private static final String TYPE = "type"; |
kalagesa | 4201954 | 2017-03-14 18:00:47 +0530 | [diff] [blame] | 43 | private static final String AVAILABLE = "available"; |
| 44 | private static final String ROLE = "role"; |
HIGUCHI Yuta | 5bb99a4 | 2015-03-19 16:52:15 -0700 | [diff] [blame] | 45 | private static final String MFR = "mfr"; |
| 46 | private static final String HW = "hw"; |
| 47 | private static final String SW = "sw"; |
| 48 | private static final String SERIAL = "serial"; |
| 49 | private static final String CHASSIS_ID = "chassisId"; |
kalagesa | 4201954 | 2017-03-14 18:00:47 +0530 | [diff] [blame] | 50 | private static final String DRIVER = "driver"; |
Ray Milkey | 054e23d | 2018-03-22 13:37:11 -0700 | [diff] [blame] | 51 | private static final String LAST_UPDATE = "lastUpdate"; |
| 52 | private static final String HUMAN_READABLE_LAST_UPDATE = "humanReadableLastUpdate"; |
HIGUCHI Yuta | 5bb99a4 | 2015-03-19 16:52:15 -0700 | [diff] [blame] | 53 | |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 54 | @Override |
| 55 | public ObjectNode encode(Device device, CodecContext context) { |
| 56 | checkNotNull(device, "Device cannot be null"); |
Ray Milkey | 3078fc0 | 2015-05-06 16:14:14 -0700 | [diff] [blame] | 57 | DeviceService service = context.getService(DeviceService.class); |
kalagesa | 4201954 | 2017-03-14 18:00:47 +0530 | [diff] [blame] | 58 | DriverService driveService = context.getService(DriverService.class); |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 59 | ObjectNode result = context.mapper().createObjectNode() |
HIGUCHI Yuta | 5bb99a4 | 2015-03-19 16:52:15 -0700 | [diff] [blame] | 60 | .put(ID, device.id().toString()) |
| 61 | .put(TYPE, device.type().name()) |
kalagesa | 4201954 | 2017-03-14 18:00:47 +0530 | [diff] [blame] | 62 | .put(AVAILABLE, service.isAvailable(device.id())) |
| 63 | .put(ROLE, service.getRole(device.id()).toString()) |
HIGUCHI Yuta | 5bb99a4 | 2015-03-19 16:52:15 -0700 | [diff] [blame] | 64 | .put(MFR, device.manufacturer()) |
| 65 | .put(HW, device.hwVersion()) |
| 66 | .put(SW, device.swVersion()) |
| 67 | .put(SERIAL, device.serialNumber()) |
kalagesa | 4201954 | 2017-03-14 18:00:47 +0530 | [diff] [blame] | 68 | .put(DRIVER, driveService.getDriver(device.id()).name()) |
Ray Milkey | 054e23d | 2018-03-22 13:37:11 -0700 | [diff] [blame] | 69 | .put(CHASSIS_ID, device.chassisId().toString()) |
| 70 | .put(LAST_UPDATE, Long.toString(service.getLastUpdatedInstant(device.id()))) |
| 71 | .put(HUMAN_READABLE_LAST_UPDATE, service.localStatus(device.id())); |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 72 | return annotate(result, device, context); |
| 73 | } |
| 74 | |
HIGUCHI Yuta | 5bb99a4 | 2015-03-19 16:52:15 -0700 | [diff] [blame] | 75 | |
| 76 | /** |
| 77 | * {@inheritDoc} |
| 78 | * |
| 79 | * Note: ProviderId is not part of JSON representation. |
| 80 | * Returned object will have random ProviderId set. |
| 81 | */ |
| 82 | @Override |
| 83 | public Device decode(ObjectNode json, CodecContext context) { |
| 84 | if (json == null || !json.isObject()) { |
| 85 | return null; |
| 86 | } |
| 87 | |
| 88 | DeviceId id = deviceId(json.get(ID).asText()); |
| 89 | // TODO: add providerId to JSON if we need to recover them. |
| 90 | ProviderId pid = new ProviderId(id.uri().getScheme(), "DeviceCodec"); |
| 91 | |
| 92 | Type type = Type.valueOf(json.get(TYPE).asText()); |
| 93 | String mfr = json.get(MFR).asText(); |
| 94 | String hw = json.get(HW).asText(); |
| 95 | String sw = json.get(SW).asText(); |
| 96 | String serial = json.get(SERIAL).asText(); |
| 97 | ChassisId chassisId = new ChassisId(json.get(CHASSIS_ID).asText()); |
| 98 | Annotations annotations = extractAnnotations(json, context); |
| 99 | |
| 100 | return new DefaultDevice(pid, id, type, mfr, hw, sw, serial, |
| 101 | chassisId, annotations); |
| 102 | } |
Seyeon Jeong | 357bcec | 2020-02-28 01:17:34 -0800 | [diff] [blame] | 103 | |
| 104 | /** |
| 105 | * Extracts annotations of given Object. |
| 106 | * |
| 107 | * @param deviceNode annotated JSON object node representing a device |
| 108 | * @param context decode context |
| 109 | * @return extracted Annotations |
| 110 | */ |
| 111 | @Override |
| 112 | protected Annotations extractAnnotations(ObjectNode deviceNode, CodecContext context) { |
| 113 | ObjectNode annotationsNode = get(deviceNode, "annotations"); |
| 114 | if (annotationsNode != null) { |
| 115 | // add needed fields to the annotations of the Device object |
| 116 | if (deviceNode.get(AVAILABLE) != null) { |
| 117 | annotationsNode.put(AVAILABLE, deviceNode.get(AVAILABLE).asText()); |
| 118 | } |
| 119 | return context.codec(Annotations.class).decode(annotationsNode, context); |
| 120 | } else { |
| 121 | return DefaultAnnotations.EMPTY; |
| 122 | } |
| 123 | } |
| 124 | |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 125 | } |