blob: f04106e437250ee38693763a5eca8e24229abf8d [file] [log] [blame]
Jian Lifeb84802021-01-12 16:34:49 +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 org.onlab.packet.IpAddress;
22import org.onosproject.codec.CodecContext;
23import org.onosproject.codec.JsonCodec;
24import org.onosproject.kubevirtnetworking.api.DefaultKubevirtNetwork;
25import org.onosproject.kubevirtnetworking.api.KubevirtHostRoute;
26import org.onosproject.kubevirtnetworking.api.KubevirtIpPool;
27import org.onosproject.kubevirtnetworking.api.KubevirtNetwork;
28import org.slf4j.Logger;
29
30import java.util.HashSet;
31import java.util.Set;
32import java.util.stream.IntStream;
33
34import static com.google.common.base.Preconditions.checkNotNull;
35import static org.onlab.util.Tools.nullIsIllegal;
36import static org.slf4j.LoggerFactory.getLogger;
37
38/**
39 * Kubevirt network codec used for serializing and de-serializing JSON string.
40 */
41public final class KubevirtNetworkCodec extends JsonCodec<KubevirtNetwork> {
42
43 private final Logger log = getLogger(getClass());
44
45 private static final String NETWORK_ID = "networkId";
46 private static final String TYPE = "type";
47 private static final String NAME = "name";
48 private static final String MTU = "mtu";
49 private static final String SEGMENT_ID = "segmentId";
50 private static final String GATEWAY_IP = "gatewayIp";
51 private static final String CIDR = "cidr";
52 private static final String HOST_ROUTES = "hostRoutes";
53 private static final String IP_POOL = "ipPool";
Jian Li97e6fc32021-02-01 20:36:45 +090054 private static final String DNSES = "dnses";
Jian Lifeb84802021-01-12 16:34:49 +090055
56 private static final String MISSING_MESSAGE = " is required in KubevirtNetwork";
57
58 @Override
59 public ObjectNode encode(KubevirtNetwork network, CodecContext context) {
60 checkNotNull(network, "Kubevirt network cannot be null");
61
62 ObjectNode result = context.mapper().createObjectNode()
63 .put(NETWORK_ID, network.networkId())
64 .put(TYPE, network.type().name())
65 .put(NAME, network.name())
66 .put(MTU, network.mtu())
67 .put(GATEWAY_IP, network.gatewayIp().toString())
68 .put(CIDR, network.cidr());
69
70 if (network.segmentId() != null) {
71 result.put(SEGMENT_ID, network.segmentId());
72 }
73
74 if (network.hostRoutes() != null && !network.hostRoutes().isEmpty()) {
75 ArrayNode hostRoutes = context.mapper().createArrayNode();
76 network.hostRoutes().forEach(hostRoute -> {
77 ObjectNode hostRouteJson =
78 context.codec(KubevirtHostRoute.class).encode(hostRoute, context);
79 hostRoutes.add(hostRouteJson);
80 });
81 result.set(HOST_ROUTES, hostRoutes);
82 }
83
84 if (network.ipPool() != null) {
85 ObjectNode ipPoolJson = context.codec(KubevirtIpPool.class).encode(network.ipPool(), context);
86 result.set(IP_POOL, ipPoolJson);
87 }
88
Jian Li97e6fc32021-02-01 20:36:45 +090089 if (network.dnses() != null && !network.dnses().isEmpty()) {
90 ArrayNode dnses = context.mapper().createArrayNode();
91 network.dnses().forEach(dns -> {
92 dnses.add(dns.toString());
93 });
94 result.set(DNSES, dnses);
95 }
96
Jian Lifeb84802021-01-12 16:34:49 +090097 return result;
98 }
99
100 @Override
101 public KubevirtNetwork decode(ObjectNode json, CodecContext context) {
102 if (json == null || !json.isObject()) {
103 return null;
104 }
105
106 String networkId = nullIsIllegal(json.get(NETWORK_ID).asText(),
107 NETWORK_ID + MISSING_MESSAGE);
108 String type = nullIsIllegal(json.get(TYPE).asText(),
109 TYPE + MISSING_MESSAGE);
110 String name = nullIsIllegal(json.get(NAME).asText(),
111 NAME + MISSING_MESSAGE);
112 Integer mtu = nullIsIllegal(json.get(MTU).asInt(),
113 MTU + MISSING_MESSAGE);
114 String gatewayIp = nullIsIllegal(json.get(GATEWAY_IP).asText(),
115 GATEWAY_IP + MISSING_MESSAGE);
116 String cidr = nullIsIllegal(json.get(CIDR).asText(),
117 CIDR + MISSING_MESSAGE);
118
119 KubevirtNetwork.Builder networkBuilder = DefaultKubevirtNetwork.builder()
120 .networkId(networkId)
121 .type(KubevirtNetwork.Type.valueOf(type))
122 .name(name)
123 .mtu(mtu)
124 .gatewayIp(IpAddress.valueOf(gatewayIp))
125 .cidr(cidr);
126
Jian Li7bca1272021-01-14 11:30:36 +0900127 if (!type.equals(KubevirtNetwork.Type.FLAT.name())) {
128 JsonNode segmentIdJson = json.get(SEGMENT_ID);
129 if (segmentIdJson != null) {
130 networkBuilder.segmentId(segmentIdJson.asText());
131 }
132 }
133
Jian Lifeb84802021-01-12 16:34:49 +0900134 JsonNode ipPoolJson = json.get(IP_POOL);
135 if (ipPoolJson != null) {
136 final JsonCodec<KubevirtIpPool>
137 ipPoolCodec = context.codec(KubevirtIpPool.class);
138 networkBuilder.ipPool(ipPoolCodec.decode(
139 (ObjectNode) ipPoolJson.deepCopy(), context));
140 }
141
142 // parse host routes
143 Set<KubevirtHostRoute> hostRoutes = new HashSet<>();
144 JsonNode hostRoutesJson = json.get(HOST_ROUTES);
145 if (hostRoutesJson != null) {
146 final JsonCodec<KubevirtHostRoute>
147 hostRouteCodec = context.codec(KubevirtHostRoute.class);
148
149 IntStream.range(0, hostRoutesJson.size()).forEach(i -> {
150 ObjectNode routeJson = get(hostRoutesJson, i);
151 hostRoutes.add(hostRouteCodec.decode(routeJson, context));
152 });
153 }
154 networkBuilder.hostRoutes(hostRoutes);
155
Jian Li97e6fc32021-02-01 20:36:45 +0900156 // parse DNSes
157 Set<IpAddress> dnses = new HashSet<>();
158 JsonNode dnsesJson = json.get(DNSES);
159 if (dnsesJson != null) {
160 for (int i = 0; i < dnsesJson.size(); i++) {
161 JsonNode dnsJson = dnsesJson.get(i);
162 if (dnsJson != null) {
163 dnses.add(IpAddress.valueOf(dnsJson.asText()));
164 }
165 }
166 }
167 networkBuilder.dnses(dnses);
168
Jian Lifeb84802021-01-12 16:34:49 +0900169 log.trace("Network is {}", networkBuilder.build().toString());
170
171 return networkBuilder.build();
172 }
173}