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