Jian Li | daa7d6a | 2021-04-13 17:22:56 +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 | |
Jian Li | 7d13b59 | 2021-09-24 15:08:53 +0900 | [diff] [blame^] | 18 | import com.fasterxml.jackson.databind.JsonNode; |
Jian Li | daa7d6a | 2021-04-13 17:22:56 +0900 | [diff] [blame] | 19 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 20 | import org.onosproject.codec.CodecContext; |
| 21 | import org.onosproject.codec.JsonCodec; |
| 22 | import org.onosproject.kubevirtnetworking.api.DefaultKubevirtLoadBalancerRule; |
| 23 | import org.onosproject.kubevirtnetworking.api.KubevirtLoadBalancerRule; |
| 24 | import org.slf4j.Logger; |
| 25 | |
| 26 | import static com.google.common.base.Preconditions.checkNotNull; |
Jian Li | daa7d6a | 2021-04-13 17:22:56 +0900 | [diff] [blame] | 27 | import static org.slf4j.LoggerFactory.getLogger; |
| 28 | |
| 29 | /** |
| 30 | * Kubevirt load balancer rule codec used for serializing and de-serializing JSON string. |
| 31 | */ |
| 32 | public final class KubevirtLoadBalancerRuleCodec extends JsonCodec<KubevirtLoadBalancerRule> { |
| 33 | |
| 34 | private final Logger log = getLogger(getClass()); |
| 35 | |
| 36 | private static final String PROTOCOL = "protocol"; |
| 37 | private static final String PORT_RANGE_MAX = "portRangeMax"; |
| 38 | private static final String PORT_RANGE_MIN = "portRangeMin"; |
Jian Li | 7d13b59 | 2021-09-24 15:08:53 +0900 | [diff] [blame^] | 39 | private static final String TCP = "TCP"; |
| 40 | private static final String UDP = "UDP"; |
Jian Li | daa7d6a | 2021-04-13 17:22:56 +0900 | [diff] [blame] | 41 | |
| 42 | private static final String MISSING_MESSAGE = " is required in KubevirtLoadBalancerRule"; |
| 43 | |
| 44 | @Override |
| 45 | public ObjectNode encode(KubevirtLoadBalancerRule rule, CodecContext context) { |
| 46 | checkNotNull(rule, "Kubevirt load balancer rule cannot be null"); |
| 47 | |
Daniel Park | 05a9458 | 2021-05-12 10:57:02 +0900 | [diff] [blame] | 48 | ObjectNode result = context.mapper().createObjectNode().put(PROTOCOL, rule.protocol()); |
| 49 | |
Jian Li | 7d13b59 | 2021-09-24 15:08:53 +0900 | [diff] [blame^] | 50 | if (rule.protocol().equalsIgnoreCase(TCP) || rule.protocol().equalsIgnoreCase(UDP)) { |
Daniel Park | 05a9458 | 2021-05-12 10:57:02 +0900 | [diff] [blame] | 51 | result.put(PORT_RANGE_MAX, rule.portRangeMax()).put(PORT_RANGE_MIN, rule.portRangeMin()); |
| 52 | } |
| 53 | return result; |
Jian Li | daa7d6a | 2021-04-13 17:22:56 +0900 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | @Override |
| 57 | public KubevirtLoadBalancerRule decode(ObjectNode json, CodecContext context) { |
| 58 | if (json == null || !json.isObject()) { |
| 59 | return null; |
| 60 | } |
| 61 | |
Daniel Park | 05a9458 | 2021-05-12 10:57:02 +0900 | [diff] [blame] | 62 | KubevirtLoadBalancerRule.Builder builder = DefaultKubevirtLoadBalancerRule.builder(); |
Jian Li | daa7d6a | 2021-04-13 17:22:56 +0900 | [diff] [blame] | 63 | |
Jian Li | 7d13b59 | 2021-09-24 15:08:53 +0900 | [diff] [blame^] | 64 | JsonNode protocolJson = json.get(PROTOCOL); |
| 65 | String protocol = ""; |
| 66 | if (protocolJson != null) { |
| 67 | protocol = protocolJson.asText(); |
| 68 | } |
Daniel Park | 05a9458 | 2021-05-12 10:57:02 +0900 | [diff] [blame] | 69 | builder.protocol(protocol); |
| 70 | |
Jian Li | 7d13b59 | 2021-09-24 15:08:53 +0900 | [diff] [blame^] | 71 | if (protocol.equalsIgnoreCase(TCP) || protocol.equalsIgnoreCase(UDP)) { |
Daniel Park | 05a9458 | 2021-05-12 10:57:02 +0900 | [diff] [blame] | 72 | Integer portRangeMax = json.get(PORT_RANGE_MAX).asInt(); |
| 73 | Integer portRangeMin = json.get(PORT_RANGE_MIN).asInt(); |
| 74 | builder.portRangeMax(portRangeMax).portRangeMin(portRangeMin); |
| 75 | } |
| 76 | |
| 77 | return builder.build(); |
Jian Li | daa7d6a | 2021-04-13 17:22:56 +0900 | [diff] [blame] | 78 | } |
| 79 | } |