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 | |
Jian Li | 667c6eb | 2019-01-07 23:01:12 +0900 | [diff] [blame^] | 22 | import java.io.IOException; |
| 23 | import java.net.InetSocketAddress; |
| 24 | import java.net.Socket; |
| 25 | import java.net.SocketAddress; |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 26 | import java.util.Dictionary; |
Jian Li | 6c92b3c | 2018-08-03 11:26:55 +0900 | [diff] [blame] | 27 | import java.util.Optional; |
| 28 | import java.util.Set; |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 29 | |
| 30 | import static org.onlab.util.Tools.get; |
| 31 | |
| 32 | /** |
| 33 | * An utility that used in openstack telemetry app. |
| 34 | */ |
| 35 | public final class OpenstackTelemetryUtil { |
| 36 | |
boyoung2 | a8549d2 | 2018-11-23 20:42:37 +0900 | [diff] [blame] | 37 | private static final String PROTOCOL_NAME_TCP = "tcp"; |
| 38 | private static final String PROTOCOL_NAME_UDP = "udp"; |
| 39 | private static final String PROTOCOL_NAME_ANY = "any"; |
| 40 | private static final int ARBITRARY_PROTOCOL = 0x0; |
Jian Li | 667c6eb | 2019-01-07 23:01:12 +0900 | [diff] [blame^] | 41 | private static final int TIMEOUT = 2000; |
boyoung2 | a8549d2 | 2018-11-23 20:42:37 +0900 | [diff] [blame] | 42 | |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 43 | /** |
| 44 | * Prevents object instantiation from external. |
| 45 | */ |
| 46 | private OpenstackTelemetryUtil() { |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Gets Boolean property from the propertyName |
| 51 | * Return null if propertyName is not found. |
| 52 | * |
| 53 | * @param properties properties to be looked up |
| 54 | * @param propertyName the name of the property to look up |
| 55 | * @return value when the propertyName is defined or return null |
| 56 | */ |
| 57 | public static Boolean getBooleanProperty(Dictionary<?, ?> properties, |
| 58 | String propertyName) { |
| 59 | Boolean value; |
| 60 | try { |
| 61 | String s = get(properties, propertyName); |
| 62 | value = Strings.isNullOrEmpty(s) ? null : Boolean.valueOf(s); |
| 63 | } catch (ClassCastException e) { |
| 64 | value = null; |
| 65 | } |
| 66 | return value; |
| 67 | } |
Jian Li | 6c92b3c | 2018-08-03 11:26:55 +0900 | [diff] [blame] | 68 | |
| 69 | /** |
| 70 | * Obtains the property value with specified property key name. |
| 71 | * |
| 72 | * @param properties a collection of properties |
| 73 | * @param name key name |
| 74 | * @return mapping value |
| 75 | */ |
| 76 | public static boolean getPropertyValueAsBoolean(Set<ConfigProperty> properties, String name) { |
| 77 | Optional<ConfigProperty> property = |
| 78 | properties.stream().filter(p -> p.name().equals(name)).findFirst(); |
| 79 | |
| 80 | return property.map(ConfigProperty::asBoolean).orElse(false); |
| 81 | } |
Jian Li | 3ed7f30 | 2018-08-27 17:16:27 +0900 | [diff] [blame] | 82 | |
| 83 | /** |
boyoung2 | a8549d2 | 2018-11-23 20:42:37 +0900 | [diff] [blame] | 84 | * Obtains transport protocol type from the given string. |
| 85 | * |
| 86 | * @param str transport protocol name |
| 87 | * @return transport protocol type |
| 88 | */ |
| 89 | public static byte getProtocolTypeFromString(String str) { |
| 90 | switch (str.toLowerCase()) { |
| 91 | case PROTOCOL_NAME_TCP: |
| 92 | return IPv4.PROTOCOL_TCP; |
| 93 | case PROTOCOL_NAME_UDP: |
| 94 | return IPv4.PROTOCOL_UDP; |
| 95 | default: |
| 96 | return ARBITRARY_PROTOCOL; |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Obtains protocol name from the protocol type. |
| 102 | * |
| 103 | * @param type transport protocol type |
| 104 | * @return transport protocol name |
| 105 | */ |
| 106 | public static String getProtocolNameFromType(byte type) { |
| 107 | switch (type) { |
| 108 | case IPv4.PROTOCOL_TCP: |
| 109 | return PROTOCOL_NAME_TCP; |
| 110 | case IPv4.PROTOCOL_UDP: |
| 111 | return PROTOCOL_NAME_UDP; |
| 112 | case ARBITRARY_PROTOCOL: |
| 113 | return PROTOCOL_NAME_ANY; |
| 114 | default: |
| 115 | return PROTOCOL_NAME_ANY; |
| 116 | } |
| 117 | } |
Jian Li | 667c6eb | 2019-01-07 23:01:12 +0900 | [diff] [blame^] | 118 | |
| 119 | /** |
| 120 | * Tests the connectivity with the given address and port. |
| 121 | * |
| 122 | * @param address address |
| 123 | * @param port port number |
| 124 | * @return true if the given address and port is accessible, false otherwise |
| 125 | */ |
| 126 | public static boolean testConnectivity(String address, int port) { |
| 127 | |
| 128 | boolean isConnected = false; |
| 129 | SocketAddress socketAddress = new InetSocketAddress(address, port); |
| 130 | Socket socket = new Socket(); |
| 131 | try { |
| 132 | socket.connect(socketAddress, TIMEOUT); |
| 133 | socket.close(); |
| 134 | isConnected = true; |
| 135 | } catch (IOException ignored) { |
| 136 | } |
| 137 | |
| 138 | return isConnected; |
| 139 | } |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 140 | } |