blob: 4cc6609f7555459030f52a31cf31af4bc31526b0 [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 Lie2a04ce2020-07-01 19:07:02 +090047 private static final String SEGMENT_ID = "segmentId";
Jian Li49109b52019-01-22 00:17:28 +090048 private static final String MANAGEMENT_IP = "managementIp";
49 private static final String DATA_IP = "dataIp";
50 private static final String INTEGRATION_BRIDGE = "integrationBridge";
Jian Libf562c22019-04-15 18:07:14 +090051 private static final String EXTERNAL_BRIDGE = "externalBridge";
Jian Li1a2eb5d2019-08-27 02:07:05 +090052 private static final String LOCAL_BRIDGE = "localBridge";
Jian Lie2a04ce2020-07-01 19:07:02 +090053 private static final String TUNNEL_BRIDGE = "tunnelBridge";
Jian Li49109b52019-01-22 00:17:28 +090054 private static final String STATE = "state";
Jian Li0c632722019-05-08 15:58:04 +090055 private static final String EXTERNAL_INTF = "externalInterface";
56 private static final String EXTERNAL_BRIDGE_IP = "externalBridgeIp";
57 private static final String EXTERNAL_GATEWAY_IP = "externalGatewayIp";
Jian Lic2242bd2020-09-03 13:12:14 +090058 private static final String EXTERNAL_GATEWAY_MAC = "externalGatewayMac";
Jian Li49109b52019-01-22 00:17:28 +090059
60 private static final String MISSING_MESSAGE = " is required in K8sNode";
61
62 @Override
63 public ObjectNode encode(K8sNode node, CodecContext context) {
64 checkNotNull(node, "Kubernetes node cannot be null");
65
66 ObjectNode result = context.mapper().createObjectNode()
Jian Lie2a04ce2020-07-01 19:07:02 +090067 .put(CLUSTER_NAME, node.clusterName())
Jian Li49109b52019-01-22 00:17:28 +090068 .put(HOSTNAME, node.hostname())
69 .put(TYPE, node.type().name())
Jian Lie2a04ce2020-07-01 19:07:02 +090070 .put(SEGMENT_ID, node.segmentId())
Jian Li49109b52019-01-22 00:17:28 +090071 .put(STATE, node.state().name())
72 .put(MANAGEMENT_IP, node.managementIp().toString());
73
74 if (node.intgBridge() != null) {
75 result.put(INTEGRATION_BRIDGE, node.intgBridge().toString());
76 }
77
Jian Libf562c22019-04-15 18:07:14 +090078 if (node.extBridge() != null) {
79 result.put(EXTERNAL_BRIDGE, node.extBridge().toString());
80 }
81
Jian Li1a2eb5d2019-08-27 02:07:05 +090082 if (node.localBridge() != null) {
83 result.put(LOCAL_BRIDGE, node.localBridge().toString());
84 }
85
Jian Lie2a04ce2020-07-01 19:07:02 +090086 if (node.tunBridge() != null) {
87 result.put(TUNNEL_BRIDGE, node.tunBridge().toString());
88 }
89
Jian Li49109b52019-01-22 00:17:28 +090090 if (node.dataIp() != null) {
91 result.put(DATA_IP, node.dataIp().toString());
92 }
93
Jian Li0c632722019-05-08 15:58:04 +090094 if (node.extIntf() != null) {
95 result.put(EXTERNAL_INTF, node.extIntf());
96 }
97
98 if (node.extBridgeIp() != null) {
99 result.put(EXTERNAL_BRIDGE_IP, node.extBridgeIp().toString());
100 }
101
102 if (node.extGatewayIp() != null) {
103 result.put(EXTERNAL_GATEWAY_IP, node.extGatewayIp().toString());
104 }
105
Jian Lic2242bd2020-09-03 13:12:14 +0900106 if (node.extGatewayMac() != null) {
107 result.put(EXTERNAL_GATEWAY_MAC, node.extGatewayMac().toString());
108 }
109
Jian Li49109b52019-01-22 00:17:28 +0900110 return result;
111 }
112
113 @Override
114 public K8sNode decode(ObjectNode json, CodecContext context) {
115 if (json == null || !json.isObject()) {
116 return null;
117 }
118
Jian Lie2a04ce2020-07-01 19:07:02 +0900119 String clusterName = json.get(CLUSTER_NAME).asText();
120
121 if (StringUtils.isEmpty(clusterName)) {
122 clusterName = DEFAULT_CLUSTER_NAME;
123 }
124
Jian Li49109b52019-01-22 00:17:28 +0900125 String hostname = nullIsIllegal(json.get(HOSTNAME).asText(),
126 HOSTNAME + MISSING_MESSAGE);
127 String type = nullIsIllegal(json.get(TYPE).asText(),
128 TYPE + MISSING_MESSAGE);
129 String mIp = nullIsIllegal(json.get(MANAGEMENT_IP).asText(),
130 MANAGEMENT_IP + MISSING_MESSAGE);
131
132 DefaultK8sNode.Builder nodeBuilder = DefaultK8sNode.builder()
Jian Lie2a04ce2020-07-01 19:07:02 +0900133 .clusterName(clusterName)
Jian Li49109b52019-01-22 00:17:28 +0900134 .hostname(hostname)
135 .type(K8sNode.Type.valueOf(type))
136 .state(K8sNodeState.INIT)
137 .managementIp(IpAddress.valueOf(mIp));
138
139 if (json.get(DATA_IP) != null) {
140 nodeBuilder.dataIp(IpAddress.valueOf(json.get(DATA_IP).asText()));
141 }
142
Jian Lie2a04ce2020-07-01 19:07:02 +0900143 JsonNode segmentIdJson = json.get(SEGMENT_ID);
144 int segmentId = DEFAULT_SEGMENT_ID;
145 if (segmentIdJson != null) {
146 segmentId = segmentIdJson.asInt();
147 }
148 nodeBuilder.segmentId(segmentId);
149
Jian Li49109b52019-01-22 00:17:28 +0900150 JsonNode intBridgeJson = json.get(INTEGRATION_BRIDGE);
151 if (intBridgeJson != null) {
152 nodeBuilder.intgBridge(DeviceId.deviceId(intBridgeJson.asText()));
153 }
154
Jian Libf562c22019-04-15 18:07:14 +0900155 JsonNode extBridgeJson = json.get(EXTERNAL_BRIDGE);
156 if (extBridgeJson != null) {
157 nodeBuilder.extBridge(DeviceId.deviceId(extBridgeJson.asText()));
158 }
159
Jian Li1a2eb5d2019-08-27 02:07:05 +0900160 JsonNode localBridgeJson = json.get(LOCAL_BRIDGE);
161 if (localBridgeJson != null) {
162 nodeBuilder.localBridge(DeviceId.deviceId(localBridgeJson.asText()));
163 }
164
Jian Lie2a04ce2020-07-01 19:07:02 +0900165 JsonNode tunBridgeJson = json.get(TUNNEL_BRIDGE);
166 if (tunBridgeJson != null) {
167 nodeBuilder.tunBridge(DeviceId.deviceId(tunBridgeJson.asText()));
168 }
169
Jian Li0c632722019-05-08 15:58:04 +0900170 JsonNode extIntfJson = json.get(EXTERNAL_INTF);
171 if (extIntfJson != null) {
172 nodeBuilder.extIntf(extIntfJson.asText());
173 }
174
175 JsonNode extBridgeIpJson = json.get(EXTERNAL_BRIDGE_IP);
176 if (extBridgeIpJson != null) {
177 nodeBuilder.extBridgeIp(IpAddress.valueOf(extBridgeIpJson.asText()));
178 }
179
180 JsonNode extGatewayIpJson = json.get(EXTERNAL_GATEWAY_IP);
181 if (extGatewayIpJson != null) {
182 nodeBuilder.extGatewayIp(IpAddress.valueOf(extGatewayIpJson.asText()));
183 }
184
Jian Lic2242bd2020-09-03 13:12:14 +0900185 JsonNode extGatewayMacJson = json.get(EXTERNAL_GATEWAY_MAC);
186 if (extGatewayMacJson != null) {
187 nodeBuilder.extGatewayMac(MacAddress.valueOf(extGatewayMacJson.asText()));
188 }
189
Jian Li49109b52019-01-22 00:17:28 +0900190 log.trace("node is {}", nodeBuilder.build().toString());
191
192 return nodeBuilder.build();
193 }
194}