blob: 73a2dba727fc8b667a6e3a68945663713e33001f [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;
Andrea Campanella3a224b42019-08-29 16:51:14 -070020import org.onosproject.net.ChannelSpacing;
21import org.onosproject.net.DeviceId;
22import org.onosproject.net.GridType;
23import org.onosproject.net.OchSignal;
24import org.onosproject.net.Port;
25import org.onosproject.net.PortNumber;
26import org.onosproject.net.device.DeviceService;
27import org.onosproject.net.optical.OchPort;
28import org.onosproject.net.optical.device.OchPortHelper;
29
30import java.util.Optional;
MaoLu937cf422017-03-03 23:31:46 -080031
32import static com.google.common.base.Strings.isNullOrEmpty;
33
34/**
35 * Roadm utilities.
36 */
37public 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";
MaoLu937cf422017-03-03 23:31:46 -080044 public static final String NO_ROWS_MESSAGE = "No items found";
MaoLu2846b112017-05-15 17:18:55 -070045 // 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 Campanella3a224b42019-08-29 16:51:14 -070050 private static final long BASE_FREQUENCY = 193100000; //Working in Mhz
51
MaoLu937cf422017-03-03 23:31:46 -080052
MaoLu937cf422017-03-03 23:31:46 -080053 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) {
MaoLu2f7eadb2017-05-02 15:38:43 -070063 return value == null ? UNKNOWN : String.valueOf(value.asGHz());
MaoLu937cf422017-03-03 23:31:46 -080064 }
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) {
MaoLu2f7eadb2017-05-02 15:38:43 -070073 return value == null ? UNKNOWN : String.valueOf(value.asTHz());
MaoLu937cf422017-03-03 23:31:46 -080074 }
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 Campanella3a224b42019-08-29 16:51:14 -0700120
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();
134 int multiplier = getMultplier(wavelength, gridType, channelSpacing);
135 return new OchSignal(gridType, channelSpacing, multiplier, slotGranularity);
136 } else {
137 return null;
138 }
139
140 }
141
142 private static int getMultplier(double wavelength, GridType gridType, ChannelSpacing channelSpacing) {
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 }
155 if (wavelength > baseFreq) {
156 return (int) ((wavelength - baseFreq) / (channelSpacing.frequency().asMHz()));
157 } else {
158 return (int) ((baseFreq - wavelength) / (channelSpacing.frequency().asMHz()));
159 }
160 }
MaoLu937cf422017-03-03 23:31:46 -0800161}