blob: 5267c770937e3bae21755722c3d6302ffedf834b [file] [log] [blame]
Jian Licc01e452020-12-21 01:44:05 +09001/*
2 * Copyright 2020-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.kubevirtnode.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.kubevirtnode.api.DefaultKubevirtNode;
Daniel Park5ff76b72022-09-26 22:58:53 +090025import org.onosproject.kubevirtnode.api.KubernetesExternalLbInterface;
Jian Licc01e452020-12-21 01:44:05 +090026import org.onosproject.kubevirtnode.api.KubevirtNode;
27import org.onosproject.kubevirtnode.api.KubevirtNodeState;
28import org.onosproject.kubevirtnode.api.KubevirtPhyInterface;
29import org.onosproject.net.DeviceId;
30import org.slf4j.Logger;
31
32import java.util.ArrayList;
33import java.util.List;
34import java.util.stream.IntStream;
35
36import static com.google.common.base.Preconditions.checkNotNull;
37import static org.onlab.util.Tools.nullIsIllegal;
38import static org.onosproject.kubevirtnode.api.Constants.DATA_IP;
39import static org.onosproject.kubevirtnode.api.Constants.HOST_NAME;
40import static org.onosproject.kubevirtnode.api.Constants.MANAGEMENT_IP;
41import static org.slf4j.LoggerFactory.getLogger;
42
43/**
44 * Kubevirt node codec used for serializing and de-serializing JSON string.
45 */
46public final class KubevirtNodeCodec extends JsonCodec<KubevirtNode> {
47
48 private final Logger log = getLogger(getClass());
49
50 private static final String TYPE = "type";
51 private static final String INTEGRATION_BRIDGE = "integrationBridge";
Jian Li4fe40e52021-01-06 03:29:58 +090052 private static final String TUNNEL_BRIDGE = "tunnelBridge";
Jian Licc01e452020-12-21 01:44:05 +090053 private static final String STATE = "state";
54 private static final String PHYSICAL_INTERFACES = "phyIntfs";
Daniel Park515f5f32021-02-22 17:12:20 +090055 private static final String GATEWAY_BRIDGE_NAME = "gatewayBridgeName";
Daniel Park5ff76b72022-09-26 22:58:53 +090056 private static final String KUBERNETES_EXTERNAL_LB_INTERFACE = "kubernetesExternalLbInterface";
Jian Licc01e452020-12-21 01:44:05 +090057
Daniel Park5ff76b72022-09-26 22:58:53 +090058 private static final String MISSING_MESSAGE = " is required in KubevirtNode";
Jian Licc01e452020-12-21 01:44:05 +090059
60 @Override
61 public ObjectNode encode(KubevirtNode node, CodecContext context) {
62 checkNotNull(node, "Kubevirt node cannot be null");
63
64 ObjectNode result = context.mapper().createObjectNode()
65 .put(HOST_NAME, node.hostname())
66 .put(TYPE, node.type().name())
67 .put(STATE, node.state().name())
68 .put(MANAGEMENT_IP, node.managementIp().toString());
69
70 // serialize integration bridge config
71 if (node.intgBridge() != null) {
72 result.put(INTEGRATION_BRIDGE, node.intgBridge().toString());
73 }
74
Jian Li4fe40e52021-01-06 03:29:58 +090075 // serialize tunnel bridge config
76 if (node.tunBridge() != null) {
77 result.put(TUNNEL_BRIDGE, node.tunBridge().toString());
78 }
79
Jian Licc01e452020-12-21 01:44:05 +090080 // serialize data IP only if it presents
81 if (node.dataIp() != null) {
82 result.put(DATA_IP, node.dataIp().toString());
83 }
84
85 // serialize physical interfaces, it is valid only if any of physical interface presents
86 if (node.phyIntfs() != null && !node.phyIntfs().isEmpty()) {
87 ArrayNode phyIntfs = context.mapper().createArrayNode();
88 node.phyIntfs().forEach(phyIntf -> {
89 ObjectNode phyIntfJson =
90 context.codec(KubevirtPhyInterface.class).encode(phyIntf, context);
91 phyIntfs.add(phyIntfJson);
92 });
93 result.set(PHYSICAL_INTERFACES, phyIntfs);
94 }
95
Daniel Park515f5f32021-02-22 17:12:20 +090096 // serialize external bridge if exist
97 if (node.gatewayBridgeName() != null) {
98 result.put(GATEWAY_BRIDGE_NAME, node.gatewayBridgeName());
99 }
100
Daniel Park5ff76b72022-09-26 22:58:53 +0900101 // serialize kubernetex external load balancer interface if exist
102 if (node.kubernetesExternalLbInterface() != null) {
103 ObjectNode elbIntfJson = context.codec(KubernetesExternalLbInterface.class)
104 .encode(node.kubernetesExternalLbInterface(), context);
105 result.put(KUBERNETES_EXTERNAL_LB_INTERFACE, elbIntfJson.toString());
Daniel Park734b5532022-09-26 15:13:59 +0900106 }
107
Jian Licc01e452020-12-21 01:44:05 +0900108 return result;
109 }
110
111 @Override
112 public KubevirtNode decode(ObjectNode json, CodecContext context) {
113 if (json == null || !json.isObject()) {
114 return null;
115 }
116
117 String hostname = nullIsIllegal(json.get(HOST_NAME).asText(),
118 HOST_NAME + MISSING_MESSAGE);
119 String type = nullIsIllegal(json.get(TYPE).asText(),
120 TYPE + MISSING_MESSAGE);
121 String mIp = nullIsIllegal(json.get(MANAGEMENT_IP).asText(),
122 MANAGEMENT_IP + MISSING_MESSAGE);
123
124 KubevirtNode.Builder nodeBuilder = DefaultKubevirtNode.builder()
125 .hostname(hostname)
126 .type(KubevirtNode.Type.valueOf(type))
127 .state(KubevirtNodeState.INIT)
128 .managementIp(IpAddress.valueOf(mIp));
129
130 if (json.get(DATA_IP) != null) {
131 nodeBuilder.dataIp(IpAddress.valueOf(json.get(DATA_IP).asText()));
132 }
133
134 JsonNode intBridgeJson = json.get(INTEGRATION_BRIDGE);
135 if (intBridgeJson != null) {
136 nodeBuilder.intgBridge(DeviceId.deviceId(intBridgeJson.asText()));
137 }
138
Jian Li4fe40e52021-01-06 03:29:58 +0900139 JsonNode tunBridgeJson = json.get(TUNNEL_BRIDGE);
140 if (tunBridgeJson != null) {
141 nodeBuilder.tunBridge(DeviceId.deviceId(tunBridgeJson.asText()));
142 }
143
Jian Licc01e452020-12-21 01:44:05 +0900144 // parse physical interfaces
145 List<KubevirtPhyInterface> phyIntfs = new ArrayList<>();
146 JsonNode phyIntfsJson = json.get(PHYSICAL_INTERFACES);
147 if (phyIntfsJson != null) {
148 final JsonCodec<KubevirtPhyInterface>
149 phyIntfCodec = context.codec(KubevirtPhyInterface.class);
150
151 IntStream.range(0, phyIntfsJson.size()).forEach(i -> {
152 ObjectNode intfJson = get(phyIntfsJson, i);
153 phyIntfs.add(phyIntfCodec.decode(intfJson, context));
154 });
155 }
156 nodeBuilder.phyIntfs(phyIntfs);
157
Daniel Park515f5f32021-02-22 17:12:20 +0900158 JsonNode externalBridgeJson = json.get(GATEWAY_BRIDGE_NAME);
159 if (externalBridgeJson != null) {
160 nodeBuilder.gatewayBridgeName(externalBridgeJson.asText());
161 }
162
Daniel Park5ff76b72022-09-26 22:58:53 +0900163 JsonNode elbIntfJson = json.get(KUBERNETES_EXTERNAL_LB_INTERFACE);
Daniel Park734b5532022-09-26 15:13:59 +0900164
Daniel Park5ff76b72022-09-26 22:58:53 +0900165 if (elbIntfJson != null) {
166 final JsonCodec<KubernetesExternalLbInterface>
167 kubernetesExternalLbInterfaceCodecJsonCodec = context.codec(KubernetesExternalLbInterface.class);
168 ObjectNode elbIntfObjNode = elbIntfJson.deepCopy();
Daniel Park734b5532022-09-26 15:13:59 +0900169
Daniel Park5ff76b72022-09-26 22:58:53 +0900170 nodeBuilder.kubernetesExternalLbInterface(
171 kubernetesExternalLbInterfaceCodecJsonCodec.decode(elbIntfObjNode, context));
Daniel Park734b5532022-09-26 15:13:59 +0900172
Daniel Park734b5532022-09-26 15:13:59 +0900173 }
174
175 log.trace("node is {}", nodeBuilder.build());
Jian Licc01e452020-12-21 01:44:05 +0900176
177 return nodeBuilder.build();
178 }
179}