blob: cce89b051a128a44f7a3d3fedd57ccdefe463a27 [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2014-present Open Networking Foundation
Thomas Vachuska781d18b2014-10-27 10:31:25 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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
Thomas Vachuska781d18b2014-10-27 10:31:25 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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.
Thomas Vachuska781d18b2014-10-27 10:31:25 -070015 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.provider.of.device.impl;
tomb5a46e62014-08-26 14:20:00 -070017
Marc De Leenheer8aba62f2017-04-25 14:33:37 -070018import com.google.common.base.Strings;
19import com.google.common.collect.ImmutableList;
20import com.google.common.collect.Lists;
21import com.google.common.collect.Maps;
22import com.google.common.collect.Sets;
tomb5a46e62014-08-26 14:20:00 -070023import org.apache.felix.scr.annotations.Activate;
24import org.apache.felix.scr.annotations.Component;
25import org.apache.felix.scr.annotations.Deactivate;
Dusan Pajinbab8a5e2015-07-24 17:37:19 +020026import org.apache.felix.scr.annotations.Modified;
27import org.apache.felix.scr.annotations.Property;
tomb5a46e62014-08-26 14:20:00 -070028import org.apache.felix.scr.annotations.Reference;
29import org.apache.felix.scr.annotations.ReferenceCardinality;
Thomas Vachuskaf0397b52015-05-29 13:50:17 -070030import org.onlab.packet.ChassisId;
Marc De Leenheerb9311372015-07-09 11:36:49 -070031import org.onlab.util.Frequency;
32import org.onlab.util.Spectrum;
Yafit Hadara9a73de2015-09-06 13:52:52 +030033import org.onosproject.cfg.ComponentConfigService;
Thomas Vachuskab52a0142015-04-21 17:48:15 -070034import org.onosproject.net.AnnotationKeys;
Marc De Leenheerfc913dd2015-07-30 16:04:55 -070035import org.onosproject.net.ChannelSpacing;
Toru Furusawa72ee30c2016-01-08 13:29:04 -080036import org.onosproject.net.CltSignalType;
Brian O'Connorabafb502014-12-02 22:26:20 -080037import org.onosproject.net.DefaultAnnotations;
Yuta HIGUCHI2341e602017-03-08 20:10:08 -080038import org.onosproject.net.DefaultAnnotations.Builder;
Yafit Hadara9a73de2015-09-06 13:52:52 +030039import org.onosproject.net.Device;
Brian O'Connorabafb502014-12-02 22:26:20 -080040import org.onosproject.net.DeviceId;
Marc De Leenheerfc913dd2015-07-30 16:04:55 -070041import org.onosproject.net.GridType;
Brian O'Connorabafb502014-12-02 22:26:20 -080042import org.onosproject.net.MastershipRole;
Marc De Leenheerfc913dd2015-07-30 16:04:55 -070043import org.onosproject.net.OchSignal;
44import org.onosproject.net.OduSignalType;
Rimon Ashkenazy8ebfff02016-02-01 11:56:36 +020045import org.onosproject.net.OtuSignalType;
Brian O'Connorabafb502014-12-02 22:26:20 -080046import org.onosproject.net.Port;
Yuta HIGUCHIf42a2cc2017-05-02 17:04:31 -070047import org.onosproject.net.Port.Type;
Brian O'Connorabafb502014-12-02 22:26:20 -080048import org.onosproject.net.PortNumber;
49import org.onosproject.net.SparseAnnotations;
Jimmy Yan4deb03b2016-06-24 10:53:54 -070050import org.onosproject.net.behaviour.LambdaQuery;
Brian O'Connorabafb502014-12-02 22:26:20 -080051import org.onosproject.net.device.DefaultDeviceDescription;
52import org.onosproject.net.device.DefaultPortDescription;
sangho538108b2015-04-08 14:29:20 -070053import org.onosproject.net.device.DefaultPortStatistics;
Brian O'Connorabafb502014-12-02 22:26:20 -080054import org.onosproject.net.device.DeviceDescription;
Thomas Vachuska164ecf62018-05-08 17:29:55 -070055import org.onosproject.net.device.DeviceEvent;
56import org.onosproject.net.device.DeviceListener;
Brian O'Connorabafb502014-12-02 22:26:20 -080057import org.onosproject.net.device.DeviceProvider;
58import org.onosproject.net.device.DeviceProviderRegistry;
59import org.onosproject.net.device.DeviceProviderService;
Thomas Vachuska164ecf62018-05-08 17:29:55 -070060import org.onosproject.net.device.DeviceService;
Brian O'Connorabafb502014-12-02 22:26:20 -080061import org.onosproject.net.device.PortDescription;
sangho538108b2015-04-08 14:29:20 -070062import org.onosproject.net.device.PortStatistics;
Victor Silvaac8dab02016-11-28 16:33:08 -030063import org.onosproject.net.driver.Driver;
Jimmy Yan4deb03b2016-06-24 10:53:54 -070064import org.onosproject.net.driver.DriverHandler;
Victor Silvaac8dab02016-11-28 16:33:08 -030065import org.onosproject.net.driver.DriverService;
Jimmy Yan4deb03b2016-06-24 10:53:54 -070066import org.onosproject.net.driver.HandlerBehaviour;
Brian O'Connorabafb502014-12-02 22:26:20 -080067import org.onosproject.net.provider.AbstractProvider;
68import org.onosproject.net.provider.ProviderId;
69import org.onosproject.openflow.controller.Dpid;
70import org.onosproject.openflow.controller.OpenFlowController;
sangho538108b2015-04-08 14:29:20 -070071import org.onosproject.openflow.controller.OpenFlowEventListener;
Ayaka Koshibe5460d622015-05-14 12:19:19 -070072import org.onosproject.openflow.controller.OpenFlowOpticalSwitch;
Brian O'Connorabafb502014-12-02 22:26:20 -080073import org.onosproject.openflow.controller.OpenFlowSwitch;
74import org.onosproject.openflow.controller.OpenFlowSwitchListener;
Ayaka Koshibe5460d622015-05-14 12:19:19 -070075import org.onosproject.openflow.controller.PortDescPropertyType;
Brian O'Connorabafb502014-12-02 22:26:20 -080076import org.onosproject.openflow.controller.RoleState;
Dusan Pajinbab8a5e2015-07-24 17:37:19 +020077import org.osgi.service.component.ComponentContext;
Marc De Leenheerc662d322016-02-18 16:05:10 -080078import org.projectfloodlight.openflow.protocol.OFCalientPortDescProp;
79import org.projectfloodlight.openflow.protocol.OFCalientPortDescPropOptical;
Marc De Leenheerb9311372015-07-09 11:36:49 -070080import org.projectfloodlight.openflow.protocol.OFCalientPortDescStatsEntry;
Laszlo Papp58174812018-01-16 13:29:47 +000081import org.projectfloodlight.openflow.protocol.OFCapabilities;
Saurav Dasa2d37502016-03-25 17:50:40 -070082import org.projectfloodlight.openflow.protocol.OFErrorMsg;
83import org.projectfloodlight.openflow.protocol.OFErrorType;
Yafit Hadara9a73de2015-09-06 13:52:52 +030084import org.projectfloodlight.openflow.protocol.OFExpPort;
85import org.projectfloodlight.openflow.protocol.OFExpPortDescPropOpticalTransport;
86import org.projectfloodlight.openflow.protocol.OFExpPortOpticalTransportLayerEntry;
Ayaka Koshibee8708e32014-10-22 13:40:18 -070087import org.projectfloodlight.openflow.protocol.OFFactory;
sangho538108b2015-04-08 14:29:20 -070088import org.projectfloodlight.openflow.protocol.OFMessage;
Yafit Hadara9a73de2015-09-06 13:52:52 +030089import org.projectfloodlight.openflow.protocol.OFObject;
alshabib4680bb62014-09-04 17:15:08 -070090import org.projectfloodlight.openflow.protocol.OFPortConfig;
alshabib25c8eec2014-09-04 16:41:31 -070091import org.projectfloodlight.openflow.protocol.OFPortDesc;
Laszlo Papp8e8ff052018-01-16 09:55:45 +000092import org.projectfloodlight.openflow.protocol.OFPortDescProp;
Yuta HIGUCHI2341e602017-03-08 20:10:08 -080093import org.projectfloodlight.openflow.protocol.OFPortDescPropEthernet;
94import org.projectfloodlight.openflow.protocol.OFPortDescPropOptical;
Marc De Leenheerfc913dd2015-07-30 16:04:55 -070095import org.projectfloodlight.openflow.protocol.OFPortDescPropOpticalTransport;
Palash Kala496ef1a22018-03-21 14:57:50 +090096import org.projectfloodlight.openflow.protocol.OFPortDescStatsRequest;
Thomas Vachuskad16ce182014-10-29 17:25:29 -070097import org.projectfloodlight.openflow.protocol.OFPortFeatures;
Saurav Dasa2d37502016-03-25 17:50:40 -070098import org.projectfloodlight.openflow.protocol.OFPortMod;
Ayaka Koshibe5460d622015-05-14 12:19:19 -070099import org.projectfloodlight.openflow.protocol.OFPortOptical;
Yafit Hadara9a73de2015-09-06 13:52:52 +0300100import org.projectfloodlight.openflow.protocol.OFPortOpticalTransportLayerClass;
101import org.projectfloodlight.openflow.protocol.OFPortOpticalTransportSignalType;
alshabibafc514a2014-12-01 14:44:05 -0800102import org.projectfloodlight.openflow.protocol.OFPortReason;
alshabib4680bb62014-09-04 17:15:08 -0700103import org.projectfloodlight.openflow.protocol.OFPortState;
sangho538108b2015-04-08 14:29:20 -0700104import org.projectfloodlight.openflow.protocol.OFPortStatsEntry;
Laszlo Papp7cf60372018-01-11 00:06:43 +0000105import org.projectfloodlight.openflow.protocol.OFPortStatsPropOptical;
sangho538108b2015-04-08 14:29:20 -0700106import org.projectfloodlight.openflow.protocol.OFPortStatsReply;
alshabiba14f3642014-09-05 09:31:31 -0700107import org.projectfloodlight.openflow.protocol.OFPortStatus;
sangho538108b2015-04-08 14:29:20 -0700108import org.projectfloodlight.openflow.protocol.OFStatsReply;
Thomas Vachuska893bf4b2015-05-29 18:13:18 -0700109import org.projectfloodlight.openflow.protocol.OFStatsReplyFlags;
sangho538108b2015-04-08 14:29:20 -0700110import org.projectfloodlight.openflow.protocol.OFStatsType;
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700111import org.projectfloodlight.openflow.protocol.OFVersion;
Yuta HIGUCHIf42a2cc2017-05-02 17:04:31 -0700112import org.projectfloodlight.openflow.protocol.ver14.OFOpticalPortFeaturesSerializerVer14;
Laszlo Papp7cf60372018-01-11 00:06:43 +0000113import org.projectfloodlight.openflow.protocol.ver14.OFPortStatsOpticalFlagsSerializerVer14;
Saurav Dasa2d37502016-03-25 17:50:40 -0700114import org.projectfloodlight.openflow.types.OFPort;
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700115import org.projectfloodlight.openflow.types.PortSpeed;
tomb5a46e62014-08-26 14:20:00 -0700116import org.slf4j.Logger;
tom5f38b3a2014-08-27 23:50:54 -0700117
Marc De Leenheer8aba62f2017-04-25 14:33:37 -0700118import java.util.ArrayList;
119import java.util.Collection;
120import java.util.Collections;
121import java.util.Comparator;
122import java.util.Dictionary;
Jimmy Jine9b7a022016-08-12 16:56:48 -0700123import java.util.EnumSet;
Marc De Leenheer8aba62f2017-04-25 14:33:37 -0700124import java.util.HashMap;
125import java.util.HashSet;
126import java.util.List;
Yuta HIGUCHI2341e602017-03-08 20:10:08 -0800127import java.util.Optional;
Marc De Leenheer8aba62f2017-04-25 14:33:37 -0700128import java.util.Properties;
129import java.util.Set;
130import java.util.Timer;
131
132import static com.google.common.base.Preconditions.checkArgument;
133import static com.google.common.base.Strings.isNullOrEmpty;
134import static org.onlab.util.Tools.get;
Thomas Vachuska164ecf62018-05-08 17:29:55 -0700135import static org.onosproject.net.Device.Type.CONTROLLER;
Marc De Leenheer8aba62f2017-04-25 14:33:37 -0700136import static org.onosproject.net.DeviceId.deviceId;
137import static org.onosproject.net.Port.Type.COPPER;
138import static org.onosproject.net.Port.Type.FIBER;
Thomas Vachuska164ecf62018-05-08 17:29:55 -0700139import static org.onosproject.net.device.DeviceEvent.Type.DEVICE_REMOVED;
Marc De Leenheer8aba62f2017-04-25 14:33:37 -0700140import static org.onosproject.net.optical.device.OchPortHelper.ochPortDescription;
141import static org.onosproject.net.optical.device.OduCltPortHelper.oduCltPortDescription;
142import static org.onosproject.net.optical.device.OmsPortHelper.omsPortDescription;
143import static org.onosproject.net.optical.device.OtuPortHelper.otuPortDescription;
144import static org.onosproject.openflow.controller.Dpid.dpid;
145import static org.onosproject.openflow.controller.Dpid.uri;
146import static org.slf4j.LoggerFactory.getLogger;
tom782a7cf2014-09-11 23:58:38 -0700147
tomb5a46e62014-08-26 14:20:00 -0700148/**
tomb1260e42014-08-26 18:39:57 -0700149 * Provider which uses an OpenFlow controller to detect network
tome06f8552014-08-26 16:58:42 -0700150 * infrastructure devices.
tomb5a46e62014-08-26 14:20:00 -0700151 */
tomb1260e42014-08-26 18:39:57 -0700152@Component(immediate = true)
tomab21e7c2014-08-26 15:23:08 -0700153public class OpenFlowDeviceProvider extends AbstractProvider implements DeviceProvider {
tomb5a46e62014-08-26 14:20:00 -0700154
alshabiba89cc582014-09-09 16:43:00 -0700155 private static final Logger LOG = getLogger(OpenFlowDeviceProvider.class);
Thomas Vachuskad07c0922015-10-06 14:48:06 -0700156
Yuta HIGUCHI2341e602017-03-08 20:10:08 -0800157 // TODO Some duplicate with one defined in OpticalAnnotations
158 // slice out optical specific handling and consolidate.
159 /**
160 * Annotation key for minimum frequency in Hz.
161 * Value is expected to be an integer.
162 */
163 public static final String AK_MIN_FREQ_HZ = "minFrequency";
164
165 /**
Laszlo Pappbf2cd742018-01-16 11:49:04 +0000166 * Annotation key for minimum lambda in nm.
167 * Value is expected to be an integer.
168 */
169 public static final String AK_MIN_LMDA_NM = "minLambda";
170
171 /**
Yuta HIGUCHI2341e602017-03-08 20:10:08 -0800172 * Annotation key for maximum frequency in Hz.
173 * Value is expected be an integer.
174 */
175 public static final String AK_MAX_FREQ_HZ = "maxFrequency";
176
177 /**
Laszlo Pappbf2cd742018-01-16 11:49:04 +0000178 * Annotation key for maximum lambda in nm.
179 * Value is expected be an integer.
180 */
181 public static final String AK_MAX_LMDA_NM = "maxLambda";
182
183 /**
184 * Annotation key for grid frequency in Hz.
Yuta HIGUCHI2341e602017-03-08 20:10:08 -0800185 * Value is expected to be an integer.
186 */
187 public static final String AK_GRID_HZ = "grid";
188
Laszlo Papp20cfb6f2018-01-08 16:06:53 +0000189 /**
Laszlo Pappbf2cd742018-01-16 11:49:04 +0000190 * Annotation key for grid lambda in nm.
191 * Value is expected to be an integer.
192 */
193 public static final String AK_GRID_LMDA_NM = "gridLambda";
194
195 /**
Yuta HIGUCHI2341e602017-03-08 20:10:08 -0800196 * Annotation key for minimum frequency in Hz.
197 * Value is expected to be an integer.
198 */
199 public static final String AK_TX_MIN_FREQ_HZ = "txMinFrequency";
200
201 /**
Laszlo Pappbf2cd742018-01-16 11:49:04 +0000202 * Annotation key for minimum lambda in nm.
203 * Value is expected to be an integer.
204 */
205 public static final String AK_TX_MIN_LMDA_NM = "txMinLambda";
206
207 /**
Yuta HIGUCHI2341e602017-03-08 20:10:08 -0800208 * Annotation key for maximum frequency in Hz.
209 * Value is expected be an integer.
210 */
211 public static final String AK_TX_MAX_FREQ_HZ = "txMaxFrequency";
212
213 /**
Laszlo Pappbf2cd742018-01-16 11:49:04 +0000214 * Annotation key for maximum lambda in nm.
215 * Value is expected be an integer.
216 */
217 public static final String AK_TX_MAX_LMDA_NM = "txMaxLambda";
218
219 /**
220 * Annotation key for grid frequency in Hz.
Yuta HIGUCHI2341e602017-03-08 20:10:08 -0800221 * Value is expected to be an integer.
222 */
223 public static final String AK_TX_GRID_HZ = "txGrid";
224
225 /**
Laszlo Pappbf2cd742018-01-16 11:49:04 +0000226 * Annotation key for grid lambda in nm.
227 * Value is expected to be an integer.
228 */
229 public static final String AK_TX_GRID_LMDA_NM = "txGridLambda";
230
231 /**
Yuta HIGUCHI2341e602017-03-08 20:10:08 -0800232 * Annotation key for minimum frequency in Hz.
233 * Value is expected to be an integer.
234 */
235 public static final String AK_RX_MIN_FREQ_HZ = "rxMinFrequency";
236
237 /**
Laszlo Pappbf2cd742018-01-16 11:49:04 +0000238 * Annotation key for minimum lambda in nm.
239 * Value is expected to be an integer.
240 */
241 public static final String AK_RX_MIN_LMDA_NM = "rxMinLambda";
242
243 /**
Yuta HIGUCHI2341e602017-03-08 20:10:08 -0800244 * Annotation key for maximum frequency in Hz.
245 * Value is expected be an integer.
246 */
247 public static final String AK_RX_MAX_FREQ_HZ = "rxMaxFrequency";
248
249 /**
Laszlo Pappbf2cd742018-01-16 11:49:04 +0000250 * Annotation key for maximum lambda in nm.
251 * Value is expected be an integer.
252 */
253 public static final String AK_RX_MAX_LMDA_NM = "rxMaxLambda";
254
255 /**
256 * Annotation key for grid frequency in Hz.
Yuta HIGUCHI2341e602017-03-08 20:10:08 -0800257 * Value is expected to be an integer.
258 */
259 public static final String AK_RX_GRID_HZ = "rxGrid";
260
Laszlo Papp20cfb6f2018-01-08 16:06:53 +0000261 /**
Laszlo Pappbf2cd742018-01-16 11:49:04 +0000262 * Annotation key for grid lambda in nm.
263 * Value is expected to be an integer.
264 */
265 public static final String AK_RX_GRID_LMDA_NM = "rxGridLambda";
266
267 /**
Laszlo Papp8e8ff052018-01-16 09:55:45 +0000268 * Annotation key for indicating frequency must be used instead of
269 * wavelength for port tuning.
270 * Value is expected to be "enabled" or "disabled"
271 */
272 public static final String AK_USE_FREQ_FEATURE = "useFreqFeature";
273
274 /**
Laszlo Papp20cfb6f2018-01-08 16:06:53 +0000275 * Annotation key for minimum transmit power in dBm*10.
276 * Value is expected to be an integer.
277 */
278 public static final String AK_TX_PWR_MIN = "txPowerMin";
279
280 /**
281 * Annotation key for maximum transmit power in dBm*10.
282 * Value is expected to be an integer.
283 */
284 public static final String AK_TX_PWR_MAX = "txPowerMax";
285
Laszlo Papp7cf60372018-01-11 00:06:43 +0000286
287
288 // Port Stats annotations
289
290 /**
291 * Annotation key for transmit frequency in Hz.
292 * Value is expected be an integer.
293 */
294 public static final String AK_TX_FREQ_HZ = "txFrequency";
295
296 /**
Laszlo Pappbf2cd742018-01-16 11:49:04 +0000297 * Annotation key for transmit lambda in nm.
298 * Value is expected be an integer.
299 */
300 public static final String AK_TX_LMDA_NM = "txLambda";
301
302 /**
303 * Annotation key for transmit offset frequency in Hz.
Laszlo Papp7cf60372018-01-11 00:06:43 +0000304 * Value is expected be an integer.
305 */
306 public static final String AK_TX_OFFSET_HZ = "txOffset";
307
308 /**
Laszlo Pappbf2cd742018-01-16 11:49:04 +0000309 * Annotation key for transmit offset in nm.
310 * Value is expected be an integer.
311 */
312 public static final String AK_TX_OFFSET_LMDA_NM = "txOffsetLambda";
313
314 /**
315 * Annotation key for transmit grid spacing frequency in Hz.
Laszlo Papp7cf60372018-01-11 00:06:43 +0000316 * Value is expected be an integer.
317 */
318 public static final String AK_TX_GRID_SPAN_HZ = "txGridSpan";
319
320 /**
Laszlo Pappbf2cd742018-01-16 11:49:04 +0000321 * Annotation key for transmit grid spacing lambda in nm.
322 * Value is expected be an integer.
323 */
324 public static final String AK_TX_GRID_SPAN_LMDA_NM = "txGridSpanLambda";
325
326 /**
Laszlo Papp7cf60372018-01-11 00:06:43 +0000327 * Annotation key for receive frequency in Hz.
328 * Value is expected be an integer.
329 */
330 public static final String AK_RX_FREQ_HZ = "rxFrequency";
331
332 /**
Laszlo Pappbf2cd742018-01-16 11:49:04 +0000333 * Annotation key for receive lambda in nm.
334 * Value is expected be an integer.
335 */
336 public static final String AK_RX_LMDA_NM = "rxLambda";
337
338 /**
339 * Annotation key for receive offset frequency in Hz.
Laszlo Papp7cf60372018-01-11 00:06:43 +0000340 * Value is expected be an integer.
341 */
342 public static final String AK_RX_OFFSET_HZ = "rxOffset";
343
344 /**
Laszlo Pappbf2cd742018-01-16 11:49:04 +0000345 * Annotation key for receive offset lambda in nm.
346 * Value is expected be an integer.
347 */
348 public static final String AK_RX_OFFSET_LMDA_NM = "rxOffsetLambda";
349
350 /**
351 * Annotation key for receive grid spacing frequency in Hz.
Laszlo Papp7cf60372018-01-11 00:06:43 +0000352 * Value is expected be an integer.
353 */
354 public static final String AK_RX_GRID_SPAN_HZ = "rxGridSpan";
355
Laszlo Pappbf2cd742018-01-16 11:49:04 +0000356 /**
357 * Annotation key for receive grid spacing lambda in nm.
358 * Value is expected be an integer.
359 */
360 public static final String AK_RX_GRID_SPAN_LMDA_NM = "rxGridSpanLambda";
361
Laszlo Papp7cf60372018-01-11 00:06:43 +0000362 /**
363 * Annotation key for transmit power in dBm*10.
364 * Value is expected to be an integer.
365 */
366 public static final String AK_TX_PWR = "txPower";
367
368 /**
369 * Annotation key for receive power feature.
370 * Value is expected to be "enabled" or "disabled"
371 */
372 public static final String AK_RX_PWR_FEATURE = "rxPwrFeature";
373
374 /**
375 * Annotation key for receive power in dBm*10.
376 * Value is expected to be an integer.
377 */
378 public static final String AK_RX_PWR = "rxPower";
379
380 /**
381 * Annotation key for transmit bias feature.
382 * Value is expected to be "enabled" or "disabled"
383 */
384 public static final String AK_TX_BIAS_FEATURE = "txBiasFeature";
385
386 /**
387 * Annotation key for transmit bias current in mA*10.
388 * Value is expected to be an integer.
389 */
390 public static final String AK_BIAS_CURRENT = "biasCurrent";
391
392 /**
393 * Annotation key for transmit temperature feature.
394 * Value is expected to be "enabled" or "disabled"
395 */
396 public static final String AK_TX_TEMP_FEATURE = "txTempFeature";
397
398 /**
399 * Annotation key for transmit laser temperature in C*10.
400 * Value is expected to be an integer.
401 */
402 public static final String AK_TEMPERATURE = "temperature";
403
404
405 // Common feature annotations
406
407 /**
408 * Annotation key for transmit tune feature.
409 * Value is expected to be "enabled" or "disabled"
410 */
411 public static final String AK_TX_TUNE_FEATURE = "txTuneFeature";
412
413 /**
414 * Annotation key for receive tune feature.
415 * Value is expected to be "enabled" or "disabled"
416 */
417 public static final String AK_RX_TUNE_FEATURE = "rxTuneFeature";
418
419 /**
420 * Annotation key for transmit power feature.
421 * Value is expected to be "enabled" or "disabled"
422 */
423 public static final String AK_TX_PWR_FEATURE = "txPwrFeature";
424
425
Charles Chan25b77322015-12-13 01:00:56 -0800426 //TODO consider renaming KBPS and MBPS (as they are used to convert by division)
427 private static final long KBPS = 1_000;
Ray Milkey3717e602018-02-01 13:49:47 -0800428 private static final long MBPS = 1_000L * 1_000L;
Rimon Ashkenazye3201032016-01-11 14:27:30 +0200429 private static final Frequency FREQ50 = Frequency.ofGHz(50);
430 private static final Frequency FREQ191_7 = Frequency.ofGHz(191_700);
Sho SHIMIZU00762ee2016-01-05 16:32:24 -0800431 private static final Frequency FREQ4_4 = Frequency.ofGHz(4_400);
tomb5a46e62014-08-26 14:20:00 -0700432
Laszlo Pappbf2cd742018-01-16 11:49:04 +0000433 private static final long C = 299792458; // speed of light in m/s
Thomas Vachuska164ecf62018-05-08 17:29:55 -0700434 public static final String SCHEME = "of";
Laszlo Pappbf2cd742018-01-16 11:49:04 +0000435
tomb5a46e62014-08-26 14:20:00 -0700436 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
tom96dfcab2014-08-28 09:26:03 -0700437 protected DeviceProviderRegistry providerRegistry;
tomab21e7c2014-08-26 15:23:08 -0700438
tom5f38b3a2014-08-27 23:50:54 -0700439 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Thomas Vachuska164ecf62018-05-08 17:29:55 -0700440 protected DeviceService deviceService;
441
442 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
tom5f38b3a2014-08-27 23:50:54 -0700443 protected OpenFlowController controller;
444
Dusan Pajinbab8a5e2015-07-24 17:37:19 +0200445 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
446 protected ComponentConfigService cfgService;
447
Victor Silvaac8dab02016-11-28 16:33:08 -0300448 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
449 protected DriverService driverService;
450
tomab21e7c2014-08-26 15:23:08 -0700451 private DeviceProviderService providerService;
tomb5a46e62014-08-26 14:20:00 -0700452
sangho538108b2015-04-08 14:29:20 -0700453 private final InternalDeviceProvider listener = new InternalDeviceProvider();
Thomas Vachuska164ecf62018-05-08 17:29:55 -0700454 private final InternalDeviceListener deviceListener = new InternalDeviceListener();
sangho538108b2015-04-08 14:29:20 -0700455
Simon Hunt7a307652016-06-23 01:17:29 -0700456 private static final String POLL_PROP_NAME = "portStatsPollFrequency";
457 private static final int POLL_INTERVAL = 5;
458 @Property(name = POLL_PROP_NAME, intValue = POLL_INTERVAL,
Dusan Pajinbab8a5e2015-07-24 17:37:19 +0200459 label = "Frequency (in seconds) for polling switch Port statistics")
460 private int portStatsPollFrequency = POLL_INTERVAL;
sangho538108b2015-04-08 14:29:20 -0700461
Laszlo Pappbf2cd742018-01-16 11:49:04 +0000462 private static final String PROP_FREQ = "propertyFrequency";
463 private static final boolean DEFAULT_PROP_FREQ = true;
464 @Property(name = PROP_FREQ, boolValue = DEFAULT_PROP_FREQ,
465 label = "It indicates frequency must be used instead of wavelength for port tuning.")
466 private static boolean propFreq = DEFAULT_PROP_FREQ;
467
Victor Silvaff5871b2016-10-04 18:08:47 -0300468 private final Timer timer = new Timer("onos-openflow-portstats-collector");
sangyun-han07a572f2016-07-29 21:14:27 +0900469
sangho538108b2015-04-08 14:29:20 -0700470 private HashMap<Dpid, PortStatsCollector> collectors = Maps.newHashMap();
tomd40fc7a2014-09-04 16:41:10 -0700471
tomab21e7c2014-08-26 15:23:08 -0700472 /**
473 * Creates an OpenFlow device provider.
474 */
475 public OpenFlowDeviceProvider() {
Thomas Vachuska164ecf62018-05-08 17:29:55 -0700476 super(new ProviderId(SCHEME, "org.onosproject.provider.openflow"));
tomab21e7c2014-08-26 15:23:08 -0700477 }
478
tomb5a46e62014-08-26 14:20:00 -0700479 @Activate
Dusan Pajinbab8a5e2015-07-24 17:37:19 +0200480 public void activate(ComponentContext context) {
481 cfgService.registerProperties(getClass());
tom96dfcab2014-08-28 09:26:03 -0700482 providerService = providerRegistry.register(this);
Thomas Vachuska164ecf62018-05-08 17:29:55 -0700483 deviceService.addListener(deviceListener);
tomd40fc7a2014-09-04 16:41:10 -0700484 controller.addListener(listener);
sangho538108b2015-04-08 14:29:20 -0700485 controller.addEventListener(listener);
Jian Li152b8852015-12-07 14:47:25 -0800486
Victor Silva7944d142016-07-08 00:14:55 -0300487 modified(context);
488
Thomas Vachuskad07c0922015-10-06 14:48:06 -0700489 connectInitialDevices();
alshabiba89cc582014-09-09 16:43:00 -0700490 LOG.info("Started");
tomb5a46e62014-08-26 14:20:00 -0700491 }
492
493 @Deactivate
Dusan Pajinbab8a5e2015-07-24 17:37:19 +0200494 public void deactivate(ComponentContext context) {
495 cfgService.unregisterProperties(getClass(), false);
Ray Milkey7abe50b2016-04-13 11:57:44 -0700496 listener.disable();
Thomas Vachuska164ecf62018-05-08 17:29:55 -0700497 deviceService.removeListener(deviceListener);
tomd40fc7a2014-09-04 16:41:10 -0700498 controller.removeListener(listener);
Thomas Vachuskad07c0922015-10-06 14:48:06 -0700499 providerRegistry.unregister(this);
Thomas Vachuska74bcd922015-06-02 13:08:57 -0700500 collectors.values().forEach(PortStatsCollector::stop);
Thomas Vachuskaa394b952016-06-14 15:02:09 -0700501 collectors.clear();
tomab21e7c2014-08-26 15:23:08 -0700502 providerService = null;
alshabiba89cc582014-09-09 16:43:00 -0700503 LOG.info("Stopped");
tomb5a46e62014-08-26 14:20:00 -0700504 }
505
Dusan Pajinbab8a5e2015-07-24 17:37:19 +0200506 @Modified
507 public void modified(ComponentContext context) {
Victor Silva7944d142016-07-08 00:14:55 -0300508 Dictionary<?, ?> properties = context != null ? context.getProperties() : new Properties();
Dusan Pajinbab8a5e2015-07-24 17:37:19 +0200509 int newPortStatsPollFrequency;
510 try {
Simon Hunt7a307652016-06-23 01:17:29 -0700511 String s = get(properties, POLL_PROP_NAME);
Dusan Pajinbab8a5e2015-07-24 17:37:19 +0200512 newPortStatsPollFrequency = isNullOrEmpty(s) ? portStatsPollFrequency : Integer.parseInt(s.trim());
513
514 } catch (NumberFormatException | ClassCastException e) {
515 newPortStatsPollFrequency = portStatsPollFrequency;
516 }
517
518 if (newPortStatsPollFrequency != portStatsPollFrequency) {
519 portStatsPollFrequency = newPortStatsPollFrequency;
520 collectors.values().forEach(psc -> psc.adjustPollInterval(portStatsPollFrequency));
521 }
522
523 LOG.info("Settings: portStatsPollFrequency={}", portStatsPollFrequency);
524 }
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700525
Thomas Vachuskad07c0922015-10-06 14:48:06 -0700526 private void connectInitialDevices() {
527 for (OpenFlowSwitch sw : controller.getSwitches()) {
528 try {
529 listener.switchAdded(new Dpid(sw.getId()));
530 } catch (Exception e) {
531 LOG.warn("Failed initially adding {} : {}", sw.getStringId(), e.getMessage());
532 LOG.debug("Error details:", e);
533 // disconnect to trigger switch-add later
534 sw.disconnectSwitch();
535 }
Thomas Vachuskad07c0922015-10-06 14:48:06 -0700536 }
537 }
538
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700539 @Override
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700540 public boolean isReachable(DeviceId deviceId) {
541 OpenFlowSwitch sw = controller.getSwitch(dpid(deviceId.uri()));
Thomas Vachuskad07c0922015-10-06 14:48:06 -0700542 return sw != null && sw.isConnected();
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700543 }
544
tomab21e7c2014-08-26 15:23:08 -0700545 @Override
Ayaka Koshibe78bcbc12014-11-19 14:28:58 -0800546 public void triggerProbe(DeviceId deviceId) {
Madan Jampanic3328762015-05-31 12:39:27 -0700547 LOG.debug("Triggering probe on device {}", deviceId);
Ayaka Koshibee8708e32014-10-22 13:40:18 -0700548
Yuta HIGUCHI802d0e82014-10-31 16:07:37 -0700549 final Dpid dpid = dpid(deviceId.uri());
550 OpenFlowSwitch sw = controller.getSwitch(dpid);
551 if (sw == null || !sw.isConnected()) {
Ayaka Koshibe78bcbc12014-11-19 14:28:58 -0800552 LOG.error("Failed to probe device {} on sw={}", deviceId, sw);
Yuta HIGUCHI802d0e82014-10-31 16:07:37 -0700553 providerService.deviceDisconnected(deviceId);
Thomas Vachuska74bcd922015-06-02 13:08:57 -0700554 return;
Yuta HIGUCHI802d0e82014-10-31 16:07:37 -0700555 } else {
Ayaka Koshibe78bcbc12014-11-19 14:28:58 -0800556 LOG.trace("Confirmed device {} connection", deviceId);
Yuta HIGUCHI802d0e82014-10-31 16:07:37 -0700557 }
Ayaka Koshibee8708e32014-10-22 13:40:18 -0700558
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700559 // Prompt an update of port information. We can use any XID for this.
Ayaka Koshibee8708e32014-10-22 13:40:18 -0700560 OFFactory fact = sw.factory();
561 switch (fact.getVersion()) {
562 case OF_10:
563 sw.sendMsg(fact.buildFeaturesRequest().setXid(0).build());
564 break;
565 case OF_13:
Yuta HIGUCHI6512f3e2017-05-18 17:21:24 -0700566 case OF_14:
567 case OF_15:
Ayaka Koshibee8708e32014-10-22 13:40:18 -0700568 sw.sendMsg(fact.buildPortDescStatsRequest().setXid(0).build());
569 break;
570 default:
571 LOG.warn("Unhandled protocol version");
572 }
tomab21e7c2014-08-26 15:23:08 -0700573 }
574
575 @Override
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700576 public void roleChanged(DeviceId deviceId, MastershipRole newRole) {
alshabibf1216ed2014-09-03 11:53:54 -0700577 switch (newRole) {
tom782a7cf2014-09-11 23:58:38 -0700578 case MASTER:
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700579 controller.setRole(dpid(deviceId.uri()), RoleState.MASTER);
tom782a7cf2014-09-11 23:58:38 -0700580 break;
581 case STANDBY:
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700582 controller.setRole(dpid(deviceId.uri()), RoleState.EQUAL);
tom782a7cf2014-09-11 23:58:38 -0700583 break;
584 case NONE:
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700585 controller.setRole(dpid(deviceId.uri()), RoleState.SLAVE);
tom782a7cf2014-09-11 23:58:38 -0700586 break;
587 default:
588 LOG.error("Unknown Mastership state : {}", newRole);
alshabibf1216ed2014-09-03 11:53:54 -0700589
590 }
HIGUCHI Yuta1979f552015-12-28 21:24:26 -0800591 LOG.debug("Accepting mastership role change to {} for device {}", newRole, deviceId);
tomab21e7c2014-08-26 15:23:08 -0700592 }
593
Saurav Dasa2d37502016-03-25 17:50:40 -0700594 @Override
595 public void changePortState(DeviceId deviceId, PortNumber portNumber,
596 boolean enable) {
597 final Dpid dpid = dpid(deviceId.uri());
598 OpenFlowSwitch sw = controller.getSwitch(dpid);
599 if (sw == null || !sw.isConnected()) {
600 LOG.error("Failed to change portState on device {}", deviceId);
601 return;
602 }
603 OFPortMod.Builder pmb = sw.factory().buildPortMod();
604 OFPort port = OFPort.of((int) portNumber.toLong());
605 pmb.setPortNo(port);
Jimmy Jine9b7a022016-08-12 16:56:48 -0700606 Set<OFPortConfig> portConfig = EnumSet.noneOf(OFPortConfig.class);
607 if (!enable) {
608 portConfig.add(OFPortConfig.PORT_DOWN);
Saurav Dasa2d37502016-03-25 17:50:40 -0700609 }
Jimmy Jine9b7a022016-08-12 16:56:48 -0700610 pmb.setConfig(portConfig);
611 Set<OFPortConfig> portMask = EnumSet.noneOf(OFPortConfig.class);
612 portMask.add(OFPortConfig.PORT_DOWN);
613 pmb.setMask(portMask);
Saurav Dasa2d37502016-03-25 17:50:40 -0700614 pmb.setAdvertise(0x0);
615 for (OFPortDesc pd : sw.getPorts()) {
616 if (pd.getPortNo().equals(port)) {
617 pmb.setHwAddr(pd.getHwAddr());
618 break;
619 }
620 }
621 sw.sendMsg(Collections.singletonList(pmb.build()));
622 }
623
Thomas Vachuska893bf4b2015-05-29 18:13:18 -0700624 private void pushPortMetrics(Dpid dpid, List<OFPortStatsEntry> portStatsEntries) {
HIGUCHI Yuta1979f552015-12-28 21:24:26 -0800625 DeviceId deviceId = DeviceId.deviceId(Dpid.uri(dpid));
Ray Milkey3c4124a2016-08-31 12:09:06 -0700626 Collection<PortStatistics> stats =
627 buildPortStatistics(deviceId, ImmutableList.copyOf(portStatsEntries));
sangho538108b2015-04-08 14:29:20 -0700628 providerService.updatePortStatistics(deviceId, stats);
629 }
630
Laszlo Papp8e8ff052018-01-16 09:55:45 +0000631 private static String lambdaToAnnotationHz(long lambda) {
632 // ref. OF1.5: wavelength (lambda) as nm * 100
633
Laszlo Papp8e8ff052018-01-16 09:55:45 +0000634 // f = c / λ
635 // (m/s) * (nm/m) / (nm * 100) * 100
636 // annotations is in Hz
Laszlo Pappbf2cd742018-01-16 11:49:04 +0000637 return Long.toString(lambda == 0 ? lambda : (C * 1_000_000_000 / lambda * 100));
Laszlo Papp8e8ff052018-01-16 09:55:45 +0000638 }
Laszlo Papp7cf60372018-01-11 00:06:43 +0000639
Laszlo Pappbf2cd742018-01-16 11:49:04 +0000640 private static String mhzToAnnotationNm(long freqMhz) {
641 // λ = c / f
642 // (m/s) * (nm/m) / (1000000 * 1/s)
643 // annotations is in nm
644 return Long.toString(freqMhz == 0 ? freqMhz : (C * 1_000_000_000 / Frequency.ofMHz(freqMhz).asHz()));
645 }
646
647
Laszlo Papp7cf60372018-01-11 00:06:43 +0000648 private static String mhzToAnnotation(long freqMhz) {
649 return Long.toString(Frequency.ofMHz(freqMhz).asHz());
650 }
651
Laszlo Papp8e8ff052018-01-16 09:55:45 +0000652 private static String freqLmdaToAnnotation(long freqLmda, boolean useFreq) {
Laszlo Pappbf2cd742018-01-16 11:49:04 +0000653 if (useFreq) {
654 if (propFreq) {
655 mhzToAnnotation(freqLmda);
656 } else {
657 mhzToAnnotationNm(freqLmda);
658 }
659 } else if (propFreq) {
660 lambdaToAnnotationHz(freqLmda);
661 }
662 return Double.toString(freqLmda / 100.0);
Laszlo Papp8e8ff052018-01-16 09:55:45 +0000663 }
664
Thomas Vachuska893bf4b2015-05-29 18:13:18 -0700665 private Collection<PortStatistics> buildPortStatistics(DeviceId deviceId,
666 List<OFPortStatsEntry> entries) {
sangho538108b2015-04-08 14:29:20 -0700667 HashSet<PortStatistics> stats = Sets.newHashSet();
Laszlo Papp8e8ff052018-01-16 09:55:45 +0000668 final Dpid dpid = dpid(deviceId.uri());
669 OpenFlowSwitch sw = controller.getSwitch(dpid);
sangho538108b2015-04-08 14:29:20 -0700670
Thomas Vachuska893bf4b2015-05-29 18:13:18 -0700671 for (OFPortStatsEntry entry : entries) {
Thomas Vachuskaf0397b52015-05-29 13:50:17 -0700672 try {
Thomas Vachuska4e6025e2015-06-03 11:25:21 -0700673 if (entry == null || entry.getPortNo() == null || entry.getPortNo().getPortNumber() < 0) {
Thomas Vachuskaf0397b52015-05-29 13:50:17 -0700674 continue;
675 }
Laszlo Papp7cf60372018-01-11 00:06:43 +0000676 DefaultAnnotations.Builder annotations = DefaultAnnotations.builder();
Laszlo Papp9dfb5122018-01-12 21:10:13 +0000677 boolean propSupported = entry.getVersion().getWireVersion() >= OFVersion.OF_14.getWireVersion();
678 Optional<OFPortStatsPropOptical> optical = propSupported ?
679 entry.getProperties().stream()
Laszlo Papp7cf60372018-01-11 00:06:43 +0000680 .filter(OFPortStatsPropOptical.class::isInstance)
681 .map(OFPortStatsPropOptical.class::cast)
Laszlo Papp9dfb5122018-01-12 21:10:13 +0000682 .findAny() : Optional.empty();
Laszlo Papp7cf60372018-01-11 00:06:43 +0000683 if (optical.isPresent()) {
684 long flags = optical.get().getFlags();
685
Laszlo Papp8e8ff052018-01-16 09:55:45 +0000686 boolean useFreq = false;
687 for (OFPortDesc pd : sw.getPorts()) {
688 if (pd.getPortNo().equals(entry.getPortNo())) {
689 for (OFPortDescProp prop : pd.getProperties()) {
690 if (prop instanceof OFPortDescPropOptical) {
691 OFPortDescPropOptical oprop = (OFPortDescPropOptical) prop;
692 long supported = oprop.getSupported();
693 int useFreqVal = OFOpticalPortFeaturesSerializerVer14.USE_FREQ_VAL;
694 if ((supported & useFreqVal) != 0) {
695 useFreq = true;
696 break;
697 }
698 }
699 }
700 }
701 }
702
Laszlo Papp7cf60372018-01-11 00:06:43 +0000703 int txTune = OFPortStatsOpticalFlagsSerializerVer14.TX_TUNE_VAL;
704 long txFreq = optical.get().getTxFreqLmda();
705 long txOffset = optical.get().getTxOffset();
706 long txGridSpan = optical.get().getTxGridSpan();
707 annotations.set(AK_TX_TUNE_FEATURE, ((flags & txTune) != 0) ? "enabled" : "disabled");
Laszlo Pappbf2cd742018-01-16 11:49:04 +0000708 annotations.set(propFreq ? AK_TX_FREQ_HZ : AK_TX_LMDA_NM,
709 freqLmdaToAnnotation(txFreq, useFreq));
710 annotations.set(propFreq ? AK_TX_OFFSET_HZ : AK_TX_OFFSET_LMDA_NM,
711 freqLmdaToAnnotation(txOffset, useFreq));
712 annotations.set(propFreq ? AK_TX_GRID_SPAN_HZ : AK_TX_GRID_SPAN_LMDA_NM,
713 freqLmdaToAnnotation(txGridSpan, useFreq));
Laszlo Papp7cf60372018-01-11 00:06:43 +0000714
715 int rxTune = OFPortStatsOpticalFlagsSerializerVer14.RX_TUNE_VAL;
716 long rxFreq = optical.get().getRxFreqLmda();
717 long rxOffset = optical.get().getRxOffset();
718 long rxGridSpan = optical.get().getRxGridSpan();
719 annotations.set(AK_RX_TUNE_FEATURE, ((flags & rxTune) != 0) ? "enabled" : "disabled");
Laszlo Pappbf2cd742018-01-16 11:49:04 +0000720 annotations.set(propFreq ? AK_RX_FREQ_HZ : AK_RX_LMDA_NM,
721 freqLmdaToAnnotation(rxFreq, useFreq));
722 annotations.set(propFreq ? AK_RX_OFFSET_HZ : AK_RX_OFFSET_LMDA_NM,
723 freqLmdaToAnnotation(rxOffset, useFreq));
724 annotations.set(propFreq ? AK_RX_GRID_SPAN_HZ : AK_RX_GRID_SPAN_LMDA_NM,
725 freqLmdaToAnnotation(rxGridSpan, useFreq));
Laszlo Papp7cf60372018-01-11 00:06:43 +0000726
727 int txPwrVal = OFPortStatsOpticalFlagsSerializerVer14.TX_PWR_VAL;
728 int txPwr = optical.get().getTxPwr();
729 annotations.set(AK_TX_PWR_FEATURE, ((flags & txPwrVal) != 0) ? "enabled" : "disabled");
730 annotations.set(AK_TX_PWR, Integer.toString(txPwr));
731
732 int rxPwrVal = OFPortStatsOpticalFlagsSerializerVer14.RX_PWR_VAL;
733 int rxPwr = optical.get().getRxPwr();
734 annotations.set(AK_RX_PWR_FEATURE, ((flags & rxPwrVal) != 0) ? "enabled" : "disabled");
735 annotations.set(AK_RX_PWR, Integer.toString(rxPwr));
736
737 int txBias = OFPortStatsOpticalFlagsSerializerVer14.TX_BIAS_VAL;
738 int biasCurrent = optical.get().getBiasCurrent();
739 annotations.set(AK_TX_BIAS_FEATURE, ((flags & txBias) != 0) ? "enabled" : "disabled");
740 annotations.set(AK_BIAS_CURRENT, Integer.toString(biasCurrent));
741
742 int txTemp = OFPortStatsOpticalFlagsSerializerVer14.TX_TEMP_VAL;
743 int temperature = optical.get().getTemperature();
744 annotations.set(AK_TX_TEMP_FEATURE, ((flags & txTemp) != 0) ? "enabled" : "disabled");
745 annotations.set(AK_TEMPERATURE, Integer.toString(temperature));
746 }
Thomas Vachuskaf0397b52015-05-29 13:50:17 -0700747 DefaultPortStatistics.Builder builder = DefaultPortStatistics.builder();
748 DefaultPortStatistics stat = builder.setDeviceId(deviceId)
749 .setPort(entry.getPortNo().getPortNumber())
750 .setPacketsReceived(entry.getRxPackets().getValue())
751 .setPacketsSent(entry.getTxPackets().getValue())
752 .setBytesReceived(entry.getRxBytes().getValue())
753 .setBytesSent(entry.getTxBytes().getValue())
754 .setPacketsRxDropped(entry.getRxDropped().getValue())
755 .setPacketsTxDropped(entry.getTxDropped().getValue())
756 .setPacketsRxErrors(entry.getRxErrors().getValue())
757 .setPacketsTxErrors(entry.getTxErrors().getValue())
758 .setDurationSec(entry.getVersion() == OFVersion.OF_10 ? 0 : entry.getDurationSec())
759 .setDurationNano(entry.getVersion() == OFVersion.OF_10 ? 0 : entry.getDurationNsec())
Laszlo Papp7cf60372018-01-11 00:06:43 +0000760 .setAnnotations(annotations.build())
Thomas Vachuskaf0397b52015-05-29 13:50:17 -0700761 .build();
sangho538108b2015-04-08 14:29:20 -0700762
Thomas Vachuskaf0397b52015-05-29 13:50:17 -0700763 stats.add(stat);
764 } catch (Exception e) {
765 LOG.warn("Unable to process port stats", e);
766 }
sangho538108b2015-04-08 14:29:20 -0700767 }
768
769 return Collections.unmodifiableSet(stats);
sangho538108b2015-04-08 14:29:20 -0700770 }
771
772 private class InternalDeviceProvider implements OpenFlowSwitchListener, OpenFlowEventListener {
Thomas Vachuska893bf4b2015-05-29 18:13:18 -0700773
Srikanth Vavilapalli78baf582015-06-05 11:40:14 -0700774 private HashMap<Dpid, List<OFPortStatsEntry>> portStatsReplies = new HashMap<>();
Ray Milkey7abe50b2016-04-13 11:57:44 -0700775 private boolean isDisabled = false;
Thomas Vachuska893bf4b2015-05-29 18:13:18 -0700776
alshabibf1216ed2014-09-03 11:53:54 -0700777 @Override
tomd1900f32014-09-03 14:08:16 -0700778 public void switchAdded(Dpid dpid) {
alshabib6f5460b2014-09-03 14:46:17 -0700779 if (providerService == null) {
780 return;
781 }
tom782a7cf2014-09-11 23:58:38 -0700782 DeviceId did = deviceId(uri(dpid));
alshabib6f5460b2014-09-03 14:46:17 -0700783 OpenFlowSwitch sw = controller.getSwitch(dpid);
Lei Xudee1aff2015-10-16 00:45:10 -0500784 if (sw == null) {
sangyun-han147e6f42016-07-26 17:14:41 +0900785 LOG.error("Switch {} is not found", dpid);
Lei Xudee1aff2015-10-16 00:45:10 -0500786 return;
787 }
alshabib6f5460b2014-09-03 14:46:17 -0700788
alshabib7911a052014-10-16 17:49:37 -0700789 ChassisId cId = new ChassisId(dpid.value());
Ray Milkeye53f1712015-01-16 09:17:16 -0800790
Victor Silvaac8dab02016-11-28 16:33:08 -0300791 DefaultAnnotations.Builder annotationsBuilder = DefaultAnnotations.builder()
andreafe3308f2015-10-06 15:51:25 -0700792 .set(AnnotationKeys.PROTOCOL, sw.factory().getVersion().toString())
793 .set(AnnotationKeys.CHANNEL_ID, sw.channelId())
Victor Silvaac8dab02016-11-28 16:33:08 -0300794 .set(AnnotationKeys.MANAGEMENT_ADDRESS, sw.channelId().split(":")[0]);
795
Yuta HIGUCHI2341e602017-03-08 20:10:08 -0800796 // FIXME following ignores driver specified by name
Victor Silvaac8dab02016-11-28 16:33:08 -0300797 Driver driver = driverService.getDriver(sw.manufacturerDescription(),
798 sw.hardwareDescription(),
799 sw.softwareDescription());
Thomas Vachuskaa79cbf72017-01-13 15:07:04 -0800800 // FIXME: The following breaks the STC tests and will require to be revisited.
801// if (driver != null) {
802// annotationsBuilder.set(AnnotationKeys.DRIVER, driver.name());
803// }
Victor Silvaac8dab02016-11-28 16:33:08 -0300804
805 SparseAnnotations annotations = annotationsBuilder.build();
Ray Milkeye53f1712015-01-16 09:17:16 -0800806
tomd1900f32014-09-03 14:08:16 -0700807 DeviceDescription description =
Marc De Leenheerb9311372015-07-09 11:36:49 -0700808 new DefaultDeviceDescription(did.uri(), sw.deviceType(),
Ray Milkeyd3edd032015-01-16 11:38:58 -0800809 sw.manufacturerDescription(),
tom782a7cf2014-09-11 23:58:38 -0700810 sw.hardwareDescription(),
811 sw.softwareDescription(),
alshabib7911a052014-10-16 17:49:37 -0700812 sw.serialNumber(),
Thomas Vachuska82041f52014-11-30 22:14:02 -0800813 cId, annotations);
tom782a7cf2014-09-11 23:58:38 -0700814 providerService.deviceConnected(did, description);
Ayaka Koshibe5460d622015-05-14 12:19:19 -0700815 providerService.updatePorts(did, buildPortDescriptions(sw));
Palash Kala496ef1a22018-03-21 14:57:50 +0900816 //sends port description stats request again if OF version supports
817 if (sw.features().getVersion().compareTo(OFVersion.OF_13) >= 0) {
818 sendPortDescStatsRequest(sw);
819 }
sangho538108b2015-04-08 14:29:20 -0700820
Laszlo Papp58174812018-01-16 13:29:47 +0000821 if (sw.features().getCapabilities().contains(OFCapabilities.PORT_STATS)) {
822 PortStatsCollector psc = new PortStatsCollector(timer, sw, portStatsPollFrequency);
823 stopCollectorIfNeeded(collectors.put(dpid, psc));
824 psc.start();
825 }
Lei Xudee1aff2015-10-16 00:45:10 -0500826
827 //figure out race condition for collectors.remove() and collectors.put()
828 if (controller.getSwitch(dpid) == null) {
829 switchRemoved(dpid);
830 }
alshabib25c8eec2014-09-04 16:41:31 -0700831 }
832
Palash Kala496ef1a22018-03-21 14:57:50 +0900833 /**
834 * Sends port description statistic request to switch if supported.
835 */
836 private void sendPortDescStatsRequest(OpenFlowSwitch sw) {
837 if (sw == null) {
838 return;
839 }
840 OFPortDescStatsRequest descStatsRequest = sw.factory().buildPortDescStatsRequest()
841 .build();
842 sw.sendMsg(descStatsRequest);
843 }
844
Thomas Vachuskaa394b952016-06-14 15:02:09 -0700845 private void stopCollectorIfNeeded(PortStatsCollector collector) {
846 if (collector != null) {
847 collector.stop();
848 }
849 }
850
alshabibf1216ed2014-09-03 11:53:54 -0700851 @Override
852 public void switchRemoved(Dpid dpid) {
Victor Silva03265dd2016-12-08 11:20:36 -0300853 stopCollectorIfNeeded(collectors.remove(dpid));
alshabib6f5460b2014-09-03 14:46:17 -0700854 if (providerService == null) {
855 return;
856 }
tom782a7cf2014-09-11 23:58:38 -0700857 providerService.deviceDisconnected(deviceId(uri(dpid)));
sangho538108b2015-04-08 14:29:20 -0700858 }
Ayaka Koshibe38594c22014-10-22 13:36:12 -0700859
860 @Override
861 public void switchChanged(Dpid dpid) {
HIGUCHI Yuta1979f552015-12-28 21:24:26 -0800862 LOG.debug("switchChanged({})", dpid);
Ayaka Koshibe38594c22014-10-22 13:36:12 -0700863 if (providerService == null) {
864 return;
865 }
866 DeviceId did = deviceId(uri(dpid));
867 OpenFlowSwitch sw = controller.getSwitch(dpid);
Lei Xudee1aff2015-10-16 00:45:10 -0500868 if (sw == null) {
sangyun-han147e6f42016-07-26 17:14:41 +0900869 LOG.error("Switch {} is not found", dpid);
Lei Xudee1aff2015-10-16 00:45:10 -0500870 return;
871 }
HIGUCHI Yuta1979f552015-12-28 21:24:26 -0800872 final List<PortDescription> ports = buildPortDescriptions(sw);
873 LOG.debug("switchChanged({}) {}", did, ports);
874 providerService.updatePorts(did, ports);
Ayaka Koshibe38594c22014-10-22 13:36:12 -0700875 }
876
alshabiba14f3642014-09-05 09:31:31 -0700877 @Override
878 public void portChanged(Dpid dpid, OFPortStatus status) {
HIGUCHI Yuta1979f552015-12-28 21:24:26 -0800879 LOG.debug("portChanged({},{})", dpid, status);
alshabibafc514a2014-12-01 14:44:05 -0800880 PortDescription portDescription = buildPortDescription(status);
Michal Machce774332017-01-25 11:02:55 +0100881 if (status.getReason() != OFPortReason.DELETE) {
882 providerService.portStatusChanged(deviceId(uri(dpid)), portDescription);
883 } else {
884 providerService.deletePort(deviceId(uri(dpid)), portDescription);
885 }
alshabibf1216ed2014-09-03 11:53:54 -0700886 }
887
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700888 @Override
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700889 public void receivedRoleReply(Dpid dpid, RoleState requested, RoleState response) {
HIGUCHI Yuta1979f552015-12-28 21:24:26 -0800890 LOG.debug("receivedRoleReply({},{},{})", dpid, requested, response);
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700891 MastershipRole request = roleOf(requested);
892 MastershipRole reply = roleOf(response);
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700893 providerService.receivedRoleReply(deviceId(uri(dpid)), request, reply);
894 }
895
896 /**
897 * Translates a RoleState to the corresponding MastershipRole.
898 *
Thomas Vachuskafc52fec2015-05-18 19:13:56 -0700899 * @param response role state
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700900 * @return a MastershipRole
901 */
902 private MastershipRole roleOf(RoleState response) {
903 switch (response) {
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700904 case MASTER:
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700905 return MastershipRole.MASTER;
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700906 case EQUAL:
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700907 return MastershipRole.STANDBY;
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700908 case SLAVE:
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700909 return MastershipRole.NONE;
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700910 default:
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700911 LOG.warn("unknown role {}", response);
912 return null;
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700913 }
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700914 }
915
alshabiba14f3642014-09-05 09:31:31 -0700916 /**
917 * Builds a list of port descriptions for a given list of ports.
tomff7eb7c2014-09-08 12:49:03 -0700918 *
alshabiba14f3642014-09-05 09:31:31 -0700919 * @return list of portdescriptions
920 */
Ayaka Koshibe5460d622015-05-14 12:19:19 -0700921 private List<PortDescription> buildPortDescriptions(OpenFlowSwitch sw) {
Yuta HIGUCHIf83c8cf2017-12-17 14:33:49 -0800922 List<OFPortDesc> ofPorts = sw.getPorts();
923 final List<PortDescription> portDescs = new ArrayList<>(ofPorts.size());
Rimon Ashkenazy8ebfff02016-02-01 11:56:36 +0200924 if (!((Device.Type.ROADM.equals(sw.deviceType())) ||
MaoLuc201ae42017-02-06 17:57:01 -0800925 (Device.Type.OTN.equals(sw.deviceType())) ||
926 (Device.Type.OPTICAL_AMPLIFIER.equals(sw.deviceType())))) {
Yuta HIGUCHI2341e602017-03-08 20:10:08 -0800927 // build regular (=non-optical) Device ports
Yuta HIGUCHIf83c8cf2017-12-17 14:33:49 -0800928 ofPorts.forEach(port -> portDescs.add(buildPortDescription(port)));
Yafit Hadara9a73de2015-09-06 13:52:52 +0300929 }
Marc De Leenheerb9311372015-07-09 11:36:49 -0700930
Yuta HIGUCHI2341e602017-03-08 20:10:08 -0800931 // TODO handle Optical Device, but plain OF devices(1.4 and later)
932
Marc De Leenheerb9311372015-07-09 11:36:49 -0700933 OpenFlowOpticalSwitch opsw;
934 switch (sw.deviceType()) {
935 case ROADM:
Rimon Ashkenazy8ebfff02016-02-01 11:56:36 +0200936 case OTN:
MaoLuc201ae42017-02-06 17:57:01 -0800937 case OPTICAL_AMPLIFIER:
Marc De Leenheerb9311372015-07-09 11:36:49 -0700938 opsw = (OpenFlowOpticalSwitch) sw;
Yafit Hadara9a73de2015-09-06 13:52:52 +0300939 List<OFPortDesc> ports = opsw.getPorts();
940 LOG.debug("SW ID {} , ETH- ODU CLT Ports {}", opsw.getId(), ports);
941 // ODU client ports are reported as ETH
942 ports.forEach(port -> portDescs.add(buildOduCltPortDescription(port)));
943
Marc De Leenheerb9311372015-07-09 11:36:49 -0700944 opsw.getPortTypes().forEach(type -> {
Yafit Hadara9a73de2015-09-06 13:52:52 +0300945 List<? extends OFObject> portsOf = opsw.getPortsOf(type);
946 LOG.debug("Ports Of{}", portsOf);
947 portsOf.forEach(
948 op -> {
Jimmy Yan4deb03b2016-06-24 10:53:54 -0700949 portDescs.add(buildPortDescription(type, op, opsw));
Yafit Hadara9a73de2015-09-06 13:52:52 +0300950 }
951 );
Marc De Leenheerb9311372015-07-09 11:36:49 -0700952 });
953 break;
954 case FIBER_SWITCH:
955 opsw = (OpenFlowOpticalSwitch) sw;
956 opsw.getPortTypes().forEach(type -> {
Yuta HIGUCHIf42a2cc2017-05-02 17:04:31 -0700957 opsw.getPortsOf(type).forEach(op -> {
958 if (op instanceof OFPortDesc) {
959 // ports using standard optical extension
960 // TODO OFMessage -> PortDescription should
961 // probably be a Behaviour
962 portDescs.add(oms(buildPortDescription((OFPortDesc) op)));
963 } else if (op instanceof OFCalientPortDescStatsEntry) {
964 // calient extension
965 portDescs.add(buildPortDescription((OFCalientPortDescStatsEntry) op));
966 } else {
967 LOG.warn("Unexpected FIBER_SWITCH port {} on {}",
968 op, sw.getStringId());
969 }
970 });
Marc De Leenheerb9311372015-07-09 11:36:49 -0700971 });
972 break;
973 default:
974 break;
alshabib4680bb62014-09-04 17:15:08 -0700975 }
Marc De Leenheerb9311372015-07-09 11:36:49 -0700976
alshabib4680bb62014-09-04 17:15:08 -0700977 return portDescs;
978 }
979
Yuta HIGUCHIf42a2cc2017-05-02 17:04:31 -0700980 /**
981 * Ensures returned PortDescription is an OMS port.
982 *
983 * @param descr input PortDescription
984 * @return OMS PortDescription
985 */
986 private PortDescription oms(PortDescription descr) {
987 // Hack until OFMessage -> PortDescription transformation
988 // becomes a Behaviour
989 if (descr.type() == Type.OMS) {
990 return descr;
991 }
992
993 Builder builder = DefaultAnnotations.builder();
994 builder.putAll(descr.annotations());
995
996 // set reasonable default when mandatory key is missing
997 if (Strings.isNullOrEmpty(descr.annotations().value(AK_MIN_FREQ_HZ))) {
998 builder.set(AK_MIN_FREQ_HZ, String.valueOf(Spectrum.O_BAND_MIN.asHz()));
999 }
1000
1001 if (Strings.isNullOrEmpty(descr.annotations().value(AK_MAX_FREQ_HZ))) {
1002 builder.set(AK_MAX_FREQ_HZ, String.valueOf(Spectrum.O_BAND_MAX.asHz()));
1003 }
1004
1005 if (Strings.isNullOrEmpty(descr.annotations().value(AK_GRID_HZ))) {
1006 builder.set(AK_GRID_HZ, String.valueOf(Frequency.ofGHz(50).asHz()));
1007 }
1008
1009 return DefaultPortDescription.builder(descr)
1010 .type(Type.OMS)
1011 .annotations(builder.build())
1012 .build();
1013 }
1014
Yafit Hadara9a73de2015-09-06 13:52:52 +03001015 private PortDescription buildOduCltPortDescription(OFPortDesc port) {
1016 PortNumber portNo = PortNumber.portNumber(port.getPortNo().getPortNumber());
1017 boolean enabled = !port.getState().contains(OFPortState.LINK_DOWN) &&
1018 !port.getConfig().contains(OFPortConfig.PORT_DOWN);
Rimon Ashkenazyd15ebf52016-01-06 14:36:13 +02001019 Long portSpeedInMbps = portSpeed(port);
Toru Furusawa72ee30c2016-01-08 13:29:04 -08001020 CltSignalType sigType = null;
Yafit Hadara9a73de2015-09-06 13:52:52 +03001021
Rimon Ashkenazyd15ebf52016-01-06 14:36:13 +02001022 switch (portSpeedInMbps.toString()) {
1023 case "1000":
Toru Furusawa72ee30c2016-01-08 13:29:04 -08001024 sigType = CltSignalType.CLT_1GBE;
Yafit Hadara9a73de2015-09-06 13:52:52 +03001025 break;
Rimon Ashkenazyd15ebf52016-01-06 14:36:13 +02001026 case "10000":
Toru Furusawa72ee30c2016-01-08 13:29:04 -08001027 sigType = CltSignalType.CLT_10GBE;
Yafit Hadara9a73de2015-09-06 13:52:52 +03001028 break;
Rimon Ashkenazyd15ebf52016-01-06 14:36:13 +02001029 case "40000":
Toru Furusawa72ee30c2016-01-08 13:29:04 -08001030 sigType = CltSignalType.CLT_40GBE;
Yafit Hadara9a73de2015-09-06 13:52:52 +03001031 break;
Rimon Ashkenazyd15ebf52016-01-06 14:36:13 +02001032 case "100000":
Toru Furusawa72ee30c2016-01-08 13:29:04 -08001033 sigType = CltSignalType.CLT_100GBE;
Yafit Hadara9a73de2015-09-06 13:52:52 +03001034 break;
1035 default:
Ray Milkey986a47a2018-01-25 11:38:51 -08001036 throw new IllegalArgumentException("Un recognize OduClt speed: " + portSpeedInMbps.toString());
Yafit Hadara9a73de2015-09-06 13:52:52 +03001037 }
1038
1039 SparseAnnotations annotations = buildOduCltAnnotation(port);
HIGUCHI Yuta4c0ef6b2016-05-02 19:45:41 -07001040 return oduCltPortDescription(portNo, enabled, sigType, annotations);
Yafit Hadara9a73de2015-09-06 13:52:52 +03001041 }
1042
1043 private SparseAnnotations buildOduCltAnnotation(OFPortDesc port) {
1044 SparseAnnotations annotations = null;
1045 String portName = Strings.emptyToNull(port.getName());
1046 if (portName != null) {
1047 annotations = DefaultAnnotations.builder()
1048 .set(AnnotationKeys.PORT_NAME, portName)
1049 .set(AnnotationKeys.STATIC_PORT, Boolean.TRUE.toString()).build();
1050 }
1051 return annotations;
1052 }
1053
Jimmy Yan4deb03b2016-06-24 10:53:54 -07001054 private PortDescription buildPortDescription(PortDescPropertyType ptype, OFObject port,
1055 OpenFlowOpticalSwitch opsw) {
yjimmyy646aa022016-07-05 12:09:50 -07001056 if (port instanceof OFPortOptical) {
Jimmy Yan4deb03b2016-06-24 10:53:54 -07001057 return buildPortDescription(ptype, (OFPortOptical) port, opsw);
Yafit Hadara9a73de2015-09-06 13:52:52 +03001058 }
1059 return buildPortDescription(ptype, (OFExpPort) port);
1060 }
1061
Rimon Ashkenazy8ebfff02016-02-01 11:56:36 +02001062 private boolean matchingOtuPortSignalTypes(OFPortOpticalTransportSignalType sigType,
1063 OduSignalType oduSignalType) {
1064 switch (sigType) {
1065 case OTU2:
1066 if (oduSignalType == OduSignalType.ODU2) {
1067 return true;
1068 }
1069 break;
1070 case OTU4:
1071 if (oduSignalType == OduSignalType.ODU4) {
1072 return true;
1073 }
1074 break;
1075 default:
1076 break;
1077 }
1078 return false;
1079 }
Yafit Hadara9a73de2015-09-06 13:52:52 +03001080 /**
1081 * Build a portDescription from a given a port description describing some
1082 * Optical port.
1083 *
1084 * @param ptype description property type.
1085 * @param port the port to build from.
1086 * @return portDescription for the port.
1087 */
1088 private PortDescription buildPortDescription(PortDescPropertyType ptype, OFExpPort port) {
1089 PortNumber portNo = PortNumber.portNumber(port.getPortNo().getPortNumber());
1090 boolean enabled = !port.getState().contains(OFPortState.LINK_DOWN)
1091 && !port.getConfig().contains(OFPortConfig.PORT_DOWN);
mskalac584bd92017-11-28 15:51:17 +01001092 boolean adminDown = port.getConfig().contains(OFPortConfig.PORT_DOWN);
1093 SparseAnnotations annotations = makePortAnnotation(port.getName(),
1094 port.getHwAddr().toString(),
1095 adminDown).build();
Yafit Hadara9a73de2015-09-06 13:52:52 +03001096
1097 OFExpPortDescPropOpticalTransport firstProp = port.getProperties().get(0);
1098 OFPortOpticalTransportSignalType sigType = firstProp.getPortSignalType();
1099
HIGUCHI Yuta34a3f692016-01-09 21:08:57 -08001100 PortDescription portDes = null;
Yafit Hadara9a73de2015-09-06 13:52:52 +03001101 switch (sigType) {
1102 case OMSN:
HIGUCHI Yuta95d83e82016-04-26 12:13:48 -07001103 portDes = omsPortDescription(portNo, enabled,
Rimon Ashkenazye3201032016-01-11 14:27:30 +02001104 FREQ191_7, FREQ191_7.add(FREQ4_4), FREQ50, annotations);
Yafit Hadara9a73de2015-09-06 13:52:52 +03001105 break;
1106 case OCH:
1107 OFExpPortOpticalTransportLayerEntry entry = firstProp.getFeatures().get(0).getValue().get(0);
1108 OFPortOpticalTransportLayerClass layerClass = entry.getLayerClass();
1109 if (!OFPortOpticalTransportLayerClass.ODU.equals(layerClass)) {
1110 LOG.error("Unsupported layer Class {} ", layerClass);
1111 return null;
1112 }
1113
1114 // convert to ONOS OduSignalType
1115 OduSignalType oduSignalType = OpenFlowDeviceValueMapper.
1116 lookupOduSignalType((byte) entry.getSignalType());
1117 //OchSignal is needed for OchPortDescription constructor,
1118 //yet not relevant for tunable OCH port, creating with default parameters
1119 OchSignal signalId = new OchSignal(GridType.DWDM, ChannelSpacing.CHL_50GHZ, 1, 1);
1120
HIGUCHI Yuta34a3f692016-01-09 21:08:57 -08001121 portDes = ochPortDescription(portNo, enabled,
1122 oduSignalType, true,
1123 signalId, annotations);
Yafit Hadara9a73de2015-09-06 13:52:52 +03001124
1125 break;
1126 case OTU2:
1127 case OTU4:
Rimon Ashkenazy8ebfff02016-02-01 11:56:36 +02001128 entry = firstProp.getFeatures().get(0).getValue().get(0);
1129 layerClass = entry.getLayerClass();
1130 if (!OFPortOpticalTransportLayerClass.ODU.equals(layerClass)) {
1131 LOG.error("Unsupported layer Class {} ", layerClass);
1132 return null;
1133 }
1134
1135 // convert to ONOS OduSignalType
1136 OduSignalType oduSignalTypeOtuPort = OpenFlowDeviceValueMapper.
1137 lookupOduSignalType((byte) entry.getSignalType());
1138 if (!matchingOtuPortSignalTypes(sigType, oduSignalTypeOtuPort)) {
1139 LOG.error("Wrong oduSignalType {} for OTU Port sigType {} ", oduSignalTypeOtuPort, sigType);
1140 return null;
1141 }
1142 OtuSignalType otuSignalType =
1143 ((sigType == OFPortOpticalTransportSignalType.OTU2) ? OtuSignalType.OTU2 :
1144 OtuSignalType.OTU4);
HIGUCHI Yuta5be3e822016-05-03 13:51:42 -07001145 portDes = otuPortDescription(portNo, enabled, otuSignalType, annotations);
Rimon Ashkenazy8ebfff02016-02-01 11:56:36 +02001146 break;
Yafit Hadara9a73de2015-09-06 13:52:52 +03001147 default:
1148 break;
1149 }
1150
1151 return portDes;
1152 }
1153
alshabiba14f3642014-09-05 09:31:31 -07001154 /**
Yuta HIGUCHI2341e602017-03-08 20:10:08 -08001155 * Creates an annotation builder for the port name if one is available.
Ray Milkeye0fade72015-01-15 13:29:47 -08001156 *
lishuai4ee42042015-11-30 17:19:21 +08001157 * @param portName the port name
1158 * @param portMac the port mac
mskalac584bd92017-11-28 15:51:17 +01001159 * @param adminDown the port admin state
1160 * @return annotation builder containing port admin state, port name
1161 * and/or port MAC if any of the two is found
Ray Milkeye0fade72015-01-15 13:29:47 -08001162 */
mskalac584bd92017-11-28 15:51:17 +01001163 private DefaultAnnotations.Builder makePortAnnotation(String portName, String portMac, boolean adminDown) {
Andrea Campanellac8b871a2017-01-19 13:53:05 -08001164 DefaultAnnotations.Builder builder = DefaultAnnotations.builder();
lishuai4ee42042015-11-30 17:19:21 +08001165 String pName = Strings.emptyToNull(portName);
1166 String pMac = Strings.emptyToNull(portMac);
Andrea Campanellac8b871a2017-01-19 13:53:05 -08001167 if (pName != null) {
1168 builder.set(AnnotationKeys.PORT_NAME, pName);
Ray Milkeye0fade72015-01-15 13:29:47 -08001169 }
Andrea Campanellac8b871a2017-01-19 13:53:05 -08001170 if (pMac != null) {
1171 builder.set(AnnotationKeys.PORT_MAC, pMac);
1172 }
mskalac584bd92017-11-28 15:51:17 +01001173 String adminState = adminDown ? "disabled" : "enabled";
1174 builder.set(AnnotationKeys.ADMIN_STATE, adminState);
Yuta HIGUCHI2341e602017-03-08 20:10:08 -08001175 return builder;
1176 }
1177
Yuta HIGUCHI2341e602017-03-08 20:10:08 -08001178 private PortDescription buildPortDescription14(OFPortDesc port) {
1179 PortNumber portNo = PortNumber.portNumber(port.getPortNo().getPortNumber());
1180 boolean enabled =
1181 !port.getState().contains(OFPortState.LINK_DOWN) &&
1182 !port.getConfig().contains(OFPortConfig.PORT_DOWN);
mskalac584bd92017-11-28 15:51:17 +01001183 boolean adminDown = port.getConfig().contains(OFPortConfig.PORT_DOWN);
Yuta HIGUCHI2341e602017-03-08 20:10:08 -08001184 Builder annotations = makePortAnnotation(port.getName(),
mskalac584bd92017-11-28 15:51:17 +01001185 port.getHwAddr().toString(),
1186 adminDown);
Yuta HIGUCHI2341e602017-03-08 20:10:08 -08001187
1188 Optional<OFPortDescPropEthernet> ether = port.getProperties().stream()
1189 .filter(OFPortDescPropEthernet.class::isInstance)
1190 .map(OFPortDescPropEthernet.class::cast)
1191 .findAny();
1192 if (ether.isPresent()) {
1193 // ethernet port
1194 // TODO parse other part of OFPortDescPropEthernet if necessary
Yuta HIGUCHI53e47962018-03-01 23:50:48 -08001195 return DefaultPortDescription.builder()
1196 .withPortNumber(portNo)
1197 .isEnabled(enabled)
1198 .type(COPPER)
1199 .portSpeed(portSpeed(port))
1200 .annotations(annotations.build())
1201 .build();
Yuta HIGUCHI2341e602017-03-08 20:10:08 -08001202 }
1203
1204 Optional<OFPortDescPropOptical> optical = port.getProperties().stream()
1205 .filter(OFPortDescPropOptical.class::isInstance)
1206 .map(OFPortDescPropOptical.class::cast)
1207 .findAny();
1208 if (optical.isPresent()) {
1209 // optical port
1210
Yuta HIGUCHIf42a2cc2017-05-02 17:04:31 -07001211 // FIXME is there a OF version neutral way to access
1212 // OFOpticalPortFeaturesSerializerVer14
Yuta HIGUCHI2341e602017-03-08 20:10:08 -08001213
Laszlo Papp20cfb6f2018-01-08 16:06:53 +00001214 long supported = optical.get().getSupported();
1215 long rxMin = optical.get().getRxMinFreqLmda();
1216 long rxMax = optical.get().getRxMaxFreqLmda();
1217 long rxGrid = optical.get().getRxGridFreqLmda();
1218 long txMin = optical.get().getTxMinFreqLmda();
1219 long txMax = optical.get().getTxMaxFreqLmda();
1220 long txGrid = optical.get().getTxGridFreqLmda();
1221
1222 int txTune = OFOpticalPortFeaturesSerializerVer14.TX_TUNE_VAL;
1223 int rxTune = OFOpticalPortFeaturesSerializerVer14.RX_TUNE_VAL;
1224 annotations.set(AK_TX_TUNE_FEATURE,
1225 ((supported & txTune) != 0) ? "enabled" : "disabled");
1226 annotations.set(AK_RX_TUNE_FEATURE,
1227 ((supported & rxTune) != 0) ? "enabled" : "disabled");
1228
Yuta HIGUCHI2341e602017-03-08 20:10:08 -08001229 // wire value for OFOpticalPortFeatures.USE_FREQ
Laszlo Papp8e8ff052018-01-16 09:55:45 +00001230 boolean useFreq = (supported & OFOpticalPortFeaturesSerializerVer14.USE_FREQ_VAL) != 0;
1231 annotations.set(AK_USE_FREQ_FEATURE, useFreq ? "enabled" : "disabled");
Yuta HIGUCHI2341e602017-03-08 20:10:08 -08001232
Laszlo Pappbf2cd742018-01-16 11:49:04 +00001233 annotations.set(propFreq ? AK_RX_MIN_FREQ_HZ : AK_RX_MIN_LMDA_NM,
1234 freqLmdaToAnnotation(rxMin, useFreq));
1235 annotations.set(propFreq ? AK_RX_MAX_FREQ_HZ : AK_RX_MAX_LMDA_NM,
1236 freqLmdaToAnnotation(rxMax, useFreq));
1237 annotations.set(propFreq ? AK_RX_GRID_HZ : AK_RX_GRID_LMDA_NM,
1238 freqLmdaToAnnotation(rxGrid, useFreq));
Yuta HIGUCHI2341e602017-03-08 20:10:08 -08001239
Laszlo Pappbf2cd742018-01-16 11:49:04 +00001240 annotations.set(propFreq ? AK_TX_MIN_FREQ_HZ : AK_TX_MIN_LMDA_NM,
1241 freqLmdaToAnnotation(txMin, useFreq));
1242 annotations.set(propFreq ? AK_TX_MAX_FREQ_HZ : AK_TX_MAX_LMDA_NM,
1243 freqLmdaToAnnotation(txMax, useFreq));
1244 annotations.set(propFreq ? AK_TX_GRID_HZ : AK_TX_GRID_LMDA_NM,
1245 freqLmdaToAnnotation(txGrid, useFreq));
Yuta HIGUCHI2341e602017-03-08 20:10:08 -08001246
Laszlo Papp8e8ff052018-01-16 09:55:45 +00001247 // FIXME pretty confident this is not going to happen
1248 // unless Device models Tx/Rx ports as separate port
1249 if (rxMin == txMin) {
Laszlo Pappbf2cd742018-01-16 11:49:04 +00001250 annotations.set(propFreq ? AK_MIN_FREQ_HZ : AK_MIN_LMDA_NM,
Laszlo Papp8e8ff052018-01-16 09:55:45 +00001251 freqLmdaToAnnotation(rxMin, useFreq));
1252 }
1253 if (rxMax == txMax) {
Laszlo Pappbf2cd742018-01-16 11:49:04 +00001254 annotations.set(propFreq ? AK_MAX_FREQ_HZ : AK_MAX_LMDA_NM,
Laszlo Papp8e8ff052018-01-16 09:55:45 +00001255 freqLmdaToAnnotation(rxMax, useFreq));
1256 }
1257 if (rxGrid == txGrid) {
Laszlo Pappbf2cd742018-01-16 11:49:04 +00001258 annotations.set(propFreq ? AK_GRID_HZ : AK_GRID_LMDA_NM,
Laszlo Papp8e8ff052018-01-16 09:55:45 +00001259 freqLmdaToAnnotation(rxGrid, useFreq));
Yuta HIGUCHI2341e602017-03-08 20:10:08 -08001260 }
Laszlo Papp20cfb6f2018-01-08 16:06:53 +00001261
1262 int txPwr = OFOpticalPortFeaturesSerializerVer14.TX_PWR_VAL;
1263 long txPwrMin = optical.get().getTxPwrMin();
1264 long txPwrMax = optical.get().getTxPwrMax();
1265 annotations.set(AK_TX_PWR_FEATURE, ((supported & txPwr) != 0) ? "enabled" : "disabled");
1266 annotations.set(AK_TX_PWR_MIN, Long.toString(txPwrMin));
1267 annotations.set(AK_TX_PWR_MAX, Long.toString(txPwrMax));
Yuta HIGUCHI2341e602017-03-08 20:10:08 -08001268
1269 // TODO How to determine appropriate port type?
1270
Yuta HIGUCHI53e47962018-03-01 23:50:48 -08001271 return DefaultPortDescription.builder()
1272 .withPortNumber(portNo)
1273 .isEnabled(enabled)
1274 .type(FIBER)
1275 .portSpeed(portSpeed(port))
1276 .annotations(annotations.build())
1277 .build();
Yuta HIGUCHI2341e602017-03-08 20:10:08 -08001278 }
1279
1280 // fall back default
Yuta HIGUCHI53e47962018-03-01 23:50:48 -08001281 return DefaultPortDescription.builder()
1282 .withPortNumber(portNo)
1283 .isEnabled(enabled)
1284 .type(COPPER)
1285 .portSpeed(portSpeed(port))
1286 .annotations(annotations.build())
1287 .build();
Yuta HIGUCHI2341e602017-03-08 20:10:08 -08001288
Ray Milkeye0fade72015-01-15 13:29:47 -08001289 }
1290
1291 /**
Ayaka Koshibe5460d622015-05-14 12:19:19 -07001292 * Build a portDescription from a given Ethernet port description.
tomff7eb7c2014-09-08 12:49:03 -07001293 *
alshabiba14f3642014-09-05 09:31:31 -07001294 * @param port the port to build from.
1295 * @return portDescription for the port.
1296 */
1297 private PortDescription buildPortDescription(OFPortDesc port) {
Yuta HIGUCHI2341e602017-03-08 20:10:08 -08001298 if (port.getVersion().wireVersion >= OFVersion.OF_14.getWireVersion()) {
1299 return buildPortDescription14(port);
1300 }
Thomas Vachuskad16ce182014-10-29 17:25:29 -07001301 PortNumber portNo = PortNumber.portNumber(port.getPortNo().getPortNumber());
1302 boolean enabled =
1303 !port.getState().contains(OFPortState.LINK_DOWN) &&
1304 !port.getConfig().contains(OFPortConfig.PORT_DOWN);
1305 Port.Type type = port.getCurr().contains(OFPortFeatures.PF_FIBER) ? FIBER : COPPER;
mskalac584bd92017-11-28 15:51:17 +01001306 boolean adminDown = port.getConfig().contains(OFPortConfig.PORT_DOWN);
1307 SparseAnnotations annotations = makePortAnnotation(port.getName(),
1308 port.getHwAddr().toString(),
1309 adminDown).build();
Yuta HIGUCHI53e47962018-03-01 23:50:48 -08001310 return DefaultPortDescription.builder()
1311 .withPortNumber(portNo)
1312 .isEnabled(enabled)
1313 .type(type)
1314 .portSpeed(portSpeed(port))
1315 .annotations(annotations)
1316 .build();
alshabiba14f3642014-09-05 09:31:31 -07001317 }
Ayaka Koshibeab91cc42014-09-25 10:20:52 -07001318
Ayaka Koshibe5460d622015-05-14 12:19:19 -07001319 /**
1320 * Build a portDescription from a given a port description describing some
1321 * Optical port.
1322 *
1323 * @param port description property type.
1324 * @param port the port to build from.
1325 * @return portDescription for the port.
1326 */
Jimmy Yan4deb03b2016-06-24 10:53:54 -07001327 private PortDescription buildPortDescription(PortDescPropertyType ptype, OFPortOptical port,
1328 OpenFlowOpticalSwitch opsw) {
Jon Hallcbd1b392017-01-18 20:15:44 -08001329 checkArgument(!port.getDesc().isEmpty());
Marc De Leenheerfc913dd2015-07-30 16:04:55 -07001330
Ayaka Koshibe5460d622015-05-14 12:19:19 -07001331 // Minimally functional fixture. This needs to be fixed as we add better support.
1332 PortNumber portNo = PortNumber.portNumber(port.getPortNo().getPortNumber());
Ayaka Koshibe74b55272015-05-28 15:16:04 -07001333
Ayaka Koshibe5460d622015-05-14 12:19:19 -07001334 boolean enabled = !port.getState().contains(OFPortState.LINK_DOWN)
1335 && !port.getConfig().contains(OFPortConfig.PORT_DOWN);
mskalac584bd92017-11-28 15:51:17 +01001336 boolean adminDown = port.getConfig().contains(OFPortConfig.PORT_DOWN);
1337 SparseAnnotations annotations = makePortAnnotation(port.getName(),
1338 port.getHwAddr().toString(),
1339 adminDown).build();
Ayaka Koshibe5460d622015-05-14 12:19:19 -07001340
1341 if (port.getVersion() == OFVersion.OF_13
1342 && ptype == PortDescPropertyType.OPTICAL_TRANSPORT) {
1343 // At this point, not much is carried in the optical port message.
Ayaka Koshibeae541732015-05-19 13:37:27 -07001344 LOG.debug("Optical transport port message {}", port.toString());
Ayaka Koshibe5460d622015-05-14 12:19:19 -07001345 } else {
1346 // removable once 1.4+ support complete.
Ayaka Koshibeae541732015-05-19 13:37:27 -07001347 LOG.debug("Unsupported optical port properties");
Ayaka Koshibe5460d622015-05-14 12:19:19 -07001348 }
Marc De Leenheerfc913dd2015-07-30 16:04:55 -07001349
1350 OFPortDescPropOpticalTransport desc = port.getDesc().get(0);
1351 switch (desc.getPortSignalType()) {
1352 // FIXME: use constants once loxi has full optical extensions
1353 case 2: // OMS port
1354 // Assume complete optical spectrum and 50 GHz grid
Jimmy Yan4deb03b2016-06-24 10:53:54 -07001355 Set<OchSignal> signals = null;
1356 if (opsw instanceof HandlerBehaviour) {
1357 DriverHandler driverHandler = ((HandlerBehaviour) opsw).handler();
1358 if (driverHandler != null && driverHandler.hasBehaviour(LambdaQuery.class)) {
1359 try {
1360 signals = driverHandler.behaviour(LambdaQuery.class).queryLambdas(portNo);
1361 } catch (NullPointerException e) {
1362 signals = null;
1363 }
1364 }
1365 }
1366 Frequency minFreq;
1367 Frequency maxFreq;
1368 Frequency channelSpacing;
1369 if (signals == null || signals.isEmpty()) {
1370 minFreq = Spectrum.U_BAND_MIN;
1371 maxFreq = Spectrum.O_BAND_MAX;
1372 channelSpacing = Frequency.ofGHz(50);
1373 } else {
1374 Comparator<OchSignal> compare =
1375 (OchSignal a, OchSignal b) -> a.spacingMultiplier() - b.spacingMultiplier();
1376 OchSignal minOch = Collections.min(signals, compare);
1377 OchSignal maxOch = Collections.max(signals, compare);
1378 minFreq = minOch.centralFrequency();
1379 maxFreq = maxOch.centralFrequency();
1380 channelSpacing = minOch.channelSpacing().frequency();
1381 }
1382 return omsPortDescription(portNo, enabled, minFreq,
1383 maxFreq, channelSpacing, annotations);
Marc De Leenheerfc913dd2015-07-30 16:04:55 -07001384 case 5: // OCH port
1385 OchSignal signal = new OchSignal(GridType.DWDM, ChannelSpacing.CHL_50GHZ, 0, 4);
HIGUCHI Yuta34a3f692016-01-09 21:08:57 -08001386 return ochPortDescription(portNo, enabled, OduSignalType.ODU4,
Marc De Leenheerfc913dd2015-07-30 16:04:55 -07001387 true, signal, annotations);
1388 default:
1389 break;
1390 }
1391
Yuta HIGUCHI53e47962018-03-01 23:50:48 -08001392 return DefaultPortDescription.builder()
1393 .withPortNumber(portNo)
1394 .isEnabled(enabled)
1395 .type(FIBER)
1396 .portSpeed(0)
1397 .annotations(annotations)
1398 .build();
Ayaka Koshibe5460d622015-05-14 12:19:19 -07001399 }
1400
Marc De Leenheerb9311372015-07-09 11:36:49 -07001401 /**
1402 * Build a portDescription from a given port description describing a fiber switch optical port.
1403 *
1404 * @param port description property type.
1405 * @param port the port to build from.
1406 * @return portDescription for the port.
1407 */
1408 private PortDescription buildPortDescription(OFCalientPortDescStatsEntry port) {
1409 PortNumber portNo = PortNumber.portNumber(port.getPortNo().getPortNumber());
1410
Marc De Leenheerc662d322016-02-18 16:05:10 -08001411 // Use the alias name if it's available
1412 String name = port.getName();
1413 List<OFCalientPortDescProp> props = port.getProperties();
Jon Hallcbd1b392017-01-18 20:15:44 -08001414 if (props != null && !props.isEmpty()) {
Marc De Leenheerc662d322016-02-18 16:05:10 -08001415 OFCalientPortDescPropOptical propOptical = (OFCalientPortDescPropOptical) props.get(0);
1416 if (propOptical != null) {
1417 name = propOptical.getInAlias();
1418 }
1419 }
1420
Marc De Leenheerb9311372015-07-09 11:36:49 -07001421 // FIXME when Calient OF agent reports port status
1422 boolean enabled = true;
mskalac584bd92017-11-28 15:51:17 +01001423 boolean adminDown = false;
1424 SparseAnnotations annotations = makePortAnnotation(name, port.getHwAddr().toString(), adminDown).build();
Marc De Leenheerb9311372015-07-09 11:36:49 -07001425
Marc De Leenheerfc913dd2015-07-30 16:04:55 -07001426 // S160 data sheet
1427 // Wavelength range: 1260 - 1630 nm, grid is irrelevant for this type of switch
HIGUCHI Yuta95d83e82016-04-26 12:13:48 -07001428 return omsPortDescription(portNo, enabled,
Yuta HIGUCHIf42a2cc2017-05-02 17:04:31 -07001429 Spectrum.O_BAND_MIN, Spectrum.O_BAND_MAX, Frequency.ofGHz(50), annotations);
Marc De Leenheerb9311372015-07-09 11:36:49 -07001430 }
1431
alshabibafc514a2014-12-01 14:44:05 -08001432 private PortDescription buildPortDescription(OFPortStatus status) {
1433 OFPortDesc port = status.getDesc();
1434 if (status.getReason() != OFPortReason.DELETE) {
1435 return buildPortDescription(port);
1436 } else {
Yuta HIGUCHI6512f3e2017-05-18 17:21:24 -07001437 PortDescription desc = buildPortDescription(port);
1438 if (desc.isEnabled()) {
1439 return DefaultPortDescription.builder(desc)
1440 .isEnabled(false)
1441 .build();
1442 }
1443 return desc;
alshabibafc514a2014-12-01 14:44:05 -08001444 }
1445 }
1446
Yuta HIGUCHI2341e602017-03-08 20:10:08 -08001447 /**
1448 * Returns port speed in Mbps.
1449 *
1450 * @param port description to parse
1451 * @return port speed in Mbps
1452 */
Thomas Vachuskad16ce182014-10-29 17:25:29 -07001453 private long portSpeed(OFPortDesc port) {
Yuta HIGUCHI2341e602017-03-08 20:10:08 -08001454 if (port.getVersion().getWireVersion() >= OFVersion.OF_14.getWireVersion()) {
1455 // OFPortDescPropEthernet
1456 return port.getProperties().stream()
1457 .filter(OFPortDescPropEthernet.class::isInstance)
1458 .map(OFPortDescPropEthernet.class::cast)
1459 .mapToLong(OFPortDescPropEthernet::getCurrSpeed)
1460 .map(kbps -> kbps / KBPS)
1461 .findAny()
1462 .orElse(PortSpeed.SPEED_NONE.getSpeedBps() / MBPS);
1463 }
Thomas Vachuskad16ce182014-10-29 17:25:29 -07001464 if (port.getVersion() == OFVersion.OF_13) {
Charles Chan25b77322015-12-13 01:00:56 -08001465 // Note: getCurrSpeed() returns a value in kbps (this also applies to OF_11 and OF_12)
1466 return port.getCurrSpeed() / KBPS;
Thomas Vachuskad16ce182014-10-29 17:25:29 -07001467 }
Yuta HIGUCHI2341e602017-03-08 20:10:08 -08001468 // < OF1.3
Thomas Vachuskad16ce182014-10-29 17:25:29 -07001469 PortSpeed portSpeed = PortSpeed.SPEED_NONE;
1470 for (OFPortFeatures feat : port.getCurr()) {
1471 portSpeed = PortSpeed.max(portSpeed, feat.getPortSpeed());
1472 }
Thomas Vachuska98eda532014-10-29 17:31:02 -07001473 return portSpeed.getSpeedBps() / MBPS;
Thomas Vachuskad16ce182014-10-29 17:25:29 -07001474 }
sangho538108b2015-04-08 14:29:20 -07001475
1476 @Override
1477 public void handleMessage(Dpid dpid, OFMessage msg) {
Ray Milkey7abe50b2016-04-13 11:57:44 -07001478 if (isDisabled) {
1479 return;
sangho538108b2015-04-08 14:29:20 -07001480 }
Ray Milkey7abe50b2016-04-13 11:57:44 -07001481
1482 try {
1483 switch (msg.getType()) {
1484 case STATS_REPLY:
1485 if (((OFStatsReply) msg).getStatsType() == OFStatsType.PORT) {
1486 OFPortStatsReply portStatsReply = (OFPortStatsReply) msg;
1487 List<OFPortStatsEntry> portStatsReplyList = portStatsReplies.get(dpid);
1488 if (portStatsReplyList == null) {
Madan Jampani84382b92016-06-22 08:26:49 -07001489 portStatsReplyList = Lists.newCopyOnWriteArrayList();
Ray Milkey7abe50b2016-04-13 11:57:44 -07001490 }
1491 portStatsReplyList.addAll(portStatsReply.getEntries());
1492 portStatsReplies.put(dpid, portStatsReplyList);
1493 if (!portStatsReply.getFlags().contains(OFStatsReplyFlags.REPLY_MORE)) {
Charles Chanb3ef0db2016-05-03 11:11:00 -07001494 List<OFPortStatsEntry> statsEntries = portStatsReplies.get(dpid);
1495 if (statsEntries != null) {
1496 pushPortMetrics(dpid, statsEntries);
1497 statsEntries.clear();
1498 }
Ray Milkey7abe50b2016-04-13 11:57:44 -07001499 }
yjimmyy646aa022016-07-05 12:09:50 -07001500 } else if (((OFStatsReply) msg).getStatsType() == OFStatsType.EXPERIMENTER) {
1501 OpenFlowSwitch sw = controller.getSwitch(dpid);
sangyun-han147e6f42016-07-26 17:14:41 +09001502 if (sw == null) {
1503 LOG.error("Switch {} is not found", dpid);
1504 break;
1505 }
yjimmyy646aa022016-07-05 12:09:50 -07001506 if (sw instanceof OpenFlowOpticalSwitch) {
1507 // Optical switch uses experimenter stats message to update power
1508 List<PortDescription> portDescs =
1509 ((OpenFlowOpticalSwitch) sw).processExpPortStats(msg);
1510 if (!portDescs.isEmpty()) {
1511 providerService.updatePorts(DeviceId.deviceId(Dpid.uri(dpid)), portDescs);
1512 }
1513 }
Ray Milkey7abe50b2016-04-13 11:57:44 -07001514 }
1515 break;
1516 case ERROR:
1517 if (((OFErrorMsg) msg).getErrType() == OFErrorType.PORT_MOD_FAILED) {
1518 LOG.error("port mod failed");
1519 }
Ray Milkeyd6a67c32018-02-02 10:30:35 -08001520 break;
Ray Milkey7abe50b2016-04-13 11:57:44 -07001521 default:
1522 break;
1523 }
1524 } catch (IllegalStateException e) {
1525 // system is shutting down and the providerService is no longer
1526 // valid. Messages cannot be processed.
1527 }
1528 }
1529
1530 private void disable() {
1531 isDisabled = true;
sangho538108b2015-04-08 14:29:20 -07001532 }
alshabibf1216ed2014-09-03 11:53:54 -07001533 }
Saurav Dasa2d37502016-03-25 17:50:40 -07001534
Thomas Vachuska164ecf62018-05-08 17:29:55 -07001535 class InternalDeviceListener implements DeviceListener {
Saurav Dasa2d37502016-03-25 17:50:40 -07001536
Thomas Vachuska164ecf62018-05-08 17:29:55 -07001537 @Override
1538 public boolean isRelevant(DeviceEvent event) {
1539 return event.subject().type() != CONTROLLER && event.type() == DEVICE_REMOVED
1540 && event.subject().id().uri().getScheme().equals(SCHEME);
1541 }
1542
1543 @Override
1544 public void event(DeviceEvent event) {
1545 DeviceId deviceId = event.subject().id();
1546 Dpid dpid = dpid(deviceId.uri());
1547 OpenFlowSwitch sw = controller.getSwitch(dpid);
1548 if (sw != null) {
1549 LOG.debug("Forcing disconnect for device {}", deviceId);
1550 PortStatsCollector portStatsCollector = collectors.remove(dpid);
1551 if (portStatsCollector != null) {
1552 portStatsCollector.stop();
1553 }
1554 sw.disconnectSwitch();
1555 }
1556 }
1557 }
tomb5a46e62014-08-26 14:20:00 -07001558}