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