blob: c7051bbad7d7299fd502ab35c2c07065cdc5dc52 [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;
Jian Li7bca1272021-01-14 11:30:36 +090020import org.apache.commons.net.util.SubnetUtils;
21import org.onlab.packet.IpAddress;
Jian Li9871cd52021-01-09 00:19:02 +090022import org.onosproject.cfg.ConfigProperty;
23import org.slf4j.Logger;
24import org.slf4j.LoggerFactory;
25
Jian Li260231e2021-01-13 18:05:00 +090026import java.io.IOException;
Jian Li7bca1272021-01-14 11:30:36 +090027import java.util.Arrays;
28import java.util.HashSet;
Jian Li260231e2021-01-13 18:05:00 +090029import java.util.List;
Jian Li9871cd52021-01-09 00:19:02 +090030import java.util.Optional;
31import java.util.Set;
Jian Li7bca1272021-01-14 11:30:36 +090032import java.util.stream.Collectors;
Jian Li9871cd52021-01-09 00:19:02 +090033
34/**
35 * An utility that used in KubeVirt networking app.
36 */
37public final class KubevirtNetworkingUtil {
38
39 private static final Logger log = LoggerFactory.getLogger(KubevirtNetworkingUtil.class);
40
41 private static final int PORT_NAME_MAX_LENGTH = 15;
42
43 /**
44 * Prevents object installation from external.
45 */
46 private KubevirtNetworkingUtil() {
47 }
48
49 /**
50 * Obtains the boolean property value with specified property key name.
51 *
52 * @param properties a collection of properties
53 * @param name key name
54 * @return mapping value
55 */
56 public static boolean getPropertyValueAsBoolean(Set<ConfigProperty> properties,
57 String name) {
58 Optional<ConfigProperty> property =
59 properties.stream().filter(p -> p.name().equals(name)).findFirst();
60
61 return property.map(ConfigProperty::asBoolean).orElse(false);
62 }
63
64 /**
65 * Re-structures the OVS port name.
66 * The length of OVS port name should be not large than 15.
67 *
68 * @param portName original port name
69 * @return re-structured OVS port name
70 */
71 public static String structurePortName(String portName) {
72
73 // The size of OVS port name should not be larger than 15
74 if (portName.length() > PORT_NAME_MAX_LENGTH) {
75 return StringUtils.substring(portName, 0, PORT_NAME_MAX_LENGTH);
76 }
77
78 return portName;
79 }
Jian Li260231e2021-01-13 18:05:00 +090080
81 /**
82 * Generates string format based on the given string length list.
83 *
84 * @param stringLengths a list of string lengths
85 * @return string format (e.g., %-28s%-15s%-24s%-20s%-15s)
86 */
87 public static String genFormatString(List<Integer> stringLengths) {
88 StringBuilder fsb = new StringBuilder();
89 stringLengths.forEach(length -> {
90 fsb.append("%-");
91 fsb.append(length);
92 fsb.append("s");
93 });
94 return fsb.toString();
95 }
96
97 /**
98 * Prints out the JSON string in pretty format.
99 *
100 * @param mapper Object mapper
101 * @param jsonString JSON string
102 * @return pretty formatted JSON string
103 */
104 public static String prettyJson(ObjectMapper mapper, String jsonString) {
105 try {
106 Object jsonObject = mapper.readValue(jsonString, Object.class);
107 return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(jsonObject);
108 } catch (IOException e) {
109 log.debug("Json string parsing exception caused by {}", e);
110 }
111 return null;
112 }
Jian Li7bca1272021-01-14 11:30:36 +0900113
114 /**
115 * Obtains valid IP addresses of the given subnet.
116 *
117 * @param cidr CIDR
118 * @return set of IP addresses
119 */
120 public static Set<IpAddress> getSubnetIps(String cidr) {
121 SubnetUtils utils = new SubnetUtils(cidr);
122 utils.setInclusiveHostCount(false);
123 SubnetUtils.SubnetInfo info = utils.getInfo();
124 Set<String> allAddresses =
125 new HashSet<>(Arrays.asList(info.getAllAddresses()));
126
127 if (allAddresses.size() > 2) {
128 allAddresses.remove(info.getLowAddress());
129 allAddresses.remove(info.getHighAddress());
130 }
131
132 return allAddresses.stream()
133 .map(IpAddress::valueOf).collect(Collectors.toSet());
134 }
135
136 /**
137 * Calculate the broadcast address from given IP address and subnet prefix length.
138 *
139 * @param ipAddr IP address
140 * @param prefixLength subnet prefix length
141 * @return broadcast address
142 */
143 public static String getBroadcastAddr(String ipAddr, int prefixLength) {
144 String subnet = ipAddr + "/" + prefixLength;
145 SubnetUtils utils = new SubnetUtils(subnet);
146 return utils.getInfo().getBroadcastAddress();
147 }
Jian Li9871cd52021-01-09 00:19:02 +0900148}