blob: 2a938e0d2dc06e4215180a5a04e0e97d68f621ff [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;
27import org.onosproject.k8snode.api.K8sNodeState;
28import org.onosproject.net.DeviceId;
29import org.slf4j.Logger;
30
31import static com.google.common.base.Preconditions.checkNotNull;
32import static org.onlab.util.Tools.nullIsIllegal;
Jian Lie2a04ce2020-07-01 19:07:02 +090033import static org.onosproject.k8snode.api.Constants.DEFAULT_CLUSTER_NAME;
34import static org.onosproject.k8snode.api.Constants.DEFAULT_SEGMENT_ID;
Jian Li49109b52019-01-22 00:17:28 +090035import static org.slf4j.LoggerFactory.getLogger;
36
37/**
38 * Kubernetes node codec used for serializing and de-serializing JSON string.
39 */
40public final class K8sNodeCodec extends JsonCodec<K8sNode> {
41
42 private final Logger log = getLogger(getClass());
43
Jian Lie2a04ce2020-07-01 19:07:02 +090044 private static final String CLUSTER_NAME = "clusterName";
Jian Li49109b52019-01-22 00:17:28 +090045 private static final String HOSTNAME = "hostname";
46 private static final String TYPE = "type";
Jian Li732c3422020-09-07 17:01:11 +090047 private static final String MODE = "mode";
Jian Lie2a04ce2020-07-01 19:07:02 +090048 private static final String SEGMENT_ID = "segmentId";
Jian Li49109b52019-01-22 00:17:28 +090049 private static final String MANAGEMENT_IP = "managementIp";
50 private static final String DATA_IP = "dataIp";
51 private static final String INTEGRATION_BRIDGE = "integrationBridge";
Jian Libf562c22019-04-15 18:07:14 +090052 private static final String EXTERNAL_BRIDGE = "externalBridge";
Jian Li1a2eb5d2019-08-27 02:07:05 +090053 private static final String LOCAL_BRIDGE = "localBridge";
Jian Lie2a04ce2020-07-01 19:07:02 +090054 private static final String TUNNEL_BRIDGE = "tunnelBridge";
Jian Li49109b52019-01-22 00:17:28 +090055 private static final String STATE = "state";
Jian Li0c632722019-05-08 15:58:04 +090056 private static final String EXTERNAL_INTF = "externalInterface";
57 private static final String EXTERNAL_BRIDGE_IP = "externalBridgeIp";
58 private static final String EXTERNAL_GATEWAY_IP = "externalGatewayIp";
Jian Lic2242bd2020-09-03 13:12:14 +090059 private static final String EXTERNAL_GATEWAY_MAC = "externalGatewayMac";
Jian Li49109b52019-01-22 00:17:28 +090060
61 private static final String MISSING_MESSAGE = " is required in K8sNode";
62
63 @Override
64 public ObjectNode encode(K8sNode node, CodecContext context) {
65 checkNotNull(node, "Kubernetes node cannot be null");
66
67 ObjectNode result = context.mapper().createObjectNode()
Jian Lie2a04ce2020-07-01 19:07:02 +090068 .put(CLUSTER_NAME, node.clusterName())
Jian Li49109b52019-01-22 00:17:28 +090069 .put(HOSTNAME, node.hostname())
70 .put(TYPE, node.type().name())
Jian Li732c3422020-09-07 17:01:11 +090071 .put(MODE, node.mode().name())
Jian Lie2a04ce2020-07-01 19:07:02 +090072 .put(SEGMENT_ID, node.segmentId())
Jian Li49109b52019-01-22 00:17:28 +090073 .put(STATE, node.state().name())
74 .put(MANAGEMENT_IP, node.managementIp().toString());
75
76 if (node.intgBridge() != null) {
77 result.put(INTEGRATION_BRIDGE, node.intgBridge().toString());
78 }
79
Jian Libf562c22019-04-15 18:07:14 +090080 if (node.extBridge() != null) {
81 result.put(EXTERNAL_BRIDGE, node.extBridge().toString());
82 }
83
Jian Li1a2eb5d2019-08-27 02:07:05 +090084 if (node.localBridge() != null) {
85 result.put(LOCAL_BRIDGE, node.localBridge().toString());
86 }
87
Jian Lie2a04ce2020-07-01 19:07:02 +090088 if (node.tunBridge() != null) {
89 result.put(TUNNEL_BRIDGE, node.tunBridge().toString());
90 }
91
Jian Li49109b52019-01-22 00:17:28 +090092 if (node.dataIp() != null) {
93 result.put(DATA_IP, node.dataIp().toString());
94 }
95
Jian Li0c632722019-05-08 15:58:04 +090096 if (node.extIntf() != null) {
97 result.put(EXTERNAL_INTF, node.extIntf());
98 }
99
100 if (node.extBridgeIp() != null) {
101 result.put(EXTERNAL_BRIDGE_IP, node.extBridgeIp().toString());
102 }
103
104 if (node.extGatewayIp() != null) {
105 result.put(EXTERNAL_GATEWAY_IP, node.extGatewayIp().toString());
106 }
107
Jian Lic2242bd2020-09-03 13:12:14 +0900108 if (node.extGatewayMac() != null) {
109 result.put(EXTERNAL_GATEWAY_MAC, node.extGatewayMac().toString());
110 }
111
Jian Li49109b52019-01-22 00:17:28 +0900112 return result;
113 }
114
115 @Override
116 public K8sNode decode(ObjectNode json, CodecContext context) {
117 if (json == null || !json.isObject()) {
118 return null;
119 }
120
Jian Lie2a04ce2020-07-01 19:07:02 +0900121 String clusterName = json.get(CLUSTER_NAME).asText();
122
123 if (StringUtils.isEmpty(clusterName)) {
124 clusterName = DEFAULT_CLUSTER_NAME;
125 }
126
Jian Li49109b52019-01-22 00:17:28 +0900127 String hostname = nullIsIllegal(json.get(HOSTNAME).asText(),
128 HOSTNAME + MISSING_MESSAGE);
129 String type = nullIsIllegal(json.get(TYPE).asText(),
130 TYPE + MISSING_MESSAGE);
131 String mIp = nullIsIllegal(json.get(MANAGEMENT_IP).asText(),
132 MANAGEMENT_IP + MISSING_MESSAGE);
133
134 DefaultK8sNode.Builder nodeBuilder = DefaultK8sNode.builder()
Jian Lie2a04ce2020-07-01 19:07:02 +0900135 .clusterName(clusterName)
Jian Li49109b52019-01-22 00:17:28 +0900136 .hostname(hostname)
137 .type(K8sNode.Type.valueOf(type))
138 .state(K8sNodeState.INIT)
139 .managementIp(IpAddress.valueOf(mIp));
140
141 if (json.get(DATA_IP) != null) {
142 nodeBuilder.dataIp(IpAddress.valueOf(json.get(DATA_IP).asText()));
143 }
144
Jian Lie2a04ce2020-07-01 19:07:02 +0900145 JsonNode segmentIdJson = json.get(SEGMENT_ID);
146 int segmentId = DEFAULT_SEGMENT_ID;
147 if (segmentIdJson != null) {
148 segmentId = segmentIdJson.asInt();
149 }
150 nodeBuilder.segmentId(segmentId);
151
Jian Li49109b52019-01-22 00:17:28 +0900152 JsonNode intBridgeJson = json.get(INTEGRATION_BRIDGE);
153 if (intBridgeJson != null) {
154 nodeBuilder.intgBridge(DeviceId.deviceId(intBridgeJson.asText()));
155 }
156
Jian Libf562c22019-04-15 18:07:14 +0900157 JsonNode extBridgeJson = json.get(EXTERNAL_BRIDGE);
158 if (extBridgeJson != null) {
159 nodeBuilder.extBridge(DeviceId.deviceId(extBridgeJson.asText()));
160 }
161
Jian Li1a2eb5d2019-08-27 02:07:05 +0900162 JsonNode localBridgeJson = json.get(LOCAL_BRIDGE);
163 if (localBridgeJson != null) {
164 nodeBuilder.localBridge(DeviceId.deviceId(localBridgeJson.asText()));
165 }
166
Jian Lie2a04ce2020-07-01 19:07:02 +0900167 JsonNode tunBridgeJson = json.get(TUNNEL_BRIDGE);
168 if (tunBridgeJson != null) {
169 nodeBuilder.tunBridge(DeviceId.deviceId(tunBridgeJson.asText()));
170 }
171
Jian Li0c632722019-05-08 15:58:04 +0900172 JsonNode extIntfJson = json.get(EXTERNAL_INTF);
173 if (extIntfJson != null) {
174 nodeBuilder.extIntf(extIntfJson.asText());
175 }
176
177 JsonNode extBridgeIpJson = json.get(EXTERNAL_BRIDGE_IP);
178 if (extBridgeIpJson != null) {
179 nodeBuilder.extBridgeIp(IpAddress.valueOf(extBridgeIpJson.asText()));
180 }
181
182 JsonNode extGatewayIpJson = json.get(EXTERNAL_GATEWAY_IP);
183 if (extGatewayIpJson != null) {
184 nodeBuilder.extGatewayIp(IpAddress.valueOf(extGatewayIpJson.asText()));
185 }
186
Jian Lic2242bd2020-09-03 13:12:14 +0900187 JsonNode extGatewayMacJson = json.get(EXTERNAL_GATEWAY_MAC);
188 if (extGatewayMacJson != null) {
189 nodeBuilder.extGatewayMac(MacAddress.valueOf(extGatewayMacJson.asText()));
190 }
191
Jian Li49109b52019-01-22 00:17:28 +0900192 log.trace("node is {}", nodeBuilder.build().toString());
193
194 return nodeBuilder.build();
195 }
196}