blob: dba7d352ce1d548435dc78ed4f90f4cf98152ce0 [file] [log] [blame]
Jian Li7c4a8822020-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 Lic4604302020-12-25 02:24:16 +090019import org.apache.commons.lang.StringUtils;
20import org.onlab.packet.IpAddress;
21import org.onosproject.kubevirtnode.api.KubevirtApiConfig;
Jian Li7c4a8822020-12-21 11:25:12 +090022import org.slf4j.Logger;
23import org.slf4j.LoggerFactory;
24
25import java.io.IOException;
26import java.util.List;
27
28/**
29 * An utility that used in KubeVirt node app.
30 */
31public final class KubevirtNodeUtil {
32
33 private static final Logger log = LoggerFactory.getLogger(KubevirtNodeUtil.class);
34
35 private static final String COLON_SLASH = "://";
36 private static final String COLON = ":";
37
38 private static final int HEX_LENGTH = 16;
39 private static final String OF_PREFIX = "of:";
40 private static final String ZERO = "0";
41
42 /**
43 * Prevents object installation from external.
44 */
45 private KubevirtNodeUtil() {
46 }
47
48 /**
Jian Lic4604302020-12-25 02:24:16 +090049 * Generates endpoint URL by referring to scheme, ipAddress and port.
50 *
51 * @param apiConfig kubernetes API config
52 * @return generated endpoint URL
53 */
54 public static String endpoint(KubevirtApiConfig apiConfig) {
55 return endpoint(apiConfig.scheme(), apiConfig.ipAddress(), apiConfig.port());
56 }
57
58 /**
59 * Generates endpoint URL by referring to scheme, ipAddress and port.
60 *
61 * @param scheme scheme
62 * @param ipAddress IP address
63 * @param port port number
64 * @return generated endpoint URL
65 */
66 public static String endpoint(KubevirtApiConfig.Scheme scheme, IpAddress ipAddress, int port) {
67 StringBuilder endpoint = new StringBuilder();
68 String protocol = StringUtils.lowerCase(scheme.name());
69
70 endpoint.append(protocol);
71 endpoint.append(COLON_SLASH);
72 endpoint.append(ipAddress.toString());
73 endpoint.append(COLON);
74 endpoint.append(port);
75
76 return endpoint.toString();
77 }
78
79 /**
Jian Li7c4a8822020-12-21 11:25:12 +090080 * Generates a DPID (of:0000000000000001) from an index value.
81 *
82 * @param index index value
83 * @return generated DPID
84 */
85 public static String genDpid(long index) {
86 if (index < 0) {
87 return null;
88 }
89
90 String hexStr = Long.toHexString(index);
91
92 StringBuilder zeroPadding = new StringBuilder();
93 for (int i = 0; i < HEX_LENGTH - hexStr.length(); i++) {
94 zeroPadding.append(ZERO);
95 }
96
97 return OF_PREFIX + zeroPadding.toString() + hexStr;
98 }
99
100 /**
101 * Generates string format based on the given string length list.
102 *
103 * @param stringLengths a list of string lengths
104 * @return string format (e.g., %-28s%-15s%-24s%-20s%-15s)
105 */
106 public static String genFormatString(List<Integer> stringLengths) {
107 StringBuilder fsb = new StringBuilder();
108 stringLengths.forEach(length -> {
109 fsb.append("%-");
110 fsb.append(length);
111 fsb.append("s");
112 });
113 return fsb.toString();
114 }
115
116 /**
117 * Prints out the JSON string in pretty format.
118 *
119 * @param mapper Object mapper
120 * @param jsonString JSON string
121 * @return pretty formatted JSON string
122 */
123 public static String prettyJson(ObjectMapper mapper, String jsonString) {
124 try {
125 Object jsonObject = mapper.readValue(jsonString, Object.class);
126 return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(jsonObject);
127 } catch (IOException e) {
128 log.debug("Json string parsing exception caused by {}", e);
129 }
130 return null;
131 }
132}