Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019-present Open Networking Foundation |
| 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.k8snode.codec; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 20 | import org.onlab.packet.IpAddress; |
| 21 | import org.onosproject.codec.CodecContext; |
| 22 | import org.onosproject.codec.JsonCodec; |
| 23 | import org.onosproject.k8snode.api.DefaultK8sNode; |
| 24 | import org.onosproject.k8snode.api.K8sNode; |
| 25 | import org.onosproject.k8snode.api.K8sNodeState; |
| 26 | import org.onosproject.net.DeviceId; |
| 27 | import org.slf4j.Logger; |
| 28 | |
| 29 | import static com.google.common.base.Preconditions.checkNotNull; |
| 30 | import static org.onlab.util.Tools.nullIsIllegal; |
| 31 | import static org.slf4j.LoggerFactory.getLogger; |
| 32 | |
| 33 | /** |
| 34 | * Kubernetes node codec used for serializing and de-serializing JSON string. |
| 35 | */ |
| 36 | public final class K8sNodeCodec extends JsonCodec<K8sNode> { |
| 37 | |
| 38 | private final Logger log = getLogger(getClass()); |
| 39 | |
| 40 | private static final String HOSTNAME = "hostname"; |
| 41 | private static final String TYPE = "type"; |
| 42 | private static final String MANAGEMENT_IP = "managementIp"; |
| 43 | private static final String DATA_IP = "dataIp"; |
| 44 | private static final String INTEGRATION_BRIDGE = "integrationBridge"; |
Jian Li | bf562c2 | 2019-04-15 18:07:14 +0900 | [diff] [blame] | 45 | private static final String EXTERNAL_BRIDGE = "externalBridge"; |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 46 | private static final String STATE = "state"; |
Jian Li | 0c63272 | 2019-05-08 15:58:04 +0900 | [diff] [blame] | 47 | private static final String EXTERNAL_INTF = "externalInterface"; |
| 48 | private static final String EXTERNAL_BRIDGE_IP = "externalBridgeIp"; |
| 49 | private static final String EXTERNAL_GATEWAY_IP = "externalGatewayIp"; |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 50 | |
| 51 | private static final String MISSING_MESSAGE = " is required in K8sNode"; |
| 52 | |
| 53 | @Override |
| 54 | public ObjectNode encode(K8sNode node, CodecContext context) { |
| 55 | checkNotNull(node, "Kubernetes node cannot be null"); |
| 56 | |
| 57 | ObjectNode result = context.mapper().createObjectNode() |
| 58 | .put(HOSTNAME, node.hostname()) |
| 59 | .put(TYPE, node.type().name()) |
| 60 | .put(STATE, node.state().name()) |
| 61 | .put(MANAGEMENT_IP, node.managementIp().toString()); |
| 62 | |
| 63 | if (node.intgBridge() != null) { |
| 64 | result.put(INTEGRATION_BRIDGE, node.intgBridge().toString()); |
| 65 | } |
| 66 | |
Jian Li | bf562c2 | 2019-04-15 18:07:14 +0900 | [diff] [blame] | 67 | if (node.extBridge() != null) { |
| 68 | result.put(EXTERNAL_BRIDGE, node.extBridge().toString()); |
| 69 | } |
| 70 | |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 71 | if (node.dataIp() != null) { |
| 72 | result.put(DATA_IP, node.dataIp().toString()); |
| 73 | } |
| 74 | |
Jian Li | 0c63272 | 2019-05-08 15:58:04 +0900 | [diff] [blame] | 75 | if (node.extIntf() != null) { |
| 76 | result.put(EXTERNAL_INTF, node.extIntf()); |
| 77 | } |
| 78 | |
| 79 | if (node.extBridgeIp() != null) { |
| 80 | result.put(EXTERNAL_BRIDGE_IP, node.extBridgeIp().toString()); |
| 81 | } |
| 82 | |
| 83 | if (node.extGatewayIp() != null) { |
| 84 | result.put(EXTERNAL_GATEWAY_IP, node.extGatewayIp().toString()); |
| 85 | } |
| 86 | |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 87 | return result; |
| 88 | } |
| 89 | |
| 90 | @Override |
| 91 | public K8sNode decode(ObjectNode json, CodecContext context) { |
| 92 | if (json == null || !json.isObject()) { |
| 93 | return null; |
| 94 | } |
| 95 | |
| 96 | String hostname = nullIsIllegal(json.get(HOSTNAME).asText(), |
| 97 | HOSTNAME + MISSING_MESSAGE); |
| 98 | String type = nullIsIllegal(json.get(TYPE).asText(), |
| 99 | TYPE + MISSING_MESSAGE); |
| 100 | String mIp = nullIsIllegal(json.get(MANAGEMENT_IP).asText(), |
| 101 | MANAGEMENT_IP + MISSING_MESSAGE); |
| 102 | |
| 103 | DefaultK8sNode.Builder nodeBuilder = DefaultK8sNode.builder() |
| 104 | .hostname(hostname) |
| 105 | .type(K8sNode.Type.valueOf(type)) |
| 106 | .state(K8sNodeState.INIT) |
| 107 | .managementIp(IpAddress.valueOf(mIp)); |
| 108 | |
| 109 | if (json.get(DATA_IP) != null) { |
| 110 | nodeBuilder.dataIp(IpAddress.valueOf(json.get(DATA_IP).asText())); |
| 111 | } |
| 112 | |
| 113 | JsonNode intBridgeJson = json.get(INTEGRATION_BRIDGE); |
| 114 | if (intBridgeJson != null) { |
| 115 | nodeBuilder.intgBridge(DeviceId.deviceId(intBridgeJson.asText())); |
| 116 | } |
| 117 | |
Jian Li | bf562c2 | 2019-04-15 18:07:14 +0900 | [diff] [blame] | 118 | JsonNode extBridgeJson = json.get(EXTERNAL_BRIDGE); |
| 119 | if (extBridgeJson != null) { |
| 120 | nodeBuilder.extBridge(DeviceId.deviceId(extBridgeJson.asText())); |
| 121 | } |
| 122 | |
Jian Li | 0c63272 | 2019-05-08 15:58:04 +0900 | [diff] [blame] | 123 | JsonNode extIntfJson = json.get(EXTERNAL_INTF); |
| 124 | if (extIntfJson != null) { |
| 125 | nodeBuilder.extIntf(extIntfJson.asText()); |
| 126 | } |
| 127 | |
| 128 | JsonNode extBridgeIpJson = json.get(EXTERNAL_BRIDGE_IP); |
| 129 | if (extBridgeIpJson != null) { |
| 130 | nodeBuilder.extBridgeIp(IpAddress.valueOf(extBridgeIpJson.asText())); |
| 131 | } |
| 132 | |
| 133 | JsonNode extGatewayIpJson = json.get(EXTERNAL_GATEWAY_IP); |
| 134 | if (extGatewayIpJson != null) { |
| 135 | nodeBuilder.extGatewayIp(IpAddress.valueOf(extGatewayIpJson.asText())); |
| 136 | } |
| 137 | |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 138 | log.trace("node is {}", nodeBuilder.build().toString()); |
| 139 | |
| 140 | return nodeBuilder.build(); |
| 141 | } |
| 142 | } |