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"; |
| 44 | |
| 45 | private static final String MISSING_MESSAGE = " is required in KubevirtApiConfig"; |
| 46 | |
| 47 | @Override |
| 48 | public ObjectNode encode(KubevirtApiConfig entity, CodecContext context) { |
| 49 | ObjectNode node = context.mapper().createObjectNode() |
| 50 | .put(SCHEME, entity.scheme().name()) |
| 51 | .put(IP_ADDRESS, entity.ipAddress().toString()) |
| 52 | .put(PORT, entity.port()) |
| 53 | .put(STATE, entity.state().name()); |
| 54 | |
| 55 | if (entity.scheme() == HTTPS) { |
| 56 | node.put(CA_CERT_DATA, entity.caCertData()) |
| 57 | .put(CLIENT_CERT_DATA, entity.clientCertData()) |
| 58 | .put(CLIENT_KEY_DATA, entity.clientKeyData()); |
| 59 | |
| 60 | if (entity.token() != null) { |
| 61 | node.put(TOKEN, entity.token()); |
| 62 | } |
| 63 | |
| 64 | } else { |
| 65 | if (entity.token() != null) { |
| 66 | node.put(TOKEN, entity.token()); |
| 67 | } |
| 68 | |
| 69 | if (entity.caCertData() != null) { |
| 70 | node.put(CA_CERT_DATA, entity.caCertData()); |
| 71 | } |
| 72 | |
| 73 | if (entity.clientCertData() != null) { |
| 74 | node.put(CLIENT_CERT_DATA, entity.clientCertData()); |
| 75 | } |
| 76 | |
| 77 | if (entity.clientKeyData() != null) { |
| 78 | node.put(CLIENT_KEY_DATA, entity.clientKeyData()); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | return node; |
| 83 | } |
| 84 | |
| 85 | @Override |
| 86 | public KubevirtApiConfig decode(ObjectNode json, CodecContext context) { |
| 87 | if (json == null || !json.isObject()) { |
| 88 | return null; |
| 89 | } |
| 90 | |
| 91 | KubevirtApiConfig.Scheme scheme = KubevirtApiConfig.Scheme.valueOf(nullIsIllegal( |
| 92 | json.get(SCHEME).asText(), SCHEME + MISSING_MESSAGE)); |
| 93 | IpAddress ipAddress = IpAddress.valueOf(nullIsIllegal( |
| 94 | json.get(IP_ADDRESS).asText(), IP_ADDRESS + MISSING_MESSAGE)); |
| 95 | int port = json.get(PORT).asInt(); |
| 96 | |
| 97 | KubevirtApiConfig.Builder builder = DefaultKubevirtApiConfig.builder() |
| 98 | .scheme(scheme) |
| 99 | .ipAddress(ipAddress) |
| 100 | .port(port) |
| 101 | .state(DISCONNECTED); |
| 102 | |
| 103 | JsonNode tokenJson = json.get(TOKEN); |
| 104 | JsonNode caCertDataJson = json.get(CA_CERT_DATA); |
| 105 | JsonNode clientCertDataJson = json.get(CLIENT_CERT_DATA); |
| 106 | JsonNode clientKeyDataJson = json.get(CLIENT_KEY_DATA); |
| 107 | |
| 108 | String token = ""; |
| 109 | String caCertData = ""; |
| 110 | String clientCertData = ""; |
| 111 | String clientKeyData = ""; |
| 112 | |
| 113 | if (scheme == HTTPS) { |
| 114 | caCertData = nullIsIllegal(caCertDataJson.asText(), |
| 115 | CA_CERT_DATA + MISSING_MESSAGE); |
| 116 | clientCertData = nullIsIllegal(clientCertDataJson.asText(), |
| 117 | CLIENT_CERT_DATA + MISSING_MESSAGE); |
| 118 | clientKeyData = nullIsIllegal(clientKeyDataJson.asText(), |
| 119 | CLIENT_KEY_DATA + MISSING_MESSAGE); |
| 120 | |
| 121 | if (tokenJson != null) { |
| 122 | token = tokenJson.asText(); |
| 123 | } |
| 124 | |
| 125 | } else { |
| 126 | if (tokenJson != null) { |
| 127 | token = tokenJson.asText(); |
| 128 | } |
| 129 | |
| 130 | if (caCertDataJson != null) { |
| 131 | caCertData = caCertDataJson.asText(); |
| 132 | } |
| 133 | |
| 134 | if (clientCertDataJson != null) { |
| 135 | clientCertData = clientCertDataJson.asText(); |
| 136 | } |
| 137 | |
| 138 | if (clientKeyDataJson != null) { |
| 139 | clientKeyData = clientKeyDataJson.asText(); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | if (StringUtils.isNotEmpty(token)) { |
| 144 | builder.token(token); |
| 145 | } |
| 146 | |
| 147 | if (StringUtils.isNotEmpty(caCertData)) { |
| 148 | builder.caCertData(caCertData); |
| 149 | } |
| 150 | |
| 151 | if (StringUtils.isNotEmpty(clientCertData)) { |
| 152 | builder.clientCertData(clientCertData); |
| 153 | } |
| 154 | |
| 155 | if (StringUtils.isNotEmpty(clientKeyData)) { |
| 156 | builder.clientKeyData(clientKeyData); |
| 157 | } |
| 158 | |
| 159 | return builder.build(); |
| 160 | } |
| 161 | } |