Thomas Vachuska | de563cf | 2015-04-01 00:28:50 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 | */ |
| 16 | package org.onosproject.codec.impl; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 19 | import org.onlab.packet.IpAddress; |
| 20 | import org.onosproject.cluster.ClusterService; |
| 21 | import org.onosproject.cluster.ControllerNode; |
| 22 | import org.onosproject.cluster.DefaultControllerNode; |
| 23 | import org.onosproject.cluster.NodeId; |
| 24 | import org.onosproject.codec.CodecContext; |
| 25 | import org.onosproject.codec.JsonCodec; |
| 26 | |
| 27 | import static com.google.common.base.Preconditions.checkNotNull; |
| 28 | import static org.onosproject.cluster.DefaultControllerNode.DEFAULT_PORT; |
| 29 | |
| 30 | /** |
| 31 | * Device JSON codec. |
| 32 | */ |
| 33 | public 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 Milkey | 3078fc0 | 2015-05-06 16:14:14 -0700 | [diff] [blame] | 38 | ClusterService service = context.getService(ClusterService.class); |
Thomas Vachuska | de563cf | 2015-04-01 00:28:50 -0700 | [diff] [blame] | 39 | 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 | } |