Jian Li | b68b9a8 | 2016-02-23 10:25:54 +0900 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Jian Li | b68b9a8 | 2016-02-23 10:25:54 +0900 | [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.codec.impl; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 20 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 21 | import com.google.common.collect.BiMap; |
| 22 | import com.google.common.collect.HashBiMap; |
| 23 | import com.google.common.collect.Sets; |
| 24 | import org.onosproject.cluster.NodeId; |
| 25 | import org.onosproject.codec.CodecContext; |
Simon Hunt | 5361221 | 2016-12-04 17:19:52 -0800 | [diff] [blame] | 26 | import org.onosproject.net.Annotations; |
Jian Li | b68b9a8 | 2016-02-23 10:25:54 +0900 | [diff] [blame] | 27 | import org.onosproject.net.region.DefaultRegion; |
| 28 | import org.onosproject.net.region.Region; |
| 29 | import org.onosproject.net.region.RegionId; |
| 30 | |
| 31 | import java.util.ArrayList; |
| 32 | import java.util.List; |
| 33 | import java.util.Set; |
| 34 | import java.util.stream.IntStream; |
| 35 | |
| 36 | import static com.google.common.base.Preconditions.checkNotNull; |
| 37 | import static org.onlab.util.Tools.nullIsIllegal; |
| 38 | |
| 39 | /** |
| 40 | * Codec for the Region class. |
| 41 | */ |
Simon Hunt | 5361221 | 2016-12-04 17:19:52 -0800 | [diff] [blame] | 42 | public class RegionCodec extends AnnotatedCodec<Region> { |
Jian Li | b68b9a8 | 2016-02-23 10:25:54 +0900 | [diff] [blame] | 43 | |
| 44 | // JSON field names |
| 45 | private static final String REGION_ID = "id"; |
| 46 | private static final String NAME = "name"; |
| 47 | private static final String TYPE = "type"; |
| 48 | private static final String MASTERS = "masters"; |
| 49 | private static final String NODE_ID = "nodeId"; |
Simon Hunt | 964d948 | 2016-02-24 22:42:01 -0800 | [diff] [blame] | 50 | |
| 51 | private static final String NULL_REGION_MSG = "Region cannot be null"; |
| 52 | private static final String MISSING_MEMBER_MSG = " member is required in Region"; |
Jian Li | b68b9a8 | 2016-02-23 10:25:54 +0900 | [diff] [blame] | 53 | |
| 54 | private static final BiMap<String, Region.Type> REGION_TYPE_MAP = HashBiMap.create(); |
| 55 | |
| 56 | static { |
Simon Hunt | 964d948 | 2016-02-24 22:42:01 -0800 | [diff] [blame] | 57 | // key is String representation of Region.Type; value is Region.Type |
| 58 | for (Region.Type t : Region.Type.values()) { |
| 59 | REGION_TYPE_MAP.put(t.name(), t); |
| 60 | } |
Jian Li | b68b9a8 | 2016-02-23 10:25:54 +0900 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | @Override |
| 64 | public ObjectNode encode(Region region, CodecContext context) { |
Simon Hunt | 964d948 | 2016-02-24 22:42:01 -0800 | [diff] [blame] | 65 | checkNotNull(region, NULL_REGION_MSG); |
Jian Li | b68b9a8 | 2016-02-23 10:25:54 +0900 | [diff] [blame] | 66 | |
| 67 | ObjectNode result = context.mapper().createObjectNode() |
| 68 | .put(REGION_ID, region.id().toString()) |
| 69 | .put(NAME, region.name()) |
| 70 | .put(TYPE, region.type().toString()); |
| 71 | |
| 72 | ArrayNode masters = context.mapper().createArrayNode(); |
| 73 | |
| 74 | region.masters().forEach(sets -> { |
| 75 | ArrayNode setsJson = context.mapper().createArrayNode(); |
| 76 | sets.forEach(nodeId -> setsJson.add(nodeId.toString())); |
| 77 | masters.add(setsJson); |
| 78 | }); |
| 79 | result.set(MASTERS, masters); |
Simon Hunt | 5361221 | 2016-12-04 17:19:52 -0800 | [diff] [blame] | 80 | return annotate(result, region, context); |
Jian Li | b68b9a8 | 2016-02-23 10:25:54 +0900 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | @Override |
| 84 | public Region decode(ObjectNode json, CodecContext context) { |
| 85 | if (json == null || !json.isObject()) { |
| 86 | return null; |
| 87 | } |
| 88 | |
| 89 | // parse masters |
| 90 | List<Set<NodeId>> masters = new ArrayList<>(); |
| 91 | JsonNode mastersJson = json.get(MASTERS); |
| 92 | checkNotNull(mastersJson); |
| 93 | |
Simon Hunt | 964d948 | 2016-02-24 22:42:01 -0800 | [diff] [blame] | 94 | IntStream.range(0, mastersJson.size()).forEach(i -> { |
| 95 | JsonNode setsJson = mastersJson.get(i); |
| 96 | final Set<NodeId> nodeIds = Sets.newHashSet(); |
| 97 | if (setsJson != null && setsJson.isArray()) { |
| 98 | Set<NodeId> localNodeIds = Sets.newHashSet(); |
| 99 | IntStream.range(0, setsJson.size()).forEach(j -> { |
| 100 | JsonNode nodeIdJson = setsJson.get(j); |
| 101 | localNodeIds.add(decodeNodeId(nodeIdJson)); |
| 102 | }); |
| 103 | nodeIds.addAll(localNodeIds); |
| 104 | } |
| 105 | masters.add(nodeIds); |
| 106 | }); |
Jian Li | b68b9a8 | 2016-02-23 10:25:54 +0900 | [diff] [blame] | 107 | |
Simon Hunt | 964d948 | 2016-02-24 22:42:01 -0800 | [diff] [blame] | 108 | RegionId regionId = RegionId.regionId(extractMember(REGION_ID, json)); |
| 109 | String name = extractMember(NAME, json); |
| 110 | Region.Type type = REGION_TYPE_MAP.get(extractMember(TYPE, json)); |
Simon Hunt | 5361221 | 2016-12-04 17:19:52 -0800 | [diff] [blame] | 111 | Annotations annots = extractAnnotations(json, context); |
Jian Li | b68b9a8 | 2016-02-23 10:25:54 +0900 | [diff] [blame] | 112 | |
Simon Hunt | 5361221 | 2016-12-04 17:19:52 -0800 | [diff] [blame] | 113 | return new DefaultRegion(regionId, name, type, annots, masters); |
Jian Li | b68b9a8 | 2016-02-23 10:25:54 +0900 | [diff] [blame] | 114 | } |
| 115 | |
Simon Hunt | 964d948 | 2016-02-24 22:42:01 -0800 | [diff] [blame] | 116 | private String extractMember(String key, ObjectNode json) { |
| 117 | return nullIsIllegal(json.get(key), key + MISSING_MEMBER_MSG).asText(); |
| 118 | } |
| 119 | |
Jian Li | b68b9a8 | 2016-02-23 10:25:54 +0900 | [diff] [blame] | 120 | /** |
| 121 | * Decodes node id json to node id object. |
| 122 | * |
| 123 | * @param json json object |
| 124 | * @return decoded node id object |
| 125 | */ |
Jian Li | 79359df | 2016-02-24 17:29:54 +0900 | [diff] [blame] | 126 | private NodeId decodeNodeId(JsonNode json) { |
Simon Hunt | 964d948 | 2016-02-24 22:42:01 -0800 | [diff] [blame] | 127 | return NodeId.nodeId(nullIsIllegal(json, NODE_ID + |
| 128 | MISSING_MEMBER_MSG).asText()); |
Jian Li | b68b9a8 | 2016-02-23 10:25:54 +0900 | [diff] [blame] | 129 | } |
| 130 | } |