blob: f1ea1c9f2c6c0f56717096fe9979e885f4b67b40 [file] [log] [blame]
Jian Li171582e2020-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;
25import org.onosproject.kubevirtnode.api.KubevirtNode;
26import org.onosproject.kubevirtnode.api.KubevirtNodeState;
27import org.onosproject.kubevirtnode.api.KubevirtPhyInterface;
28import org.onosproject.net.DeviceId;
29import org.slf4j.Logger;
30
31import java.util.ArrayList;
32import java.util.List;
33import java.util.stream.IntStream;
34
35import static com.google.common.base.Preconditions.checkNotNull;
36import static org.onlab.util.Tools.nullIsIllegal;
37import static org.onosproject.kubevirtnode.api.Constants.DATA_IP;
38import static org.onosproject.kubevirtnode.api.Constants.HOST_NAME;
39import static org.onosproject.kubevirtnode.api.Constants.MANAGEMENT_IP;
40import static org.slf4j.LoggerFactory.getLogger;
41
42/**
43 * Kubevirt node codec used for serializing and de-serializing JSON string.
44 */
45public final class KubevirtNodeCodec extends JsonCodec<KubevirtNode> {
46
47 private final Logger log = getLogger(getClass());
48
49 private static final String TYPE = "type";
50 private static final String INTEGRATION_BRIDGE = "integrationBridge";
Jian Li138f51f2021-01-06 03:29:58 +090051 private static final String TUNNEL_BRIDGE = "tunnelBridge";
Jian Li171582e2020-12-21 01:44:05 +090052 private static final String STATE = "state";
53 private static final String PHYSICAL_INTERFACES = "phyIntfs";
Daniel Park54d8baf2021-02-22 17:12:20 +090054 private static final String GATEWAY_BRIDGE_NAME = "gatewayBridgeName";
Jian Li171582e2020-12-21 01:44:05 +090055
56 private static final String MISSING_MESSAGE = " is required in OpenstackNode";
57
58 @Override
59 public ObjectNode encode(KubevirtNode node, CodecContext context) {
60 checkNotNull(node, "Kubevirt node cannot be null");
61
62 ObjectNode result = context.mapper().createObjectNode()
63 .put(HOST_NAME, node.hostname())
64 .put(TYPE, node.type().name())
65 .put(STATE, node.state().name())
66 .put(MANAGEMENT_IP, node.managementIp().toString());
67
68 // serialize integration bridge config
69 if (node.intgBridge() != null) {
70 result.put(INTEGRATION_BRIDGE, node.intgBridge().toString());
71 }
72
Jian Li138f51f2021-01-06 03:29:58 +090073 // serialize tunnel bridge config
74 if (node.tunBridge() != null) {
75 result.put(TUNNEL_BRIDGE, node.tunBridge().toString());
76 }
77
Jian Li171582e2020-12-21 01:44:05 +090078 // serialize data IP only if it presents
79 if (node.dataIp() != null) {
80 result.put(DATA_IP, node.dataIp().toString());
81 }
82
83 // serialize physical interfaces, it is valid only if any of physical interface presents
84 if (node.phyIntfs() != null && !node.phyIntfs().isEmpty()) {
85 ArrayNode phyIntfs = context.mapper().createArrayNode();
86 node.phyIntfs().forEach(phyIntf -> {
87 ObjectNode phyIntfJson =
88 context.codec(KubevirtPhyInterface.class).encode(phyIntf, context);
89 phyIntfs.add(phyIntfJson);
90 });
91 result.set(PHYSICAL_INTERFACES, phyIntfs);
92 }
93
Daniel Park54d8baf2021-02-22 17:12:20 +090094 // serialize external bridge if exist
95 if (node.gatewayBridgeName() != null) {
96 result.put(GATEWAY_BRIDGE_NAME, node.gatewayBridgeName());
97 }
98
Jian Li171582e2020-12-21 01:44:05 +090099 return result;
100 }
101
102 @Override
103 public KubevirtNode decode(ObjectNode json, CodecContext context) {
104 if (json == null || !json.isObject()) {
105 return null;
106 }
107
108 String hostname = nullIsIllegal(json.get(HOST_NAME).asText(),
109 HOST_NAME + MISSING_MESSAGE);
110 String type = nullIsIllegal(json.get(TYPE).asText(),
111 TYPE + MISSING_MESSAGE);
112 String mIp = nullIsIllegal(json.get(MANAGEMENT_IP).asText(),
113 MANAGEMENT_IP + MISSING_MESSAGE);
114
115 KubevirtNode.Builder nodeBuilder = DefaultKubevirtNode.builder()
116 .hostname(hostname)
117 .type(KubevirtNode.Type.valueOf(type))
118 .state(KubevirtNodeState.INIT)
119 .managementIp(IpAddress.valueOf(mIp));
120
121 if (json.get(DATA_IP) != null) {
122 nodeBuilder.dataIp(IpAddress.valueOf(json.get(DATA_IP).asText()));
123 }
124
125 JsonNode intBridgeJson = json.get(INTEGRATION_BRIDGE);
126 if (intBridgeJson != null) {
127 nodeBuilder.intgBridge(DeviceId.deviceId(intBridgeJson.asText()));
128 }
129
Jian Li138f51f2021-01-06 03:29:58 +0900130 JsonNode tunBridgeJson = json.get(TUNNEL_BRIDGE);
131 if (tunBridgeJson != null) {
132 nodeBuilder.tunBridge(DeviceId.deviceId(tunBridgeJson.asText()));
133 }
134
Jian Li171582e2020-12-21 01:44:05 +0900135 // parse physical interfaces
136 List<KubevirtPhyInterface> phyIntfs = new ArrayList<>();
137 JsonNode phyIntfsJson = json.get(PHYSICAL_INTERFACES);
138 if (phyIntfsJson != null) {
139 final JsonCodec<KubevirtPhyInterface>
140 phyIntfCodec = context.codec(KubevirtPhyInterface.class);
141
142 IntStream.range(0, phyIntfsJson.size()).forEach(i -> {
143 ObjectNode intfJson = get(phyIntfsJson, i);
144 phyIntfs.add(phyIntfCodec.decode(intfJson, context));
145 });
146 }
147 nodeBuilder.phyIntfs(phyIntfs);
148
Daniel Park54d8baf2021-02-22 17:12:20 +0900149 JsonNode externalBridgeJson = json.get(GATEWAY_BRIDGE_NAME);
150 if (externalBridgeJson != null) {
151 nodeBuilder.gatewayBridgeName(externalBridgeJson.asText());
152 }
153
Jian Li171582e2020-12-21 01:44:05 +0900154 log.trace("node is {}", nodeBuilder.build().toString());
155
156 return nodeBuilder.build();
157 }
158}