blob: a9573694f0fb60a39a28de14ab504303bf1ed75a [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 Li3ed7f302018-08-27 17:16:27 +090020import org.onosproject.openstacktelemetry.api.TelemetryAdminService;
21import org.onosproject.openstacktelemetry.api.config.TelemetryConfig;
Jian Li4df75b12018-06-07 22:11:04 +090022
23import java.util.Dictionary;
Jian Li6c92b3c2018-08-03 11:26:55 +090024import java.util.Optional;
25import java.util.Set;
Jian Li4df75b12018-06-07 22:11:04 +090026
27import static org.onlab.util.Tools.get;
28
29/**
30 * An utility that used in openstack telemetry app.
31 */
32public final class OpenstackTelemetryUtil {
33
34 /**
35 * Prevents object instantiation from external.
36 */
37 private OpenstackTelemetryUtil() {
38 }
39
40 /**
41 * Gets Boolean property from the propertyName
42 * Return null if propertyName is not found.
43 *
44 * @param properties properties to be looked up
45 * @param propertyName the name of the property to look up
46 * @return value when the propertyName is defined or return null
47 */
48 public static Boolean getBooleanProperty(Dictionary<?, ?> properties,
49 String propertyName) {
50 Boolean value;
51 try {
52 String s = get(properties, propertyName);
53 value = Strings.isNullOrEmpty(s) ? null : Boolean.valueOf(s);
54 } catch (ClassCastException e) {
55 value = null;
56 }
57 return value;
58 }
Jian Li6c92b3c2018-08-03 11:26:55 +090059
60 /**
61 * Obtains the property value with specified property key name.
62 *
63 * @param properties a collection of properties
64 * @param name key name
65 * @return mapping value
66 */
67 public static boolean getPropertyValueAsBoolean(Set<ConfigProperty> properties, String name) {
68 Optional<ConfigProperty> property =
69 properties.stream().filter(p -> p.name().equals(name)).findFirst();
70
71 return property.map(ConfigProperty::asBoolean).orElse(false);
72 }
Jian Li3ed7f302018-08-27 17:16:27 +090073
74 /**
75 * Initializes the telemetry service due tue configuration changes.
76 *
77 *
78 * @param adminService telemetry admin service
79 * @param config telemetry configuration
80 * @param enable service enable flag
81 */
82 public static void initTelemetryService(TelemetryAdminService adminService,
83 TelemetryConfig config, boolean enable) {
84 if (enable) {
85 if (adminService.isRunning()) {
86 adminService.restart(config);
87 } else {
88 adminService.start(config);
89 }
90 } else {
91 if (adminService.isRunning()) {
92 adminService.stop();
93 }
94 }
95 }
Jian Li4df75b12018-06-07 22:11:04 +090096}