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.api; |
| 17 | |
| 18 | import com.google.common.base.MoreObjects; |
| 19 | import org.onlab.packet.IpAddress; |
| 20 | |
| 21 | import java.util.Objects; |
| 22 | |
| 23 | import static com.google.common.base.Preconditions.checkArgument; |
| 24 | import static org.onosproject.k8snode.api.K8sApiConfig.Scheme.HTTPS; |
| 25 | |
| 26 | /** |
| 27 | * Default implementation of kubernetes API configuration. |
| 28 | */ |
| 29 | public final class DefaultK8sApiConfig implements K8sApiConfig { |
| 30 | |
| 31 | private static final String NOT_NULL_MSG = "API Config % cannot be null"; |
| 32 | |
| 33 | private final Scheme scheme; |
| 34 | private final IpAddress ipAddress; |
| 35 | private final int port; |
| 36 | private final String token; |
| 37 | private final String caCertData; |
| 38 | private final String clientCertData; |
| 39 | private final String clientKeyData; |
| 40 | |
| 41 | private DefaultK8sApiConfig(Scheme scheme, IpAddress ipAddress, int port, |
| 42 | String token, String caCertData, |
| 43 | String clientCertData, String clientKeyData) { |
| 44 | this.scheme = scheme; |
| 45 | this.ipAddress = ipAddress; |
| 46 | this.port = port; |
| 47 | this.token = token; |
| 48 | this.caCertData = caCertData; |
| 49 | this.clientCertData = clientCertData; |
| 50 | this.clientKeyData = clientKeyData; |
| 51 | } |
| 52 | |
| 53 | @Override |
| 54 | public Scheme scheme() { |
| 55 | return scheme; |
| 56 | } |
| 57 | |
| 58 | @Override |
| 59 | public IpAddress ipAddress() { |
| 60 | return ipAddress; |
| 61 | } |
| 62 | |
| 63 | @Override |
| 64 | public int port() { |
| 65 | return port; |
| 66 | } |
| 67 | |
| 68 | @Override |
| 69 | public String token() { |
| 70 | return token; |
| 71 | } |
| 72 | |
| 73 | @Override |
| 74 | public String caCertData() { |
| 75 | return caCertData; |
| 76 | } |
| 77 | |
| 78 | @Override |
| 79 | public String clientCertData() { |
| 80 | return clientCertData; |
| 81 | } |
| 82 | |
| 83 | @Override |
| 84 | public String clientKeyData() { |
| 85 | return clientKeyData; |
| 86 | } |
| 87 | |
| 88 | @Override |
| 89 | public boolean equals(Object o) { |
| 90 | if (this == o) { |
| 91 | return true; |
| 92 | } |
| 93 | if (o == null || getClass() != o.getClass()) { |
| 94 | return false; |
| 95 | } |
| 96 | DefaultK8sApiConfig that = (DefaultK8sApiConfig) o; |
| 97 | return port == that.port && |
| 98 | scheme == that.scheme && |
| 99 | ipAddress.equals(that.ipAddress) && |
| 100 | token.equals(that.token) && |
| 101 | caCertData.equals(that.caCertData) && |
| 102 | clientCertData.equals(that.clientCertData) && |
| 103 | clientKeyData.equals(that.clientKeyData); |
| 104 | } |
| 105 | |
| 106 | @Override |
| 107 | public int hashCode() { |
| 108 | return Objects.hash(scheme, ipAddress, port, token, caCertData, |
| 109 | clientCertData, clientKeyData); |
| 110 | } |
| 111 | |
| 112 | @Override |
| 113 | public String toString() { |
| 114 | return MoreObjects.toStringHelper(this) |
| 115 | .add("scheme", scheme) |
| 116 | .add("ipAddress", ipAddress) |
| 117 | .add("port", port) |
| 118 | .add("token", token) |
| 119 | .add("caCertData", caCertData) |
| 120 | .add("clientCertData", clientCertData) |
| 121 | .add("clientKeyData", clientKeyData) |
| 122 | .toString(); |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Returns new builder instance. |
| 127 | * |
| 128 | * @return kubernetes API server config builder |
| 129 | */ |
| 130 | public static Builder builder() { |
| 131 | return new Builder(); |
| 132 | } |
| 133 | |
| 134 | public static final class Builder implements K8sApiConfig.Builder { |
| 135 | |
| 136 | private Scheme scheme; |
| 137 | private IpAddress ipAddress; |
| 138 | private int port; |
| 139 | private String token; |
| 140 | private String caCertData; |
| 141 | private String clientCertData; |
| 142 | private String clientKeyData; |
| 143 | |
| 144 | @Override |
| 145 | public K8sApiConfig build() { |
| 146 | checkArgument(scheme != null, NOT_NULL_MSG, "scheme"); |
| 147 | checkArgument(ipAddress != null, NOT_NULL_MSG, "ipAddress"); |
| 148 | |
| 149 | if (scheme == HTTPS) { |
| 150 | checkArgument(token != null, NOT_NULL_MSG, "token"); |
| 151 | checkArgument(caCertData != null, NOT_NULL_MSG, "caCertData"); |
| 152 | checkArgument(clientCertData != null, NOT_NULL_MSG, "clientCertData"); |
| 153 | checkArgument(clientKeyData != null, NOT_NULL_MSG, "clientKeyData"); |
| 154 | } |
| 155 | |
| 156 | return new DefaultK8sApiConfig(scheme, ipAddress, port, token, |
| 157 | caCertData, clientCertData, clientKeyData); |
| 158 | } |
| 159 | |
| 160 | @Override |
| 161 | public Builder scheme(Scheme scheme) { |
| 162 | this.scheme = scheme; |
| 163 | return this; |
| 164 | } |
| 165 | |
| 166 | @Override |
| 167 | public Builder ipAddress(IpAddress ipAddress) { |
| 168 | this.ipAddress = ipAddress; |
| 169 | return this; |
| 170 | } |
| 171 | |
| 172 | @Override |
| 173 | public Builder port(int port) { |
| 174 | this.port = port; |
| 175 | return this; |
| 176 | } |
| 177 | |
| 178 | @Override |
| 179 | public Builder token(String token) { |
| 180 | this.token = token; |
| 181 | return this; |
| 182 | } |
| 183 | |
| 184 | @Override |
| 185 | public Builder caCertData(String caCertData) { |
| 186 | this.caCertData = caCertData; |
| 187 | return this; |
| 188 | } |
| 189 | |
| 190 | @Override |
| 191 | public Builder clientCertData(String clientCertData) { |
| 192 | this.clientCertData = clientCertData; |
| 193 | return this; |
| 194 | } |
| 195 | |
| 196 | @Override |
| 197 | public Builder clientKeyData(String clientKeyData) { |
| 198 | this.clientKeyData = clientKeyData; |
| 199 | return this; |
| 200 | } |
| 201 | } |
| 202 | } |