| /* |
| * Copyright 2021-present Open Networking Foundation |
| * |
| * Licensed under the Apache License, Version 2.0 (the "License"); |
| * you may not use this file except in compliance with the License. |
| * You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| */ |
| package org.onosproject.kubevirtnetworking.util; |
| |
| import com.fasterxml.jackson.databind.ObjectMapper; |
| import org.apache.commons.lang.StringUtils; |
| import org.apache.commons.net.util.SubnetUtils; |
| import org.onlab.packet.IpAddress; |
| import org.onosproject.cfg.ConfigProperty; |
| import org.slf4j.Logger; |
| import org.slf4j.LoggerFactory; |
| |
| import java.io.IOException; |
| import java.util.Arrays; |
| import java.util.HashSet; |
| import java.util.List; |
| import java.util.Optional; |
| import java.util.Set; |
| import java.util.stream.Collectors; |
| |
| /** |
| * An utility that used in KubeVirt networking app. |
| */ |
| public final class KubevirtNetworkingUtil { |
| |
| private static final Logger log = LoggerFactory.getLogger(KubevirtNetworkingUtil.class); |
| |
| private static final int PORT_NAME_MAX_LENGTH = 15; |
| |
| /** |
| * Prevents object installation from external. |
| */ |
| private KubevirtNetworkingUtil() { |
| } |
| |
| /** |
| * Obtains the boolean property value with specified property key name. |
| * |
| * @param properties a collection of properties |
| * @param name key name |
| * @return mapping value |
| */ |
| public static boolean getPropertyValueAsBoolean(Set<ConfigProperty> properties, |
| String name) { |
| Optional<ConfigProperty> property = |
| properties.stream().filter(p -> p.name().equals(name)).findFirst(); |
| |
| return property.map(ConfigProperty::asBoolean).orElse(false); |
| } |
| |
| /** |
| * Re-structures the OVS port name. |
| * The length of OVS port name should be not large than 15. |
| * |
| * @param portName original port name |
| * @return re-structured OVS port name |
| */ |
| public static String structurePortName(String portName) { |
| |
| // The size of OVS port name should not be larger than 15 |
| if (portName.length() > PORT_NAME_MAX_LENGTH) { |
| return StringUtils.substring(portName, 0, PORT_NAME_MAX_LENGTH); |
| } |
| |
| return portName; |
| } |
| |
| /** |
| * Generates string format based on the given string length list. |
| * |
| * @param stringLengths a list of string lengths |
| * @return string format (e.g., %-28s%-15s%-24s%-20s%-15s) |
| */ |
| public static String genFormatString(List<Integer> stringLengths) { |
| StringBuilder fsb = new StringBuilder(); |
| stringLengths.forEach(length -> { |
| fsb.append("%-"); |
| fsb.append(length); |
| fsb.append("s"); |
| }); |
| return fsb.toString(); |
| } |
| |
| /** |
| * Prints out the JSON string in pretty format. |
| * |
| * @param mapper Object mapper |
| * @param jsonString JSON string |
| * @return pretty formatted JSON string |
| */ |
| public static String prettyJson(ObjectMapper mapper, String jsonString) { |
| try { |
| Object jsonObject = mapper.readValue(jsonString, Object.class); |
| return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(jsonObject); |
| } catch (IOException e) { |
| log.debug("Json string parsing exception caused by {}", e); |
| } |
| return null; |
| } |
| |
| /** |
| * Obtains valid IP addresses of the given subnet. |
| * |
| * @param cidr CIDR |
| * @return set of IP addresses |
| */ |
| public static Set<IpAddress> getSubnetIps(String cidr) { |
| SubnetUtils utils = new SubnetUtils(cidr); |
| utils.setInclusiveHostCount(false); |
| SubnetUtils.SubnetInfo info = utils.getInfo(); |
| Set<String> allAddresses = |
| new HashSet<>(Arrays.asList(info.getAllAddresses())); |
| |
| if (allAddresses.size() > 2) { |
| allAddresses.remove(info.getLowAddress()); |
| allAddresses.remove(info.getHighAddress()); |
| } |
| |
| return allAddresses.stream() |
| .map(IpAddress::valueOf).collect(Collectors.toSet()); |
| } |
| |
| /** |
| * Calculate the broadcast address from given IP address and subnet prefix length. |
| * |
| * @param ipAddr IP address |
| * @param prefixLength subnet prefix length |
| * @return broadcast address |
| */ |
| public static String getBroadcastAddr(String ipAddr, int prefixLength) { |
| String subnet = ipAddr + "/" + prefixLength; |
| SubnetUtils utils = new SubnetUtils(subnet); |
| return utils.getInfo().getBroadcastAddress(); |
| } |
| } |