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; |
Jian Li | e2a04ce | 2020-07-01 19:07:02 +0900 | [diff] [blame] | 20 | import org.apache.commons.lang.StringUtils; |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 21 | import org.onlab.packet.IpAddress; |
Jian Li | c2242bd | 2020-09-03 13:12:14 +0900 | [diff] [blame] | 22 | import org.onlab.packet.MacAddress; |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 23 | import org.onosproject.codec.CodecContext; |
| 24 | import org.onosproject.codec.JsonCodec; |
| 25 | import org.onosproject.k8snode.api.DefaultK8sNode; |
| 26 | import org.onosproject.k8snode.api.K8sNode; |
| 27 | import org.onosproject.k8snode.api.K8sNodeState; |
| 28 | import org.onosproject.net.DeviceId; |
| 29 | import org.slf4j.Logger; |
| 30 | |
| 31 | import static com.google.common.base.Preconditions.checkNotNull; |
| 32 | import static org.onlab.util.Tools.nullIsIllegal; |
Jian Li | e2a04ce | 2020-07-01 19:07:02 +0900 | [diff] [blame] | 33 | import static org.onosproject.k8snode.api.Constants.DEFAULT_CLUSTER_NAME; |
| 34 | import static org.onosproject.k8snode.api.Constants.DEFAULT_SEGMENT_ID; |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 35 | import static org.slf4j.LoggerFactory.getLogger; |
| 36 | |
| 37 | /** |
| 38 | * Kubernetes node codec used for serializing and de-serializing JSON string. |
| 39 | */ |
| 40 | public final class K8sNodeCodec extends JsonCodec<K8sNode> { |
| 41 | |
| 42 | private final Logger log = getLogger(getClass()); |
| 43 | |
Jian Li | e2a04ce | 2020-07-01 19:07:02 +0900 | [diff] [blame] | 44 | private static final String CLUSTER_NAME = "clusterName"; |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 45 | private static final String HOSTNAME = "hostname"; |
| 46 | private static final String TYPE = "type"; |
Jian Li | 732c342 | 2020-09-07 17:01:11 +0900 | [diff] [blame] | 47 | private static final String MODE = "mode"; |
Jian Li | e2a04ce | 2020-07-01 19:07:02 +0900 | [diff] [blame] | 48 | private static final String SEGMENT_ID = "segmentId"; |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 49 | private static final String MANAGEMENT_IP = "managementIp"; |
| 50 | private static final String DATA_IP = "dataIp"; |
Jian Li | 4294af7 | 2020-10-07 02:12:33 +0900 | [diff] [blame] | 51 | private static final String NODE_IP = "nodeIp"; |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 52 | private static final String INTEGRATION_BRIDGE = "integrationBridge"; |
Jian Li | bf562c2 | 2019-04-15 18:07:14 +0900 | [diff] [blame] | 53 | private static final String EXTERNAL_BRIDGE = "externalBridge"; |
Jian Li | 1a2eb5d | 2019-08-27 02:07:05 +0900 | [diff] [blame] | 54 | private static final String LOCAL_BRIDGE = "localBridge"; |
Jian Li | e2a04ce | 2020-07-01 19:07:02 +0900 | [diff] [blame] | 55 | private static final String TUNNEL_BRIDGE = "tunnelBridge"; |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 56 | private static final String STATE = "state"; |
Jian Li | 0c63272 | 2019-05-08 15:58:04 +0900 | [diff] [blame] | 57 | private static final String EXTERNAL_INTF = "externalInterface"; |
| 58 | private static final String EXTERNAL_BRIDGE_IP = "externalBridgeIp"; |
| 59 | private static final String EXTERNAL_GATEWAY_IP = "externalGatewayIp"; |
Jian Li | c2242bd | 2020-09-03 13:12:14 +0900 | [diff] [blame] | 60 | private static final String EXTERNAL_GATEWAY_MAC = "externalGatewayMac"; |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 61 | |
| 62 | private static final String MISSING_MESSAGE = " is required in K8sNode"; |
| 63 | |
| 64 | @Override |
| 65 | public ObjectNode encode(K8sNode node, CodecContext context) { |
| 66 | checkNotNull(node, "Kubernetes node cannot be null"); |
| 67 | |
| 68 | ObjectNode result = context.mapper().createObjectNode() |
Jian Li | e2a04ce | 2020-07-01 19:07:02 +0900 | [diff] [blame] | 69 | .put(CLUSTER_NAME, node.clusterName()) |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 70 | .put(HOSTNAME, node.hostname()) |
| 71 | .put(TYPE, node.type().name()) |
Jian Li | 732c342 | 2020-09-07 17:01:11 +0900 | [diff] [blame] | 72 | .put(MODE, node.mode().name()) |
Jian Li | e2a04ce | 2020-07-01 19:07:02 +0900 | [diff] [blame] | 73 | .put(SEGMENT_ID, node.segmentId()) |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 74 | .put(STATE, node.state().name()) |
Jian Li | 4294af7 | 2020-10-07 02:12:33 +0900 | [diff] [blame] | 75 | .put(MANAGEMENT_IP, node.managementIp().toString()) |
| 76 | .put(NODE_IP, node.nodeIp().toString()); |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 77 | |
| 78 | if (node.intgBridge() != null) { |
| 79 | result.put(INTEGRATION_BRIDGE, node.intgBridge().toString()); |
| 80 | } |
| 81 | |
Jian Li | bf562c2 | 2019-04-15 18:07:14 +0900 | [diff] [blame] | 82 | if (node.extBridge() != null) { |
| 83 | result.put(EXTERNAL_BRIDGE, node.extBridge().toString()); |
| 84 | } |
| 85 | |
Jian Li | 1a2eb5d | 2019-08-27 02:07:05 +0900 | [diff] [blame] | 86 | if (node.localBridge() != null) { |
| 87 | result.put(LOCAL_BRIDGE, node.localBridge().toString()); |
| 88 | } |
| 89 | |
Jian Li | e2a04ce | 2020-07-01 19:07:02 +0900 | [diff] [blame] | 90 | if (node.tunBridge() != null) { |
| 91 | result.put(TUNNEL_BRIDGE, node.tunBridge().toString()); |
| 92 | } |
| 93 | |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 94 | if (node.dataIp() != null) { |
| 95 | result.put(DATA_IP, node.dataIp().toString()); |
| 96 | } |
| 97 | |
Jian Li | 0c63272 | 2019-05-08 15:58:04 +0900 | [diff] [blame] | 98 | if (node.extIntf() != null) { |
| 99 | result.put(EXTERNAL_INTF, node.extIntf()); |
| 100 | } |
| 101 | |
| 102 | if (node.extBridgeIp() != null) { |
| 103 | result.put(EXTERNAL_BRIDGE_IP, node.extBridgeIp().toString()); |
| 104 | } |
| 105 | |
| 106 | if (node.extGatewayIp() != null) { |
| 107 | result.put(EXTERNAL_GATEWAY_IP, node.extGatewayIp().toString()); |
| 108 | } |
| 109 | |
Jian Li | c2242bd | 2020-09-03 13:12:14 +0900 | [diff] [blame] | 110 | if (node.extGatewayMac() != null) { |
| 111 | result.put(EXTERNAL_GATEWAY_MAC, node.extGatewayMac().toString()); |
| 112 | } |
| 113 | |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 114 | return result; |
| 115 | } |
| 116 | |
| 117 | @Override |
| 118 | public K8sNode decode(ObjectNode json, CodecContext context) { |
| 119 | if (json == null || !json.isObject()) { |
| 120 | return null; |
| 121 | } |
| 122 | |
Jian Li | e2a04ce | 2020-07-01 19:07:02 +0900 | [diff] [blame] | 123 | String clusterName = json.get(CLUSTER_NAME).asText(); |
| 124 | |
| 125 | if (StringUtils.isEmpty(clusterName)) { |
| 126 | clusterName = DEFAULT_CLUSTER_NAME; |
| 127 | } |
| 128 | |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 129 | String hostname = nullIsIllegal(json.get(HOSTNAME).asText(), |
| 130 | HOSTNAME + MISSING_MESSAGE); |
| 131 | String type = nullIsIllegal(json.get(TYPE).asText(), |
| 132 | TYPE + MISSING_MESSAGE); |
| 133 | String mIp = nullIsIllegal(json.get(MANAGEMENT_IP).asText(), |
| 134 | MANAGEMENT_IP + MISSING_MESSAGE); |
Jian Li | 4294af7 | 2020-10-07 02:12:33 +0900 | [diff] [blame] | 135 | String nIp = nullIsIllegal(json.get(NODE_IP).asText(), |
| 136 | NODE_IP + MISSING_MESSAGE); |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 137 | |
| 138 | DefaultK8sNode.Builder nodeBuilder = DefaultK8sNode.builder() |
Jian Li | e2a04ce | 2020-07-01 19:07:02 +0900 | [diff] [blame] | 139 | .clusterName(clusterName) |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 140 | .hostname(hostname) |
| 141 | .type(K8sNode.Type.valueOf(type)) |
| 142 | .state(K8sNodeState.INIT) |
Jian Li | 4294af7 | 2020-10-07 02:12:33 +0900 | [diff] [blame] | 143 | .managementIp(IpAddress.valueOf(mIp)) |
| 144 | .nodeIp(IpAddress.valueOf(nIp)); |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 145 | |
| 146 | if (json.get(DATA_IP) != null) { |
| 147 | nodeBuilder.dataIp(IpAddress.valueOf(json.get(DATA_IP).asText())); |
| 148 | } |
| 149 | |
Jian Li | e2a04ce | 2020-07-01 19:07:02 +0900 | [diff] [blame] | 150 | JsonNode segmentIdJson = json.get(SEGMENT_ID); |
| 151 | int segmentId = DEFAULT_SEGMENT_ID; |
| 152 | if (segmentIdJson != null) { |
| 153 | segmentId = segmentIdJson.asInt(); |
| 154 | } |
| 155 | nodeBuilder.segmentId(segmentId); |
| 156 | |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 157 | JsonNode intBridgeJson = json.get(INTEGRATION_BRIDGE); |
| 158 | if (intBridgeJson != null) { |
| 159 | nodeBuilder.intgBridge(DeviceId.deviceId(intBridgeJson.asText())); |
| 160 | } |
| 161 | |
Jian Li | bf562c2 | 2019-04-15 18:07:14 +0900 | [diff] [blame] | 162 | JsonNode extBridgeJson = json.get(EXTERNAL_BRIDGE); |
| 163 | if (extBridgeJson != null) { |
| 164 | nodeBuilder.extBridge(DeviceId.deviceId(extBridgeJson.asText())); |
| 165 | } |
| 166 | |
Jian Li | 1a2eb5d | 2019-08-27 02:07:05 +0900 | [diff] [blame] | 167 | JsonNode localBridgeJson = json.get(LOCAL_BRIDGE); |
| 168 | if (localBridgeJson != null) { |
| 169 | nodeBuilder.localBridge(DeviceId.deviceId(localBridgeJson.asText())); |
| 170 | } |
| 171 | |
Jian Li | e2a04ce | 2020-07-01 19:07:02 +0900 | [diff] [blame] | 172 | JsonNode tunBridgeJson = json.get(TUNNEL_BRIDGE); |
| 173 | if (tunBridgeJson != null) { |
| 174 | nodeBuilder.tunBridge(DeviceId.deviceId(tunBridgeJson.asText())); |
| 175 | } |
| 176 | |
Jian Li | 0c63272 | 2019-05-08 15:58:04 +0900 | [diff] [blame] | 177 | JsonNode extIntfJson = json.get(EXTERNAL_INTF); |
| 178 | if (extIntfJson != null) { |
| 179 | nodeBuilder.extIntf(extIntfJson.asText()); |
| 180 | } |
| 181 | |
| 182 | JsonNode extBridgeIpJson = json.get(EXTERNAL_BRIDGE_IP); |
| 183 | if (extBridgeIpJson != null) { |
| 184 | nodeBuilder.extBridgeIp(IpAddress.valueOf(extBridgeIpJson.asText())); |
| 185 | } |
| 186 | |
| 187 | JsonNode extGatewayIpJson = json.get(EXTERNAL_GATEWAY_IP); |
| 188 | if (extGatewayIpJson != null) { |
| 189 | nodeBuilder.extGatewayIp(IpAddress.valueOf(extGatewayIpJson.asText())); |
| 190 | } |
| 191 | |
Jian Li | c2242bd | 2020-09-03 13:12:14 +0900 | [diff] [blame] | 192 | JsonNode extGatewayMacJson = json.get(EXTERNAL_GATEWAY_MAC); |
| 193 | if (extGatewayMacJson != null) { |
| 194 | nodeBuilder.extGatewayMac(MacAddress.valueOf(extGatewayMacJson.asText())); |
| 195 | } |
| 196 | |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 197 | log.trace("node is {}", nodeBuilder.build().toString()); |
| 198 | |
| 199 | return nodeBuilder.build(); |
| 200 | } |
| 201 | } |