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 | 58b3398 | 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 | b121844 | 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 | 58b3398 | 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 | 58b3398 | 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 | 58b3398 | 2020-07-01 19:07:02 +0900 | [diff] [blame] | 47 | private static final String SEGMENT_ID = "segmentId"; |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 48 | private static final String MANAGEMENT_IP = "managementIp"; |
| 49 | private static final String DATA_IP = "dataIp"; |
| 50 | private static final String INTEGRATION_BRIDGE = "integrationBridge"; |
Jian Li | bf562c2 | 2019-04-15 18:07:14 +0900 | [diff] [blame] | 51 | private static final String EXTERNAL_BRIDGE = "externalBridge"; |
Jian Li | 121ddfe | 2019-08-27 02:07:05 +0900 | [diff] [blame] | 52 | private static final String LOCAL_BRIDGE = "localBridge"; |
Jian Li | 58b3398 | 2020-07-01 19:07:02 +0900 | [diff] [blame] | 53 | private static final String TUNNEL_BRIDGE = "tunnelBridge"; |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 54 | private static final String STATE = "state"; |
Jian Li | 7709eb4 | 2019-05-08 15:58:04 +0900 | [diff] [blame] | 55 | private static final String EXTERNAL_INTF = "externalInterface"; |
| 56 | private static final String EXTERNAL_BRIDGE_IP = "externalBridgeIp"; |
| 57 | private static final String EXTERNAL_GATEWAY_IP = "externalGatewayIp"; |
Jian Li | b121844 | 2020-09-03 13:12:14 +0900 | [diff] [blame] | 58 | private static final String EXTERNAL_GATEWAY_MAC = "externalGatewayMac"; |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 59 | |
| 60 | private static final String MISSING_MESSAGE = " is required in K8sNode"; |
| 61 | |
| 62 | @Override |
| 63 | public ObjectNode encode(K8sNode node, CodecContext context) { |
| 64 | checkNotNull(node, "Kubernetes node cannot be null"); |
| 65 | |
| 66 | ObjectNode result = context.mapper().createObjectNode() |
Jian Li | 58b3398 | 2020-07-01 19:07:02 +0900 | [diff] [blame] | 67 | .put(CLUSTER_NAME, node.clusterName()) |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 68 | .put(HOSTNAME, node.hostname()) |
| 69 | .put(TYPE, node.type().name()) |
Jian Li | 58b3398 | 2020-07-01 19:07:02 +0900 | [diff] [blame] | 70 | .put(SEGMENT_ID, node.segmentId()) |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 71 | .put(STATE, node.state().name()) |
| 72 | .put(MANAGEMENT_IP, node.managementIp().toString()); |
| 73 | |
| 74 | if (node.intgBridge() != null) { |
| 75 | result.put(INTEGRATION_BRIDGE, node.intgBridge().toString()); |
| 76 | } |
| 77 | |
Jian Li | bf562c2 | 2019-04-15 18:07:14 +0900 | [diff] [blame] | 78 | if (node.extBridge() != null) { |
| 79 | result.put(EXTERNAL_BRIDGE, node.extBridge().toString()); |
| 80 | } |
| 81 | |
Jian Li | 121ddfe | 2019-08-27 02:07:05 +0900 | [diff] [blame] | 82 | if (node.localBridge() != null) { |
| 83 | result.put(LOCAL_BRIDGE, node.localBridge().toString()); |
| 84 | } |
| 85 | |
Jian Li | 58b3398 | 2020-07-01 19:07:02 +0900 | [diff] [blame] | 86 | if (node.tunBridge() != null) { |
| 87 | result.put(TUNNEL_BRIDGE, node.tunBridge().toString()); |
| 88 | } |
| 89 | |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 90 | if (node.dataIp() != null) { |
| 91 | result.put(DATA_IP, node.dataIp().toString()); |
| 92 | } |
| 93 | |
Jian Li | 7709eb4 | 2019-05-08 15:58:04 +0900 | [diff] [blame] | 94 | if (node.extIntf() != null) { |
| 95 | result.put(EXTERNAL_INTF, node.extIntf()); |
| 96 | } |
| 97 | |
| 98 | if (node.extBridgeIp() != null) { |
| 99 | result.put(EXTERNAL_BRIDGE_IP, node.extBridgeIp().toString()); |
| 100 | } |
| 101 | |
| 102 | if (node.extGatewayIp() != null) { |
| 103 | result.put(EXTERNAL_GATEWAY_IP, node.extGatewayIp().toString()); |
| 104 | } |
| 105 | |
Jian Li | b121844 | 2020-09-03 13:12:14 +0900 | [diff] [blame] | 106 | if (node.extGatewayMac() != null) { |
| 107 | result.put(EXTERNAL_GATEWAY_MAC, node.extGatewayMac().toString()); |
| 108 | } |
| 109 | |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 110 | return result; |
| 111 | } |
| 112 | |
| 113 | @Override |
| 114 | public K8sNode decode(ObjectNode json, CodecContext context) { |
| 115 | if (json == null || !json.isObject()) { |
| 116 | return null; |
| 117 | } |
| 118 | |
Jian Li | 58b3398 | 2020-07-01 19:07:02 +0900 | [diff] [blame] | 119 | String clusterName = json.get(CLUSTER_NAME).asText(); |
| 120 | |
| 121 | if (StringUtils.isEmpty(clusterName)) { |
| 122 | clusterName = DEFAULT_CLUSTER_NAME; |
| 123 | } |
| 124 | |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 125 | String hostname = nullIsIllegal(json.get(HOSTNAME).asText(), |
| 126 | HOSTNAME + MISSING_MESSAGE); |
| 127 | String type = nullIsIllegal(json.get(TYPE).asText(), |
| 128 | TYPE + MISSING_MESSAGE); |
| 129 | String mIp = nullIsIllegal(json.get(MANAGEMENT_IP).asText(), |
| 130 | MANAGEMENT_IP + MISSING_MESSAGE); |
| 131 | |
| 132 | DefaultK8sNode.Builder nodeBuilder = DefaultK8sNode.builder() |
Jian Li | 58b3398 | 2020-07-01 19:07:02 +0900 | [diff] [blame] | 133 | .clusterName(clusterName) |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 134 | .hostname(hostname) |
| 135 | .type(K8sNode.Type.valueOf(type)) |
| 136 | .state(K8sNodeState.INIT) |
| 137 | .managementIp(IpAddress.valueOf(mIp)); |
| 138 | |
| 139 | if (json.get(DATA_IP) != null) { |
| 140 | nodeBuilder.dataIp(IpAddress.valueOf(json.get(DATA_IP).asText())); |
| 141 | } |
| 142 | |
Jian Li | 58b3398 | 2020-07-01 19:07:02 +0900 | [diff] [blame] | 143 | JsonNode segmentIdJson = json.get(SEGMENT_ID); |
| 144 | int segmentId = DEFAULT_SEGMENT_ID; |
| 145 | if (segmentIdJson != null) { |
| 146 | segmentId = segmentIdJson.asInt(); |
| 147 | } |
| 148 | nodeBuilder.segmentId(segmentId); |
| 149 | |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 150 | JsonNode intBridgeJson = json.get(INTEGRATION_BRIDGE); |
| 151 | if (intBridgeJson != null) { |
| 152 | nodeBuilder.intgBridge(DeviceId.deviceId(intBridgeJson.asText())); |
| 153 | } |
| 154 | |
Jian Li | bf562c2 | 2019-04-15 18:07:14 +0900 | [diff] [blame] | 155 | JsonNode extBridgeJson = json.get(EXTERNAL_BRIDGE); |
| 156 | if (extBridgeJson != null) { |
| 157 | nodeBuilder.extBridge(DeviceId.deviceId(extBridgeJson.asText())); |
| 158 | } |
| 159 | |
Jian Li | 121ddfe | 2019-08-27 02:07:05 +0900 | [diff] [blame] | 160 | JsonNode localBridgeJson = json.get(LOCAL_BRIDGE); |
| 161 | if (localBridgeJson != null) { |
| 162 | nodeBuilder.localBridge(DeviceId.deviceId(localBridgeJson.asText())); |
| 163 | } |
| 164 | |
Jian Li | 58b3398 | 2020-07-01 19:07:02 +0900 | [diff] [blame] | 165 | JsonNode tunBridgeJson = json.get(TUNNEL_BRIDGE); |
| 166 | if (tunBridgeJson != null) { |
| 167 | nodeBuilder.tunBridge(DeviceId.deviceId(tunBridgeJson.asText())); |
| 168 | } |
| 169 | |
Jian Li | 7709eb4 | 2019-05-08 15:58:04 +0900 | [diff] [blame] | 170 | JsonNode extIntfJson = json.get(EXTERNAL_INTF); |
| 171 | if (extIntfJson != null) { |
| 172 | nodeBuilder.extIntf(extIntfJson.asText()); |
| 173 | } |
| 174 | |
| 175 | JsonNode extBridgeIpJson = json.get(EXTERNAL_BRIDGE_IP); |
| 176 | if (extBridgeIpJson != null) { |
| 177 | nodeBuilder.extBridgeIp(IpAddress.valueOf(extBridgeIpJson.asText())); |
| 178 | } |
| 179 | |
| 180 | JsonNode extGatewayIpJson = json.get(EXTERNAL_GATEWAY_IP); |
| 181 | if (extGatewayIpJson != null) { |
| 182 | nodeBuilder.extGatewayIp(IpAddress.valueOf(extGatewayIpJson.asText())); |
| 183 | } |
| 184 | |
Jian Li | b121844 | 2020-09-03 13:12:14 +0900 | [diff] [blame] | 185 | JsonNode extGatewayMacJson = json.get(EXTERNAL_GATEWAY_MAC); |
| 186 | if (extGatewayMacJson != null) { |
| 187 | nodeBuilder.extGatewayMac(MacAddress.valueOf(extGatewayMacJson.asText())); |
| 188 | } |
| 189 | |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 190 | log.trace("node is {}", nodeBuilder.build().toString()); |
| 191 | |
| 192 | return nodeBuilder.build(); |
| 193 | } |
| 194 | } |