HIGUCHI Yuta | 34a3f69 | 2016-01-09 21:08:57 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
HIGUCHI Yuta | 34a3f69 | 2016-01-09 21:08:57 -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 | */ |
| 16 | package org.onosproject.net.optical.json; |
| 17 | |
| 18 | import static com.google.common.base.Preconditions.checkArgument; |
| 19 | |
| 20 | import org.onosproject.net.ChannelSpacing; |
| 21 | import org.onosproject.net.GridType; |
| 22 | import org.onosproject.net.OchSignal; |
| 23 | |
| 24 | import com.fasterxml.jackson.databind.JsonNode; |
| 25 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 26 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 27 | import com.google.common.annotations.Beta; |
| 28 | |
| 29 | // TODO define common interface for JsonCodec for annotation? |
| 30 | // unlike existing JsonCodec, this use-case requires that encode/decode is |
| 31 | // reversible. (e.g., obj.equals(decode(encode(obj)))) |
| 32 | /** |
| 33 | * JSON codec for OchSignal. |
| 34 | */ |
| 35 | @Beta |
| 36 | public abstract class OchSignalCodec { |
| 37 | |
| 38 | // TODO should probably use shared mapper across optical codecs. |
| 39 | private static final ObjectMapper MAPPER = new ObjectMapper(); |
| 40 | |
| 41 | /** |
| 42 | * Creates an instance of {@link OchSignal} from JSON representation. |
| 43 | * |
| 44 | * @param obj JSON Object representing OchSignal |
| 45 | * @return OchSignal |
| 46 | * @throws IllegalArgumentException - if JSON object is ill-formed |
| 47 | * @see OchSignalCodec#encode(OchSignal) |
| 48 | */ |
| 49 | public static OchSignal decode(ObjectNode obj) { |
| 50 | final GridType gridType; |
| 51 | final ChannelSpacing channelSpacing; |
| 52 | final int spacingMultiplier; |
| 53 | final int slotGranularity; |
| 54 | |
| 55 | String s; |
| 56 | s = obj.get("channelSpacing").textValue(); |
| 57 | checkArgument(s != null, "ill-formed channelSpacing"); |
| 58 | channelSpacing = Enum.valueOf(ChannelSpacing.class, s); |
| 59 | |
| 60 | s = obj.get("gridType").textValue(); |
| 61 | checkArgument(s != null, "ill-formed gridType"); |
| 62 | gridType = Enum.valueOf(GridType.class, s); |
| 63 | |
| 64 | JsonNode node; |
| 65 | node = obj.get("spacingMultiplier"); |
| 66 | checkArgument(node.canConvertToInt(), "ill-formed spacingMultiplier"); |
| 67 | spacingMultiplier = node.asInt(); |
| 68 | |
| 69 | node = obj.get("slotGranularity"); |
| 70 | checkArgument(node.canConvertToInt(), "ill-formed slotGranularity"); |
| 71 | slotGranularity = node.asInt(); |
| 72 | |
| 73 | return new OchSignal(gridType, channelSpacing, spacingMultiplier, slotGranularity); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Returns a JSON Object representation of this instance. |
| 78 | * |
Ray Milkey | bb23e0b | 2016-08-02 17:00:21 -0700 | [diff] [blame] | 79 | * @param j Och signal object |
HIGUCHI Yuta | 34a3f69 | 2016-01-09 21:08:57 -0800 | [diff] [blame] | 80 | * @return JSON Object representing OchSignal |
| 81 | */ |
| 82 | public static ObjectNode encode(OchSignal j) { |
| 83 | ObjectNode obj = MAPPER.createObjectNode(); |
| 84 | obj.put("channelSpacing", j.channelSpacing().toString()); |
| 85 | obj.put("gridType", j.gridType().toString()); |
| 86 | obj.put("slotGranularity", j.slotGranularity()); |
| 87 | obj.put("spacingMultiplier", j.spacingMultiplier()); |
| 88 | return obj; |
| 89 | } |
| 90 | |
| 91 | } |