Jian Li | 810f58c | 2021-02-27 01:10:50 +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 com.google.common.collect.ImmutableMap; |
| 22 | import org.onlab.packet.IpAddress; |
| 23 | import org.onlab.packet.MacAddress; |
| 24 | import org.onosproject.codec.CodecContext; |
| 25 | import org.onosproject.codec.JsonCodec; |
| 26 | import org.onosproject.kubevirtnetworking.api.DefaultKubevirtRouter; |
| 27 | import org.onosproject.kubevirtnetworking.api.KubevirtPeerRouter; |
| 28 | import org.onosproject.kubevirtnetworking.api.KubevirtRouter; |
| 29 | import org.slf4j.Logger; |
| 30 | |
| 31 | import java.util.HashSet; |
| 32 | import java.util.Map; |
| 33 | import java.util.Set; |
| 34 | |
| 35 | import static com.google.common.base.Preconditions.checkNotNull; |
| 36 | import static org.onlab.util.Tools.nullIsIllegal; |
| 37 | import static org.slf4j.LoggerFactory.getLogger; |
| 38 | |
| 39 | /** |
| 40 | * Kubevirt router codec used for serializing and de-serializing JSON string. |
| 41 | */ |
| 42 | public final class KubevirtRouterCodec extends JsonCodec<KubevirtRouter> { |
| 43 | |
| 44 | private final Logger log = getLogger(getClass()); |
| 45 | |
| 46 | private static final String NAME = "name"; |
| 47 | private static final String DESCRIPTION = "description"; |
| 48 | private static final String ENABLE_SNAT = "enableSnat"; |
| 49 | private static final String INTERNAL = "internal"; |
| 50 | private static final String EXTERNAL = "external"; |
| 51 | private static final String PEER_ROUTER = "peerRouter"; |
| 52 | private static final String IP_ADDRESS = "ip"; |
| 53 | private static final String MAC_ADDRESS = "mac"; |
| 54 | private static final String NETWORK = "network"; |
Daniel Park | 2884b23 | 2021-03-04 18:58:47 +0900 | [diff] [blame] | 55 | private static final String GATEWAY = "gateway"; |
Jian Li | 810f58c | 2021-02-27 01:10:50 +0900 | [diff] [blame] | 56 | |
| 57 | private static final String MISSING_MESSAGE = " is required in KubevirtRouter"; |
| 58 | |
| 59 | @Override |
| 60 | public ObjectNode encode(KubevirtRouter router, CodecContext context) { |
Jian Li | 073f1ba | 2021-02-28 03:50:15 +0900 | [diff] [blame] | 61 | checkNotNull(router, "Kubevirt router cannot be null"); |
Jian Li | 810f58c | 2021-02-27 01:10:50 +0900 | [diff] [blame] | 62 | |
| 63 | ObjectNode result = context.mapper().createObjectNode() |
| 64 | .put(NAME, router.name()) |
Jian Li | 9756cec | 2021-03-16 16:06:33 +0900 | [diff] [blame] | 65 | .put(ENABLE_SNAT, router.enableSnat()) |
| 66 | .put(MAC_ADDRESS, router.mac().toString()); |
Jian Li | 810f58c | 2021-02-27 01:10:50 +0900 | [diff] [blame] | 67 | |
| 68 | if (router.description() != null) { |
| 69 | result.put(DESCRIPTION, router.description()); |
| 70 | } |
| 71 | |
| 72 | if (router.internal() != null && !router.internal().isEmpty()) { |
| 73 | ArrayNode internal = context.mapper().createArrayNode(); |
| 74 | router.internal().forEach(internal::add); |
| 75 | |
| 76 | result.set(INTERNAL, internal); |
| 77 | } |
| 78 | |
| 79 | if (router.external() != null && !router.external().isEmpty()) { |
| 80 | ArrayNode external = context.mapper().createArrayNode(); |
| 81 | router.external().forEach((k, v) -> { |
| 82 | ObjectNode item = context.mapper().createObjectNode(); |
| 83 | item.put(IP_ADDRESS, k); |
| 84 | item.put(NETWORK, v); |
| 85 | external.add(item); |
| 86 | }); |
| 87 | result.set(EXTERNAL, external); |
| 88 | } |
| 89 | |
| 90 | if (router.peerRouter() != null) { |
| 91 | ObjectNode peerRouter = context.mapper().createObjectNode(); |
| 92 | peerRouter.put(IP_ADDRESS, router.peerRouter().ipAddress().toString()); |
| 93 | |
| 94 | if (router.peerRouter().macAddress() != null) { |
| 95 | peerRouter.put(MAC_ADDRESS, router.peerRouter().macAddress().toString()); |
| 96 | } |
| 97 | |
| 98 | result.set(PEER_ROUTER, peerRouter); |
| 99 | } |
| 100 | |
Daniel Park | 2884b23 | 2021-03-04 18:58:47 +0900 | [diff] [blame] | 101 | if (router.electedGateway() != null) { |
| 102 | result.put(GATEWAY, router.electedGateway()); |
| 103 | } |
| 104 | |
Jian Li | 810f58c | 2021-02-27 01:10:50 +0900 | [diff] [blame] | 105 | return result; |
| 106 | } |
| 107 | |
| 108 | @Override |
| 109 | public KubevirtRouter decode(ObjectNode json, CodecContext context) { |
| 110 | if (json == null || !json.isObject()) { |
| 111 | return null; |
| 112 | } |
| 113 | |
| 114 | String name = nullIsIllegal(json.get(NAME).asText(), |
| 115 | NAME + MISSING_MESSAGE); |
| 116 | |
Jian Li | 9756cec | 2021-03-16 16:06:33 +0900 | [diff] [blame] | 117 | String vrouterMac = nullIsIllegal(json.get(MAC_ADDRESS).asText(), |
| 118 | MAC_ADDRESS + MISSING_MESSAGE); |
| 119 | |
Jian Li | 810f58c | 2021-02-27 01:10:50 +0900 | [diff] [blame] | 120 | KubevirtRouter.Builder builder = DefaultKubevirtRouter.builder() |
Jian Li | 9756cec | 2021-03-16 16:06:33 +0900 | [diff] [blame] | 121 | .name(name) |
| 122 | .mac(MacAddress.valueOf(vrouterMac)); |
Jian Li | 810f58c | 2021-02-27 01:10:50 +0900 | [diff] [blame] | 123 | |
| 124 | JsonNode descriptionJson = json.get(DESCRIPTION); |
| 125 | if (descriptionJson != null) { |
| 126 | builder.description(descriptionJson.asText()); |
| 127 | } |
| 128 | |
| 129 | JsonNode enableSnatJson = json.get(ENABLE_SNAT); |
| 130 | if (enableSnatJson != null) { |
| 131 | builder.enableSnat(enableSnatJson.asBoolean()); |
| 132 | } |
Daniel Park | 2884b23 | 2021-03-04 18:58:47 +0900 | [diff] [blame] | 133 | JsonNode electedGwJson = json.get(GATEWAY); |
| 134 | if (electedGwJson != null) { |
| 135 | builder.electedGateway(electedGwJson.asText()); |
| 136 | } |
Jian Li | 810f58c | 2021-02-27 01:10:50 +0900 | [diff] [blame] | 137 | |
| 138 | ArrayNode internalJson = (ArrayNode) json.get(INTERNAL); |
| 139 | Set<String> internal = new HashSet<>(); |
| 140 | if (internalJson != null) { |
| 141 | for (int i = 0; i < internalJson.size(); i++) { |
| 142 | internal.add(internalJson.get(i).asText()); |
| 143 | } |
| 144 | builder.internal(internal); |
| 145 | } |
| 146 | |
| 147 | ObjectNode externalJson = (ObjectNode) json.get(EXTERNAL); |
| 148 | if (externalJson != null) { |
| 149 | Map<String, String> external = ImmutableMap.of( |
| 150 | externalJson.get(IP_ADDRESS).asText(), |
| 151 | externalJson.get(NETWORK).asText()); |
| 152 | builder.external(external); |
| 153 | } |
| 154 | |
| 155 | ObjectNode peerRouterJson = (ObjectNode) json.get(PEER_ROUTER); |
| 156 | if (peerRouterJson != null) { |
| 157 | JsonNode ipJson = peerRouterJson.get(IP_ADDRESS); |
| 158 | JsonNode macJson = peerRouterJson.get(MAC_ADDRESS); |
| 159 | |
| 160 | if (ipJson != null && macJson != null) { |
| 161 | IpAddress ip = IpAddress.valueOf(ipJson.asText()); |
| 162 | MacAddress mac = MacAddress.valueOf(macJson.asText()); |
| 163 | KubevirtPeerRouter peer = new KubevirtPeerRouter(ip, mac); |
| 164 | builder.peerRouter(peer); |
| 165 | } |
| 166 | |
| 167 | // if mac address is not specified, we will not add mac address to peer router |
| 168 | if (ipJson != null && macJson == null) { |
| 169 | IpAddress ip = IpAddress.valueOf(ipJson.asText()); |
| 170 | KubevirtPeerRouter peer = new KubevirtPeerRouter(ip, null); |
| 171 | builder.peerRouter(peer); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | return builder.build(); |
| 176 | } |
| 177 | } |