blob: e79843f5a1fb9dbb5a153be8680e30bc6cc5868d [file] [log] [blame]
Jian Lif2483072020-12-25 02:24:16 +09001/*
2 * Copyright 2020-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.kubevirtnode.codec;
17
18import com.fasterxml.jackson.databind.JsonNode;
19import com.fasterxml.jackson.databind.node.ObjectNode;
20import org.apache.commons.lang.StringUtils;
21import org.onlab.packet.IpAddress;
22import org.onosproject.codec.CodecContext;
23import org.onosproject.codec.JsonCodec;
24import org.onosproject.kubevirtnode.api.DefaultKubevirtApiConfig;
25import org.onosproject.kubevirtnode.api.KubevirtApiConfig;
26
27import static org.onlab.util.Tools.nullIsIllegal;
28import static org.onosproject.kubevirtnode.api.KubevirtApiConfig.Scheme.HTTPS;
29import static org.onosproject.kubevirtnode.api.KubevirtApiConfig.State.DISCONNECTED;
30
31/**
32 * KubeVirt API server config codec used for serializing and de-serializing JSON string.
33 */
34public final class KubevirtApiConfigCodec extends JsonCodec<KubevirtApiConfig> {
35
36 private static final String SCHEME = "scheme";
37 private static final String IP_ADDRESS = "ipAddress";
38 private static final String PORT = "port";
39 private static final String STATE = "state";
40 private static final String TOKEN = "token";
41 private static final String CA_CERT_DATA = "caCertData";
42 private static final String CLIENT_CERT_DATA = "clientCertData";
43 private static final String CLIENT_KEY_DATA = "clientKeyData";
Jian Li94b6d162021-04-15 17:09:11 +090044 private static final String SERVICE_FQDN = "serviceFqdn";
Jian Li3c3b1632021-04-28 17:24:56 +090045 private static final String API_SERVER_FQDN = "apiServerFqdn";
Jian Lie0eaf5c2021-09-06 10:02:13 +090046 private static final String CONTROLLER_IP = "controllerIp";
Daniel Parke42aeb72022-04-27 12:33:45 +090047 private static final String DATACENTER_ID = "datacenterId";
48 private static final String CLUSTER_ID = "clusterId";
Jian Lif2483072020-12-25 02:24:16 +090049
50 private static final String MISSING_MESSAGE = " is required in KubevirtApiConfig";
51
52 @Override
53 public ObjectNode encode(KubevirtApiConfig entity, CodecContext context) {
54 ObjectNode node = context.mapper().createObjectNode()
55 .put(SCHEME, entity.scheme().name())
56 .put(IP_ADDRESS, entity.ipAddress().toString())
57 .put(PORT, entity.port())
Daniel Parke42aeb72022-04-27 12:33:45 +090058 .put(STATE, entity.state().name())
59 .put(DATACENTER_ID, entity.datacenterId())
60 .put(CLUSTER_ID, entity.clusterId());
Jian Lif2483072020-12-25 02:24:16 +090061
62 if (entity.scheme() == HTTPS) {
63 node.put(CA_CERT_DATA, entity.caCertData())
64 .put(CLIENT_CERT_DATA, entity.clientCertData())
65 .put(CLIENT_KEY_DATA, entity.clientKeyData());
66
67 if (entity.token() != null) {
68 node.put(TOKEN, entity.token());
69 }
70
71 } else {
72 if (entity.token() != null) {
73 node.put(TOKEN, entity.token());
74 }
75
76 if (entity.caCertData() != null) {
77 node.put(CA_CERT_DATA, entity.caCertData());
78 }
79
80 if (entity.clientCertData() != null) {
81 node.put(CLIENT_CERT_DATA, entity.clientCertData());
82 }
83
84 if (entity.clientKeyData() != null) {
85 node.put(CLIENT_KEY_DATA, entity.clientKeyData());
86 }
87 }
88
Jian Li94b6d162021-04-15 17:09:11 +090089 if (entity.serviceFqdn() != null) {
90 node.put(SERVICE_FQDN, entity.serviceFqdn());
91 }
92
Jian Li3c3b1632021-04-28 17:24:56 +090093 if (entity.apiServerFqdn() != null) {
94 node.put(API_SERVER_FQDN, entity.apiServerFqdn());
95 }
96
Jian Lie0eaf5c2021-09-06 10:02:13 +090097 if (entity.controllerIp() != null) {
98 node.put(CONTROLLER_IP, entity.controllerIp().toString());
99 }
100
Jian Lif2483072020-12-25 02:24:16 +0900101 return node;
102 }
103
104 @Override
105 public KubevirtApiConfig decode(ObjectNode json, CodecContext context) {
106 if (json == null || !json.isObject()) {
107 return null;
108 }
109
110 KubevirtApiConfig.Scheme scheme = KubevirtApiConfig.Scheme.valueOf(nullIsIllegal(
111 json.get(SCHEME).asText(), SCHEME + MISSING_MESSAGE));
112 IpAddress ipAddress = IpAddress.valueOf(nullIsIllegal(
113 json.get(IP_ADDRESS).asText(), IP_ADDRESS + MISSING_MESSAGE));
114 int port = json.get(PORT).asInt();
115
116 KubevirtApiConfig.Builder builder = DefaultKubevirtApiConfig.builder()
117 .scheme(scheme)
118 .ipAddress(ipAddress)
119 .port(port)
120 .state(DISCONNECTED);
121
122 JsonNode tokenJson = json.get(TOKEN);
123 JsonNode caCertDataJson = json.get(CA_CERT_DATA);
124 JsonNode clientCertDataJson = json.get(CLIENT_CERT_DATA);
125 JsonNode clientKeyDataJson = json.get(CLIENT_KEY_DATA);
Jian Li94b6d162021-04-15 17:09:11 +0900126 JsonNode serviceFqdn = json.get(SERVICE_FQDN);
Jian Li3c3b1632021-04-28 17:24:56 +0900127 JsonNode apiServerFqdn = json.get(API_SERVER_FQDN);
Jian Lie0eaf5c2021-09-06 10:02:13 +0900128 JsonNode controllerIp = json.get(CONTROLLER_IP);
Daniel Parke42aeb72022-04-27 12:33:45 +0900129 JsonNode datacenterId = json.get(DATACENTER_ID);
130 JsonNode clusterId = json.get(CLUSTER_ID);
Jian Lif2483072020-12-25 02:24:16 +0900131
132 String token = "";
133 String caCertData = "";
134 String clientCertData = "";
135 String clientKeyData = "";
136
137 if (scheme == HTTPS) {
138 caCertData = nullIsIllegal(caCertDataJson.asText(),
139 CA_CERT_DATA + MISSING_MESSAGE);
140 clientCertData = nullIsIllegal(clientCertDataJson.asText(),
141 CLIENT_CERT_DATA + MISSING_MESSAGE);
142 clientKeyData = nullIsIllegal(clientKeyDataJson.asText(),
143 CLIENT_KEY_DATA + MISSING_MESSAGE);
144
145 if (tokenJson != null) {
146 token = tokenJson.asText();
147 }
148
149 } else {
150 if (tokenJson != null) {
151 token = tokenJson.asText();
152 }
153
154 if (caCertDataJson != null) {
155 caCertData = caCertDataJson.asText();
156 }
157
158 if (clientCertDataJson != null) {
159 clientCertData = clientCertDataJson.asText();
160 }
161
162 if (clientKeyDataJson != null) {
163 clientKeyData = clientKeyDataJson.asText();
164 }
165 }
166
167 if (StringUtils.isNotEmpty(token)) {
168 builder.token(token);
169 }
170
171 if (StringUtils.isNotEmpty(caCertData)) {
172 builder.caCertData(caCertData);
173 }
174
175 if (StringUtils.isNotEmpty(clientCertData)) {
176 builder.clientCertData(clientCertData);
177 }
178
179 if (StringUtils.isNotEmpty(clientKeyData)) {
180 builder.clientKeyData(clientKeyData);
181 }
182
Jian Li94b6d162021-04-15 17:09:11 +0900183 if (serviceFqdn != null) {
184 builder.serviceFqdn(serviceFqdn.asText());
185 }
186
Jian Li3c3b1632021-04-28 17:24:56 +0900187 if (apiServerFqdn != null) {
188 builder.apiServerFqdn(apiServerFqdn.asText());
189 }
190
Jian Lie0eaf5c2021-09-06 10:02:13 +0900191 if (controllerIp != null) {
192 builder.controllerIp(IpAddress.valueOf(controllerIp.asText()));
193 }
194
Daniel Parke42aeb72022-04-27 12:33:45 +0900195 if (datacenterId != null) {
196 builder.datacenterId(datacenterId.asText());
197 }
198
199 if (clusterId != null) {
200 builder.clusterId(clusterId.asText());
201 }
202
Jian Lif2483072020-12-25 02:24:16 +0900203 return builder.build();
204 }
205}