blob: c8cc3773320cdd78cce5913db10861066c9c82c6 [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";
51 private static final String CIDR = "cidr";
52 private static final String HOST_ROUTES = "hostRoutes";
53 private static final String IP_POOL = "ipPool";
54
55 private static final String MISSING_MESSAGE = " is required in KubevirtNetwork";
56
57 @Override
58 public ObjectNode encode(KubevirtNetwork network, CodecContext context) {
59 checkNotNull(network, "Kubevirt network cannot be null");
60
61 ObjectNode result = context.mapper().createObjectNode()
62 .put(NETWORK_ID, network.networkId())
63 .put(TYPE, network.type().name())
64 .put(NAME, network.name())
65 .put(MTU, network.mtu())
66 .put(GATEWAY_IP, network.gatewayIp().toString())
67 .put(CIDR, network.cidr());
68
69 if (network.segmentId() != null) {
70 result.put(SEGMENT_ID, network.segmentId());
71 }
72
73 if (network.hostRoutes() != null && !network.hostRoutes().isEmpty()) {
74 ArrayNode hostRoutes = context.mapper().createArrayNode();
75 network.hostRoutes().forEach(hostRoute -> {
76 ObjectNode hostRouteJson =
77 context.codec(KubevirtHostRoute.class).encode(hostRoute, context);
78 hostRoutes.add(hostRouteJson);
79 });
80 result.set(HOST_ROUTES, hostRoutes);
81 }
82
83 if (network.ipPool() != null) {
84 ObjectNode ipPoolJson = context.codec(KubevirtIpPool.class).encode(network.ipPool(), context);
85 result.set(IP_POOL, ipPoolJson);
86 }
87
88 return result;
89 }
90
91 @Override
92 public KubevirtNetwork decode(ObjectNode json, CodecContext context) {
93 if (json == null || !json.isObject()) {
94 return null;
95 }
96
97 String networkId = nullIsIllegal(json.get(NETWORK_ID).asText(),
98 NETWORK_ID + MISSING_MESSAGE);
99 String type = nullIsIllegal(json.get(TYPE).asText(),
100 TYPE + MISSING_MESSAGE);
101 String name = nullIsIllegal(json.get(NAME).asText(),
102 NAME + MISSING_MESSAGE);
103 Integer mtu = nullIsIllegal(json.get(MTU).asInt(),
104 MTU + MISSING_MESSAGE);
105 String gatewayIp = nullIsIllegal(json.get(GATEWAY_IP).asText(),
106 GATEWAY_IP + MISSING_MESSAGE);
107 String cidr = nullIsIllegal(json.get(CIDR).asText(),
108 CIDR + MISSING_MESSAGE);
109
110 KubevirtNetwork.Builder networkBuilder = DefaultKubevirtNetwork.builder()
111 .networkId(networkId)
112 .type(KubevirtNetwork.Type.valueOf(type))
113 .name(name)
114 .mtu(mtu)
115 .gatewayIp(IpAddress.valueOf(gatewayIp))
116 .cidr(cidr);
117
Jian Li3ba5c582021-01-14 11:30:36 +0900118 if (!type.equals(KubevirtNetwork.Type.FLAT.name())) {
119 JsonNode segmentIdJson = json.get(SEGMENT_ID);
120 if (segmentIdJson != null) {
121 networkBuilder.segmentId(segmentIdJson.asText());
122 }
123 }
124
Jian Lifc0b8902021-01-12 16:34:49 +0900125 JsonNode ipPoolJson = json.get(IP_POOL);
126 if (ipPoolJson != null) {
127 final JsonCodec<KubevirtIpPool>
128 ipPoolCodec = context.codec(KubevirtIpPool.class);
129 networkBuilder.ipPool(ipPoolCodec.decode(
130 (ObjectNode) ipPoolJson.deepCopy(), context));
131 }
132
133 // parse host routes
134 Set<KubevirtHostRoute> hostRoutes = new HashSet<>();
135 JsonNode hostRoutesJson = json.get(HOST_ROUTES);
136 if (hostRoutesJson != null) {
137 final JsonCodec<KubevirtHostRoute>
138 hostRouteCodec = context.codec(KubevirtHostRoute.class);
139
140 IntStream.range(0, hostRoutesJson.size()).forEach(i -> {
141 ObjectNode routeJson = get(hostRoutesJson, i);
142 hostRoutes.add(hostRouteCodec.decode(routeJson, context));
143 });
144 }
145 networkBuilder.hostRoutes(hostRoutes);
146
147 log.trace("Network is {}", networkBuilder.build().toString());
148
149 return networkBuilder.build();
150 }
151}