Jian Li | 9871cd5 | 2021-01-09 00:19:02 +0900 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.kubevirtnetworking.util; |
| 17 | |
Jian Li | 260231e | 2021-01-13 18:05:00 +0900 | [diff] [blame^] | 18 | import com.fasterxml.jackson.databind.ObjectMapper; |
Jian Li | 9871cd5 | 2021-01-09 00:19:02 +0900 | [diff] [blame] | 19 | import org.apache.commons.lang.StringUtils; |
| 20 | import org.onosproject.cfg.ConfigProperty; |
| 21 | import org.slf4j.Logger; |
| 22 | import org.slf4j.LoggerFactory; |
| 23 | |
Jian Li | 260231e | 2021-01-13 18:05:00 +0900 | [diff] [blame^] | 24 | import java.io.IOException; |
| 25 | import java.util.List; |
Jian Li | 9871cd5 | 2021-01-09 00:19:02 +0900 | [diff] [blame] | 26 | import java.util.Optional; |
| 27 | import java.util.Set; |
| 28 | |
| 29 | /** |
| 30 | * An utility that used in KubeVirt networking app. |
| 31 | */ |
| 32 | public 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 Li | 260231e | 2021-01-13 18:05:00 +0900 | [diff] [blame^] | 75 | |
| 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 Li | 9871cd5 | 2021-01-09 00:19:02 +0900 | [diff] [blame] | 108 | } |