blob: 2bed2826cb603a8bdae7f5e9003f9eb86324d9a3 [file] [log] [blame]
Jian Lib68a2b02016-05-02 11:23:32 -07001/*
2 * Copyright 2016-present 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.codec.impl;
17
18import com.fasterxml.jackson.databind.JsonNode;
19import com.fasterxml.jackson.databind.node.ArrayNode;
20import com.fasterxml.jackson.databind.node.ObjectNode;
21import org.onosproject.cluster.NodeId;
22import org.onosproject.cluster.RoleInfo;
23import org.onosproject.codec.CodecContext;
24import org.onosproject.codec.JsonCodec;
25
26import java.util.ArrayList;
27import java.util.List;
28import java.util.stream.IntStream;
29
30import static com.google.common.base.Preconditions.checkNotNull;
31import static org.onlab.util.Tools.nullIsIllegal;
32
33/**
34 * Codec for role info.
35 */
36public final class RoleInfoCodec extends JsonCodec<RoleInfo> {
37
38 // JSON field names
39 private static final String MASTER = "master";
40 private static final String BACKUPS = "backups";
41
Jian Lif96d41f2016-05-03 09:49:12 -070042 private static final String MISSING_MEMBER_MESSAGE = " member is required in RoleInfo";
Jian Lib68a2b02016-05-02 11:23:32 -070043
44 @Override
45 public ObjectNode encode(RoleInfo roleInfo, CodecContext context) {
46 checkNotNull(roleInfo, "RoleInfo cannot be null");
47
48 ObjectNode result = context.mapper().createObjectNode();
49
50 if (roleInfo.master() != null) {
51 result.put(MASTER, roleInfo.master().id());
52 }
53
54 ArrayNode backups = context.mapper().createArrayNode();
55 roleInfo.backups().forEach(backup -> backups.add(backup.id()));
56
57 if (roleInfo.backups().size() != 0) {
58 result.set(BACKUPS, backups);
59 }
60
61 return result;
62 }
63
64 @Override
65 public RoleInfo decode(ObjectNode json, CodecContext context) {
66 if (json == null || !json.isObject()) {
67 return null;
68 }
69
70 // parse node identifier of master
71 NodeId nodeId = json.get(MASTER) == null ?
72 null : NodeId.nodeId(json.get(MASTER).asText());
73
74 // parse node identifier of backups
75 List<NodeId> backups = new ArrayList<>();
76
77 ArrayNode backupsJson = (ArrayNode) nullIsIllegal(json.get(BACKUPS),
78 BACKUPS + MISSING_MEMBER_MESSAGE);
79
80 IntStream.range(0, backupsJson.size()).forEach(i -> {
81 JsonNode backupJson = nullIsIllegal(backupsJson.get(i),
82 "Backup node id cannot be null");
83 backups.add(NodeId.nodeId(backupJson.asText()));
84 });
85
86 return new RoleInfo(nodeId, backups);
87 }
88}