Jian Li | f248307 | 2020-12-25 02:24:16 +0900 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.kubevirtnode.codec; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 20 | import org.apache.commons.lang.StringUtils; |
| 21 | import org.onlab.packet.IpAddress; |
| 22 | import org.onosproject.codec.CodecContext; |
| 23 | import org.onosproject.codec.JsonCodec; |
| 24 | import org.onosproject.kubevirtnode.api.DefaultKubevirtApiConfig; |
| 25 | import org.onosproject.kubevirtnode.api.KubevirtApiConfig; |
| 26 | |
| 27 | import static org.onlab.util.Tools.nullIsIllegal; |
| 28 | import static org.onosproject.kubevirtnode.api.KubevirtApiConfig.Scheme.HTTPS; |
| 29 | import 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 | */ |
| 34 | public 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 Li | 94b6d16 | 2021-04-15 17:09:11 +0900 | [diff] [blame] | 44 | private static final String SERVICE_FQDN = "serviceFqdn"; |
Jian Li | 3c3b163 | 2021-04-28 17:24:56 +0900 | [diff] [blame] | 45 | private static final String API_SERVER_FQDN = "apiServerFqdn"; |
Jian Li | e0eaf5c | 2021-09-06 10:02:13 +0900 | [diff] [blame] | 46 | private static final String CONTROLLER_IP = "controllerIp"; |
Daniel Park | e42aeb7 | 2022-04-27 12:33:45 +0900 | [diff] [blame^] | 47 | private static final String DATACENTER_ID = "datacenterId"; |
| 48 | private static final String CLUSTER_ID = "clusterId"; |
Jian Li | f248307 | 2020-12-25 02:24:16 +0900 | [diff] [blame] | 49 | |
| 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 Park | e42aeb7 | 2022-04-27 12:33:45 +0900 | [diff] [blame^] | 58 | .put(STATE, entity.state().name()) |
| 59 | .put(DATACENTER_ID, entity.datacenterId()) |
| 60 | .put(CLUSTER_ID, entity.clusterId()); |
Jian Li | f248307 | 2020-12-25 02:24:16 +0900 | [diff] [blame] | 61 | |
| 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 Li | 94b6d16 | 2021-04-15 17:09:11 +0900 | [diff] [blame] | 89 | if (entity.serviceFqdn() != null) { |
| 90 | node.put(SERVICE_FQDN, entity.serviceFqdn()); |
| 91 | } |
| 92 | |
Jian Li | 3c3b163 | 2021-04-28 17:24:56 +0900 | [diff] [blame] | 93 | if (entity.apiServerFqdn() != null) { |
| 94 | node.put(API_SERVER_FQDN, entity.apiServerFqdn()); |
| 95 | } |
| 96 | |
Jian Li | e0eaf5c | 2021-09-06 10:02:13 +0900 | [diff] [blame] | 97 | if (entity.controllerIp() != null) { |
| 98 | node.put(CONTROLLER_IP, entity.controllerIp().toString()); |
| 99 | } |
| 100 | |
Jian Li | f248307 | 2020-12-25 02:24:16 +0900 | [diff] [blame] | 101 | 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 Li | 94b6d16 | 2021-04-15 17:09:11 +0900 | [diff] [blame] | 126 | JsonNode serviceFqdn = json.get(SERVICE_FQDN); |
Jian Li | 3c3b163 | 2021-04-28 17:24:56 +0900 | [diff] [blame] | 127 | JsonNode apiServerFqdn = json.get(API_SERVER_FQDN); |
Jian Li | e0eaf5c | 2021-09-06 10:02:13 +0900 | [diff] [blame] | 128 | JsonNode controllerIp = json.get(CONTROLLER_IP); |
Daniel Park | e42aeb7 | 2022-04-27 12:33:45 +0900 | [diff] [blame^] | 129 | JsonNode datacenterId = json.get(DATACENTER_ID); |
| 130 | JsonNode clusterId = json.get(CLUSTER_ID); |
Jian Li | f248307 | 2020-12-25 02:24:16 +0900 | [diff] [blame] | 131 | |
| 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 Li | 94b6d16 | 2021-04-15 17:09:11 +0900 | [diff] [blame] | 183 | if (serviceFqdn != null) { |
| 184 | builder.serviceFqdn(serviceFqdn.asText()); |
| 185 | } |
| 186 | |
Jian Li | 3c3b163 | 2021-04-28 17:24:56 +0900 | [diff] [blame] | 187 | if (apiServerFqdn != null) { |
| 188 | builder.apiServerFqdn(apiServerFqdn.asText()); |
| 189 | } |
| 190 | |
Jian Li | e0eaf5c | 2021-09-06 10:02:13 +0900 | [diff] [blame] | 191 | if (controllerIp != null) { |
| 192 | builder.controllerIp(IpAddress.valueOf(controllerIp.asText())); |
| 193 | } |
| 194 | |
Daniel Park | e42aeb7 | 2022-04-27 12:33:45 +0900 | [diff] [blame^] | 195 | if (datacenterId != null) { |
| 196 | builder.datacenterId(datacenterId.asText()); |
| 197 | } |
| 198 | |
| 199 | if (clusterId != null) { |
| 200 | builder.clusterId(clusterId.asText()); |
| 201 | } |
| 202 | |
Jian Li | f248307 | 2020-12-25 02:24:16 +0900 | [diff] [blame] | 203 | return builder.build(); |
| 204 | } |
| 205 | } |