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 | |
| 18 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 19 | import org.onosproject.codec.CodecContext; |
| 20 | import org.onosproject.codec.JsonCodec; |
| 21 | import org.onosproject.kubevirtnetworking.api.DefaultKubevirtLoadBalancerRule; |
| 22 | import org.onosproject.kubevirtnetworking.api.KubevirtLoadBalancerRule; |
| 23 | import org.slf4j.Logger; |
| 24 | |
| 25 | import static com.google.common.base.Preconditions.checkNotNull; |
| 26 | import static org.onlab.util.Tools.nullIsIllegal; |
| 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"; |
Daniel Park | 05a9458 | 2021-05-12 10:57:02 +0900 | [diff] [blame] | 39 | private static final String ICMP = "ICMP"; |
Jian Li | daa7d6a | 2021-04-13 17:22:56 +0900 | [diff] [blame] | 40 | |
| 41 | private static final String MISSING_MESSAGE = " is required in KubevirtLoadBalancerRule"; |
| 42 | |
| 43 | @Override |
| 44 | public ObjectNode encode(KubevirtLoadBalancerRule rule, CodecContext context) { |
| 45 | checkNotNull(rule, "Kubevirt load balancer rule cannot be null"); |
| 46 | |
Daniel Park | 05a9458 | 2021-05-12 10:57:02 +0900 | [diff] [blame] | 47 | ObjectNode result = context.mapper().createObjectNode().put(PROTOCOL, rule.protocol()); |
| 48 | |
| 49 | if (!rule.protocol().equalsIgnoreCase(ICMP)) { |
| 50 | result.put(PORT_RANGE_MAX, rule.portRangeMax()).put(PORT_RANGE_MIN, rule.portRangeMin()); |
| 51 | } |
| 52 | return result; |
Jian Li | daa7d6a | 2021-04-13 17:22:56 +0900 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | @Override |
| 56 | public KubevirtLoadBalancerRule decode(ObjectNode json, CodecContext context) { |
| 57 | if (json == null || !json.isObject()) { |
| 58 | return null; |
| 59 | } |
| 60 | |
Daniel Park | 05a9458 | 2021-05-12 10:57:02 +0900 | [diff] [blame] | 61 | KubevirtLoadBalancerRule.Builder builder = DefaultKubevirtLoadBalancerRule.builder(); |
Jian Li | daa7d6a | 2021-04-13 17:22:56 +0900 | [diff] [blame] | 62 | |
Daniel Park | 05a9458 | 2021-05-12 10:57:02 +0900 | [diff] [blame] | 63 | String protocol = nullIsIllegal(json.get(PROTOCOL).asText(), PROTOCOL + MISSING_MESSAGE); |
| 64 | |
| 65 | builder.protocol(protocol); |
| 66 | |
| 67 | if (!protocol.equalsIgnoreCase(ICMP)) { |
| 68 | Integer portRangeMax = json.get(PORT_RANGE_MAX).asInt(); |
| 69 | Integer portRangeMin = json.get(PORT_RANGE_MIN).asInt(); |
| 70 | builder.portRangeMax(portRangeMax).portRangeMin(portRangeMin); |
| 71 | } |
| 72 | |
| 73 | return builder.build(); |
Jian Li | daa7d6a | 2021-04-13 17:22:56 +0900 | [diff] [blame] | 74 | } |
| 75 | } |