blob: f2a35058a880da0d33f020a291ac14d43993a44f [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";
Jian Lib636f702021-03-03 14:46:50 +090041 private static final String NETWORK_NAME = "networkName";
Jian Li073f1ba2021-02-28 03:50:15 +090042 private static final String FLOATING_IP = "floatingIp";
43 private static final String FIXED_IP = "fixedIp";
44
45 private static final String MISSING_MESSAGE = " is required in KubevirtFloatingIp";
46
47 @Override
48 public ObjectNode encode(KubevirtFloatingIp fip, CodecContext context) {
49 checkNotNull(fip, "Kubevirt floating IP cannot be null");
50
51 ObjectNode result = context.mapper().createObjectNode()
52 .put(ID, fip.id())
53 .put(ROUTER_NAME, fip.routerName())
Jian Lib636f702021-03-03 14:46:50 +090054 .put(NETWORK_NAME, fip.networkName())
Jian Li073f1ba2021-02-28 03:50:15 +090055 .put(FLOATING_IP, fip.floatingIp().toString());
56
57 if (fip.podName() != null) {
58 result.put(POD_NAME, fip.podName());
59 }
60
61 if (fip.fixedIp() != null) {
62 result.put(FIXED_IP, fip.fixedIp().toString());
63 }
64
65 return result;
66 }
67
68 @Override
69 public KubevirtFloatingIp decode(ObjectNode json, CodecContext context) {
70 if (json == null || !json.isObject()) {
71 return null;
72 }
73
74 String id = nullIsIllegal(json.get(ID).asText(), ID + MISSING_MESSAGE);
75 String routerName = nullIsIllegal(json.get(ROUTER_NAME).asText(),
76 ROUTER_NAME + MISSING_MESSAGE);
77 String floatingIp = nullIsIllegal(json.get(FLOATING_IP).asText(),
78 FLOATING_IP + MISSING_MESSAGE);
Jian Lib636f702021-03-03 14:46:50 +090079 String networkName = nullIsIllegal(json.get(NETWORK_NAME).asText(),
80 NETWORK_NAME + MISSING_MESSAGE);
Jian Li073f1ba2021-02-28 03:50:15 +090081
82 KubevirtFloatingIp.Builder builder = DefaultKubevirtFloatingIp.builder()
83 .id(id)
84 .routerName(routerName)
Jian Lib636f702021-03-03 14:46:50 +090085 .networkName(networkName)
Jian Li073f1ba2021-02-28 03:50:15 +090086 .floatingIp(IpAddress.valueOf(floatingIp));
87
88 JsonNode podName = json.get(POD_NAME);
89 if (podName != null) {
90 builder.podName(podName.asText());
91 }
92
93 JsonNode fixedIp = json.get(FIXED_IP);
94 if (fixedIp != null) {
95 builder.fixedIp(IpAddress.valueOf(fixedIp.asText()));
96 }
97
98 return builder.build();
99 }
100}