blob: 5f003e0b0423b2b37ec83526c20eaa1e7f5321a9 [file] [log] [blame]
Jian Lib68b9a82016-02-23 10:25:54 +09001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Jian Lib68b9a82016-02-23 10:25:54 +09003 *
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.codec.impl;
17
18import com.fasterxml.jackson.databind.JsonNode;
19import com.fasterxml.jackson.databind.node.ArrayNode;
20import com.fasterxml.jackson.databind.node.ObjectNode;
21import com.google.common.collect.BiMap;
22import com.google.common.collect.HashBiMap;
23import com.google.common.collect.Sets;
24import org.onosproject.cluster.NodeId;
25import org.onosproject.codec.CodecContext;
26import org.onosproject.codec.JsonCodec;
27import org.onosproject.net.region.DefaultRegion;
28import org.onosproject.net.region.Region;
29import org.onosproject.net.region.RegionId;
30
31import java.util.ArrayList;
32import java.util.List;
33import java.util.Set;
34import java.util.stream.IntStream;
35
36import static com.google.common.base.Preconditions.checkNotNull;
37import static org.onlab.util.Tools.nullIsIllegal;
38
39/**
40 * Codec for the Region class.
41 */
42public class RegionCodec extends JsonCodec<Region> {
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 Hunt964d9482016-02-24 22:42:01 -080050
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 Lib68b9a82016-02-23 10:25:54 +090053
54 private static final BiMap<String, Region.Type> REGION_TYPE_MAP = HashBiMap.create();
55
56 static {
Simon Hunt964d9482016-02-24 22:42:01 -080057 // 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 Lib68b9a82016-02-23 10:25:54 +090061 }
62
63 @Override
64 public ObjectNode encode(Region region, CodecContext context) {
Simon Hunt964d9482016-02-24 22:42:01 -080065 checkNotNull(region, NULL_REGION_MSG);
Jian Lib68b9a82016-02-23 10:25:54 +090066
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);
80 return result;
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 Hunt964d9482016-02-24 22:42:01 -080094 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 Lib68b9a82016-02-23 10:25:54 +0900107
Simon Hunt964d9482016-02-24 22:42:01 -0800108 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));
Jian Lib68b9a82016-02-23 10:25:54 +0900111
112 return new DefaultRegion(regionId, name, type, masters);
113 }
114
Simon Hunt964d9482016-02-24 22:42:01 -0800115 private String extractMember(String key, ObjectNode json) {
116 return nullIsIllegal(json.get(key), key + MISSING_MEMBER_MSG).asText();
117 }
118
Jian Lib68b9a82016-02-23 10:25:54 +0900119 /**
120 * Decodes node id json to node id object.
121 *
122 * @param json json object
123 * @return decoded node id object
124 */
Jian Li79359df2016-02-24 17:29:54 +0900125 private NodeId decodeNodeId(JsonNode json) {
Simon Hunt964d9482016-02-24 22:42:01 -0800126 return NodeId.nodeId(nullIsIllegal(json, NODE_ID +
127 MISSING_MEMBER_MSG).asText());
Jian Lib68b9a82016-02-23 10:25:54 +0900128 }
129}