Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-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.openstacktelemetry.util; |
| 17 | |
| 18 | import com.google.common.base.Strings; |
boyoung2 | a8549d2 | 2018-11-23 20:42:37 +0900 | [diff] [blame] | 19 | import org.onlab.packet.IPv4; |
Jian Li | 6c92b3c | 2018-08-03 11:26:55 +0900 | [diff] [blame] | 20 | import org.onosproject.cfg.ConfigProperty; |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 21 | |
| 22 | import java.util.Dictionary; |
Jian Li | 6c92b3c | 2018-08-03 11:26:55 +0900 | [diff] [blame] | 23 | import java.util.Optional; |
| 24 | import java.util.Set; |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 25 | |
| 26 | import static org.onlab.util.Tools.get; |
| 27 | |
| 28 | /** |
| 29 | * An utility that used in openstack telemetry app. |
| 30 | */ |
| 31 | public final class OpenstackTelemetryUtil { |
| 32 | |
boyoung2 | a8549d2 | 2018-11-23 20:42:37 +0900 | [diff] [blame] | 33 | private static final String PROTOCOL_NAME_TCP = "tcp"; |
| 34 | private static final String PROTOCOL_NAME_UDP = "udp"; |
| 35 | private static final String PROTOCOL_NAME_ANY = "any"; |
| 36 | private static final int ARBITRARY_PROTOCOL = 0x0; |
| 37 | |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 38 | /** |
| 39 | * Prevents object instantiation from external. |
| 40 | */ |
| 41 | private OpenstackTelemetryUtil() { |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Gets Boolean property from the propertyName |
| 46 | * Return null if propertyName is not found. |
| 47 | * |
| 48 | * @param properties properties to be looked up |
| 49 | * @param propertyName the name of the property to look up |
| 50 | * @return value when the propertyName is defined or return null |
| 51 | */ |
| 52 | public static Boolean getBooleanProperty(Dictionary<?, ?> properties, |
| 53 | String propertyName) { |
| 54 | Boolean value; |
| 55 | try { |
| 56 | String s = get(properties, propertyName); |
| 57 | value = Strings.isNullOrEmpty(s) ? null : Boolean.valueOf(s); |
| 58 | } catch (ClassCastException e) { |
| 59 | value = null; |
| 60 | } |
| 61 | return value; |
| 62 | } |
Jian Li | 6c92b3c | 2018-08-03 11:26:55 +0900 | [diff] [blame] | 63 | |
| 64 | /** |
| 65 | * Obtains the property value with specified property key name. |
| 66 | * |
| 67 | * @param properties a collection of properties |
| 68 | * @param name key name |
| 69 | * @return mapping value |
| 70 | */ |
| 71 | public static boolean getPropertyValueAsBoolean(Set<ConfigProperty> properties, String name) { |
| 72 | Optional<ConfigProperty> property = |
| 73 | properties.stream().filter(p -> p.name().equals(name)).findFirst(); |
| 74 | |
| 75 | return property.map(ConfigProperty::asBoolean).orElse(false); |
| 76 | } |
Jian Li | 3ed7f30 | 2018-08-27 17:16:27 +0900 | [diff] [blame] | 77 | |
| 78 | /** |
boyoung2 | a8549d2 | 2018-11-23 20:42:37 +0900 | [diff] [blame] | 79 | * Obtains transport protocol type from the given string. |
| 80 | * |
| 81 | * @param str transport protocol name |
| 82 | * @return transport protocol type |
| 83 | */ |
| 84 | public static byte getProtocolTypeFromString(String str) { |
| 85 | switch (str.toLowerCase()) { |
| 86 | case PROTOCOL_NAME_TCP: |
| 87 | return IPv4.PROTOCOL_TCP; |
| 88 | case PROTOCOL_NAME_UDP: |
| 89 | return IPv4.PROTOCOL_UDP; |
| 90 | default: |
| 91 | return ARBITRARY_PROTOCOL; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Obtains protocol name from the protocol type. |
| 97 | * |
| 98 | * @param type transport protocol type |
| 99 | * @return transport protocol name |
| 100 | */ |
| 101 | public static String getProtocolNameFromType(byte type) { |
| 102 | switch (type) { |
| 103 | case IPv4.PROTOCOL_TCP: |
| 104 | return PROTOCOL_NAME_TCP; |
| 105 | case IPv4.PROTOCOL_UDP: |
| 106 | return PROTOCOL_NAME_UDP; |
| 107 | case ARBITRARY_PROTOCOL: |
| 108 | return PROTOCOL_NAME_ANY; |
| 109 | default: |
| 110 | return PROTOCOL_NAME_ANY; |
| 111 | } |
| 112 | } |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 113 | } |