MaoLu | 937cf42 | 2017-03-03 23:31:46 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
MaoLu | 937cf42 | 2017-03-03 23:31:46 -0800 | [diff] [blame] | 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"; |
MaoLu | 2846b11 | 2017-05-15 17:18:55 -0700 | [diff] [blame] | 34 | // Optical protection switch operations. |
| 35 | // There are 3 operations for protection switch now: AUTOMATIC, FORCE, MANUAL. |
| 36 | public static final String OPS_OPT_AUTO = "AUTOMATIC"; |
| 37 | public static final String OPS_OPT_FORCE = "FORCE"; |
| 38 | public static final String OPS_OPT_MANUAL = "MANUAL"; |
MaoLu | 937cf42 | 2017-03-03 23:31:46 -0800 | [diff] [blame] | 39 | |
MaoLu | 937cf42 | 2017-03-03 23:31:46 -0800 | [diff] [blame] | 40 | private RoadmUtil() { |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Formats Hz to GHz. |
| 45 | * |
| 46 | * @param value Hz in string format |
| 47 | * @return GHz in string format |
| 48 | */ |
| 49 | public static String asGHz(Frequency value) { |
MaoLu | 2f7eadb | 2017-05-02 15:38:43 -0700 | [diff] [blame] | 50 | return value == null ? UNKNOWN : String.valueOf(value.asGHz()); |
MaoLu | 937cf42 | 2017-03-03 23:31:46 -0800 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Formats Hz to THz. |
| 55 | * |
| 56 | * @param value Hz in string format |
| 57 | * @return THz in string format |
| 58 | */ |
| 59 | public static String asTHz(Frequency value) { |
MaoLu | 2f7eadb | 2017-05-02 15:38:43 -0700 | [diff] [blame] | 60 | return value == null ? UNKNOWN : String.valueOf(value.asTHz()); |
MaoLu | 937cf42 | 2017-03-03 23:31:46 -0800 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Gives a default value if the string is null or empty. |
| 65 | * |
| 66 | * @param value the string value |
| 67 | * @param defaultValue default value if null or empty |
| 68 | * @return processed string |
| 69 | */ |
| 70 | public static String defaultString(String value, String defaultValue) { |
| 71 | return isNullOrEmpty(value) ? defaultValue : value; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Gives a default value if the object is null. |
| 76 | * |
| 77 | * @param object the object |
| 78 | * @param defaultValue default value if null |
| 79 | * @return processed string |
| 80 | */ |
| 81 | public static String objectToString(Object object, String defaultValue) { |
| 82 | return object == null ? defaultValue : String.valueOf(object); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Gets value from annotations, if not exists, return default value. |
| 87 | * |
| 88 | * @param annotations the annotations |
| 89 | * @param key key value |
| 90 | * @param defaultValue default value |
| 91 | * @return value in string format |
| 92 | */ |
| 93 | public static String getAnnotation(Annotations annotations, String key, String defaultValue) { |
| 94 | return defaultString(annotations.value(key), defaultValue); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Gets value from annotations, default value is NA. |
| 99 | * |
| 100 | * @param annotations the annotations |
| 101 | * @param key key value |
| 102 | * @return value in string format |
| 103 | */ |
| 104 | public static String getAnnotation(Annotations annotations, String key) { |
| 105 | return getAnnotation(annotations, key, NA); |
| 106 | } |
| 107 | } |