blob: 4f4e6bfd8145551067c39fa839a2e14eec0d6429 [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 Li58b33982020-07-01 19:07:02 +090020import org.apache.commons.lang.StringUtils;
Jian Li49109b52019-01-22 00:17:28 +090021import org.onlab.packet.IpAddress;
Jian Lib1218442020-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 Li58b33982020-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 Li58b33982020-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 Li3cb86e32020-09-07 17:01:11 +090047 private static final String MODE = "mode";
Jian Li58b33982020-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";
Jian Li9bc67772020-10-07 02:12:33 +090051 private static final String NODE_IP = "nodeIp";
Jian Li49109b52019-01-22 00:17:28 +090052 private static final String INTEGRATION_BRIDGE = "integrationBridge";
Jian Libf562c22019-04-15 18:07:14 +090053 private static final String EXTERNAL_BRIDGE = "externalBridge";
Jian Li121ddfe2019-08-27 02:07:05 +090054 private static final String LOCAL_BRIDGE = "localBridge";
Jian Li58b33982020-07-01 19:07:02 +090055 private static final String TUNNEL_BRIDGE = "tunnelBridge";
Jian Li49109b52019-01-22 00:17:28 +090056 private static final String STATE = "state";
Jian Li7709eb42019-05-08 15:58:04 +090057 private static final String EXTERNAL_INTF = "externalInterface";
58 private static final String EXTERNAL_BRIDGE_IP = "externalBridgeIp";
59 private static final String EXTERNAL_GATEWAY_IP = "externalGatewayIp";
Jian Lib1218442020-09-03 13:12:14 +090060 private static final String EXTERNAL_GATEWAY_MAC = "externalGatewayMac";
Jian Li49109b52019-01-22 00:17:28 +090061
62 private static final String MISSING_MESSAGE = " is required in K8sNode";
63
64 @Override
65 public ObjectNode encode(K8sNode node, CodecContext context) {
66 checkNotNull(node, "Kubernetes node cannot be null");
67
68 ObjectNode result = context.mapper().createObjectNode()
Jian Li58b33982020-07-01 19:07:02 +090069 .put(CLUSTER_NAME, node.clusterName())
Jian Li49109b52019-01-22 00:17:28 +090070 .put(HOSTNAME, node.hostname())
71 .put(TYPE, node.type().name())
Jian Li3cb86e32020-09-07 17:01:11 +090072 .put(MODE, node.mode().name())
Jian Li58b33982020-07-01 19:07:02 +090073 .put(SEGMENT_ID, node.segmentId())
Jian Li49109b52019-01-22 00:17:28 +090074 .put(STATE, node.state().name())
Jian Li9bc67772020-10-07 02:12:33 +090075 .put(MANAGEMENT_IP, node.managementIp().toString())
76 .put(NODE_IP, node.nodeIp().toString());
Jian Li49109b52019-01-22 00:17:28 +090077
78 if (node.intgBridge() != null) {
79 result.put(INTEGRATION_BRIDGE, node.intgBridge().toString());
80 }
81
Jian Libf562c22019-04-15 18:07:14 +090082 if (node.extBridge() != null) {
83 result.put(EXTERNAL_BRIDGE, node.extBridge().toString());
84 }
85
Jian Li121ddfe2019-08-27 02:07:05 +090086 if (node.localBridge() != null) {
87 result.put(LOCAL_BRIDGE, node.localBridge().toString());
88 }
89
Jian Li58b33982020-07-01 19:07:02 +090090 if (node.tunBridge() != null) {
91 result.put(TUNNEL_BRIDGE, node.tunBridge().toString());
92 }
93
Jian Li49109b52019-01-22 00:17:28 +090094 if (node.dataIp() != null) {
95 result.put(DATA_IP, node.dataIp().toString());
96 }
97
Jian Li7709eb42019-05-08 15:58:04 +090098 if (node.extIntf() != null) {
99 result.put(EXTERNAL_INTF, node.extIntf());
100 }
101
102 if (node.extBridgeIp() != null) {
103 result.put(EXTERNAL_BRIDGE_IP, node.extBridgeIp().toString());
104 }
105
106 if (node.extGatewayIp() != null) {
107 result.put(EXTERNAL_GATEWAY_IP, node.extGatewayIp().toString());
108 }
109
Jian Lib1218442020-09-03 13:12:14 +0900110 if (node.extGatewayMac() != null) {
111 result.put(EXTERNAL_GATEWAY_MAC, node.extGatewayMac().toString());
112 }
113
Jian Li49109b52019-01-22 00:17:28 +0900114 return result;
115 }
116
117 @Override
118 public K8sNode decode(ObjectNode json, CodecContext context) {
119 if (json == null || !json.isObject()) {
120 return null;
121 }
122
Jian Li58b33982020-07-01 19:07:02 +0900123 String clusterName = json.get(CLUSTER_NAME).asText();
124
125 if (StringUtils.isEmpty(clusterName)) {
126 clusterName = DEFAULT_CLUSTER_NAME;
127 }
128
Jian Li49109b52019-01-22 00:17:28 +0900129 String hostname = nullIsIllegal(json.get(HOSTNAME).asText(),
130 HOSTNAME + MISSING_MESSAGE);
131 String type = nullIsIllegal(json.get(TYPE).asText(),
132 TYPE + MISSING_MESSAGE);
133 String mIp = nullIsIllegal(json.get(MANAGEMENT_IP).asText(),
134 MANAGEMENT_IP + MISSING_MESSAGE);
Jian Li9bc67772020-10-07 02:12:33 +0900135 String nIp = nullIsIllegal(json.get(NODE_IP).asText(),
136 NODE_IP + MISSING_MESSAGE);
Jian Li49109b52019-01-22 00:17:28 +0900137
138 DefaultK8sNode.Builder nodeBuilder = DefaultK8sNode.builder()
Jian Li58b33982020-07-01 19:07:02 +0900139 .clusterName(clusterName)
Jian Li49109b52019-01-22 00:17:28 +0900140 .hostname(hostname)
141 .type(K8sNode.Type.valueOf(type))
142 .state(K8sNodeState.INIT)
Jian Li9bc67772020-10-07 02:12:33 +0900143 .managementIp(IpAddress.valueOf(mIp))
144 .nodeIp(IpAddress.valueOf(nIp));
Jian Li49109b52019-01-22 00:17:28 +0900145
146 if (json.get(DATA_IP) != null) {
147 nodeBuilder.dataIp(IpAddress.valueOf(json.get(DATA_IP).asText()));
148 }
149
Jian Li58b33982020-07-01 19:07:02 +0900150 JsonNode segmentIdJson = json.get(SEGMENT_ID);
151 int segmentId = DEFAULT_SEGMENT_ID;
152 if (segmentIdJson != null) {
153 segmentId = segmentIdJson.asInt();
154 }
155 nodeBuilder.segmentId(segmentId);
156
Jian Li49109b52019-01-22 00:17:28 +0900157 JsonNode intBridgeJson = json.get(INTEGRATION_BRIDGE);
158 if (intBridgeJson != null) {
159 nodeBuilder.intgBridge(DeviceId.deviceId(intBridgeJson.asText()));
160 }
161
Jian Libf562c22019-04-15 18:07:14 +0900162 JsonNode extBridgeJson = json.get(EXTERNAL_BRIDGE);
163 if (extBridgeJson != null) {
164 nodeBuilder.extBridge(DeviceId.deviceId(extBridgeJson.asText()));
165 }
166
Jian Li121ddfe2019-08-27 02:07:05 +0900167 JsonNode localBridgeJson = json.get(LOCAL_BRIDGE);
168 if (localBridgeJson != null) {
169 nodeBuilder.localBridge(DeviceId.deviceId(localBridgeJson.asText()));
170 }
171
Jian Li58b33982020-07-01 19:07:02 +0900172 JsonNode tunBridgeJson = json.get(TUNNEL_BRIDGE);
173 if (tunBridgeJson != null) {
174 nodeBuilder.tunBridge(DeviceId.deviceId(tunBridgeJson.asText()));
175 }
176
Jian Li7709eb42019-05-08 15:58:04 +0900177 JsonNode extIntfJson = json.get(EXTERNAL_INTF);
178 if (extIntfJson != null) {
179 nodeBuilder.extIntf(extIntfJson.asText());
180 }
181
182 JsonNode extBridgeIpJson = json.get(EXTERNAL_BRIDGE_IP);
183 if (extBridgeIpJson != null) {
184 nodeBuilder.extBridgeIp(IpAddress.valueOf(extBridgeIpJson.asText()));
185 }
186
187 JsonNode extGatewayIpJson = json.get(EXTERNAL_GATEWAY_IP);
188 if (extGatewayIpJson != null) {
189 nodeBuilder.extGatewayIp(IpAddress.valueOf(extGatewayIpJson.asText()));
190 }
191
Jian Lib1218442020-09-03 13:12:14 +0900192 JsonNode extGatewayMacJson = json.get(EXTERNAL_GATEWAY_MAC);
193 if (extGatewayMacJson != null) {
194 nodeBuilder.extGatewayMac(MacAddress.valueOf(extGatewayMacJson.asText()));
195 }
196
Jian Li49109b52019-01-22 00:17:28 +0900197 log.trace("node is {}", nodeBuilder.build().toString());
198
199 return nodeBuilder.build();
200 }
201}