blob: 4a7e77c56dc4efff07e6faf203d8b459bf7e501f [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;
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 Li4fe40e52021-01-06 03:29:58 +090051 private static final String TUNNEL_BRIDGE = "tunnelBridge";
Jian Licc01e452020-12-21 01:44:05 +090052 private static final String STATE = "state";
53 private static final String PHYSICAL_INTERFACES = "phyIntfs";
54
55 private static final String MISSING_MESSAGE = " is required in OpenstackNode";
56
57 @Override
58 public ObjectNode encode(KubevirtNode node, CodecContext context) {
59 checkNotNull(node, "Kubevirt node cannot be null");
60
61 ObjectNode result = context.mapper().createObjectNode()
62 .put(HOST_NAME, node.hostname())
63 .put(TYPE, node.type().name())
64 .put(STATE, node.state().name())
65 .put(MANAGEMENT_IP, node.managementIp().toString());
66
67 // serialize integration bridge config
68 if (node.intgBridge() != null) {
69 result.put(INTEGRATION_BRIDGE, node.intgBridge().toString());
70 }
71
Jian Li4fe40e52021-01-06 03:29:58 +090072 // serialize tunnel bridge config
73 if (node.tunBridge() != null) {
74 result.put(TUNNEL_BRIDGE, node.tunBridge().toString());
75 }
76
Jian Licc01e452020-12-21 01:44:05 +090077 // serialize data IP only if it presents
78 if (node.dataIp() != null) {
79 result.put(DATA_IP, node.dataIp().toString());
80 }
81
82 // serialize physical interfaces, it is valid only if any of physical interface presents
83 if (node.phyIntfs() != null && !node.phyIntfs().isEmpty()) {
84 ArrayNode phyIntfs = context.mapper().createArrayNode();
85 node.phyIntfs().forEach(phyIntf -> {
86 ObjectNode phyIntfJson =
87 context.codec(KubevirtPhyInterface.class).encode(phyIntf, context);
88 phyIntfs.add(phyIntfJson);
89 });
90 result.set(PHYSICAL_INTERFACES, phyIntfs);
91 }
92
93 return result;
94 }
95
96 @Override
97 public KubevirtNode decode(ObjectNode json, CodecContext context) {
98 if (json == null || !json.isObject()) {
99 return null;
100 }
101
102 String hostname = nullIsIllegal(json.get(HOST_NAME).asText(),
103 HOST_NAME + MISSING_MESSAGE);
104 String type = nullIsIllegal(json.get(TYPE).asText(),
105 TYPE + MISSING_MESSAGE);
106 String mIp = nullIsIllegal(json.get(MANAGEMENT_IP).asText(),
107 MANAGEMENT_IP + MISSING_MESSAGE);
108
109 KubevirtNode.Builder nodeBuilder = DefaultKubevirtNode.builder()
110 .hostname(hostname)
111 .type(KubevirtNode.Type.valueOf(type))
112 .state(KubevirtNodeState.INIT)
113 .managementIp(IpAddress.valueOf(mIp));
114
115 if (json.get(DATA_IP) != null) {
116 nodeBuilder.dataIp(IpAddress.valueOf(json.get(DATA_IP).asText()));
117 }
118
119 JsonNode intBridgeJson = json.get(INTEGRATION_BRIDGE);
120 if (intBridgeJson != null) {
121 nodeBuilder.intgBridge(DeviceId.deviceId(intBridgeJson.asText()));
122 }
123
Jian Li4fe40e52021-01-06 03:29:58 +0900124 JsonNode tunBridgeJson = json.get(TUNNEL_BRIDGE);
125 if (tunBridgeJson != null) {
126 nodeBuilder.tunBridge(DeviceId.deviceId(tunBridgeJson.asText()));
127 }
128
Jian Licc01e452020-12-21 01:44:05 +0900129 // parse physical interfaces
130 List<KubevirtPhyInterface> phyIntfs = new ArrayList<>();
131 JsonNode phyIntfsJson = json.get(PHYSICAL_INTERFACES);
132 if (phyIntfsJson != null) {
133 final JsonCodec<KubevirtPhyInterface>
134 phyIntfCodec = context.codec(KubevirtPhyInterface.class);
135
136 IntStream.range(0, phyIntfsJson.size()).forEach(i -> {
137 ObjectNode intfJson = get(phyIntfsJson, i);
138 phyIntfs.add(phyIntfCodec.decode(intfJson, context));
139 });
140 }
141 nodeBuilder.phyIntfs(phyIntfs);
142
143 log.trace("node is {}", nodeBuilder.build().toString());
144
145 return nodeBuilder.build();
146 }
147}