blob: 582d4d3520f86c03d9269d7a17b571c39f09af5d [file] [log] [blame]
Jian Lidaa7d6a2021-04-13 17:22:56 +09001/*
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 */
16package org.onosproject.kubevirtnetworking.codec;
17
Jian Li7d13b592021-09-24 15:08:53 +090018import com.fasterxml.jackson.databind.JsonNode;
Jian Lidaa7d6a2021-04-13 17:22:56 +090019import com.fasterxml.jackson.databind.node.ObjectNode;
20import org.onosproject.codec.CodecContext;
21import org.onosproject.codec.JsonCodec;
22import org.onosproject.kubevirtnetworking.api.DefaultKubevirtLoadBalancerRule;
23import org.onosproject.kubevirtnetworking.api.KubevirtLoadBalancerRule;
24import org.slf4j.Logger;
25
26import static com.google.common.base.Preconditions.checkNotNull;
Jian Lidaa7d6a2021-04-13 17:22:56 +090027import static org.slf4j.LoggerFactory.getLogger;
28
29/**
30 * Kubevirt load balancer rule codec used for serializing and de-serializing JSON string.
31 */
32public 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 Li7d13b592021-09-24 15:08:53 +090039 private static final String TCP = "TCP";
40 private static final String UDP = "UDP";
Jian Lidaa7d6a2021-04-13 17:22:56 +090041
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 Park05a94582021-05-12 10:57:02 +090048 ObjectNode result = context.mapper().createObjectNode().put(PROTOCOL, rule.protocol());
49
Jian Li7d13b592021-09-24 15:08:53 +090050 if (rule.protocol().equalsIgnoreCase(TCP) || rule.protocol().equalsIgnoreCase(UDP)) {
Daniel Park05a94582021-05-12 10:57:02 +090051 result.put(PORT_RANGE_MAX, rule.portRangeMax()).put(PORT_RANGE_MIN, rule.portRangeMin());
52 }
53 return result;
Jian Lidaa7d6a2021-04-13 17:22:56 +090054 }
55
56 @Override
57 public KubevirtLoadBalancerRule decode(ObjectNode json, CodecContext context) {
58 if (json == null || !json.isObject()) {
59 return null;
60 }
61
Daniel Park05a94582021-05-12 10:57:02 +090062 KubevirtLoadBalancerRule.Builder builder = DefaultKubevirtLoadBalancerRule.builder();
Jian Lidaa7d6a2021-04-13 17:22:56 +090063
Jian Li7d13b592021-09-24 15:08:53 +090064 JsonNode protocolJson = json.get(PROTOCOL);
65 String protocol = "";
66 if (protocolJson != null) {
67 protocol = protocolJson.asText();
68 }
Daniel Park05a94582021-05-12 10:57:02 +090069 builder.protocol(protocol);
70
Jian Li7d13b592021-09-24 15:08:53 +090071 if (protocol.equalsIgnoreCase(TCP) || protocol.equalsIgnoreCase(UDP)) {
Daniel Park05a94582021-05-12 10:57:02 +090072 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 Lidaa7d6a2021-04-13 17:22:56 +090078 }
79}