blob: 4937535f1df71a6dfb44ea25fec6936e5f638c2c [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";
55
56 private static final String MISSING_MESSAGE = " is required in KubevirtRouter";
57
58 @Override
59 public ObjectNode encode(KubevirtRouter router, CodecContext context) {
60 checkNotNull(router, "Kubevirt router pool cannot be null");
61
62 ObjectNode result = context.mapper().createObjectNode()
63 .put(NAME, router.name())
64 .put(ENABLE_SNAT, router.enableSnat());
65
66 if (router.description() != null) {
67 result.put(DESCRIPTION, router.description());
68 }
69
70 if (router.internal() != null && !router.internal().isEmpty()) {
71 ArrayNode internal = context.mapper().createArrayNode();
72 router.internal().forEach(internal::add);
73
74 result.set(INTERNAL, internal);
75 }
76
77 if (router.external() != null && !router.external().isEmpty()) {
78 ArrayNode external = context.mapper().createArrayNode();
79 router.external().forEach((k, v) -> {
80 ObjectNode item = context.mapper().createObjectNode();
81 item.put(IP_ADDRESS, k);
82 item.put(NETWORK, v);
83 external.add(item);
84 });
85 result.set(EXTERNAL, external);
86 }
87
88 if (router.peerRouter() != null) {
89 ObjectNode peerRouter = context.mapper().createObjectNode();
90 peerRouter.put(IP_ADDRESS, router.peerRouter().ipAddress().toString());
91
92 if (router.peerRouter().macAddress() != null) {
93 peerRouter.put(MAC_ADDRESS, router.peerRouter().macAddress().toString());
94 }
95
96 result.set(PEER_ROUTER, peerRouter);
97 }
98
99 return result;
100 }
101
102 @Override
103 public KubevirtRouter decode(ObjectNode json, CodecContext context) {
104 if (json == null || !json.isObject()) {
105 return null;
106 }
107
108 String name = nullIsIllegal(json.get(NAME).asText(),
109 NAME + MISSING_MESSAGE);
110
111 KubevirtRouter.Builder builder = DefaultKubevirtRouter.builder()
112 .name(name);
113
114 JsonNode descriptionJson = json.get(DESCRIPTION);
115 if (descriptionJson != null) {
116 builder.description(descriptionJson.asText());
117 }
118
119 JsonNode enableSnatJson = json.get(ENABLE_SNAT);
120 if (enableSnatJson != null) {
121 builder.enableSnat(enableSnatJson.asBoolean());
122 }
123
124 ArrayNode internalJson = (ArrayNode) json.get(INTERNAL);
125 Set<String> internal = new HashSet<>();
126 if (internalJson != null) {
127 for (int i = 0; i < internalJson.size(); i++) {
128 internal.add(internalJson.get(i).asText());
129 }
130 builder.internal(internal);
131 }
132
133 ObjectNode externalJson = (ObjectNode) json.get(EXTERNAL);
134 if (externalJson != null) {
135 Map<String, String> external = ImmutableMap.of(
136 externalJson.get(IP_ADDRESS).asText(),
137 externalJson.get(NETWORK).asText());
138 builder.external(external);
139 }
140
141 ObjectNode peerRouterJson = (ObjectNode) json.get(PEER_ROUTER);
142 if (peerRouterJson != null) {
143 JsonNode ipJson = peerRouterJson.get(IP_ADDRESS);
144 JsonNode macJson = peerRouterJson.get(MAC_ADDRESS);
145
146 if (ipJson != null && macJson != null) {
147 IpAddress ip = IpAddress.valueOf(ipJson.asText());
148 MacAddress mac = MacAddress.valueOf(macJson.asText());
149 KubevirtPeerRouter peer = new KubevirtPeerRouter(ip, mac);
150 builder.peerRouter(peer);
151 }
152
153 // if mac address is not specified, we will not add mac address to peer router
154 if (ipJson != null && macJson == null) {
155 IpAddress ip = IpAddress.valueOf(ipJson.asText());
156 KubevirtPeerRouter peer = new KubevirtPeerRouter(ip, null);
157 builder.peerRouter(peer);
158 }
159 }
160
161 return builder.build();
162 }
163}