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 | 1a2eb5d | 2019-08-27 02:07:05 +0900 | [diff] [blame] | 46 | private static final String LOCAL_BRIDGE = "localBridge"; |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 47 | private static final String STATE = "state"; |
Jian Li | 0c63272 | 2019-05-08 15:58:04 +0900 | [diff] [blame] | 48 | private static final String EXTERNAL_INTF = "externalInterface"; |
| 49 | private static final String EXTERNAL_BRIDGE_IP = "externalBridgeIp"; |
| 50 | private static final String EXTERNAL_GATEWAY_IP = "externalGatewayIp"; |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 51 | |
| 52 | private static final String MISSING_MESSAGE = " is required in K8sNode"; |
| 53 | |
| 54 | @Override |
| 55 | public ObjectNode encode(K8sNode node, CodecContext context) { |
| 56 | checkNotNull(node, "Kubernetes node cannot be null"); |
| 57 | |
| 58 | ObjectNode result = context.mapper().createObjectNode() |
| 59 | .put(HOSTNAME, node.hostname()) |
| 60 | .put(TYPE, node.type().name()) |
| 61 | .put(STATE, node.state().name()) |
| 62 | .put(MANAGEMENT_IP, node.managementIp().toString()); |
| 63 | |
| 64 | if (node.intgBridge() != null) { |
| 65 | result.put(INTEGRATION_BRIDGE, node.intgBridge().toString()); |
| 66 | } |
| 67 | |
Jian Li | bf562c2 | 2019-04-15 18:07:14 +0900 | [diff] [blame] | 68 | if (node.extBridge() != null) { |
| 69 | result.put(EXTERNAL_BRIDGE, node.extBridge().toString()); |
| 70 | } |
| 71 | |
Jian Li | 1a2eb5d | 2019-08-27 02:07:05 +0900 | [diff] [blame] | 72 | if (node.localBridge() != null) { |
| 73 | result.put(LOCAL_BRIDGE, node.localBridge().toString()); |
| 74 | } |
| 75 | |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 76 | if (node.dataIp() != null) { |
| 77 | result.put(DATA_IP, node.dataIp().toString()); |
| 78 | } |
| 79 | |
Jian Li | 0c63272 | 2019-05-08 15:58:04 +0900 | [diff] [blame] | 80 | if (node.extIntf() != null) { |
| 81 | result.put(EXTERNAL_INTF, node.extIntf()); |
| 82 | } |
| 83 | |
| 84 | if (node.extBridgeIp() != null) { |
| 85 | result.put(EXTERNAL_BRIDGE_IP, node.extBridgeIp().toString()); |
| 86 | } |
| 87 | |
| 88 | if (node.extGatewayIp() != null) { |
| 89 | result.put(EXTERNAL_GATEWAY_IP, node.extGatewayIp().toString()); |
| 90 | } |
| 91 | |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 92 | return result; |
| 93 | } |
| 94 | |
| 95 | @Override |
| 96 | public K8sNode decode(ObjectNode json, CodecContext context) { |
| 97 | if (json == null || !json.isObject()) { |
| 98 | return null; |
| 99 | } |
| 100 | |
| 101 | String hostname = nullIsIllegal(json.get(HOSTNAME).asText(), |
| 102 | HOSTNAME + MISSING_MESSAGE); |
| 103 | String type = nullIsIllegal(json.get(TYPE).asText(), |
| 104 | TYPE + MISSING_MESSAGE); |
| 105 | String mIp = nullIsIllegal(json.get(MANAGEMENT_IP).asText(), |
| 106 | MANAGEMENT_IP + MISSING_MESSAGE); |
| 107 | |
| 108 | DefaultK8sNode.Builder nodeBuilder = DefaultK8sNode.builder() |
| 109 | .hostname(hostname) |
| 110 | .type(K8sNode.Type.valueOf(type)) |
| 111 | .state(K8sNodeState.INIT) |
| 112 | .managementIp(IpAddress.valueOf(mIp)); |
| 113 | |
| 114 | if (json.get(DATA_IP) != null) { |
| 115 | nodeBuilder.dataIp(IpAddress.valueOf(json.get(DATA_IP).asText())); |
| 116 | } |
| 117 | |
| 118 | JsonNode intBridgeJson = json.get(INTEGRATION_BRIDGE); |
| 119 | if (intBridgeJson != null) { |
| 120 | nodeBuilder.intgBridge(DeviceId.deviceId(intBridgeJson.asText())); |
| 121 | } |
| 122 | |
Jian Li | bf562c2 | 2019-04-15 18:07:14 +0900 | [diff] [blame] | 123 | JsonNode extBridgeJson = json.get(EXTERNAL_BRIDGE); |
| 124 | if (extBridgeJson != null) { |
| 125 | nodeBuilder.extBridge(DeviceId.deviceId(extBridgeJson.asText())); |
| 126 | } |
| 127 | |
Jian Li | 1a2eb5d | 2019-08-27 02:07:05 +0900 | [diff] [blame] | 128 | JsonNode localBridgeJson = json.get(LOCAL_BRIDGE); |
| 129 | if (localBridgeJson != null) { |
| 130 | nodeBuilder.localBridge(DeviceId.deviceId(localBridgeJson.asText())); |
| 131 | } |
| 132 | |
Jian Li | 0c63272 | 2019-05-08 15:58:04 +0900 | [diff] [blame] | 133 | JsonNode extIntfJson = json.get(EXTERNAL_INTF); |
| 134 | if (extIntfJson != null) { |
| 135 | nodeBuilder.extIntf(extIntfJson.asText()); |
| 136 | } |
| 137 | |
| 138 | JsonNode extBridgeIpJson = json.get(EXTERNAL_BRIDGE_IP); |
| 139 | if (extBridgeIpJson != null) { |
| 140 | nodeBuilder.extBridgeIp(IpAddress.valueOf(extBridgeIpJson.asText())); |
| 141 | } |
| 142 | |
| 143 | JsonNode extGatewayIpJson = json.get(EXTERNAL_GATEWAY_IP); |
| 144 | if (extGatewayIpJson != null) { |
| 145 | nodeBuilder.extGatewayIp(IpAddress.valueOf(extGatewayIpJson.asText())); |
| 146 | } |
| 147 | |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 148 | log.trace("node is {}", nodeBuilder.build().toString()); |
| 149 | |
| 150 | return nodeBuilder.build(); |
| 151 | } |
| 152 | } |