Jian Li | cc01e45 | 2020-12-21 01:44:05 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2020-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.kubevirtnode.codec; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 20 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 21 | import org.onlab.packet.IpAddress; |
| 22 | import org.onosproject.codec.CodecContext; |
| 23 | import org.onosproject.codec.JsonCodec; |
| 24 | import org.onosproject.kubevirtnode.api.DefaultKubevirtNode; |
Daniel Park | 5ff76b7 | 2022-09-26 22:58:53 +0900 | [diff] [blame^] | 25 | import org.onosproject.kubevirtnode.api.KubernetesExternalLbInterface; |
Jian Li | cc01e45 | 2020-12-21 01:44:05 +0900 | [diff] [blame] | 26 | import org.onosproject.kubevirtnode.api.KubevirtNode; |
| 27 | import org.onosproject.kubevirtnode.api.KubevirtNodeState; |
| 28 | import org.onosproject.kubevirtnode.api.KubevirtPhyInterface; |
| 29 | import org.onosproject.net.DeviceId; |
| 30 | import org.slf4j.Logger; |
| 31 | |
| 32 | import java.util.ArrayList; |
| 33 | import java.util.List; |
| 34 | import java.util.stream.IntStream; |
| 35 | |
| 36 | import static com.google.common.base.Preconditions.checkNotNull; |
| 37 | import static org.onlab.util.Tools.nullIsIllegal; |
| 38 | import static org.onosproject.kubevirtnode.api.Constants.DATA_IP; |
| 39 | import static org.onosproject.kubevirtnode.api.Constants.HOST_NAME; |
| 40 | import static org.onosproject.kubevirtnode.api.Constants.MANAGEMENT_IP; |
| 41 | import static org.slf4j.LoggerFactory.getLogger; |
| 42 | |
| 43 | /** |
| 44 | * Kubevirt node codec used for serializing and de-serializing JSON string. |
| 45 | */ |
| 46 | public final class KubevirtNodeCodec extends JsonCodec<KubevirtNode> { |
| 47 | |
| 48 | private final Logger log = getLogger(getClass()); |
| 49 | |
| 50 | private static final String TYPE = "type"; |
| 51 | private static final String INTEGRATION_BRIDGE = "integrationBridge"; |
Jian Li | 4fe40e5 | 2021-01-06 03:29:58 +0900 | [diff] [blame] | 52 | private static final String TUNNEL_BRIDGE = "tunnelBridge"; |
Jian Li | cc01e45 | 2020-12-21 01:44:05 +0900 | [diff] [blame] | 53 | private static final String STATE = "state"; |
| 54 | private static final String PHYSICAL_INTERFACES = "phyIntfs"; |
Daniel Park | 515f5f3 | 2021-02-22 17:12:20 +0900 | [diff] [blame] | 55 | private static final String GATEWAY_BRIDGE_NAME = "gatewayBridgeName"; |
Daniel Park | 5ff76b7 | 2022-09-26 22:58:53 +0900 | [diff] [blame^] | 56 | private static final String KUBERNETES_EXTERNAL_LB_INTERFACE = "kubernetesExternalLbInterface"; |
Jian Li | cc01e45 | 2020-12-21 01:44:05 +0900 | [diff] [blame] | 57 | |
Daniel Park | 5ff76b7 | 2022-09-26 22:58:53 +0900 | [diff] [blame^] | 58 | private static final String MISSING_MESSAGE = " is required in KubevirtNode"; |
Jian Li | cc01e45 | 2020-12-21 01:44:05 +0900 | [diff] [blame] | 59 | |
| 60 | @Override |
| 61 | public ObjectNode encode(KubevirtNode node, CodecContext context) { |
| 62 | checkNotNull(node, "Kubevirt node cannot be null"); |
| 63 | |
| 64 | ObjectNode result = context.mapper().createObjectNode() |
| 65 | .put(HOST_NAME, node.hostname()) |
| 66 | .put(TYPE, node.type().name()) |
| 67 | .put(STATE, node.state().name()) |
| 68 | .put(MANAGEMENT_IP, node.managementIp().toString()); |
| 69 | |
| 70 | // serialize integration bridge config |
| 71 | if (node.intgBridge() != null) { |
| 72 | result.put(INTEGRATION_BRIDGE, node.intgBridge().toString()); |
| 73 | } |
| 74 | |
Jian Li | 4fe40e5 | 2021-01-06 03:29:58 +0900 | [diff] [blame] | 75 | // serialize tunnel bridge config |
| 76 | if (node.tunBridge() != null) { |
| 77 | result.put(TUNNEL_BRIDGE, node.tunBridge().toString()); |
| 78 | } |
| 79 | |
Jian Li | cc01e45 | 2020-12-21 01:44:05 +0900 | [diff] [blame] | 80 | // serialize data IP only if it presents |
| 81 | if (node.dataIp() != null) { |
| 82 | result.put(DATA_IP, node.dataIp().toString()); |
| 83 | } |
| 84 | |
| 85 | // serialize physical interfaces, it is valid only if any of physical interface presents |
| 86 | if (node.phyIntfs() != null && !node.phyIntfs().isEmpty()) { |
| 87 | ArrayNode phyIntfs = context.mapper().createArrayNode(); |
| 88 | node.phyIntfs().forEach(phyIntf -> { |
| 89 | ObjectNode phyIntfJson = |
| 90 | context.codec(KubevirtPhyInterface.class).encode(phyIntf, context); |
| 91 | phyIntfs.add(phyIntfJson); |
| 92 | }); |
| 93 | result.set(PHYSICAL_INTERFACES, phyIntfs); |
| 94 | } |
| 95 | |
Daniel Park | 515f5f3 | 2021-02-22 17:12:20 +0900 | [diff] [blame] | 96 | // serialize external bridge if exist |
| 97 | if (node.gatewayBridgeName() != null) { |
| 98 | result.put(GATEWAY_BRIDGE_NAME, node.gatewayBridgeName()); |
| 99 | } |
| 100 | |
Daniel Park | 5ff76b7 | 2022-09-26 22:58:53 +0900 | [diff] [blame^] | 101 | // serialize kubernetex external load balancer interface if exist |
| 102 | if (node.kubernetesExternalLbInterface() != null) { |
| 103 | ObjectNode elbIntfJson = context.codec(KubernetesExternalLbInterface.class) |
| 104 | .encode(node.kubernetesExternalLbInterface(), context); |
| 105 | result.put(KUBERNETES_EXTERNAL_LB_INTERFACE, elbIntfJson.toString()); |
Daniel Park | 734b553 | 2022-09-26 15:13:59 +0900 | [diff] [blame] | 106 | } |
| 107 | |
Jian Li | cc01e45 | 2020-12-21 01:44:05 +0900 | [diff] [blame] | 108 | return result; |
| 109 | } |
| 110 | |
| 111 | @Override |
| 112 | public KubevirtNode decode(ObjectNode json, CodecContext context) { |
| 113 | if (json == null || !json.isObject()) { |
| 114 | return null; |
| 115 | } |
| 116 | |
| 117 | String hostname = nullIsIllegal(json.get(HOST_NAME).asText(), |
| 118 | HOST_NAME + MISSING_MESSAGE); |
| 119 | String type = nullIsIllegal(json.get(TYPE).asText(), |
| 120 | TYPE + MISSING_MESSAGE); |
| 121 | String mIp = nullIsIllegal(json.get(MANAGEMENT_IP).asText(), |
| 122 | MANAGEMENT_IP + MISSING_MESSAGE); |
| 123 | |
| 124 | KubevirtNode.Builder nodeBuilder = DefaultKubevirtNode.builder() |
| 125 | .hostname(hostname) |
| 126 | .type(KubevirtNode.Type.valueOf(type)) |
| 127 | .state(KubevirtNodeState.INIT) |
| 128 | .managementIp(IpAddress.valueOf(mIp)); |
| 129 | |
| 130 | if (json.get(DATA_IP) != null) { |
| 131 | nodeBuilder.dataIp(IpAddress.valueOf(json.get(DATA_IP).asText())); |
| 132 | } |
| 133 | |
| 134 | JsonNode intBridgeJson = json.get(INTEGRATION_BRIDGE); |
| 135 | if (intBridgeJson != null) { |
| 136 | nodeBuilder.intgBridge(DeviceId.deviceId(intBridgeJson.asText())); |
| 137 | } |
| 138 | |
Jian Li | 4fe40e5 | 2021-01-06 03:29:58 +0900 | [diff] [blame] | 139 | JsonNode tunBridgeJson = json.get(TUNNEL_BRIDGE); |
| 140 | if (tunBridgeJson != null) { |
| 141 | nodeBuilder.tunBridge(DeviceId.deviceId(tunBridgeJson.asText())); |
| 142 | } |
| 143 | |
Jian Li | cc01e45 | 2020-12-21 01:44:05 +0900 | [diff] [blame] | 144 | // parse physical interfaces |
| 145 | List<KubevirtPhyInterface> phyIntfs = new ArrayList<>(); |
| 146 | JsonNode phyIntfsJson = json.get(PHYSICAL_INTERFACES); |
| 147 | if (phyIntfsJson != null) { |
| 148 | final JsonCodec<KubevirtPhyInterface> |
| 149 | phyIntfCodec = context.codec(KubevirtPhyInterface.class); |
| 150 | |
| 151 | IntStream.range(0, phyIntfsJson.size()).forEach(i -> { |
| 152 | ObjectNode intfJson = get(phyIntfsJson, i); |
| 153 | phyIntfs.add(phyIntfCodec.decode(intfJson, context)); |
| 154 | }); |
| 155 | } |
| 156 | nodeBuilder.phyIntfs(phyIntfs); |
| 157 | |
Daniel Park | 515f5f3 | 2021-02-22 17:12:20 +0900 | [diff] [blame] | 158 | JsonNode externalBridgeJson = json.get(GATEWAY_BRIDGE_NAME); |
| 159 | if (externalBridgeJson != null) { |
| 160 | nodeBuilder.gatewayBridgeName(externalBridgeJson.asText()); |
| 161 | } |
| 162 | |
Daniel Park | 5ff76b7 | 2022-09-26 22:58:53 +0900 | [diff] [blame^] | 163 | JsonNode elbIntfJson = json.get(KUBERNETES_EXTERNAL_LB_INTERFACE); |
Daniel Park | 734b553 | 2022-09-26 15:13:59 +0900 | [diff] [blame] | 164 | |
Daniel Park | 5ff76b7 | 2022-09-26 22:58:53 +0900 | [diff] [blame^] | 165 | if (elbIntfJson != null) { |
| 166 | final JsonCodec<KubernetesExternalLbInterface> |
| 167 | kubernetesExternalLbInterfaceCodecJsonCodec = context.codec(KubernetesExternalLbInterface.class); |
| 168 | ObjectNode elbIntfObjNode = elbIntfJson.deepCopy(); |
Daniel Park | 734b553 | 2022-09-26 15:13:59 +0900 | [diff] [blame] | 169 | |
Daniel Park | 5ff76b7 | 2022-09-26 22:58:53 +0900 | [diff] [blame^] | 170 | nodeBuilder.kubernetesExternalLbInterface( |
| 171 | kubernetesExternalLbInterfaceCodecJsonCodec.decode(elbIntfObjNode, context)); |
Daniel Park | 734b553 | 2022-09-26 15:13:59 +0900 | [diff] [blame] | 172 | |
Daniel Park | 734b553 | 2022-09-26 15:13:59 +0900 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | log.trace("node is {}", nodeBuilder.build()); |
Jian Li | cc01e45 | 2020-12-21 01:44:05 +0900 | [diff] [blame] | 176 | |
| 177 | return nodeBuilder.build(); |
| 178 | } |
| 179 | } |