blob: 8793c460edf045c0aa7e4bdeec9fc0937bf58e6c [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;
22import org.onosproject.codec.CodecContext;
23import org.onosproject.codec.JsonCodec;
24import org.onosproject.k8snode.api.DefaultK8sNode;
25import org.onosproject.k8snode.api.K8sNode;
26import org.onosproject.k8snode.api.K8sNodeState;
27import org.onosproject.net.DeviceId;
28import org.slf4j.Logger;
29
30import static com.google.common.base.Preconditions.checkNotNull;
31import static org.onlab.util.Tools.nullIsIllegal;
Jian Lie2a04ce2020-07-01 19:07:02 +090032import static org.onosproject.k8snode.api.Constants.DEFAULT_CLUSTER_NAME;
33import static org.onosproject.k8snode.api.Constants.DEFAULT_SEGMENT_ID;
Jian Li49109b52019-01-22 00:17:28 +090034import static org.slf4j.LoggerFactory.getLogger;
35
36/**
37 * Kubernetes node codec used for serializing and de-serializing JSON string.
38 */
39public final class K8sNodeCodec extends JsonCodec<K8sNode> {
40
41 private final Logger log = getLogger(getClass());
42
Jian Lie2a04ce2020-07-01 19:07:02 +090043 private static final String CLUSTER_NAME = "clusterName";
Jian Li49109b52019-01-22 00:17:28 +090044 private static final String HOSTNAME = "hostname";
45 private static final String TYPE = "type";
Jian Lie2a04ce2020-07-01 19:07:02 +090046 private static final String SEGMENT_ID = "segmentId";
Jian Li49109b52019-01-22 00:17:28 +090047 private static final String MANAGEMENT_IP = "managementIp";
48 private static final String DATA_IP = "dataIp";
49 private static final String INTEGRATION_BRIDGE = "integrationBridge";
Jian Libf562c22019-04-15 18:07:14 +090050 private static final String EXTERNAL_BRIDGE = "externalBridge";
Jian Li1a2eb5d2019-08-27 02:07:05 +090051 private static final String LOCAL_BRIDGE = "localBridge";
Jian Lie2a04ce2020-07-01 19:07:02 +090052 private static final String TUNNEL_BRIDGE = "tunnelBridge";
Jian Li49109b52019-01-22 00:17:28 +090053 private static final String STATE = "state";
Jian Li0c632722019-05-08 15:58:04 +090054 private static final String EXTERNAL_INTF = "externalInterface";
55 private static final String EXTERNAL_BRIDGE_IP = "externalBridgeIp";
56 private static final String EXTERNAL_GATEWAY_IP = "externalGatewayIp";
Jian Li49109b52019-01-22 00:17:28 +090057
58 private static final String MISSING_MESSAGE = " is required in K8sNode";
59
60 @Override
61 public ObjectNode encode(K8sNode node, CodecContext context) {
62 checkNotNull(node, "Kubernetes node cannot be null");
63
64 ObjectNode result = context.mapper().createObjectNode()
Jian Lie2a04ce2020-07-01 19:07:02 +090065 .put(CLUSTER_NAME, node.clusterName())
Jian Li49109b52019-01-22 00:17:28 +090066 .put(HOSTNAME, node.hostname())
67 .put(TYPE, node.type().name())
Jian Lie2a04ce2020-07-01 19:07:02 +090068 .put(SEGMENT_ID, node.segmentId())
Jian Li49109b52019-01-22 00:17:28 +090069 .put(STATE, node.state().name())
70 .put(MANAGEMENT_IP, node.managementIp().toString());
71
72 if (node.intgBridge() != null) {
73 result.put(INTEGRATION_BRIDGE, node.intgBridge().toString());
74 }
75
Jian Libf562c22019-04-15 18:07:14 +090076 if (node.extBridge() != null) {
77 result.put(EXTERNAL_BRIDGE, node.extBridge().toString());
78 }
79
Jian Li1a2eb5d2019-08-27 02:07:05 +090080 if (node.localBridge() != null) {
81 result.put(LOCAL_BRIDGE, node.localBridge().toString());
82 }
83
Jian Lie2a04ce2020-07-01 19:07:02 +090084 if (node.tunBridge() != null) {
85 result.put(TUNNEL_BRIDGE, node.tunBridge().toString());
86 }
87
Jian Li49109b52019-01-22 00:17:28 +090088 if (node.dataIp() != null) {
89 result.put(DATA_IP, node.dataIp().toString());
90 }
91
Jian Li0c632722019-05-08 15:58:04 +090092 if (node.extIntf() != null) {
93 result.put(EXTERNAL_INTF, node.extIntf());
94 }
95
96 if (node.extBridgeIp() != null) {
97 result.put(EXTERNAL_BRIDGE_IP, node.extBridgeIp().toString());
98 }
99
100 if (node.extGatewayIp() != null) {
101 result.put(EXTERNAL_GATEWAY_IP, node.extGatewayIp().toString());
102 }
103
Jian Li49109b52019-01-22 00:17:28 +0900104 return result;
105 }
106
107 @Override
108 public K8sNode decode(ObjectNode json, CodecContext context) {
109 if (json == null || !json.isObject()) {
110 return null;
111 }
112
Jian Lie2a04ce2020-07-01 19:07:02 +0900113 String clusterName = json.get(CLUSTER_NAME).asText();
114
115 if (StringUtils.isEmpty(clusterName)) {
116 clusterName = DEFAULT_CLUSTER_NAME;
117 }
118
Jian Li49109b52019-01-22 00:17:28 +0900119 String hostname = nullIsIllegal(json.get(HOSTNAME).asText(),
120 HOSTNAME + MISSING_MESSAGE);
121 String type = nullIsIllegal(json.get(TYPE).asText(),
122 TYPE + MISSING_MESSAGE);
123 String mIp = nullIsIllegal(json.get(MANAGEMENT_IP).asText(),
124 MANAGEMENT_IP + MISSING_MESSAGE);
125
126 DefaultK8sNode.Builder nodeBuilder = DefaultK8sNode.builder()
Jian Lie2a04ce2020-07-01 19:07:02 +0900127 .clusterName(clusterName)
Jian Li49109b52019-01-22 00:17:28 +0900128 .hostname(hostname)
129 .type(K8sNode.Type.valueOf(type))
130 .state(K8sNodeState.INIT)
131 .managementIp(IpAddress.valueOf(mIp));
132
133 if (json.get(DATA_IP) != null) {
134 nodeBuilder.dataIp(IpAddress.valueOf(json.get(DATA_IP).asText()));
135 }
136
Jian Lie2a04ce2020-07-01 19:07:02 +0900137 JsonNode segmentIdJson = json.get(SEGMENT_ID);
138 int segmentId = DEFAULT_SEGMENT_ID;
139 if (segmentIdJson != null) {
140 segmentId = segmentIdJson.asInt();
141 }
142 nodeBuilder.segmentId(segmentId);
143
Jian Li49109b52019-01-22 00:17:28 +0900144 JsonNode intBridgeJson = json.get(INTEGRATION_BRIDGE);
145 if (intBridgeJson != null) {
146 nodeBuilder.intgBridge(DeviceId.deviceId(intBridgeJson.asText()));
147 }
148
Jian Libf562c22019-04-15 18:07:14 +0900149 JsonNode extBridgeJson = json.get(EXTERNAL_BRIDGE);
150 if (extBridgeJson != null) {
151 nodeBuilder.extBridge(DeviceId.deviceId(extBridgeJson.asText()));
152 }
153
Jian Li1a2eb5d2019-08-27 02:07:05 +0900154 JsonNode localBridgeJson = json.get(LOCAL_BRIDGE);
155 if (localBridgeJson != null) {
156 nodeBuilder.localBridge(DeviceId.deviceId(localBridgeJson.asText()));
157 }
158
Jian Lie2a04ce2020-07-01 19:07:02 +0900159 JsonNode tunBridgeJson = json.get(TUNNEL_BRIDGE);
160 if (tunBridgeJson != null) {
161 nodeBuilder.tunBridge(DeviceId.deviceId(tunBridgeJson.asText()));
162 }
163
Jian Li0c632722019-05-08 15:58:04 +0900164 JsonNode extIntfJson = json.get(EXTERNAL_INTF);
165 if (extIntfJson != null) {
166 nodeBuilder.extIntf(extIntfJson.asText());
167 }
168
169 JsonNode extBridgeIpJson = json.get(EXTERNAL_BRIDGE_IP);
170 if (extBridgeIpJson != null) {
171 nodeBuilder.extBridgeIp(IpAddress.valueOf(extBridgeIpJson.asText()));
172 }
173
174 JsonNode extGatewayIpJson = json.get(EXTERNAL_GATEWAY_IP);
175 if (extGatewayIpJson != null) {
176 nodeBuilder.extGatewayIp(IpAddress.valueOf(extGatewayIpJson.asText()));
177 }
178
Jian Li49109b52019-01-22 00:17:28 +0900179 log.trace("node is {}", nodeBuilder.build().toString());
180
181 return nodeBuilder.build();
182 }
183}