blob: 5ca4ab6d945895d9116f9ba615bd198955e8d1de [file] [log] [blame]
Jian Li810f58c2021-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 Park2884b232021-03-04 18:58:47 +090055 private static final String GATEWAY = "gateway";
Jian Li810f58c2021-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 Li073f1ba2021-02-28 03:50:15 +090061 checkNotNull(router, "Kubevirt router cannot be null");
Jian Li810f58c2021-02-27 01:10:50 +090062
63 ObjectNode result = context.mapper().createObjectNode()
64 .put(NAME, router.name())
Jian Li9756cec2021-03-16 16:06:33 +090065 .put(ENABLE_SNAT, router.enableSnat())
66 .put(MAC_ADDRESS, router.mac().toString());
Jian Li810f58c2021-02-27 01:10:50 +090067
68 if (router.description() != null) {
69 result.put(DESCRIPTION, router.description());
70 }
71
72 if (router.internal() != null && !router.internal().isEmpty()) {
73 ArrayNode internal = context.mapper().createArrayNode();
74 router.internal().forEach(internal::add);
75
76 result.set(INTERNAL, internal);
77 }
78
79 if (router.external() != null && !router.external().isEmpty()) {
80 ArrayNode external = context.mapper().createArrayNode();
81 router.external().forEach((k, v) -> {
82 ObjectNode item = context.mapper().createObjectNode();
83 item.put(IP_ADDRESS, k);
84 item.put(NETWORK, v);
85 external.add(item);
86 });
87 result.set(EXTERNAL, external);
88 }
89
90 if (router.peerRouter() != null) {
91 ObjectNode peerRouter = context.mapper().createObjectNode();
92 peerRouter.put(IP_ADDRESS, router.peerRouter().ipAddress().toString());
93
94 if (router.peerRouter().macAddress() != null) {
95 peerRouter.put(MAC_ADDRESS, router.peerRouter().macAddress().toString());
96 }
97
98 result.set(PEER_ROUTER, peerRouter);
99 }
100
Daniel Park2884b232021-03-04 18:58:47 +0900101 if (router.electedGateway() != null) {
102 result.put(GATEWAY, router.electedGateway());
103 }
104
Jian Li810f58c2021-02-27 01:10:50 +0900105 return result;
106 }
107
108 @Override
109 public KubevirtRouter decode(ObjectNode json, CodecContext context) {
110 if (json == null || !json.isObject()) {
111 return null;
112 }
113
114 String name = nullIsIllegal(json.get(NAME).asText(),
115 NAME + MISSING_MESSAGE);
116
Jian Li9756cec2021-03-16 16:06:33 +0900117 String vrouterMac = nullIsIllegal(json.get(MAC_ADDRESS).asText(),
118 MAC_ADDRESS + MISSING_MESSAGE);
119
Jian Li810f58c2021-02-27 01:10:50 +0900120 KubevirtRouter.Builder builder = DefaultKubevirtRouter.builder()
Jian Li9756cec2021-03-16 16:06:33 +0900121 .name(name)
122 .mac(MacAddress.valueOf(vrouterMac));
Jian Li810f58c2021-02-27 01:10:50 +0900123
124 JsonNode descriptionJson = json.get(DESCRIPTION);
125 if (descriptionJson != null) {
126 builder.description(descriptionJson.asText());
127 }
128
129 JsonNode enableSnatJson = json.get(ENABLE_SNAT);
130 if (enableSnatJson != null) {
131 builder.enableSnat(enableSnatJson.asBoolean());
132 }
Daniel Park2884b232021-03-04 18:58:47 +0900133 JsonNode electedGwJson = json.get(GATEWAY);
134 if (electedGwJson != null) {
135 builder.electedGateway(electedGwJson.asText());
136 }
Jian Li810f58c2021-02-27 01:10:50 +0900137
138 ArrayNode internalJson = (ArrayNode) json.get(INTERNAL);
139 Set<String> internal = new HashSet<>();
140 if (internalJson != null) {
141 for (int i = 0; i < internalJson.size(); i++) {
142 internal.add(internalJson.get(i).asText());
143 }
144 builder.internal(internal);
145 }
146
147 ObjectNode externalJson = (ObjectNode) json.get(EXTERNAL);
148 if (externalJson != null) {
149 Map<String, String> external = ImmutableMap.of(
150 externalJson.get(IP_ADDRESS).asText(),
151 externalJson.get(NETWORK).asText());
152 builder.external(external);
153 }
154
155 ObjectNode peerRouterJson = (ObjectNode) json.get(PEER_ROUTER);
156 if (peerRouterJson != null) {
157 JsonNode ipJson = peerRouterJson.get(IP_ADDRESS);
158 JsonNode macJson = peerRouterJson.get(MAC_ADDRESS);
159
160 if (ipJson != null && macJson != null) {
161 IpAddress ip = IpAddress.valueOf(ipJson.asText());
162 MacAddress mac = MacAddress.valueOf(macJson.asText());
163 KubevirtPeerRouter peer = new KubevirtPeerRouter(ip, mac);
164 builder.peerRouter(peer);
165 }
166
167 // if mac address is not specified, we will not add mac address to peer router
168 if (ipJson != null && macJson == null) {
169 IpAddress ip = IpAddress.valueOf(ipJson.asText());
170 KubevirtPeerRouter peer = new KubevirtPeerRouter(ip, null);
171 builder.peerRouter(peer);
172 }
173 }
174
175 return builder.build();
176 }
177}