blob: 16fd082644e6bb5f5d0707a49b79c27183c2fa85 [file] [log] [blame]
Jian Li073f1ba2021-02-28 03:50:15 +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.ObjectNode;
20import org.onlab.packet.IpAddress;
21import org.onosproject.codec.CodecContext;
22import org.onosproject.codec.JsonCodec;
23import org.onosproject.kubevirtnetworking.api.DefaultKubevirtFloatingIp;
24import org.onosproject.kubevirtnetworking.api.KubevirtFloatingIp;
25import org.slf4j.Logger;
26
27import static com.google.common.base.Preconditions.checkNotNull;
28import static org.onlab.util.Tools.nullIsIllegal;
29import static org.slf4j.LoggerFactory.getLogger;
30
31/**
32 * Kubevirt floating IP codec used for serializing and de-serializing JSON string.
33 */
34public final class KubevirtFloatingIpCodec extends JsonCodec<KubevirtFloatingIp> {
35
36 private final Logger log = getLogger(getClass());
37
38 private static final String ID = "id";
39 private static final String ROUTER_NAME = "routerName";
40 private static final String POD_NAME = "podName";
41 private static final String FLOATING_IP = "floatingIp";
42 private static final String FIXED_IP = "fixedIp";
43
44 private static final String MISSING_MESSAGE = " is required in KubevirtFloatingIp";
45
46 @Override
47 public ObjectNode encode(KubevirtFloatingIp fip, CodecContext context) {
48 checkNotNull(fip, "Kubevirt floating IP cannot be null");
49
50 ObjectNode result = context.mapper().createObjectNode()
51 .put(ID, fip.id())
52 .put(ROUTER_NAME, fip.routerName())
53 .put(FLOATING_IP, fip.floatingIp().toString());
54
55 if (fip.podName() != null) {
56 result.put(POD_NAME, fip.podName());
57 }
58
59 if (fip.fixedIp() != null) {
60 result.put(FIXED_IP, fip.fixedIp().toString());
61 }
62
63 return result;
64 }
65
66 @Override
67 public KubevirtFloatingIp decode(ObjectNode json, CodecContext context) {
68 if (json == null || !json.isObject()) {
69 return null;
70 }
71
72 String id = nullIsIllegal(json.get(ID).asText(), ID + MISSING_MESSAGE);
73 String routerName = nullIsIllegal(json.get(ROUTER_NAME).asText(),
74 ROUTER_NAME + MISSING_MESSAGE);
75 String floatingIp = nullIsIllegal(json.get(FLOATING_IP).asText(),
76 FLOATING_IP + MISSING_MESSAGE);
77
78 KubevirtFloatingIp.Builder builder = DefaultKubevirtFloatingIp.builder()
79 .id(id)
80 .routerName(routerName)
81 .floatingIp(IpAddress.valueOf(floatingIp));
82
83 JsonNode podName = json.get(POD_NAME);
84 if (podName != null) {
85 builder.podName(podName.asText());
86 }
87
88 JsonNode fixedIp = json.get(FIXED_IP);
89 if (fixedIp != null) {
90 builder.fixedIp(IpAddress.valueOf(fixedIp.asText()));
91 }
92
93 return builder.build();
94 }
95}