blob: a20b400f0269de155bb905b9d26fe28a41b61cde [file] [log] [blame]
MaoLu937cf422017-03-03 23:31:46 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
MaoLu937cf422017-03-03 23:31:46 -08003 *
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.roadm;
17
18import org.onlab.util.Frequency;
19import org.onosproject.net.Annotations;
20
21import static com.google.common.base.Strings.isNullOrEmpty;
22
23/**
24 * Roadm utilities.
25 */
26public 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";
MaoLu937cf422017-03-03 23:31:46 -080033 public static final String NO_ROWS_MESSAGE = "No items found";
MaoLu2846b112017-05-15 17:18:55 -070034 // 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";
MaoLu937cf422017-03-03 23:31:46 -080039
MaoLu937cf422017-03-03 23:31:46 -080040 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) {
MaoLu2f7eadb2017-05-02 15:38:43 -070050 return value == null ? UNKNOWN : String.valueOf(value.asGHz());
MaoLu937cf422017-03-03 23:31:46 -080051 }
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) {
MaoLu2f7eadb2017-05-02 15:38:43 -070060 return value == null ? UNKNOWN : String.valueOf(value.asTHz());
MaoLu937cf422017-03-03 23:31:46 -080061 }
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}