Jian Li | fc0b890 | 2021-01-12 16:34:49 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2021-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.kubevirtnetworking.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.kubevirtnetworking.api.DefaultKubevirtNetwork; |
| 25 | import org.onosproject.kubevirtnetworking.api.KubevirtHostRoute; |
| 26 | import org.onosproject.kubevirtnetworking.api.KubevirtIpPool; |
| 27 | import org.onosproject.kubevirtnetworking.api.KubevirtNetwork; |
| 28 | import org.slf4j.Logger; |
| 29 | |
| 30 | import java.util.HashSet; |
| 31 | import java.util.Set; |
| 32 | import java.util.stream.IntStream; |
| 33 | |
| 34 | import static com.google.common.base.Preconditions.checkNotNull; |
| 35 | import static org.onlab.util.Tools.nullIsIllegal; |
| 36 | import static org.slf4j.LoggerFactory.getLogger; |
| 37 | |
| 38 | /** |
| 39 | * Kubevirt network codec used for serializing and de-serializing JSON string. |
| 40 | */ |
| 41 | public final class KubevirtNetworkCodec extends JsonCodec<KubevirtNetwork> { |
| 42 | |
| 43 | private final Logger log = getLogger(getClass()); |
| 44 | |
| 45 | private static final String NETWORK_ID = "networkId"; |
| 46 | private static final String TYPE = "type"; |
| 47 | private static final String NAME = "name"; |
| 48 | private static final String MTU = "mtu"; |
| 49 | private static final String SEGMENT_ID = "segmentId"; |
| 50 | private static final String GATEWAY_IP = "gatewayIp"; |
Jian Li | e2abe81 | 2021-08-12 18:03:30 +0900 | [diff] [blame] | 51 | private static final String DEFAULT_ROUTE = "defaultRoute"; |
Jian Li | fc0b890 | 2021-01-12 16:34:49 +0900 | [diff] [blame] | 52 | private static final String CIDR = "cidr"; |
| 53 | private static final String HOST_ROUTES = "hostRoutes"; |
| 54 | private static final String IP_POOL = "ipPool"; |
Jian Li | 4c35a26 | 2021-02-01 20:36:45 +0900 | [diff] [blame] | 55 | private static final String DNSES = "dnses"; |
Jian Li | fc0b890 | 2021-01-12 16:34:49 +0900 | [diff] [blame] | 56 | |
| 57 | private static final String MISSING_MESSAGE = " is required in KubevirtNetwork"; |
| 58 | |
| 59 | @Override |
| 60 | public ObjectNode encode(KubevirtNetwork network, CodecContext context) { |
| 61 | checkNotNull(network, "Kubevirt network cannot be null"); |
| 62 | |
| 63 | ObjectNode result = context.mapper().createObjectNode() |
| 64 | .put(NETWORK_ID, network.networkId()) |
| 65 | .put(TYPE, network.type().name()) |
| 66 | .put(NAME, network.name()) |
| 67 | .put(MTU, network.mtu()) |
| 68 | .put(GATEWAY_IP, network.gatewayIp().toString()) |
Jian Li | e2abe81 | 2021-08-12 18:03:30 +0900 | [diff] [blame] | 69 | .put(DEFAULT_ROUTE, network.defaultRoute()) |
Jian Li | fc0b890 | 2021-01-12 16:34:49 +0900 | [diff] [blame] | 70 | .put(CIDR, network.cidr()); |
| 71 | |
| 72 | if (network.segmentId() != null) { |
| 73 | result.put(SEGMENT_ID, network.segmentId()); |
| 74 | } |
| 75 | |
| 76 | if (network.hostRoutes() != null && !network.hostRoutes().isEmpty()) { |
| 77 | ArrayNode hostRoutes = context.mapper().createArrayNode(); |
| 78 | network.hostRoutes().forEach(hostRoute -> { |
| 79 | ObjectNode hostRouteJson = |
| 80 | context.codec(KubevirtHostRoute.class).encode(hostRoute, context); |
| 81 | hostRoutes.add(hostRouteJson); |
| 82 | }); |
| 83 | result.set(HOST_ROUTES, hostRoutes); |
| 84 | } |
| 85 | |
| 86 | if (network.ipPool() != null) { |
| 87 | ObjectNode ipPoolJson = context.codec(KubevirtIpPool.class).encode(network.ipPool(), context); |
| 88 | result.set(IP_POOL, ipPoolJson); |
| 89 | } |
| 90 | |
Jian Li | 4c35a26 | 2021-02-01 20:36:45 +0900 | [diff] [blame] | 91 | if (network.dnses() != null && !network.dnses().isEmpty()) { |
| 92 | ArrayNode dnses = context.mapper().createArrayNode(); |
| 93 | network.dnses().forEach(dns -> { |
| 94 | dnses.add(dns.toString()); |
| 95 | }); |
| 96 | result.set(DNSES, dnses); |
| 97 | } |
| 98 | |
Jian Li | fc0b890 | 2021-01-12 16:34:49 +0900 | [diff] [blame] | 99 | return result; |
| 100 | } |
| 101 | |
| 102 | @Override |
| 103 | public KubevirtNetwork decode(ObjectNode json, CodecContext context) { |
| 104 | if (json == null || !json.isObject()) { |
| 105 | return null; |
| 106 | } |
| 107 | |
| 108 | String networkId = nullIsIllegal(json.get(NETWORK_ID).asText(), |
| 109 | NETWORK_ID + MISSING_MESSAGE); |
| 110 | String type = nullIsIllegal(json.get(TYPE).asText(), |
| 111 | TYPE + MISSING_MESSAGE); |
| 112 | String name = nullIsIllegal(json.get(NAME).asText(), |
| 113 | NAME + MISSING_MESSAGE); |
| 114 | Integer mtu = nullIsIllegal(json.get(MTU).asInt(), |
| 115 | MTU + MISSING_MESSAGE); |
| 116 | String gatewayIp = nullIsIllegal(json.get(GATEWAY_IP).asText(), |
| 117 | GATEWAY_IP + MISSING_MESSAGE); |
Jian Li | e2abe81 | 2021-08-12 18:03:30 +0900 | [diff] [blame] | 118 | boolean defaultRoute = nullIsIllegal(json.get(DEFAULT_ROUTE).asBoolean(), |
| 119 | DEFAULT_ROUTE + MISSING_MESSAGE); |
Jian Li | fc0b890 | 2021-01-12 16:34:49 +0900 | [diff] [blame] | 120 | String cidr = nullIsIllegal(json.get(CIDR).asText(), |
| 121 | CIDR + MISSING_MESSAGE); |
| 122 | |
| 123 | KubevirtNetwork.Builder networkBuilder = DefaultKubevirtNetwork.builder() |
| 124 | .networkId(networkId) |
| 125 | .type(KubevirtNetwork.Type.valueOf(type)) |
| 126 | .name(name) |
| 127 | .mtu(mtu) |
| 128 | .gatewayIp(IpAddress.valueOf(gatewayIp)) |
Jian Li | e2abe81 | 2021-08-12 18:03:30 +0900 | [diff] [blame] | 129 | .defaultRoute(defaultRoute) |
Jian Li | fc0b890 | 2021-01-12 16:34:49 +0900 | [diff] [blame] | 130 | .cidr(cidr); |
| 131 | |
Jian Li | 3ba5c58 | 2021-01-14 11:30:36 +0900 | [diff] [blame] | 132 | if (!type.equals(KubevirtNetwork.Type.FLAT.name())) { |
| 133 | JsonNode segmentIdJson = json.get(SEGMENT_ID); |
| 134 | if (segmentIdJson != null) { |
| 135 | networkBuilder.segmentId(segmentIdJson.asText()); |
| 136 | } |
| 137 | } |
| 138 | |
Jian Li | fc0b890 | 2021-01-12 16:34:49 +0900 | [diff] [blame] | 139 | JsonNode ipPoolJson = json.get(IP_POOL); |
| 140 | if (ipPoolJson != null) { |
| 141 | final JsonCodec<KubevirtIpPool> |
| 142 | ipPoolCodec = context.codec(KubevirtIpPool.class); |
| 143 | networkBuilder.ipPool(ipPoolCodec.decode( |
| 144 | (ObjectNode) ipPoolJson.deepCopy(), context)); |
| 145 | } |
| 146 | |
| 147 | // parse host routes |
| 148 | Set<KubevirtHostRoute> hostRoutes = new HashSet<>(); |
| 149 | JsonNode hostRoutesJson = json.get(HOST_ROUTES); |
| 150 | if (hostRoutesJson != null) { |
| 151 | final JsonCodec<KubevirtHostRoute> |
| 152 | hostRouteCodec = context.codec(KubevirtHostRoute.class); |
| 153 | |
| 154 | IntStream.range(0, hostRoutesJson.size()).forEach(i -> { |
| 155 | ObjectNode routeJson = get(hostRoutesJson, i); |
| 156 | hostRoutes.add(hostRouteCodec.decode(routeJson, context)); |
| 157 | }); |
| 158 | } |
| 159 | networkBuilder.hostRoutes(hostRoutes); |
| 160 | |
Jian Li | 4c35a26 | 2021-02-01 20:36:45 +0900 | [diff] [blame] | 161 | // parse DNSes |
| 162 | Set<IpAddress> dnses = new HashSet<>(); |
| 163 | JsonNode dnsesJson = json.get(DNSES); |
| 164 | if (dnsesJson != null) { |
| 165 | for (int i = 0; i < dnsesJson.size(); i++) { |
| 166 | JsonNode dnsJson = dnsesJson.get(i); |
| 167 | if (dnsJson != null) { |
| 168 | dnses.add(IpAddress.valueOf(dnsJson.asText())); |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | networkBuilder.dnses(dnses); |
| 173 | |
Jian Li | fc0b890 | 2021-01-12 16:34:49 +0900 | [diff] [blame] | 174 | log.trace("Network is {}", networkBuilder.build().toString()); |
| 175 | |
| 176 | return networkBuilder.build(); |
| 177 | } |
| 178 | } |