blob: 41bad16737e4cb1ec654e764d1cf26af6c50c93b [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;
20import org.onlab.packet.IpAddress;
21import org.onosproject.codec.CodecContext;
22import org.onosproject.codec.JsonCodec;
23import org.onosproject.k8snode.api.DefaultK8sNode;
24import org.onosproject.k8snode.api.K8sNode;
25import org.onosproject.k8snode.api.K8sNodeState;
26import org.onosproject.net.DeviceId;
27import org.slf4j.Logger;
28
29import static com.google.common.base.Preconditions.checkNotNull;
30import static org.onlab.util.Tools.nullIsIllegal;
31import static org.slf4j.LoggerFactory.getLogger;
32
33/**
34 * Kubernetes node codec used for serializing and de-serializing JSON string.
35 */
36public final class K8sNodeCodec extends JsonCodec<K8sNode> {
37
38 private final Logger log = getLogger(getClass());
39
40 private static final String HOSTNAME = "hostname";
41 private static final String TYPE = "type";
42 private static final String MANAGEMENT_IP = "managementIp";
43 private static final String DATA_IP = "dataIp";
44 private static final String INTEGRATION_BRIDGE = "integrationBridge";
Jian Libf562c22019-04-15 18:07:14 +090045 private static final String EXTERNAL_BRIDGE = "externalBridge";
Jian Li1a2eb5d2019-08-27 02:07:05 +090046 private static final String LOCAL_BRIDGE = "localBridge";
Jian Li49109b52019-01-22 00:17:28 +090047 private static final String STATE = "state";
Jian Li0c632722019-05-08 15:58:04 +090048 private static final String EXTERNAL_INTF = "externalInterface";
49 private static final String EXTERNAL_BRIDGE_IP = "externalBridgeIp";
50 private static final String EXTERNAL_GATEWAY_IP = "externalGatewayIp";
Jian Li49109b52019-01-22 00:17:28 +090051
52 private static final String MISSING_MESSAGE = " is required in K8sNode";
53
54 @Override
55 public ObjectNode encode(K8sNode node, CodecContext context) {
56 checkNotNull(node, "Kubernetes node cannot be null");
57
58 ObjectNode result = context.mapper().createObjectNode()
59 .put(HOSTNAME, node.hostname())
60 .put(TYPE, node.type().name())
61 .put(STATE, node.state().name())
62 .put(MANAGEMENT_IP, node.managementIp().toString());
63
64 if (node.intgBridge() != null) {
65 result.put(INTEGRATION_BRIDGE, node.intgBridge().toString());
66 }
67
Jian Libf562c22019-04-15 18:07:14 +090068 if (node.extBridge() != null) {
69 result.put(EXTERNAL_BRIDGE, node.extBridge().toString());
70 }
71
Jian Li1a2eb5d2019-08-27 02:07:05 +090072 if (node.localBridge() != null) {
73 result.put(LOCAL_BRIDGE, node.localBridge().toString());
74 }
75
Jian Li49109b52019-01-22 00:17:28 +090076 if (node.dataIp() != null) {
77 result.put(DATA_IP, node.dataIp().toString());
78 }
79
Jian Li0c632722019-05-08 15:58:04 +090080 if (node.extIntf() != null) {
81 result.put(EXTERNAL_INTF, node.extIntf());
82 }
83
84 if (node.extBridgeIp() != null) {
85 result.put(EXTERNAL_BRIDGE_IP, node.extBridgeIp().toString());
86 }
87
88 if (node.extGatewayIp() != null) {
89 result.put(EXTERNAL_GATEWAY_IP, node.extGatewayIp().toString());
90 }
91
Jian Li49109b52019-01-22 00:17:28 +090092 return result;
93 }
94
95 @Override
96 public K8sNode decode(ObjectNode json, CodecContext context) {
97 if (json == null || !json.isObject()) {
98 return null;
99 }
100
101 String hostname = nullIsIllegal(json.get(HOSTNAME).asText(),
102 HOSTNAME + MISSING_MESSAGE);
103 String type = nullIsIllegal(json.get(TYPE).asText(),
104 TYPE + MISSING_MESSAGE);
105 String mIp = nullIsIllegal(json.get(MANAGEMENT_IP).asText(),
106 MANAGEMENT_IP + MISSING_MESSAGE);
107
108 DefaultK8sNode.Builder nodeBuilder = DefaultK8sNode.builder()
109 .hostname(hostname)
110 .type(K8sNode.Type.valueOf(type))
111 .state(K8sNodeState.INIT)
112 .managementIp(IpAddress.valueOf(mIp));
113
114 if (json.get(DATA_IP) != null) {
115 nodeBuilder.dataIp(IpAddress.valueOf(json.get(DATA_IP).asText()));
116 }
117
118 JsonNode intBridgeJson = json.get(INTEGRATION_BRIDGE);
119 if (intBridgeJson != null) {
120 nodeBuilder.intgBridge(DeviceId.deviceId(intBridgeJson.asText()));
121 }
122
Jian Libf562c22019-04-15 18:07:14 +0900123 JsonNode extBridgeJson = json.get(EXTERNAL_BRIDGE);
124 if (extBridgeJson != null) {
125 nodeBuilder.extBridge(DeviceId.deviceId(extBridgeJson.asText()));
126 }
127
Jian Li1a2eb5d2019-08-27 02:07:05 +0900128 JsonNode localBridgeJson = json.get(LOCAL_BRIDGE);
129 if (localBridgeJson != null) {
130 nodeBuilder.localBridge(DeviceId.deviceId(localBridgeJson.asText()));
131 }
132
Jian Li0c632722019-05-08 15:58:04 +0900133 JsonNode extIntfJson = json.get(EXTERNAL_INTF);
134 if (extIntfJson != null) {
135 nodeBuilder.extIntf(extIntfJson.asText());
136 }
137
138 JsonNode extBridgeIpJson = json.get(EXTERNAL_BRIDGE_IP);
139 if (extBridgeIpJson != null) {
140 nodeBuilder.extBridgeIp(IpAddress.valueOf(extBridgeIpJson.asText()));
141 }
142
143 JsonNode extGatewayIpJson = json.get(EXTERNAL_GATEWAY_IP);
144 if (extGatewayIpJson != null) {
145 nodeBuilder.extGatewayIp(IpAddress.valueOf(extGatewayIpJson.asText()));
146 }
147
Jian Li49109b52019-01-22 00:17:28 +0900148 log.trace("node is {}", nodeBuilder.build().toString());
149
150 return nodeBuilder.build();
151 }
152}