blob: 57f989e8d41ed069b893fb0307e7d79404c6f2a6 [file] [log] [blame]
Jian Lib230e07c2020-12-21 11:25:12 +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.util;
17
18import com.fasterxml.jackson.databind.ObjectMapper;
Jian Liaaf44b52020-12-27 23:22:46 +090019import io.fabric8.kubernetes.client.ConfigBuilder;
20import io.fabric8.kubernetes.client.DefaultKubernetesClient;
21import io.fabric8.kubernetes.client.KubernetesClient;
Jian Lif2483072020-12-25 02:24:16 +090022import org.apache.commons.lang.StringUtils;
23import org.onlab.packet.IpAddress;
24import org.onosproject.kubevirtnode.api.KubevirtApiConfig;
Jian Lib230e07c2020-12-21 11:25:12 +090025import org.slf4j.Logger;
26import org.slf4j.LoggerFactory;
27
28import java.io.IOException;
29import java.util.List;
30
31/**
32 * An utility that used in KubeVirt node app.
33 */
34public final class KubevirtNodeUtil {
35
36 private static final Logger log = LoggerFactory.getLogger(KubevirtNodeUtil.class);
37
38 private static final String COLON_SLASH = "://";
39 private static final String COLON = ":";
40
41 private static final int HEX_LENGTH = 16;
42 private static final String OF_PREFIX = "of:";
43 private static final String ZERO = "0";
44
45 /**
46 * Prevents object installation from external.
47 */
48 private KubevirtNodeUtil() {
49 }
50
51 /**
Jian Lif2483072020-12-25 02:24:16 +090052 * Generates endpoint URL by referring to scheme, ipAddress and port.
53 *
54 * @param apiConfig kubernetes API config
55 * @return generated endpoint URL
56 */
57 public static String endpoint(KubevirtApiConfig apiConfig) {
58 return endpoint(apiConfig.scheme(), apiConfig.ipAddress(), apiConfig.port());
59 }
60
61 /**
62 * Generates endpoint URL by referring to scheme, ipAddress and port.
63 *
64 * @param scheme scheme
65 * @param ipAddress IP address
66 * @param port port number
67 * @return generated endpoint URL
68 */
69 public static String endpoint(KubevirtApiConfig.Scheme scheme, IpAddress ipAddress, int port) {
70 StringBuilder endpoint = new StringBuilder();
71 String protocol = StringUtils.lowerCase(scheme.name());
72
73 endpoint.append(protocol);
74 endpoint.append(COLON_SLASH);
75 endpoint.append(ipAddress.toString());
76 endpoint.append(COLON);
77 endpoint.append(port);
78
79 return endpoint.toString();
80 }
81
82 /**
Jian Lib230e07c2020-12-21 11:25:12 +090083 * Generates a DPID (of:0000000000000001) from an index value.
84 *
85 * @param index index value
86 * @return generated DPID
87 */
88 public static String genDpid(long index) {
89 if (index < 0) {
90 return null;
91 }
92
93 String hexStr = Long.toHexString(index);
94
95 StringBuilder zeroPadding = new StringBuilder();
96 for (int i = 0; i < HEX_LENGTH - hexStr.length(); i++) {
97 zeroPadding.append(ZERO);
98 }
99
100 return OF_PREFIX + zeroPadding.toString() + hexStr;
101 }
102
103 /**
104 * Generates string format based on the given string length list.
105 *
106 * @param stringLengths a list of string lengths
107 * @return string format (e.g., %-28s%-15s%-24s%-20s%-15s)
108 */
109 public static String genFormatString(List<Integer> stringLengths) {
110 StringBuilder fsb = new StringBuilder();
111 stringLengths.forEach(length -> {
112 fsb.append("%-");
113 fsb.append(length);
114 fsb.append("s");
115 });
116 return fsb.toString();
117 }
118
119 /**
120 * Prints out the JSON string in pretty format.
121 *
122 * @param mapper Object mapper
123 * @param jsonString JSON string
124 * @return pretty formatted JSON string
125 */
126 public static String prettyJson(ObjectMapper mapper, String jsonString) {
127 try {
128 Object jsonObject = mapper.readValue(jsonString, Object.class);
129 return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(jsonObject);
130 } catch (IOException e) {
131 log.debug("Json string parsing exception caused by {}", e);
132 }
133 return null;
134 }
Jian Liaaf44b52020-12-27 23:22:46 +0900135
136 /**
137 * Obtains workable kubernetes client.
138 *
139 * @param config kubernetes API config
140 * @return kubernetes client
141 */
142 public static KubernetesClient k8sClient(KubevirtApiConfig config) {
143 if (config == null) {
144 log.warn("Kubernetes API server config is empty.");
145 return null;
146 }
147
148 String endpoint = endpoint(config);
149
150 ConfigBuilder configBuilder = new ConfigBuilder().withMasterUrl(endpoint);
151
152 if (config.scheme() == KubevirtApiConfig.Scheme.HTTPS) {
153 configBuilder.withTrustCerts(true)
154 .withCaCertData(config.caCertData())
155 .withClientCertData(config.clientCertData())
156 .withClientKeyData(config.clientKeyData());
157
158 if (StringUtils.isNotEmpty(config.token())) {
159 configBuilder.withOauthToken(config.token());
160 }
161 }
162
163 return new DefaultKubernetesClient(configBuilder.build());
164 }
Jian Lib230e07c2020-12-21 11:25:12 +0900165}