blob: e00eb525bcccf3dafd59287c102042a973338e5f [file] [log] [blame]
Jian Li9871cd52021-01-09 00:19:02 +09001/*
2 * Copyright 2021-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.kubevirtnetworking.util;
17
Jian Li260231e2021-01-13 18:05:00 +090018import com.fasterxml.jackson.databind.ObjectMapper;
Jian Li9871cd52021-01-09 00:19:02 +090019import org.apache.commons.lang.StringUtils;
20import org.onosproject.cfg.ConfigProperty;
21import org.slf4j.Logger;
22import org.slf4j.LoggerFactory;
23
Jian Li260231e2021-01-13 18:05:00 +090024import java.io.IOException;
25import java.util.List;
Jian Li9871cd52021-01-09 00:19:02 +090026import java.util.Optional;
27import java.util.Set;
28
29/**
30 * An utility that used in KubeVirt networking app.
31 */
32public final class KubevirtNetworkingUtil {
33
34 private static final Logger log = LoggerFactory.getLogger(KubevirtNetworkingUtil.class);
35
36 private static final int PORT_NAME_MAX_LENGTH = 15;
37
38 /**
39 * Prevents object installation from external.
40 */
41 private KubevirtNetworkingUtil() {
42 }
43
44 /**
45 * Obtains the boolean property value with specified property key name.
46 *
47 * @param properties a collection of properties
48 * @param name key name
49 * @return mapping value
50 */
51 public static boolean getPropertyValueAsBoolean(Set<ConfigProperty> properties,
52 String name) {
53 Optional<ConfigProperty> property =
54 properties.stream().filter(p -> p.name().equals(name)).findFirst();
55
56 return property.map(ConfigProperty::asBoolean).orElse(false);
57 }
58
59 /**
60 * Re-structures the OVS port name.
61 * The length of OVS port name should be not large than 15.
62 *
63 * @param portName original port name
64 * @return re-structured OVS port name
65 */
66 public static String structurePortName(String portName) {
67
68 // The size of OVS port name should not be larger than 15
69 if (portName.length() > PORT_NAME_MAX_LENGTH) {
70 return StringUtils.substring(portName, 0, PORT_NAME_MAX_LENGTH);
71 }
72
73 return portName;
74 }
Jian Li260231e2021-01-13 18:05:00 +090075
76 /**
77 * Generates string format based on the given string length list.
78 *
79 * @param stringLengths a list of string lengths
80 * @return string format (e.g., %-28s%-15s%-24s%-20s%-15s)
81 */
82 public static String genFormatString(List<Integer> stringLengths) {
83 StringBuilder fsb = new StringBuilder();
84 stringLengths.forEach(length -> {
85 fsb.append("%-");
86 fsb.append(length);
87 fsb.append("s");
88 });
89 return fsb.toString();
90 }
91
92 /**
93 * Prints out the JSON string in pretty format.
94 *
95 * @param mapper Object mapper
96 * @param jsonString JSON string
97 * @return pretty formatted JSON string
98 */
99 public static String prettyJson(ObjectMapper mapper, String jsonString) {
100 try {
101 Object jsonObject = mapper.readValue(jsonString, Object.class);
102 return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(jsonObject);
103 } catch (IOException e) {
104 log.debug("Json string parsing exception caused by {}", e);
105 }
106 return null;
107 }
Jian Li9871cd52021-01-09 00:19:02 +0900108}