Jian Li | 073f1ba | 2021-02-28 03:50:15 +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.ObjectNode; |
| 20 | import org.onlab.packet.IpAddress; |
| 21 | import org.onosproject.codec.CodecContext; |
| 22 | import org.onosproject.codec.JsonCodec; |
| 23 | import org.onosproject.kubevirtnetworking.api.DefaultKubevirtFloatingIp; |
| 24 | import org.onosproject.kubevirtnetworking.api.KubevirtFloatingIp; |
| 25 | import org.slf4j.Logger; |
| 26 | |
| 27 | import static com.google.common.base.Preconditions.checkNotNull; |
| 28 | import static org.onlab.util.Tools.nullIsIllegal; |
| 29 | import static org.slf4j.LoggerFactory.getLogger; |
| 30 | |
| 31 | /** |
| 32 | * Kubevirt floating IP codec used for serializing and de-serializing JSON string. |
| 33 | */ |
| 34 | public final class KubevirtFloatingIpCodec extends JsonCodec<KubevirtFloatingIp> { |
| 35 | |
| 36 | private final Logger log = getLogger(getClass()); |
| 37 | |
| 38 | private static final String ID = "id"; |
| 39 | private static final String ROUTER_NAME = "routerName"; |
| 40 | private static final String POD_NAME = "podName"; |
Jian Li | b636f70 | 2021-03-03 14:46:50 +0900 | [diff] [blame] | 41 | private static final String NETWORK_NAME = "networkName"; |
Jian Li | 073f1ba | 2021-02-28 03:50:15 +0900 | [diff] [blame] | 42 | private static final String FLOATING_IP = "floatingIp"; |
| 43 | private static final String FIXED_IP = "fixedIp"; |
| 44 | |
| 45 | private static final String MISSING_MESSAGE = " is required in KubevirtFloatingIp"; |
| 46 | |
| 47 | @Override |
| 48 | public ObjectNode encode(KubevirtFloatingIp fip, CodecContext context) { |
| 49 | checkNotNull(fip, "Kubevirt floating IP cannot be null"); |
| 50 | |
| 51 | ObjectNode result = context.mapper().createObjectNode() |
| 52 | .put(ID, fip.id()) |
| 53 | .put(ROUTER_NAME, fip.routerName()) |
Jian Li | b636f70 | 2021-03-03 14:46:50 +0900 | [diff] [blame] | 54 | .put(NETWORK_NAME, fip.networkName()) |
Jian Li | 073f1ba | 2021-02-28 03:50:15 +0900 | [diff] [blame] | 55 | .put(FLOATING_IP, fip.floatingIp().toString()); |
| 56 | |
| 57 | if (fip.podName() != null) { |
| 58 | result.put(POD_NAME, fip.podName()); |
| 59 | } |
| 60 | |
| 61 | if (fip.fixedIp() != null) { |
| 62 | result.put(FIXED_IP, fip.fixedIp().toString()); |
| 63 | } |
| 64 | |
| 65 | return result; |
| 66 | } |
| 67 | |
| 68 | @Override |
| 69 | public KubevirtFloatingIp decode(ObjectNode json, CodecContext context) { |
| 70 | if (json == null || !json.isObject()) { |
| 71 | return null; |
| 72 | } |
| 73 | |
| 74 | String id = nullIsIllegal(json.get(ID).asText(), ID + MISSING_MESSAGE); |
| 75 | String routerName = nullIsIllegal(json.get(ROUTER_NAME).asText(), |
| 76 | ROUTER_NAME + MISSING_MESSAGE); |
| 77 | String floatingIp = nullIsIllegal(json.get(FLOATING_IP).asText(), |
| 78 | FLOATING_IP + MISSING_MESSAGE); |
Jian Li | b636f70 | 2021-03-03 14:46:50 +0900 | [diff] [blame] | 79 | String networkName = nullIsIllegal(json.get(NETWORK_NAME).asText(), |
| 80 | NETWORK_NAME + MISSING_MESSAGE); |
Jian Li | 073f1ba | 2021-02-28 03:50:15 +0900 | [diff] [blame] | 81 | |
| 82 | KubevirtFloatingIp.Builder builder = DefaultKubevirtFloatingIp.builder() |
| 83 | .id(id) |
| 84 | .routerName(routerName) |
Jian Li | b636f70 | 2021-03-03 14:46:50 +0900 | [diff] [blame] | 85 | .networkName(networkName) |
Jian Li | 073f1ba | 2021-02-28 03:50:15 +0900 | [diff] [blame] | 86 | .floatingIp(IpAddress.valueOf(floatingIp)); |
| 87 | |
| 88 | JsonNode podName = json.get(POD_NAME); |
| 89 | if (podName != null) { |
| 90 | builder.podName(podName.asText()); |
| 91 | } |
| 92 | |
| 93 | JsonNode fixedIp = json.get(FIXED_IP); |
| 94 | if (fixedIp != null) { |
| 95 | builder.fixedIp(IpAddress.valueOf(fixedIp.asText())); |
| 96 | } |
| 97 | |
| 98 | return builder.build(); |
| 99 | } |
| 100 | } |