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; |
Jian Li | 6c92b3c | 2018-08-03 11:26:55 +0900 | [diff] [blame^] | 19 | import org.onosproject.cfg.ConfigProperty; |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 20 | |
| 21 | import java.util.Dictionary; |
Jian Li | 6c92b3c | 2018-08-03 11:26:55 +0900 | [diff] [blame^] | 22 | import java.util.Optional; |
| 23 | import java.util.Set; |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 24 | |
| 25 | import static org.onlab.util.Tools.get; |
| 26 | |
| 27 | /** |
| 28 | * An utility that used in openstack telemetry app. |
| 29 | */ |
| 30 | public final class OpenstackTelemetryUtil { |
| 31 | |
| 32 | /** |
| 33 | * Prevents object instantiation from external. |
| 34 | */ |
| 35 | private OpenstackTelemetryUtil() { |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Gets Boolean property from the propertyName |
| 40 | * Return null if propertyName is not found. |
| 41 | * |
| 42 | * @param properties properties to be looked up |
| 43 | * @param propertyName the name of the property to look up |
| 44 | * @return value when the propertyName is defined or return null |
| 45 | */ |
| 46 | public static Boolean getBooleanProperty(Dictionary<?, ?> properties, |
| 47 | String propertyName) { |
| 48 | Boolean value; |
| 49 | try { |
| 50 | String s = get(properties, propertyName); |
| 51 | value = Strings.isNullOrEmpty(s) ? null : Boolean.valueOf(s); |
| 52 | } catch (ClassCastException e) { |
| 53 | value = null; |
| 54 | } |
| 55 | return value; |
| 56 | } |
Jian Li | 6c92b3c | 2018-08-03 11:26:55 +0900 | [diff] [blame^] | 57 | |
| 58 | /** |
| 59 | * Obtains the property value with specified property key name. |
| 60 | * |
| 61 | * @param properties a collection of properties |
| 62 | * @param name key name |
| 63 | * @return mapping value |
| 64 | */ |
| 65 | public static boolean getPropertyValueAsBoolean(Set<ConfigProperty> properties, String name) { |
| 66 | Optional<ConfigProperty> property = |
| 67 | properties.stream().filter(p -> p.name().equals(name)).findFirst(); |
| 68 | |
| 69 | return property.map(ConfigProperty::asBoolean).orElse(false); |
| 70 | } |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 71 | } |