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"; |
MaoLu | 937cf42 | 2017-03-03 23:31:46 -0800 | [diff] [blame] | 33 | public static final String NO_ROWS_MESSAGE = "No items found"; |
| 34 | |
MaoLu | 937cf42 | 2017-03-03 23:31:46 -0800 | [diff] [blame] | 35 | private RoadmUtil() { |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Formats Hz to GHz. |
| 40 | * |
| 41 | * @param value Hz in string format |
| 42 | * @return GHz in string format |
| 43 | */ |
| 44 | public static String asGHz(Frequency value) { |
MaoLu | 2f7eadb | 2017-05-02 15:38:43 -0700 | [diff] [blame^] | 45 | return value == null ? UNKNOWN : String.valueOf(value.asGHz()); |
MaoLu | 937cf42 | 2017-03-03 23:31:46 -0800 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Formats Hz to THz. |
| 50 | * |
| 51 | * @param value Hz in string format |
| 52 | * @return THz in string format |
| 53 | */ |
| 54 | public static String asTHz(Frequency value) { |
MaoLu | 2f7eadb | 2017-05-02 15:38:43 -0700 | [diff] [blame^] | 55 | return value == null ? UNKNOWN : String.valueOf(value.asTHz()); |
MaoLu | 937cf42 | 2017-03-03 23:31:46 -0800 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Gives a default value if the string is null or empty. |
| 60 | * |
| 61 | * @param value the string value |
| 62 | * @param defaultValue default value if null or empty |
| 63 | * @return processed string |
| 64 | */ |
| 65 | public static String defaultString(String value, String defaultValue) { |
| 66 | return isNullOrEmpty(value) ? defaultValue : value; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Gives a default value if the object is null. |
| 71 | * |
| 72 | * @param object the object |
| 73 | * @param defaultValue default value if null |
| 74 | * @return processed string |
| 75 | */ |
| 76 | public static String objectToString(Object object, String defaultValue) { |
| 77 | return object == null ? defaultValue : String.valueOf(object); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Gets value from annotations, if not exists, return default value. |
| 82 | * |
| 83 | * @param annotations the annotations |
| 84 | * @param key key value |
| 85 | * @param defaultValue default value |
| 86 | * @return value in string format |
| 87 | */ |
| 88 | public static String getAnnotation(Annotations annotations, String key, String defaultValue) { |
| 89 | return defaultString(annotations.value(key), defaultValue); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Gets value from annotations, default value is NA. |
| 94 | * |
| 95 | * @param annotations the annotations |
| 96 | * @param key key value |
| 97 | * @return value in string format |
| 98 | */ |
| 99 | public static String getAnnotation(Annotations annotations, String key) { |
| 100 | return getAnnotation(annotations, key, NA); |
| 101 | } |
| 102 | } |