blob: 860da11a9c79aea2bb0c36c2052ce3e92902c298 [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;
boyoung2a8549d22018-11-23 20:42:37 +090019import org.onlab.packet.IPv4;
Jian Li6c92b3c2018-08-03 11:26:55 +090020import org.onosproject.cfg.ConfigProperty;
Jian Li3ed7f302018-08-27 17:16:27 +090021import org.onosproject.openstacktelemetry.api.TelemetryAdminService;
22import org.onosproject.openstacktelemetry.api.config.TelemetryConfig;
Jian Li4df75b12018-06-07 22:11:04 +090023
24import java.util.Dictionary;
Jian Li6c92b3c2018-08-03 11:26:55 +090025import java.util.Optional;
26import java.util.Set;
Jian Li4df75b12018-06-07 22:11:04 +090027
28import static org.onlab.util.Tools.get;
29
30/**
31 * An utility that used in openstack telemetry app.
32 */
33public final class OpenstackTelemetryUtil {
34
boyoung2a8549d22018-11-23 20:42:37 +090035 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 Li4df75b12018-06-07 22:11:04 +090040 /**
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 Li6c92b3c2018-08-03 11:26:55 +090065
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 Li3ed7f302018-08-27 17:16:27 +090079
80 /**
boyoung2a8549d22018-11-23 20:42:37 +090081 * 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 Li3ed7f302018-08-27 17:16:27 +0900117 * 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 Li4df75b12018-06-07 22:11:04 +0900138}