blob: abc4e97f4363101584ef36bd87b94439888cba34 [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;
Jian Li6c92b3c2018-08-03 11:26:55 +090019import org.onosproject.cfg.ConfigProperty;
Jian Li4df75b12018-06-07 22:11:04 +090020
21import java.util.Dictionary;
Jian Li6c92b3c2018-08-03 11:26:55 +090022import java.util.Optional;
23import java.util.Set;
Jian Li4df75b12018-06-07 22:11:04 +090024
25import static org.onlab.util.Tools.get;
26
27/**
28 * An utility that used in openstack telemetry app.
29 */
30public 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 Li6c92b3c2018-08-03 11:26:55 +090057
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 Li4df75b12018-06-07 22:11:04 +090071}