blob: 2d8a07ce1d9e63240935d77d8aabe77ffaa56f30 [file] [log] [blame]
Jian Lifc0b8902021-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";
Jian Lie2abe812021-08-12 18:03:30 +090051 private static final String DEFAULT_ROUTE = "defaultRoute";
Jian Lifc0b8902021-01-12 16:34:49 +090052 private static final String CIDR = "cidr";
53 private static final String HOST_ROUTES = "hostRoutes";
54 private static final String IP_POOL = "ipPool";
Jian Li4c35a262021-02-01 20:36:45 +090055 private static final String DNSES = "dnses";
Jian Lifc0b8902021-01-12 16:34:49 +090056
57 private static final String MISSING_MESSAGE = " is required in KubevirtNetwork";
58
59 @Override
60 public ObjectNode encode(KubevirtNetwork network, CodecContext context) {
61 checkNotNull(network, "Kubevirt network cannot be null");
62
63 ObjectNode result = context.mapper().createObjectNode()
64 .put(NETWORK_ID, network.networkId())
65 .put(TYPE, network.type().name())
66 .put(NAME, network.name())
67 .put(MTU, network.mtu())
68 .put(GATEWAY_IP, network.gatewayIp().toString())
Jian Lie2abe812021-08-12 18:03:30 +090069 .put(DEFAULT_ROUTE, network.defaultRoute())
Jian Lifc0b8902021-01-12 16:34:49 +090070 .put(CIDR, network.cidr());
71
72 if (network.segmentId() != null) {
73 result.put(SEGMENT_ID, network.segmentId());
74 }
75
76 if (network.hostRoutes() != null && !network.hostRoutes().isEmpty()) {
77 ArrayNode hostRoutes = context.mapper().createArrayNode();
78 network.hostRoutes().forEach(hostRoute -> {
79 ObjectNode hostRouteJson =
80 context.codec(KubevirtHostRoute.class).encode(hostRoute, context);
81 hostRoutes.add(hostRouteJson);
82 });
83 result.set(HOST_ROUTES, hostRoutes);
84 }
85
86 if (network.ipPool() != null) {
87 ObjectNode ipPoolJson = context.codec(KubevirtIpPool.class).encode(network.ipPool(), context);
88 result.set(IP_POOL, ipPoolJson);
89 }
90
Jian Li4c35a262021-02-01 20:36:45 +090091 if (network.dnses() != null && !network.dnses().isEmpty()) {
92 ArrayNode dnses = context.mapper().createArrayNode();
93 network.dnses().forEach(dns -> {
94 dnses.add(dns.toString());
95 });
96 result.set(DNSES, dnses);
97 }
98
Jian Lifc0b8902021-01-12 16:34:49 +090099 return result;
100 }
101
102 @Override
103 public KubevirtNetwork decode(ObjectNode json, CodecContext context) {
104 if (json == null || !json.isObject()) {
105 return null;
106 }
107
108 String networkId = nullIsIllegal(json.get(NETWORK_ID).asText(),
109 NETWORK_ID + MISSING_MESSAGE);
110 String type = nullIsIllegal(json.get(TYPE).asText(),
111 TYPE + MISSING_MESSAGE);
112 String name = nullIsIllegal(json.get(NAME).asText(),
113 NAME + MISSING_MESSAGE);
114 Integer mtu = nullIsIllegal(json.get(MTU).asInt(),
115 MTU + MISSING_MESSAGE);
116 String gatewayIp = nullIsIllegal(json.get(GATEWAY_IP).asText(),
117 GATEWAY_IP + MISSING_MESSAGE);
Jian Lie2abe812021-08-12 18:03:30 +0900118 boolean defaultRoute = nullIsIllegal(json.get(DEFAULT_ROUTE).asBoolean(),
119 DEFAULT_ROUTE + MISSING_MESSAGE);
Jian Lifc0b8902021-01-12 16:34:49 +0900120 String cidr = nullIsIllegal(json.get(CIDR).asText(),
121 CIDR + MISSING_MESSAGE);
122
123 KubevirtNetwork.Builder networkBuilder = DefaultKubevirtNetwork.builder()
124 .networkId(networkId)
125 .type(KubevirtNetwork.Type.valueOf(type))
126 .name(name)
127 .mtu(mtu)
128 .gatewayIp(IpAddress.valueOf(gatewayIp))
Jian Lie2abe812021-08-12 18:03:30 +0900129 .defaultRoute(defaultRoute)
Jian Lifc0b8902021-01-12 16:34:49 +0900130 .cidr(cidr);
131
Jian Li3ba5c582021-01-14 11:30:36 +0900132 if (!type.equals(KubevirtNetwork.Type.FLAT.name())) {
133 JsonNode segmentIdJson = json.get(SEGMENT_ID);
134 if (segmentIdJson != null) {
135 networkBuilder.segmentId(segmentIdJson.asText());
136 }
137 }
138
Jian Lifc0b8902021-01-12 16:34:49 +0900139 JsonNode ipPoolJson = json.get(IP_POOL);
140 if (ipPoolJson != null) {
141 final JsonCodec<KubevirtIpPool>
142 ipPoolCodec = context.codec(KubevirtIpPool.class);
143 networkBuilder.ipPool(ipPoolCodec.decode(
144 (ObjectNode) ipPoolJson.deepCopy(), context));
145 }
146
147 // parse host routes
148 Set<KubevirtHostRoute> hostRoutes = new HashSet<>();
149 JsonNode hostRoutesJson = json.get(HOST_ROUTES);
150 if (hostRoutesJson != null) {
151 final JsonCodec<KubevirtHostRoute>
152 hostRouteCodec = context.codec(KubevirtHostRoute.class);
153
154 IntStream.range(0, hostRoutesJson.size()).forEach(i -> {
155 ObjectNode routeJson = get(hostRoutesJson, i);
156 hostRoutes.add(hostRouteCodec.decode(routeJson, context));
157 });
158 }
159 networkBuilder.hostRoutes(hostRoutes);
160
Jian Li4c35a262021-02-01 20:36:45 +0900161 // parse DNSes
162 Set<IpAddress> dnses = new HashSet<>();
163 JsonNode dnsesJson = json.get(DNSES);
164 if (dnsesJson != null) {
165 for (int i = 0; i < dnsesJson.size(); i++) {
166 JsonNode dnsJson = dnsesJson.get(i);
167 if (dnsJson != null) {
168 dnses.add(IpAddress.valueOf(dnsJson.asText()));
169 }
170 }
171 }
172 networkBuilder.dnses(dnses);
173
Jian Lifc0b8902021-01-12 16:34:49 +0900174 log.trace("Network is {}", networkBuilder.build().toString());
175
176 return networkBuilder.build();
177 }
178}