blob: 39b1fdbeb0d1b38a5ea877524632fc11f49798f7 [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
22import java.util.Dictionary;
Jian Li6c92b3c2018-08-03 11:26:55 +090023import java.util.Optional;
24import java.util.Set;
Jian Li4df75b12018-06-07 22:11:04 +090025
26import static org.onlab.util.Tools.get;
27
28/**
29 * An utility that used in openstack telemetry app.
30 */
31public final class OpenstackTelemetryUtil {
32
boyoung2a8549d22018-11-23 20:42:37 +090033 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 Li4df75b12018-06-07 22:11:04 +090038 /**
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 Li6c92b3c2018-08-03 11:26:55 +090063
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 Li3ed7f302018-08-27 17:16:27 +090077
78 /**
boyoung2a8549d22018-11-23 20:42:37 +090079 * 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 Li4df75b12018-06-07 22:11:04 +0900113}