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 | 3ed7f30 | 2018-08-27 17:16:27 +0900 | [diff] [blame^] | 20 | import org.onosproject.openstacktelemetry.api.TelemetryAdminService; |
| 21 | import org.onosproject.openstacktelemetry.api.config.TelemetryConfig; |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 22 | |
| 23 | import java.util.Dictionary; |
Jian Li | 6c92b3c | 2018-08-03 11:26:55 +0900 | [diff] [blame] | 24 | import java.util.Optional; |
| 25 | import java.util.Set; |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 26 | |
| 27 | import static org.onlab.util.Tools.get; |
| 28 | |
| 29 | /** |
| 30 | * An utility that used in openstack telemetry app. |
| 31 | */ |
| 32 | public 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 Li | 6c92b3c | 2018-08-03 11:26:55 +0900 | [diff] [blame] | 59 | |
| 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 Li | 3ed7f30 | 2018-08-27 17:16:27 +0900 | [diff] [blame^] | 73 | |
| 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 Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 96 | } |