blob: 6e00655636accfe3c1af2cd22c6fde113f27bc47 [file] [log] [blame]
Jian Li49109b52019-01-22 00:17:28 +09001/*
2 * Copyright 2019-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.k8snode.codec;
17
18import com.fasterxml.jackson.databind.JsonNode;
19import com.fasterxml.jackson.databind.node.ObjectNode;
Jian Lie2a04ce2020-07-01 19:07:02 +090020import org.apache.commons.lang.StringUtils;
Jian Li49109b52019-01-22 00:17:28 +090021import org.onlab.packet.IpAddress;
Jian Lic2242bd2020-09-03 13:12:14 +090022import org.onlab.packet.MacAddress;
Jian Li49109b52019-01-22 00:17:28 +090023import org.onosproject.codec.CodecContext;
24import org.onosproject.codec.JsonCodec;
25import org.onosproject.k8snode.api.DefaultK8sNode;
26import org.onosproject.k8snode.api.K8sNode;
Jian Li6d2ffbf2020-11-04 15:58:18 +090027import org.onosproject.k8snode.api.K8sNodeInfo;
Jian Li49109b52019-01-22 00:17:28 +090028import org.onosproject.k8snode.api.K8sNodeState;
29import org.onosproject.net.DeviceId;
30import org.slf4j.Logger;
31
32import static com.google.common.base.Preconditions.checkNotNull;
33import static org.onlab.util.Tools.nullIsIllegal;
Jian Lie2a04ce2020-07-01 19:07:02 +090034import static org.onosproject.k8snode.api.Constants.DEFAULT_CLUSTER_NAME;
35import static org.onosproject.k8snode.api.Constants.DEFAULT_SEGMENT_ID;
Jian Li49109b52019-01-22 00:17:28 +090036import static org.slf4j.LoggerFactory.getLogger;
37
38/**
39 * Kubernetes node codec used for serializing and de-serializing JSON string.
40 */
41public final class K8sNodeCodec extends JsonCodec<K8sNode> {
42
43 private final Logger log = getLogger(getClass());
44
Jian Lie2a04ce2020-07-01 19:07:02 +090045 private static final String CLUSTER_NAME = "clusterName";
Jian Li49109b52019-01-22 00:17:28 +090046 private static final String HOSTNAME = "hostname";
47 private static final String TYPE = "type";
Jian Li732c3422020-09-07 17:01:11 +090048 private static final String MODE = "mode";
Jian Lie2a04ce2020-07-01 19:07:02 +090049 private static final String SEGMENT_ID = "segmentId";
Jian Li49109b52019-01-22 00:17:28 +090050 private static final String MANAGEMENT_IP = "managementIp";
51 private static final String DATA_IP = "dataIp";
Jian Li4294af72020-10-07 02:12:33 +090052 private static final String NODE_IP = "nodeIp";
Jian Li6d2ffbf2020-11-04 15:58:18 +090053 private static final String NODE_MAC = "nodeMac";
Jian Li49109b52019-01-22 00:17:28 +090054 private static final String INTEGRATION_BRIDGE = "integrationBridge";
Jian Libf562c22019-04-15 18:07:14 +090055 private static final String EXTERNAL_BRIDGE = "externalBridge";
Jian Li1a2eb5d2019-08-27 02:07:05 +090056 private static final String LOCAL_BRIDGE = "localBridge";
Jian Lie2a04ce2020-07-01 19:07:02 +090057 private static final String TUNNEL_BRIDGE = "tunnelBridge";
Jian Li49109b52019-01-22 00:17:28 +090058 private static final String STATE = "state";
Jian Li0c632722019-05-08 15:58:04 +090059 private static final String EXTERNAL_INTF = "externalInterface";
60 private static final String EXTERNAL_BRIDGE_IP = "externalBridgeIp";
61 private static final String EXTERNAL_GATEWAY_IP = "externalGatewayIp";
Jian Lic2242bd2020-09-03 13:12:14 +090062 private static final String EXTERNAL_GATEWAY_MAC = "externalGatewayMac";
Jian Li49109b52019-01-22 00:17:28 +090063
64 private static final String MISSING_MESSAGE = " is required in K8sNode";
65
66 @Override
67 public ObjectNode encode(K8sNode node, CodecContext context) {
68 checkNotNull(node, "Kubernetes node cannot be null");
69
70 ObjectNode result = context.mapper().createObjectNode()
Jian Lie2a04ce2020-07-01 19:07:02 +090071 .put(CLUSTER_NAME, node.clusterName())
Jian Li49109b52019-01-22 00:17:28 +090072 .put(HOSTNAME, node.hostname())
73 .put(TYPE, node.type().name())
Jian Li732c3422020-09-07 17:01:11 +090074 .put(MODE, node.mode().name())
Jian Lie2a04ce2020-07-01 19:07:02 +090075 .put(SEGMENT_ID, node.segmentId())
Jian Li49109b52019-01-22 00:17:28 +090076 .put(STATE, node.state().name())
Jian Li4294af72020-10-07 02:12:33 +090077 .put(MANAGEMENT_IP, node.managementIp().toString())
78 .put(NODE_IP, node.nodeIp().toString());
Jian Li49109b52019-01-22 00:17:28 +090079
80 if (node.intgBridge() != null) {
81 result.put(INTEGRATION_BRIDGE, node.intgBridge().toString());
82 }
83
Jian Libf562c22019-04-15 18:07:14 +090084 if (node.extBridge() != null) {
85 result.put(EXTERNAL_BRIDGE, node.extBridge().toString());
86 }
87
Jian Li1a2eb5d2019-08-27 02:07:05 +090088 if (node.localBridge() != null) {
89 result.put(LOCAL_BRIDGE, node.localBridge().toString());
90 }
91
Jian Lie2a04ce2020-07-01 19:07:02 +090092 if (node.tunBridge() != null) {
93 result.put(TUNNEL_BRIDGE, node.tunBridge().toString());
94 }
95
Jian Li49109b52019-01-22 00:17:28 +090096 if (node.dataIp() != null) {
97 result.put(DATA_IP, node.dataIp().toString());
98 }
99
Jian Li6d2ffbf2020-11-04 15:58:18 +0900100 if (node.nodeMac() != null) {
101 result.put(NODE_MAC, node.nodeMac().toString());
102 }
103
Jian Li0c632722019-05-08 15:58:04 +0900104 if (node.extIntf() != null) {
105 result.put(EXTERNAL_INTF, node.extIntf());
106 }
107
108 if (node.extBridgeIp() != null) {
109 result.put(EXTERNAL_BRIDGE_IP, node.extBridgeIp().toString());
110 }
111
112 if (node.extGatewayIp() != null) {
113 result.put(EXTERNAL_GATEWAY_IP, node.extGatewayIp().toString());
114 }
115
Jian Lic2242bd2020-09-03 13:12:14 +0900116 if (node.extGatewayMac() != null) {
117 result.put(EXTERNAL_GATEWAY_MAC, node.extGatewayMac().toString());
118 }
119
Jian Li49109b52019-01-22 00:17:28 +0900120 return result;
121 }
122
123 @Override
124 public K8sNode decode(ObjectNode json, CodecContext context) {
125 if (json == null || !json.isObject()) {
126 return null;
127 }
128
Jian Lie2a04ce2020-07-01 19:07:02 +0900129 String clusterName = json.get(CLUSTER_NAME).asText();
130
131 if (StringUtils.isEmpty(clusterName)) {
132 clusterName = DEFAULT_CLUSTER_NAME;
133 }
134
Jian Li49109b52019-01-22 00:17:28 +0900135 String hostname = nullIsIllegal(json.get(HOSTNAME).asText(),
136 HOSTNAME + MISSING_MESSAGE);
137 String type = nullIsIllegal(json.get(TYPE).asText(),
138 TYPE + MISSING_MESSAGE);
139 String mIp = nullIsIllegal(json.get(MANAGEMENT_IP).asText(),
140 MANAGEMENT_IP + MISSING_MESSAGE);
Jian Li4294af72020-10-07 02:12:33 +0900141 String nIp = nullIsIllegal(json.get(NODE_IP).asText(),
142 NODE_IP + MISSING_MESSAGE);
Jian Li49109b52019-01-22 00:17:28 +0900143
Jian Li6d2ffbf2020-11-04 15:58:18 +0900144 K8sNodeInfo nodeInfo = new K8sNodeInfo(IpAddress.valueOf(nIp), null);
145
Jian Li49109b52019-01-22 00:17:28 +0900146 DefaultK8sNode.Builder nodeBuilder = DefaultK8sNode.builder()
Jian Lie2a04ce2020-07-01 19:07:02 +0900147 .clusterName(clusterName)
Jian Li49109b52019-01-22 00:17:28 +0900148 .hostname(hostname)
149 .type(K8sNode.Type.valueOf(type))
150 .state(K8sNodeState.INIT)
Jian Li4294af72020-10-07 02:12:33 +0900151 .managementIp(IpAddress.valueOf(mIp))
Jian Li6d2ffbf2020-11-04 15:58:18 +0900152 .nodeInfo(nodeInfo);
Jian Li49109b52019-01-22 00:17:28 +0900153
154 if (json.get(DATA_IP) != null) {
155 nodeBuilder.dataIp(IpAddress.valueOf(json.get(DATA_IP).asText()));
156 }
157
Jian Lie2a04ce2020-07-01 19:07:02 +0900158 JsonNode segmentIdJson = json.get(SEGMENT_ID);
159 int segmentId = DEFAULT_SEGMENT_ID;
160 if (segmentIdJson != null) {
161 segmentId = segmentIdJson.asInt();
162 }
163 nodeBuilder.segmentId(segmentId);
164
Jian Li49109b52019-01-22 00:17:28 +0900165 JsonNode intBridgeJson = json.get(INTEGRATION_BRIDGE);
166 if (intBridgeJson != null) {
167 nodeBuilder.intgBridge(DeviceId.deviceId(intBridgeJson.asText()));
168 }
169
Jian Libf562c22019-04-15 18:07:14 +0900170 JsonNode extBridgeJson = json.get(EXTERNAL_BRIDGE);
171 if (extBridgeJson != null) {
172 nodeBuilder.extBridge(DeviceId.deviceId(extBridgeJson.asText()));
173 }
174
Jian Li1a2eb5d2019-08-27 02:07:05 +0900175 JsonNode localBridgeJson = json.get(LOCAL_BRIDGE);
176 if (localBridgeJson != null) {
177 nodeBuilder.localBridge(DeviceId.deviceId(localBridgeJson.asText()));
178 }
179
Jian Lie2a04ce2020-07-01 19:07:02 +0900180 JsonNode tunBridgeJson = json.get(TUNNEL_BRIDGE);
181 if (tunBridgeJson != null) {
182 nodeBuilder.tunBridge(DeviceId.deviceId(tunBridgeJson.asText()));
183 }
184
Jian Li0c632722019-05-08 15:58:04 +0900185 JsonNode extIntfJson = json.get(EXTERNAL_INTF);
186 if (extIntfJson != null) {
187 nodeBuilder.extIntf(extIntfJson.asText());
188 }
189
190 JsonNode extBridgeIpJson = json.get(EXTERNAL_BRIDGE_IP);
191 if (extBridgeIpJson != null) {
192 nodeBuilder.extBridgeIp(IpAddress.valueOf(extBridgeIpJson.asText()));
193 }
194
195 JsonNode extGatewayIpJson = json.get(EXTERNAL_GATEWAY_IP);
196 if (extGatewayIpJson != null) {
197 nodeBuilder.extGatewayIp(IpAddress.valueOf(extGatewayIpJson.asText()));
198 }
199
Jian Lic2242bd2020-09-03 13:12:14 +0900200 JsonNode extGatewayMacJson = json.get(EXTERNAL_GATEWAY_MAC);
201 if (extGatewayMacJson != null) {
202 nodeBuilder.extGatewayMac(MacAddress.valueOf(extGatewayMacJson.asText()));
203 }
204
Jian Li49109b52019-01-22 00:17:28 +0900205 log.trace("node is {}", nodeBuilder.build().toString());
206
207 return nodeBuilder.build();
208 }
209}