blob: 667684716dccc3a4fa3863e50c73bc82b4f1175a [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 Li49109b52019-01-22 00:17:28 +090046 private static final String STATE = "state";
47
48 private static final String MISSING_MESSAGE = " is required in K8sNode";
49
50 @Override
51 public ObjectNode encode(K8sNode node, CodecContext context) {
52 checkNotNull(node, "Kubernetes node cannot be null");
53
54 ObjectNode result = context.mapper().createObjectNode()
55 .put(HOSTNAME, node.hostname())
56 .put(TYPE, node.type().name())
57 .put(STATE, node.state().name())
58 .put(MANAGEMENT_IP, node.managementIp().toString());
59
60 if (node.intgBridge() != null) {
61 result.put(INTEGRATION_BRIDGE, node.intgBridge().toString());
62 }
63
Jian Libf562c22019-04-15 18:07:14 +090064 if (node.extBridge() != null) {
65 result.put(EXTERNAL_BRIDGE, node.extBridge().toString());
66 }
67
Jian Li49109b52019-01-22 00:17:28 +090068 if (node.dataIp() != null) {
69 result.put(DATA_IP, node.dataIp().toString());
70 }
71
72 return result;
73 }
74
75 @Override
76 public K8sNode decode(ObjectNode json, CodecContext context) {
77 if (json == null || !json.isObject()) {
78 return null;
79 }
80
81 String hostname = nullIsIllegal(json.get(HOSTNAME).asText(),
82 HOSTNAME + MISSING_MESSAGE);
83 String type = nullIsIllegal(json.get(TYPE).asText(),
84 TYPE + MISSING_MESSAGE);
85 String mIp = nullIsIllegal(json.get(MANAGEMENT_IP).asText(),
86 MANAGEMENT_IP + MISSING_MESSAGE);
87
88 DefaultK8sNode.Builder nodeBuilder = DefaultK8sNode.builder()
89 .hostname(hostname)
90 .type(K8sNode.Type.valueOf(type))
91 .state(K8sNodeState.INIT)
92 .managementIp(IpAddress.valueOf(mIp));
93
94 if (json.get(DATA_IP) != null) {
95 nodeBuilder.dataIp(IpAddress.valueOf(json.get(DATA_IP).asText()));
96 }
97
98 JsonNode intBridgeJson = json.get(INTEGRATION_BRIDGE);
99 if (intBridgeJson != null) {
100 nodeBuilder.intgBridge(DeviceId.deviceId(intBridgeJson.asText()));
101 }
102
Jian Libf562c22019-04-15 18:07:14 +0900103 JsonNode extBridgeJson = json.get(EXTERNAL_BRIDGE);
104 if (extBridgeJson != null) {
105 nodeBuilder.extBridge(DeviceId.deviceId(extBridgeJson.asText()));
106 }
107
Jian Li49109b52019-01-22 00:17:28 +0900108 log.trace("node is {}", nodeBuilder.build().toString());
109
110 return nodeBuilder.build();
111 }
112}