Daniel Park | 5ff76b7 | 2022-09-26 22:58:53 +0900 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2022-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.node.ObjectNode; |
| 19 | import org.onlab.packet.IpAddress; |
| 20 | import org.onlab.packet.MacAddress; |
| 21 | import org.onosproject.codec.CodecContext; |
| 22 | import org.onosproject.codec.JsonCodec; |
| 23 | import org.onosproject.kubevirtnode.api.DefaultKubernetesExternalLbInterface; |
| 24 | import org.onosproject.kubevirtnode.api.KubernetesExternalLbInterface; |
| 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 | * Kubernetes external load balancer interface codec used for serializing and de-serializing JSON string. |
| 33 | */ |
| 34 | public class KubernetesExternalLbInterfaceCodec extends JsonCodec<KubernetesExternalLbInterface> { |
| 35 | private final Logger log = getLogger(getClass()); |
| 36 | |
| 37 | private static final String ELB_BRIDGE_NAME = "externalLbBridgeName"; |
| 38 | private static final String ELB_IP = "externalLbIp"; |
| 39 | private static final String ELB_GW_IP = "externalLbGwIp"; |
| 40 | private static final String ELB_GW_MAC = "externalLbGwMac"; |
| 41 | |
| 42 | private static final String MISSING_MESSAGE = " is required in KubernetesExternalLbInterfaceCodec"; |
| 43 | |
| 44 | @Override |
| 45 | public ObjectNode encode(KubernetesExternalLbInterface externalLbInterface, CodecContext context) { |
| 46 | checkNotNull(externalLbInterface, "checkNotNull cannot be null"); |
| 47 | |
| 48 | ObjectNode result = context.mapper().createObjectNode() |
| 49 | .put(ELB_BRIDGE_NAME, externalLbInterface.externalLbBridgeName()) |
| 50 | .put(ELB_IP, externalLbInterface.externalLbIp().toString()) |
| 51 | .put(ELB_GW_IP, externalLbInterface.externalLbGwIp().toString()) |
| 52 | .put(ELB_GW_MAC, externalLbInterface.externalLbGwMac().toString()); |
| 53 | |
| 54 | return result; |
| 55 | } |
| 56 | |
| 57 | @Override |
| 58 | public KubernetesExternalLbInterface decode(ObjectNode json, CodecContext context) { |
| 59 | if (json == null || !json.isObject()) { |
| 60 | return null; |
| 61 | } |
| 62 | |
| 63 | String elbBridgeName = nullIsIllegal(json.get(ELB_BRIDGE_NAME).asText(), |
| 64 | ELB_BRIDGE_NAME + MISSING_MESSAGE); |
| 65 | |
| 66 | String elbIp = nullIsIllegal(json.get(ELB_IP).asText(), |
| 67 | ELB_IP + MISSING_MESSAGE); |
| 68 | |
| 69 | String elbGwIp = nullIsIllegal(json.get(ELB_GW_IP).asText(), |
| 70 | ELB_GW_IP + MISSING_MESSAGE); |
| 71 | |
| 72 | String elbGwMac = nullIsIllegal(json.get(ELB_GW_MAC).asText(), |
| 73 | ELB_GW_MAC + MISSING_MESSAGE); |
| 74 | |
| 75 | KubernetesExternalLbInterface externalLbInterface = DefaultKubernetesExternalLbInterface.builder() |
| 76 | .externalLbBridgeName(elbBridgeName) |
| 77 | .externallbGwIp(IpAddress.valueOf(elbGwIp)) |
| 78 | .externalLbIp(IpAddress.valueOf(elbIp)) |
| 79 | .externalLbGwMac(MacAddress.valueOf(elbGwMac)) |
| 80 | .build(); |
| 81 | |
| 82 | return externalLbInterface; |
| 83 | } |
| 84 | } |