blob: 08d415562b714705a3bc060db628ea9787a7f6b9 [file] [log] [blame]
HIGUCHI Yuta34a3f692016-01-09 21:08:57 -08001/*
2 * Copyright 2016 Open Networking Laboratory
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 */
16package org.onosproject.net.optical.json;
17
18import static com.google.common.base.Preconditions.checkArgument;
19
20import org.onosproject.net.ChannelSpacing;
21import org.onosproject.net.GridType;
22import org.onosproject.net.OchSignal;
23
24import com.fasterxml.jackson.databind.JsonNode;
25import com.fasterxml.jackson.databind.ObjectMapper;
26import com.fasterxml.jackson.databind.node.ObjectNode;
27import 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
36public 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 Milkeybb23e0b2016-08-02 17:00:21 -070079 * @param j Och signal object
HIGUCHI Yuta34a3f692016-01-09 21:08:57 -080080 * @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}