blob: afb7365a82d3a17ad7084749e8f2dcdfd48d26cf [file] [log] [blame]
Jian Li4df75b12018-06-07 22:11:04 +09001/*
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 */
16package org.onosproject.openstacktelemetry.util;
17
18import com.google.common.base.Strings;
boyoung2a8549d22018-11-23 20:42:37 +090019import org.onlab.packet.IPv4;
Jian Li6c92b3c2018-08-03 11:26:55 +090020import org.onosproject.cfg.ConfigProperty;
Jian Li4df75b12018-06-07 22:11:04 +090021
Jian Li667c6eb2019-01-07 23:01:12 +090022import java.io.IOException;
23import java.net.InetSocketAddress;
24import java.net.Socket;
25import java.net.SocketAddress;
Jian Li4df75b12018-06-07 22:11:04 +090026import java.util.Dictionary;
Jian Li6c92b3c2018-08-03 11:26:55 +090027import java.util.Optional;
28import java.util.Set;
Jian Li4df75b12018-06-07 22:11:04 +090029
30import static org.onlab.util.Tools.get;
31
32/**
33 * An utility that used in openstack telemetry app.
34 */
35public final class OpenstackTelemetryUtil {
36
boyoung2a8549d22018-11-23 20:42:37 +090037 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 Li667c6eb2019-01-07 23:01:12 +090041 private static final int TIMEOUT = 2000;
boyoung2a8549d22018-11-23 20:42:37 +090042
Jian Li4df75b12018-06-07 22:11:04 +090043 /**
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 Li6c92b3c2018-08-03 11:26:55 +090068
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 Li3ed7f302018-08-27 17:16:27 +090082
83 /**
boyoung2a8549d22018-11-23 20:42:37 +090084 * 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 Li667c6eb2019-01-07 23:01:12 +0900118
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 Li4df75b12018-06-07 22:11:04 +0900140}