Jian Li | feb8480 | 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"; |
| 51 | private static final String CIDR = "cidr"; |
| 52 | private static final String HOST_ROUTES = "hostRoutes"; |
| 53 | private static final String IP_POOL = "ipPool"; |
| 54 | |
| 55 | private static final String MISSING_MESSAGE = " is required in KubevirtNetwork"; |
| 56 | |
| 57 | @Override |
| 58 | public ObjectNode encode(KubevirtNetwork network, CodecContext context) { |
| 59 | checkNotNull(network, "Kubevirt network cannot be null"); |
| 60 | |
| 61 | ObjectNode result = context.mapper().createObjectNode() |
| 62 | .put(NETWORK_ID, network.networkId()) |
| 63 | .put(TYPE, network.type().name()) |
| 64 | .put(NAME, network.name()) |
| 65 | .put(MTU, network.mtu()) |
| 66 | .put(GATEWAY_IP, network.gatewayIp().toString()) |
| 67 | .put(CIDR, network.cidr()); |
| 68 | |
| 69 | if (network.segmentId() != null) { |
| 70 | result.put(SEGMENT_ID, network.segmentId()); |
| 71 | } |
| 72 | |
| 73 | if (network.hostRoutes() != null && !network.hostRoutes().isEmpty()) { |
| 74 | ArrayNode hostRoutes = context.mapper().createArrayNode(); |
| 75 | network.hostRoutes().forEach(hostRoute -> { |
| 76 | ObjectNode hostRouteJson = |
| 77 | context.codec(KubevirtHostRoute.class).encode(hostRoute, context); |
| 78 | hostRoutes.add(hostRouteJson); |
| 79 | }); |
| 80 | result.set(HOST_ROUTES, hostRoutes); |
| 81 | } |
| 82 | |
| 83 | if (network.ipPool() != null) { |
| 84 | ObjectNode ipPoolJson = context.codec(KubevirtIpPool.class).encode(network.ipPool(), context); |
| 85 | result.set(IP_POOL, ipPoolJson); |
| 86 | } |
| 87 | |
| 88 | return result; |
| 89 | } |
| 90 | |
| 91 | @Override |
| 92 | public KubevirtNetwork decode(ObjectNode json, CodecContext context) { |
| 93 | if (json == null || !json.isObject()) { |
| 94 | return null; |
| 95 | } |
| 96 | |
| 97 | String networkId = nullIsIllegal(json.get(NETWORK_ID).asText(), |
| 98 | NETWORK_ID + MISSING_MESSAGE); |
| 99 | String type = nullIsIllegal(json.get(TYPE).asText(), |
| 100 | TYPE + MISSING_MESSAGE); |
| 101 | String name = nullIsIllegal(json.get(NAME).asText(), |
| 102 | NAME + MISSING_MESSAGE); |
| 103 | Integer mtu = nullIsIllegal(json.get(MTU).asInt(), |
| 104 | MTU + MISSING_MESSAGE); |
| 105 | String gatewayIp = nullIsIllegal(json.get(GATEWAY_IP).asText(), |
| 106 | GATEWAY_IP + MISSING_MESSAGE); |
| 107 | String cidr = nullIsIllegal(json.get(CIDR).asText(), |
| 108 | CIDR + MISSING_MESSAGE); |
| 109 | |
| 110 | KubevirtNetwork.Builder networkBuilder = DefaultKubevirtNetwork.builder() |
| 111 | .networkId(networkId) |
| 112 | .type(KubevirtNetwork.Type.valueOf(type)) |
| 113 | .name(name) |
| 114 | .mtu(mtu) |
| 115 | .gatewayIp(IpAddress.valueOf(gatewayIp)) |
| 116 | .cidr(cidr); |
| 117 | |
| 118 | JsonNode ipPoolJson = json.get(IP_POOL); |
| 119 | if (ipPoolJson != null) { |
| 120 | final JsonCodec<KubevirtIpPool> |
| 121 | ipPoolCodec = context.codec(KubevirtIpPool.class); |
| 122 | networkBuilder.ipPool(ipPoolCodec.decode( |
| 123 | (ObjectNode) ipPoolJson.deepCopy(), context)); |
| 124 | } |
| 125 | |
| 126 | // parse host routes |
| 127 | Set<KubevirtHostRoute> hostRoutes = new HashSet<>(); |
| 128 | JsonNode hostRoutesJson = json.get(HOST_ROUTES); |
| 129 | if (hostRoutesJson != null) { |
| 130 | final JsonCodec<KubevirtHostRoute> |
| 131 | hostRouteCodec = context.codec(KubevirtHostRoute.class); |
| 132 | |
| 133 | IntStream.range(0, hostRoutesJson.size()).forEach(i -> { |
| 134 | ObjectNode routeJson = get(hostRoutesJson, i); |
| 135 | hostRoutes.add(hostRouteCodec.decode(routeJson, context)); |
| 136 | }); |
| 137 | } |
| 138 | networkBuilder.hostRoutes(hostRoutes); |
| 139 | |
| 140 | log.trace("Network is {}", networkBuilder.build().toString()); |
| 141 | |
| 142 | return networkBuilder.build(); |
| 143 | } |
| 144 | } |