blob: f6206b956212276f91dd9bb24908f205a1f3ee84 [file] [log] [blame]
Thomas Vachuskade563cf2015-04-01 00:28:50 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Thomas Vachuskade563cf2015-04-01 00:28:50 -07003 *
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.node.ObjectNode;
19import org.onlab.packet.IpAddress;
20import org.onosproject.cluster.ClusterService;
21import org.onosproject.cluster.ControllerNode;
22import org.onosproject.cluster.DefaultControllerNode;
23import org.onosproject.cluster.NodeId;
24import org.onosproject.codec.CodecContext;
25import org.onosproject.codec.JsonCodec;
26
27import static com.google.common.base.Preconditions.checkNotNull;
28import static org.onosproject.cluster.DefaultControllerNode.DEFAULT_PORT;
29
30/**
31 * Device JSON codec.
32 */
33public final class ControllerNodeCodec extends JsonCodec<ControllerNode> {
34
35 @Override
36 public ObjectNode encode(ControllerNode node, CodecContext context) {
37 checkNotNull(node, "Controller node cannot be null");
Ray Milkey3078fc02015-05-06 16:14:14 -070038 ClusterService service = context.getService(ClusterService.class);
Thomas Vachuskade563cf2015-04-01 00:28:50 -070039 return context.mapper().createObjectNode()
40 .put("id", node.id().toString())
41 .put("ip", node.ip().toString())
42 .put("tcpPort", node.tcpPort())
43 .put("status", service.getState(node.id()).toString());
44 }
45
46
47 @Override
48 public ControllerNode decode(ObjectNode json, CodecContext context) {
49 checkNotNull(json, "JSON cannot be null");
50 String ip = json.path("ip").asText();
51 return new DefaultControllerNode(new NodeId(json.path("id").asText(ip)),
52 IpAddress.valueOf(ip),
53 json.path("tcpPort").asInt(DEFAULT_PORT));
54 }
55
56
57}