blob: 5e0cb27e906f8242be3c4b8ddfd76bc8123b0725 [file] [log] [blame]
Jian Li3defa842019-02-12 00:31:35 +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.DefaultK8sApiConfig;
24import org.onosproject.k8snode.api.K8sApiConfig;
25import org.onosproject.k8snode.api.K8sApiConfig.Scheme;
26
27import static org.onlab.util.Tools.nullIsIllegal;
28import static org.onosproject.k8snode.api.K8sApiConfig.Scheme.HTTPS;
29
30/**
31 * Kubernetes API server config codec used for serializing and de-serializing JSON string.
32 */
33public final class K8sApiConfigCodec extends JsonCodec<K8sApiConfig> {
34
35 private static final String SCHEME = "scheme";
36 private static final String IP_ADDRESS = "ipAddress";
37 private static final String PORT = "port";
38 private static final String TOKEN = "token";
39 private static final String CA_CERT_DATA = "caCertData";
40 private static final String CLIENT_CERT_DATA = "clientCertData";
41 private static final String CLIENT_KEY_DATA = "clientKeyData";
42
43 private static final String MISSING_MESSAGE = " is required in K8sApiConfig";
44
45 @Override
46 public ObjectNode encode(K8sApiConfig entity, CodecContext context) {
47 ObjectNode node = context.mapper().createObjectNode()
48 .put(SCHEME, entity.scheme().name())
49 .put(IP_ADDRESS, entity.ipAddress().toString())
50 .put(PORT, entity.port());
51
52 if (entity.scheme() == HTTPS) {
53 node.put(TOKEN, entity.token())
54 .put(CA_CERT_DATA, entity.caCertData())
55 .put(CLIENT_CERT_DATA, entity.clientCertData())
56 .put(CLIENT_KEY_DATA, entity.clientKeyData());
57 } else {
58 if (entity.token() != null) {
59 node.put(TOKEN, entity.token());
60 }
61
62 if (entity.caCertData() != null) {
63 node.put(CA_CERT_DATA, entity.caCertData());
64 }
65
66 if (entity.clientCertData() != null) {
67 node.put(CLIENT_CERT_DATA, entity.clientCertData());
68 }
69
70 if (entity.clientKeyData() != null) {
71 node.put(CLIENT_KEY_DATA, entity.clientKeyData());
72 }
73 }
74
75 return node;
76 }
77
78 @Override
79 public K8sApiConfig decode(ObjectNode json, CodecContext context) {
80 if (json == null || !json.isObject()) {
81 return null;
82 }
83
84 Scheme scheme = Scheme.valueOf(nullIsIllegal(
85 json.get(SCHEME).asText(), SCHEME + MISSING_MESSAGE));
86 IpAddress ipAddress = IpAddress.valueOf(nullIsIllegal(
87 json.get(IP_ADDRESS).asText(), IP_ADDRESS + MISSING_MESSAGE));
88 int port = json.get(PORT).asInt();
89
90 K8sApiConfig.Builder builder = DefaultK8sApiConfig.builder()
91 .scheme(scheme)
92 .ipAddress(ipAddress)
93 .port(port);
94
95 JsonNode tokenJson = json.get(TOKEN);
96 JsonNode caCertDataJson = json.get(CA_CERT_DATA);
97 JsonNode clientCertDataJson = json.get(CLIENT_CERT_DATA);
98 JsonNode clientKeyDataJson = json.get(CLIENT_KEY_DATA);
99
100 String token = "";
101 String caCertData = "";
102 String clientCertData = "";
103 String clientKeyData = "";
104
105 if (scheme == HTTPS) {
106 token = nullIsIllegal(tokenJson.asText(),
107 TOKEN + MISSING_MESSAGE);
108 caCertData = nullIsIllegal(caCertDataJson.asText(),
109 CA_CERT_DATA + MISSING_MESSAGE);
110 clientCertData = nullIsIllegal(clientCertDataJson.asText(),
111 CLIENT_CERT_DATA + MISSING_MESSAGE);
112 clientKeyData = nullIsIllegal(clientKeyDataJson.asText(),
113 CLIENT_KEY_DATA + MISSING_MESSAGE);
114
115
116 } else {
117 if (tokenJson != null) {
118 token = tokenJson.asText();
119 }
120
121 if (caCertDataJson != null) {
122 caCertData = caCertDataJson.asText();
123 }
124
125 if (clientCertDataJson != null) {
126 clientCertData = clientCertDataJson.asText();
127 }
128
129 if (clientKeyDataJson != null) {
130 clientKeyData = clientKeyDataJson.asText();
131 }
132 }
133
134 return builder.token(token)
135 .caCertData(caCertData)
136 .clientCertData(clientCertData)
137 .clientKeyData(clientKeyData)
138 .build();
139 }
140}