MaoLu | 937cf42 | 2017-03-03 23:31:46 -0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2016-present Open Networking Laboratory |
| 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.roadm; |
| 17 | |
| 18 | import org.onlab.util.Frequency; |
| 19 | import org.onosproject.net.Annotations; |
| 20 | |
| 21 | import static com.google.common.base.Strings.isNullOrEmpty; |
| 22 | |
| 23 | /** |
| 24 | * Roadm utilities. |
| 25 | */ |
| 26 | public final class RoadmUtil { |
| 27 | |
| 28 | public static final String DEV_ID = "devId"; |
| 29 | public static final String VALID = "valid"; |
| 30 | public static final String MESSAGE = "message"; |
| 31 | public static final String NA = "N/A"; |
| 32 | public static final String UNKNOWN = "Unknown"; |
| 33 | public static final String NONE = "(none)"; |
| 34 | public static final String NO_ROWS_MESSAGE = "No items found"; |
| 35 | |
| 36 | public static final long GHZ = 1_000_000_000L; |
| 37 | public static final long THZ = 1_000_000_000_000L; |
| 38 | |
| 39 | private RoadmUtil() { |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Formats Hz to GHz. |
| 44 | * |
| 45 | * @param value Hz in string format |
| 46 | * @return GHz in string format |
| 47 | */ |
| 48 | public static String asGHz(Frequency value) { |
| 49 | return value == null ? UNKNOWN : String.valueOf((double) value.asHz() / GHZ); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Formats Hz to THz. |
| 54 | * |
| 55 | * @param value Hz in string format |
| 56 | * @return THz in string format |
| 57 | */ |
| 58 | public static String asTHz(Frequency value) { |
| 59 | return value == null ? UNKNOWN : String.valueOf((double) value.asHz() / THZ); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Gives a default value if the string is null or empty. |
| 64 | * |
| 65 | * @param value the string value |
| 66 | * @param defaultValue default value if null or empty |
| 67 | * @return processed string |
| 68 | */ |
| 69 | public static String defaultString(String value, String defaultValue) { |
| 70 | return isNullOrEmpty(value) ? defaultValue : value; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Gives a default value if the object is null. |
| 75 | * |
| 76 | * @param object the object |
| 77 | * @param defaultValue default value if null |
| 78 | * @return processed string |
| 79 | */ |
| 80 | public static String objectToString(Object object, String defaultValue) { |
| 81 | return object == null ? defaultValue : String.valueOf(object); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Gets value from annotations, if not exists, return default value. |
| 86 | * |
| 87 | * @param annotations the annotations |
| 88 | * @param key key value |
| 89 | * @param defaultValue default value |
| 90 | * @return value in string format |
| 91 | */ |
| 92 | public static String getAnnotation(Annotations annotations, String key, String defaultValue) { |
| 93 | return defaultString(annotations.value(key), defaultValue); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Gets value from annotations, default value is NA. |
| 98 | * |
| 99 | * @param annotations the annotations |
| 100 | * @param key key value |
| 101 | * @return value in string format |
| 102 | */ |
| 103 | public static String getAnnotation(Annotations annotations, String key) { |
| 104 | return getAnnotation(annotations, key, NA); |
| 105 | } |
| 106 | } |