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; |
Andrea Campanella | 6ac13ff | 2019-08-29 16:51:14 -0700 | [diff] [blame] | 20 | import org.onosproject.net.ChannelSpacing; |
| 21 | import org.onosproject.net.DeviceId; |
| 22 | import org.onosproject.net.GridType; |
| 23 | import org.onosproject.net.OchSignal; |
| 24 | import org.onosproject.net.Port; |
| 25 | import org.onosproject.net.PortNumber; |
| 26 | import org.onosproject.net.device.DeviceService; |
| 27 | import org.onosproject.net.optical.OchPort; |
| 28 | import org.onosproject.net.optical.device.OchPortHelper; |
| 29 | |
| 30 | import java.util.Optional; |
MaoLu | 937cf42 | 2017-03-03 23:31:46 -0800 | [diff] [blame] | 31 | |
| 32 | import static com.google.common.base.Strings.isNullOrEmpty; |
| 33 | |
| 34 | /** |
| 35 | * Roadm utilities. |
| 36 | */ |
| 37 | public final class RoadmUtil { |
| 38 | |
| 39 | public static final String DEV_ID = "devId"; |
| 40 | public static final String VALID = "valid"; |
| 41 | public static final String MESSAGE = "message"; |
| 42 | public static final String NA = "N/A"; |
| 43 | public static final String UNKNOWN = "Unknown"; |
MaoLu | 937cf42 | 2017-03-03 23:31:46 -0800 | [diff] [blame] | 44 | public static final String NO_ROWS_MESSAGE = "No items found"; |
MaoLu | 2846b11 | 2017-05-15 17:18:55 -0700 | [diff] [blame] | 45 | // Optical protection switch operations. |
| 46 | // There are 3 operations for protection switch now: AUTOMATIC, FORCE, MANUAL. |
| 47 | public static final String OPS_OPT_AUTO = "AUTOMATIC"; |
| 48 | public static final String OPS_OPT_FORCE = "FORCE"; |
| 49 | public static final String OPS_OPT_MANUAL = "MANUAL"; |
Andrea Campanella | 6ac13ff | 2019-08-29 16:51:14 -0700 | [diff] [blame] | 50 | private static final long BASE_FREQUENCY = 193100000; //Working in Mhz |
| 51 | |
MaoLu | 937cf42 | 2017-03-03 23:31:46 -0800 | [diff] [blame] | 52 | |
MaoLu | 937cf42 | 2017-03-03 23:31:46 -0800 | [diff] [blame] | 53 | private RoadmUtil() { |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Formats Hz to GHz. |
| 58 | * |
| 59 | * @param value Hz in string format |
| 60 | * @return GHz in string format |
| 61 | */ |
| 62 | public static String asGHz(Frequency value) { |
MaoLu | 2f7eadb | 2017-05-02 15:38:43 -0700 | [diff] [blame] | 63 | return value == null ? UNKNOWN : String.valueOf(value.asGHz()); |
MaoLu | 937cf42 | 2017-03-03 23:31:46 -0800 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Formats Hz to THz. |
| 68 | * |
| 69 | * @param value Hz in string format |
| 70 | * @return THz in string format |
| 71 | */ |
| 72 | public static String asTHz(Frequency value) { |
MaoLu | 2f7eadb | 2017-05-02 15:38:43 -0700 | [diff] [blame] | 73 | return value == null ? UNKNOWN : String.valueOf(value.asTHz()); |
MaoLu | 937cf42 | 2017-03-03 23:31:46 -0800 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Gives a default value if the string is null or empty. |
| 78 | * |
| 79 | * @param value the string value |
| 80 | * @param defaultValue default value if null or empty |
| 81 | * @return processed string |
| 82 | */ |
| 83 | public static String defaultString(String value, String defaultValue) { |
| 84 | return isNullOrEmpty(value) ? defaultValue : value; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Gives a default value if the object is null. |
| 89 | * |
| 90 | * @param object the object |
| 91 | * @param defaultValue default value if null |
| 92 | * @return processed string |
| 93 | */ |
| 94 | public static String objectToString(Object object, String defaultValue) { |
| 95 | return object == null ? defaultValue : String.valueOf(object); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Gets value from annotations, if not exists, return default value. |
| 100 | * |
| 101 | * @param annotations the annotations |
| 102 | * @param key key value |
| 103 | * @param defaultValue default value |
| 104 | * @return value in string format |
| 105 | */ |
| 106 | public static String getAnnotation(Annotations annotations, String key, String defaultValue) { |
| 107 | return defaultString(annotations.value(key), defaultValue); |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Gets value from annotations, default value is NA. |
| 112 | * |
| 113 | * @param annotations the annotations |
| 114 | * @param key key value |
| 115 | * @return value in string format |
| 116 | */ |
| 117 | public static String getAnnotation(Annotations annotations, String key) { |
| 118 | return getAnnotation(annotations, key, NA); |
| 119 | } |
Andrea Campanella | 6ac13ff | 2019-08-29 16:51:14 -0700 | [diff] [blame] | 120 | |
| 121 | public static OchSignal createOchSignalFromWavelength(double wavelength, DeviceService deviceService, |
| 122 | DeviceId deviceId, PortNumber portNumber) { |
| 123 | if (wavelength == 0L) { |
| 124 | return null; |
| 125 | } |
| 126 | Port port = deviceService.getPort(deviceId, portNumber); |
| 127 | Optional<OchPort> ochPortOpt = OchPortHelper.asOchPort(port); |
| 128 | |
| 129 | if (ochPortOpt.isPresent()) { |
| 130 | OchPort ochPort = ochPortOpt.get(); |
| 131 | GridType gridType = ochPort.lambda().gridType(); |
| 132 | ChannelSpacing channelSpacing = ochPort.lambda().channelSpacing(); |
| 133 | int slotGranularity = ochPort.lambda().slotGranularity(); |
Andrea Campanella | bfe5540 | 2019-09-08 10:16:01 -0700 | [diff] [blame] | 134 | int multiplier = getMultiplier(wavelength, gridType, channelSpacing); |
Andrea Campanella | 6ac13ff | 2019-08-29 16:51:14 -0700 | [diff] [blame] | 135 | return new OchSignal(gridType, channelSpacing, multiplier, slotGranularity); |
| 136 | } else { |
| 137 | return null; |
| 138 | } |
| 139 | |
| 140 | } |
| 141 | |
Andrea Campanella | bfe5540 | 2019-09-08 10:16:01 -0700 | [diff] [blame] | 142 | private static int getMultiplier(double wavelength, GridType gridType, ChannelSpacing channelSpacing) { |
Andrea Campanella | 6ac13ff | 2019-08-29 16:51:14 -0700 | [diff] [blame] | 143 | long baseFreq; |
| 144 | switch (gridType) { |
| 145 | case DWDM: |
| 146 | baseFreq = BASE_FREQUENCY; |
| 147 | break; |
| 148 | case CWDM: |
| 149 | case FLEX: |
| 150 | case UNKNOWN: |
| 151 | default: |
| 152 | baseFreq = 0L; |
| 153 | break; |
| 154 | } |
Andrea Campanella | bfe5540 | 2019-09-08 10:16:01 -0700 | [diff] [blame] | 155 | return (int) ((wavelength - baseFreq) / (channelSpacing.frequency().asMHz())); |
Andrea Campanella | 6ac13ff | 2019-08-29 16:51:14 -0700 | [diff] [blame] | 156 | } |
MaoLu | 937cf42 | 2017-03-03 23:31:46 -0800 | [diff] [blame] | 157 | } |