blob: c11d829230b1d0d086288c554c31672b4aa0230c [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
18import com.fasterxml.jackson.databind.JsonNode;
19import com.fasterxml.jackson.databind.node.ArrayNode;
20import com.fasterxml.jackson.databind.node.ObjectNode;
21import org.onlab.packet.IpAddress;
22import org.onosproject.codec.CodecContext;
23import org.onosproject.codec.JsonCodec;
24import org.onosproject.kubevirtnetworking.api.DefaultKubevirtLoadBalancer;
25import org.onosproject.kubevirtnetworking.api.KubevirtLoadBalancer;
26import org.onosproject.kubevirtnetworking.api.KubevirtLoadBalancerRule;
27import org.slf4j.Logger;
28
29import java.util.HashSet;
30import java.util.Set;
31import java.util.stream.IntStream;
32
33import static com.google.common.base.Preconditions.checkNotNull;
34import static org.onlab.util.Tools.nullIsIllegal;
35import static org.slf4j.LoggerFactory.getLogger;
36
37public final class KubevirtLoadBalancerCodec extends JsonCodec<KubevirtLoadBalancer> {
38
39 private final Logger log = getLogger(getClass());
40
41 private static final String NAME = "name";
42 private static final String DESCRIPTION = "description";
43 private static final String VIP = "vip";
44 private static final String NETWORK_ID = "networkId";
45 private static final String MEMBERS = "members";
46 private static final String RULES = "rules";
47
48 private static final String MISSING_MESSAGE = " is required in KubevirtLoadBalancer";
49
50 @Override
51 public ObjectNode encode(KubevirtLoadBalancer lb, CodecContext context) {
52 checkNotNull(lb, "Kubevirt load balancer cannot be null");
53
54 ObjectNode result = context.mapper().createObjectNode()
55 .put(NAME, lb.name())
56 .put(VIP, lb.vip().toString())
57 .put(NETWORK_ID, lb.networkId());
58
59 if (lb.description() != null) {
60 result.put(DESCRIPTION, lb.description());
61 }
62
63 if (lb.members() != null && !lb.members().isEmpty()) {
64 ArrayNode members = context.mapper().createArrayNode();
65 for (IpAddress ip : lb.members()) {
66 members.add(ip.toString());
67 }
68 result.set(MEMBERS, members);
69 }
70
71 if (lb.rules() != null && !lb.rules().isEmpty()) {
72 ArrayNode rules = context.mapper().createArrayNode();
73 for (KubevirtLoadBalancerRule rule : lb.rules()) {
74 ObjectNode ruleJson = context.codec(
75 KubevirtLoadBalancerRule.class).encode(rule, context);
76 rules.add(ruleJson);
77 }
78 result.set(RULES, rules);
79 }
80
81 return result;
82 }
83
84 @Override
85 public KubevirtLoadBalancer decode(ObjectNode json, CodecContext context) {
86 if (json == null || !json.isObject()) {
87 return null;
88 }
89
90 String name = nullIsIllegal(json.get(NAME).asText(), NAME + MISSING_MESSAGE);
91 IpAddress vip = IpAddress.valueOf(nullIsIllegal(json.get(VIP).asText(),
92 VIP + MISSING_MESSAGE));
93 String networkId = nullIsIllegal(json.get(NETWORK_ID).asText(),
94 NETWORK_ID + MISSING_MESSAGE);
95
96 KubevirtLoadBalancer.Builder builder = DefaultKubevirtLoadBalancer.builder()
97 .name(name)
98 .vip(vip)
99 .networkId(networkId);
100
101 JsonNode description = json.get(DESCRIPTION);
102 if (description != null) {
103 builder.description(description.asText());
104 }
105
106 ArrayNode membersJson = (ArrayNode) json.get(MEMBERS);
107 if (membersJson != null) {
108 Set<IpAddress> members = new HashSet<>();
109 for (JsonNode memberJson : membersJson) {
110 members.add(IpAddress.valueOf(memberJson.asText()));
111 }
112 builder.members(members);
113 }
114
115 JsonNode rulesJson = json.get(RULES);
116 if (rulesJson != null) {
117 Set<KubevirtLoadBalancerRule> rules = new HashSet<>();
118 IntStream.range(0, rulesJson.size())
119 .forEach(i -> {
120 ObjectNode ruleJson = get(rulesJson, i);
121 KubevirtLoadBalancerRule rule = context.codec(
122 KubevirtLoadBalancerRule.class).decode(ruleJson, context);
123 rules.add(rule);
124 });
125 builder.rules(rules);
126 }
127
128 return builder.build();
129 }
130}