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; |
boyoung2 | a8549d2 | 2018-11-23 20:42:37 +0900 | [diff] [blame^] | 19 | import org.onlab.packet.IPv4; |
Jian Li | 6c92b3c | 2018-08-03 11:26:55 +0900 | [diff] [blame] | 20 | import org.onosproject.cfg.ConfigProperty; |
Jian Li | 3ed7f30 | 2018-08-27 17:16:27 +0900 | [diff] [blame] | 21 | import org.onosproject.openstacktelemetry.api.TelemetryAdminService; |
| 22 | import org.onosproject.openstacktelemetry.api.config.TelemetryConfig; |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 23 | |
| 24 | import java.util.Dictionary; |
Jian Li | 6c92b3c | 2018-08-03 11:26:55 +0900 | [diff] [blame] | 25 | import java.util.Optional; |
| 26 | import java.util.Set; |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 27 | |
| 28 | import static org.onlab.util.Tools.get; |
| 29 | |
| 30 | /** |
| 31 | * An utility that used in openstack telemetry app. |
| 32 | */ |
| 33 | public final class OpenstackTelemetryUtil { |
| 34 | |
boyoung2 | a8549d2 | 2018-11-23 20:42:37 +0900 | [diff] [blame^] | 35 | private static final String PROTOCOL_NAME_TCP = "tcp"; |
| 36 | private static final String PROTOCOL_NAME_UDP = "udp"; |
| 37 | private static final String PROTOCOL_NAME_ANY = "any"; |
| 38 | private static final int ARBITRARY_PROTOCOL = 0x0; |
| 39 | |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 40 | /** |
| 41 | * Prevents object instantiation from external. |
| 42 | */ |
| 43 | private OpenstackTelemetryUtil() { |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Gets Boolean property from the propertyName |
| 48 | * Return null if propertyName is not found. |
| 49 | * |
| 50 | * @param properties properties to be looked up |
| 51 | * @param propertyName the name of the property to look up |
| 52 | * @return value when the propertyName is defined or return null |
| 53 | */ |
| 54 | public static Boolean getBooleanProperty(Dictionary<?, ?> properties, |
| 55 | String propertyName) { |
| 56 | Boolean value; |
| 57 | try { |
| 58 | String s = get(properties, propertyName); |
| 59 | value = Strings.isNullOrEmpty(s) ? null : Boolean.valueOf(s); |
| 60 | } catch (ClassCastException e) { |
| 61 | value = null; |
| 62 | } |
| 63 | return value; |
| 64 | } |
Jian Li | 6c92b3c | 2018-08-03 11:26:55 +0900 | [diff] [blame] | 65 | |
| 66 | /** |
| 67 | * Obtains the property value with specified property key name. |
| 68 | * |
| 69 | * @param properties a collection of properties |
| 70 | * @param name key name |
| 71 | * @return mapping value |
| 72 | */ |
| 73 | public static boolean getPropertyValueAsBoolean(Set<ConfigProperty> properties, String name) { |
| 74 | Optional<ConfigProperty> property = |
| 75 | properties.stream().filter(p -> p.name().equals(name)).findFirst(); |
| 76 | |
| 77 | return property.map(ConfigProperty::asBoolean).orElse(false); |
| 78 | } |
Jian Li | 3ed7f30 | 2018-08-27 17:16:27 +0900 | [diff] [blame] | 79 | |
| 80 | /** |
boyoung2 | a8549d2 | 2018-11-23 20:42:37 +0900 | [diff] [blame^] | 81 | * Obtains transport protocol type from the given string. |
| 82 | * |
| 83 | * @param str transport protocol name |
| 84 | * @return transport protocol type |
| 85 | */ |
| 86 | public static byte getProtocolTypeFromString(String str) { |
| 87 | switch (str.toLowerCase()) { |
| 88 | case PROTOCOL_NAME_TCP: |
| 89 | return IPv4.PROTOCOL_TCP; |
| 90 | case PROTOCOL_NAME_UDP: |
| 91 | return IPv4.PROTOCOL_UDP; |
| 92 | default: |
| 93 | return ARBITRARY_PROTOCOL; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Obtains protocol name from the protocol type. |
| 99 | * |
| 100 | * @param type transport protocol type |
| 101 | * @return transport protocol name |
| 102 | */ |
| 103 | public static String getProtocolNameFromType(byte type) { |
| 104 | switch (type) { |
| 105 | case IPv4.PROTOCOL_TCP: |
| 106 | return PROTOCOL_NAME_TCP; |
| 107 | case IPv4.PROTOCOL_UDP: |
| 108 | return PROTOCOL_NAME_UDP; |
| 109 | case ARBITRARY_PROTOCOL: |
| 110 | return PROTOCOL_NAME_ANY; |
| 111 | default: |
| 112 | return PROTOCOL_NAME_ANY; |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | /** |
Jian Li | 3ed7f30 | 2018-08-27 17:16:27 +0900 | [diff] [blame] | 117 | * Initializes the telemetry service due tue configuration changes. |
| 118 | * |
| 119 | * |
| 120 | * @param adminService telemetry admin service |
| 121 | * @param config telemetry configuration |
| 122 | * @param enable service enable flag |
| 123 | */ |
| 124 | public static void initTelemetryService(TelemetryAdminService adminService, |
| 125 | TelemetryConfig config, boolean enable) { |
| 126 | if (enable) { |
| 127 | if (adminService.isRunning()) { |
| 128 | adminService.restart(config); |
| 129 | } else { |
| 130 | adminService.start(config); |
| 131 | } |
| 132 | } else { |
| 133 | if (adminService.isRunning()) { |
| 134 | adminService.stop(); |
| 135 | } |
| 136 | } |
| 137 | } |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 138 | } |