blob: 9fc2099e7fa6a776da52b3d84c11264c4fdaf5b4 [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";
Jian Lif2483072020-12-25 02:24:16 +090047
48 private static final String MISSING_MESSAGE = " is required in KubevirtApiConfig";
49
50 @Override
51 public ObjectNode encode(KubevirtApiConfig entity, CodecContext context) {
52 ObjectNode node = context.mapper().createObjectNode()
53 .put(SCHEME, entity.scheme().name())
54 .put(IP_ADDRESS, entity.ipAddress().toString())
55 .put(PORT, entity.port())
56 .put(STATE, entity.state().name());
57
58 if (entity.scheme() == HTTPS) {
59 node.put(CA_CERT_DATA, entity.caCertData())
60 .put(CLIENT_CERT_DATA, entity.clientCertData())
61 .put(CLIENT_KEY_DATA, entity.clientKeyData());
62
63 if (entity.token() != null) {
64 node.put(TOKEN, entity.token());
65 }
66
67 } else {
68 if (entity.token() != null) {
69 node.put(TOKEN, entity.token());
70 }
71
72 if (entity.caCertData() != null) {
73 node.put(CA_CERT_DATA, entity.caCertData());
74 }
75
76 if (entity.clientCertData() != null) {
77 node.put(CLIENT_CERT_DATA, entity.clientCertData());
78 }
79
80 if (entity.clientKeyData() != null) {
81 node.put(CLIENT_KEY_DATA, entity.clientKeyData());
82 }
83 }
84
Jian Li94b6d162021-04-15 17:09:11 +090085 if (entity.serviceFqdn() != null) {
86 node.put(SERVICE_FQDN, entity.serviceFqdn());
87 }
88
Jian Li3c3b1632021-04-28 17:24:56 +090089 if (entity.apiServerFqdn() != null) {
90 node.put(API_SERVER_FQDN, entity.apiServerFqdn());
91 }
92
Jian Lie0eaf5c2021-09-06 10:02:13 +090093 if (entity.controllerIp() != null) {
94 node.put(CONTROLLER_IP, entity.controllerIp().toString());
95 }
96
Jian Lif2483072020-12-25 02:24:16 +090097 return node;
98 }
99
100 @Override
101 public KubevirtApiConfig decode(ObjectNode json, CodecContext context) {
102 if (json == null || !json.isObject()) {
103 return null;
104 }
105
106 KubevirtApiConfig.Scheme scheme = KubevirtApiConfig.Scheme.valueOf(nullIsIllegal(
107 json.get(SCHEME).asText(), SCHEME + MISSING_MESSAGE));
108 IpAddress ipAddress = IpAddress.valueOf(nullIsIllegal(
109 json.get(IP_ADDRESS).asText(), IP_ADDRESS + MISSING_MESSAGE));
110 int port = json.get(PORT).asInt();
111
112 KubevirtApiConfig.Builder builder = DefaultKubevirtApiConfig.builder()
113 .scheme(scheme)
114 .ipAddress(ipAddress)
115 .port(port)
116 .state(DISCONNECTED);
117
118 JsonNode tokenJson = json.get(TOKEN);
119 JsonNode caCertDataJson = json.get(CA_CERT_DATA);
120 JsonNode clientCertDataJson = json.get(CLIENT_CERT_DATA);
121 JsonNode clientKeyDataJson = json.get(CLIENT_KEY_DATA);
Jian Li94b6d162021-04-15 17:09:11 +0900122 JsonNode serviceFqdn = json.get(SERVICE_FQDN);
Jian Li3c3b1632021-04-28 17:24:56 +0900123 JsonNode apiServerFqdn = json.get(API_SERVER_FQDN);
Jian Lie0eaf5c2021-09-06 10:02:13 +0900124 JsonNode controllerIp = json.get(CONTROLLER_IP);
Jian Lif2483072020-12-25 02:24:16 +0900125
126 String token = "";
127 String caCertData = "";
128 String clientCertData = "";
129 String clientKeyData = "";
130
131 if (scheme == HTTPS) {
132 caCertData = nullIsIllegal(caCertDataJson.asText(),
133 CA_CERT_DATA + MISSING_MESSAGE);
134 clientCertData = nullIsIllegal(clientCertDataJson.asText(),
135 CLIENT_CERT_DATA + MISSING_MESSAGE);
136 clientKeyData = nullIsIllegal(clientKeyDataJson.asText(),
137 CLIENT_KEY_DATA + MISSING_MESSAGE);
138
139 if (tokenJson != null) {
140 token = tokenJson.asText();
141 }
142
143 } else {
144 if (tokenJson != null) {
145 token = tokenJson.asText();
146 }
147
148 if (caCertDataJson != null) {
149 caCertData = caCertDataJson.asText();
150 }
151
152 if (clientCertDataJson != null) {
153 clientCertData = clientCertDataJson.asText();
154 }
155
156 if (clientKeyDataJson != null) {
157 clientKeyData = clientKeyDataJson.asText();
158 }
159 }
160
161 if (StringUtils.isNotEmpty(token)) {
162 builder.token(token);
163 }
164
165 if (StringUtils.isNotEmpty(caCertData)) {
166 builder.caCertData(caCertData);
167 }
168
169 if (StringUtils.isNotEmpty(clientCertData)) {
170 builder.clientCertData(clientCertData);
171 }
172
173 if (StringUtils.isNotEmpty(clientKeyData)) {
174 builder.clientKeyData(clientKeyData);
175 }
176
Jian Li94b6d162021-04-15 17:09:11 +0900177 if (serviceFqdn != null) {
178 builder.serviceFqdn(serviceFqdn.asText());
179 }
180
Jian Li3c3b1632021-04-28 17:24:56 +0900181 if (apiServerFqdn != null) {
182 builder.apiServerFqdn(apiServerFqdn.asText());
183 }
184
Jian Lie0eaf5c2021-09-06 10:02:13 +0900185 if (controllerIp != null) {
186 builder.controllerIp(IpAddress.valueOf(controllerIp.asText()));
187 }
188
Jian Lif2483072020-12-25 02:24:16 +0900189 return builder.build();
190 }
191}