blob: e07ad6c726c54e216113229145f251fd3c104208 [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";
45 private static final String STATE = "state";
46
47 private static final String MISSING_MESSAGE = " is required in K8sNode";
48
49 @Override
50 public ObjectNode encode(K8sNode node, CodecContext context) {
51 checkNotNull(node, "Kubernetes node cannot be null");
52
53 ObjectNode result = context.mapper().createObjectNode()
54 .put(HOSTNAME, node.hostname())
55 .put(TYPE, node.type().name())
56 .put(STATE, node.state().name())
57 .put(MANAGEMENT_IP, node.managementIp().toString());
58
59 if (node.intgBridge() != null) {
60 result.put(INTEGRATION_BRIDGE, node.intgBridge().toString());
61 }
62
63 if (node.dataIp() != null) {
64 result.put(DATA_IP, node.dataIp().toString());
65 }
66
67 return result;
68 }
69
70 @Override
71 public K8sNode decode(ObjectNode json, CodecContext context) {
72 if (json == null || !json.isObject()) {
73 return null;
74 }
75
76 String hostname = nullIsIllegal(json.get(HOSTNAME).asText(),
77 HOSTNAME + MISSING_MESSAGE);
78 String type = nullIsIllegal(json.get(TYPE).asText(),
79 TYPE + MISSING_MESSAGE);
80 String mIp = nullIsIllegal(json.get(MANAGEMENT_IP).asText(),
81 MANAGEMENT_IP + MISSING_MESSAGE);
82
83 DefaultK8sNode.Builder nodeBuilder = DefaultK8sNode.builder()
84 .hostname(hostname)
85 .type(K8sNode.Type.valueOf(type))
86 .state(K8sNodeState.INIT)
87 .managementIp(IpAddress.valueOf(mIp));
88
89 if (json.get(DATA_IP) != null) {
90 nodeBuilder.dataIp(IpAddress.valueOf(json.get(DATA_IP).asText()));
91 }
92
93 JsonNode intBridgeJson = json.get(INTEGRATION_BRIDGE);
94 if (intBridgeJson != null) {
95 nodeBuilder.intgBridge(DeviceId.deviceId(intBridgeJson.asText()));
96 }
97
98 log.trace("node is {}", nodeBuilder.build().toString());
99
100 return nodeBuilder.build();
101 }
102}