Jian Li | b68a2b0 | 2016-05-02 11:23:32 -0700 | [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 | b68a2b0 | 2016-05-02 11:23:32 -0700 | [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 org.onosproject.cluster.NodeId; |
| 22 | import org.onosproject.cluster.RoleInfo; |
| 23 | import org.onosproject.codec.CodecContext; |
| 24 | import org.onosproject.codec.JsonCodec; |
| 25 | |
| 26 | import java.util.ArrayList; |
| 27 | import java.util.List; |
| 28 | import java.util.stream.IntStream; |
| 29 | |
| 30 | import static com.google.common.base.Preconditions.checkNotNull; |
| 31 | import static org.onlab.util.Tools.nullIsIllegal; |
| 32 | |
| 33 | /** |
| 34 | * Codec for role info. |
| 35 | */ |
| 36 | public 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 Li | f96d41f | 2016-05-03 09:49:12 -0700 | [diff] [blame] | 42 | private static final String MISSING_MEMBER_MESSAGE = " member is required in RoleInfo"; |
Jian Li | b68a2b0 | 2016-05-02 11:23:32 -0700 | [diff] [blame] | 43 | |
| 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 | |
Jon Hall | cbd1b39 | 2017-01-18 20:15:44 -0800 | [diff] [blame] | 57 | if (!roleInfo.backups().isEmpty()) { |
Jian Li | b68a2b0 | 2016-05-02 11:23:32 -0700 | [diff] [blame] | 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 | } |