blob: d1239de3928f090a982b49893eb833a0f8ed7ce5 [file] [log] [blame]
Jian Li7eb20782021-02-27 01:10:50 +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 com.google.common.collect.ImmutableMap;
22import org.onlab.packet.IpAddress;
23import org.onlab.packet.MacAddress;
24import org.onosproject.codec.CodecContext;
25import org.onosproject.codec.JsonCodec;
26import org.onosproject.kubevirtnetworking.api.DefaultKubevirtRouter;
27import org.onosproject.kubevirtnetworking.api.KubevirtPeerRouter;
28import org.onosproject.kubevirtnetworking.api.KubevirtRouter;
29import org.slf4j.Logger;
30
31import java.util.HashSet;
32import java.util.Map;
33import java.util.Set;
34
35import static com.google.common.base.Preconditions.checkNotNull;
36import static org.onlab.util.Tools.nullIsIllegal;
37import static org.slf4j.LoggerFactory.getLogger;
38
39/**
40 * Kubevirt router codec used for serializing and de-serializing JSON string.
41 */
42public final class KubevirtRouterCodec extends JsonCodec<KubevirtRouter> {
43
44 private final Logger log = getLogger(getClass());
45
46 private static final String NAME = "name";
47 private static final String DESCRIPTION = "description";
48 private static final String ENABLE_SNAT = "enableSnat";
49 private static final String INTERNAL = "internal";
50 private static final String EXTERNAL = "external";
51 private static final String PEER_ROUTER = "peerRouter";
52 private static final String IP_ADDRESS = "ip";
53 private static final String MAC_ADDRESS = "mac";
54 private static final String NETWORK = "network";
Daniel Parkb9a22022021-03-04 18:58:47 +090055 private static final String GATEWAY = "gateway";
Jian Li7eb20782021-02-27 01:10:50 +090056
57 private static final String MISSING_MESSAGE = " is required in KubevirtRouter";
58
59 @Override
60 public ObjectNode encode(KubevirtRouter router, CodecContext context) {
Jian Lie48a6172021-02-28 03:50:15 +090061 checkNotNull(router, "Kubevirt router cannot be null");
Jian Li7eb20782021-02-27 01:10:50 +090062
63 ObjectNode result = context.mapper().createObjectNode()
64 .put(NAME, router.name())
65 .put(ENABLE_SNAT, router.enableSnat());
66
67 if (router.description() != null) {
68 result.put(DESCRIPTION, router.description());
69 }
70
71 if (router.internal() != null && !router.internal().isEmpty()) {
72 ArrayNode internal = context.mapper().createArrayNode();
73 router.internal().forEach(internal::add);
74
75 result.set(INTERNAL, internal);
76 }
77
78 if (router.external() != null && !router.external().isEmpty()) {
79 ArrayNode external = context.mapper().createArrayNode();
80 router.external().forEach((k, v) -> {
81 ObjectNode item = context.mapper().createObjectNode();
82 item.put(IP_ADDRESS, k);
83 item.put(NETWORK, v);
84 external.add(item);
85 });
86 result.set(EXTERNAL, external);
87 }
88
89 if (router.peerRouter() != null) {
90 ObjectNode peerRouter = context.mapper().createObjectNode();
91 peerRouter.put(IP_ADDRESS, router.peerRouter().ipAddress().toString());
92
93 if (router.peerRouter().macAddress() != null) {
94 peerRouter.put(MAC_ADDRESS, router.peerRouter().macAddress().toString());
95 }
96
97 result.set(PEER_ROUTER, peerRouter);
98 }
99
Daniel Parkb9a22022021-03-04 18:58:47 +0900100 if (router.electedGateway() != null) {
101 result.put(GATEWAY, router.electedGateway());
102 }
103
Jian Li7eb20782021-02-27 01:10:50 +0900104 return result;
105 }
106
107 @Override
108 public KubevirtRouter decode(ObjectNode json, CodecContext context) {
109 if (json == null || !json.isObject()) {
110 return null;
111 }
112
113 String name = nullIsIllegal(json.get(NAME).asText(),
114 NAME + MISSING_MESSAGE);
115
116 KubevirtRouter.Builder builder = DefaultKubevirtRouter.builder()
117 .name(name);
118
119 JsonNode descriptionJson = json.get(DESCRIPTION);
120 if (descriptionJson != null) {
121 builder.description(descriptionJson.asText());
122 }
123
124 JsonNode enableSnatJson = json.get(ENABLE_SNAT);
125 if (enableSnatJson != null) {
126 builder.enableSnat(enableSnatJson.asBoolean());
127 }
Daniel Parkb9a22022021-03-04 18:58:47 +0900128 JsonNode electedGwJson = json.get(GATEWAY);
129 if (electedGwJson != null) {
130 builder.electedGateway(electedGwJson.asText());
131 }
Jian Li7eb20782021-02-27 01:10:50 +0900132
133 ArrayNode internalJson = (ArrayNode) json.get(INTERNAL);
134 Set<String> internal = new HashSet<>();
135 if (internalJson != null) {
136 for (int i = 0; i < internalJson.size(); i++) {
137 internal.add(internalJson.get(i).asText());
138 }
139 builder.internal(internal);
140 }
141
142 ObjectNode externalJson = (ObjectNode) json.get(EXTERNAL);
143 if (externalJson != null) {
144 Map<String, String> external = ImmutableMap.of(
145 externalJson.get(IP_ADDRESS).asText(),
146 externalJson.get(NETWORK).asText());
147 builder.external(external);
148 }
149
150 ObjectNode peerRouterJson = (ObjectNode) json.get(PEER_ROUTER);
151 if (peerRouterJson != null) {
152 JsonNode ipJson = peerRouterJson.get(IP_ADDRESS);
153 JsonNode macJson = peerRouterJson.get(MAC_ADDRESS);
154
155 if (ipJson != null && macJson != null) {
156 IpAddress ip = IpAddress.valueOf(ipJson.asText());
157 MacAddress mac = MacAddress.valueOf(macJson.asText());
158 KubevirtPeerRouter peer = new KubevirtPeerRouter(ip, mac);
159 builder.peerRouter(peer);
160 }
161
162 // if mac address is not specified, we will not add mac address to peer router
163 if (ipJson != null && macJson == null) {
164 IpAddress ip = IpAddress.valueOf(ipJson.asText());
165 KubevirtPeerRouter peer = new KubevirtPeerRouter(ip, null);
166 builder.peerRouter(peer);
167 }
168 }
169
170 return builder.build();
171 }
172}