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 | |
| 18 | import org.apache.commons.lang.StringUtils; |
| 19 | import org.onosproject.cfg.ConfigProperty; |
| 20 | import org.slf4j.Logger; |
| 21 | import org.slf4j.LoggerFactory; |
| 22 | |
| 23 | import java.util.Optional; |
| 24 | import java.util.Set; |
| 25 | |
| 26 | /** |
| 27 | * An utility that used in KubeVirt networking app. |
| 28 | */ |
| 29 | public final class KubevirtNetworkingUtil { |
| 30 | |
| 31 | private static final Logger log = LoggerFactory.getLogger(KubevirtNetworkingUtil.class); |
| 32 | |
| 33 | private static final int PORT_NAME_MAX_LENGTH = 15; |
| 34 | |
| 35 | /** |
| 36 | * Prevents object installation from external. |
| 37 | */ |
| 38 | private KubevirtNetworkingUtil() { |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Obtains the boolean property value with specified property key name. |
| 43 | * |
| 44 | * @param properties a collection of properties |
| 45 | * @param name key name |
| 46 | * @return mapping value |
| 47 | */ |
| 48 | public static boolean getPropertyValueAsBoolean(Set<ConfigProperty> properties, |
| 49 | String name) { |
| 50 | Optional<ConfigProperty> property = |
| 51 | properties.stream().filter(p -> p.name().equals(name)).findFirst(); |
| 52 | |
| 53 | return property.map(ConfigProperty::asBoolean).orElse(false); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Re-structures the OVS port name. |
| 58 | * The length of OVS port name should be not large than 15. |
| 59 | * |
| 60 | * @param portName original port name |
| 61 | * @return re-structured OVS port name |
| 62 | */ |
| 63 | public static String structurePortName(String portName) { |
| 64 | |
| 65 | // The size of OVS port name should not be larger than 15 |
| 66 | if (portName.length() > PORT_NAME_MAX_LENGTH) { |
| 67 | return StringUtils.substring(portName, 0, PORT_NAME_MAX_LENGTH); |
| 68 | } |
| 69 | |
| 70 | return portName; |
| 71 | } |
| 72 | } |