blob: f3dd6e024b2970acdb20ffd8b7da982ade3d6502 [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;
55import org.onosproject.net.device.DeviceProvider;
56import org.onosproject.net.device.DeviceProviderRegistry;
57import org.onosproject.net.device.DeviceProviderService;
58import org.onosproject.net.device.PortDescription;
sangho538108b2015-04-08 14:29:20 -070059import org.onosproject.net.device.PortStatistics;
Victor Silvaac8dab02016-11-28 16:33:08 -030060import org.onosproject.net.driver.Driver;
Jimmy Yan4deb03b2016-06-24 10:53:54 -070061import org.onosproject.net.driver.DriverHandler;
Victor Silvaac8dab02016-11-28 16:33:08 -030062import org.onosproject.net.driver.DriverService;
Jimmy Yan4deb03b2016-06-24 10:53:54 -070063import org.onosproject.net.driver.HandlerBehaviour;
Brian O'Connorabafb502014-12-02 22:26:20 -080064import org.onosproject.net.provider.AbstractProvider;
65import org.onosproject.net.provider.ProviderId;
66import org.onosproject.openflow.controller.Dpid;
67import org.onosproject.openflow.controller.OpenFlowController;
sangho538108b2015-04-08 14:29:20 -070068import org.onosproject.openflow.controller.OpenFlowEventListener;
Ayaka Koshibe5460d622015-05-14 12:19:19 -070069import org.onosproject.openflow.controller.OpenFlowOpticalSwitch;
Brian O'Connorabafb502014-12-02 22:26:20 -080070import org.onosproject.openflow.controller.OpenFlowSwitch;
71import org.onosproject.openflow.controller.OpenFlowSwitchListener;
Ayaka Koshibe5460d622015-05-14 12:19:19 -070072import org.onosproject.openflow.controller.PortDescPropertyType;
Brian O'Connorabafb502014-12-02 22:26:20 -080073import org.onosproject.openflow.controller.RoleState;
Dusan Pajinbab8a5e2015-07-24 17:37:19 +020074import org.osgi.service.component.ComponentContext;
Marc De Leenheerc662d322016-02-18 16:05:10 -080075import org.projectfloodlight.openflow.protocol.OFCalientPortDescProp;
76import org.projectfloodlight.openflow.protocol.OFCalientPortDescPropOptical;
Marc De Leenheerb9311372015-07-09 11:36:49 -070077import org.projectfloodlight.openflow.protocol.OFCalientPortDescStatsEntry;
Laszlo Papp58174812018-01-16 13:29:47 +000078import org.projectfloodlight.openflow.protocol.OFCapabilities;
Saurav Dasa2d37502016-03-25 17:50:40 -070079import org.projectfloodlight.openflow.protocol.OFErrorMsg;
80import org.projectfloodlight.openflow.protocol.OFErrorType;
Yafit Hadara9a73de2015-09-06 13:52:52 +030081import org.projectfloodlight.openflow.protocol.OFExpPort;
82import org.projectfloodlight.openflow.protocol.OFExpPortDescPropOpticalTransport;
83import org.projectfloodlight.openflow.protocol.OFExpPortOpticalTransportLayerEntry;
Ayaka Koshibee8708e32014-10-22 13:40:18 -070084import org.projectfloodlight.openflow.protocol.OFFactory;
sangho538108b2015-04-08 14:29:20 -070085import org.projectfloodlight.openflow.protocol.OFMessage;
Yafit Hadara9a73de2015-09-06 13:52:52 +030086import org.projectfloodlight.openflow.protocol.OFObject;
alshabib4680bb62014-09-04 17:15:08 -070087import org.projectfloodlight.openflow.protocol.OFPortConfig;
alshabib25c8eec2014-09-04 16:41:31 -070088import org.projectfloodlight.openflow.protocol.OFPortDesc;
Yuta HIGUCHI2341e602017-03-08 20:10:08 -080089import org.projectfloodlight.openflow.protocol.OFPortDescPropEthernet;
90import org.projectfloodlight.openflow.protocol.OFPortDescPropOptical;
Marc De Leenheerfc913dd2015-07-30 16:04:55 -070091import org.projectfloodlight.openflow.protocol.OFPortDescPropOpticalTransport;
Thomas Vachuskad16ce182014-10-29 17:25:29 -070092import org.projectfloodlight.openflow.protocol.OFPortFeatures;
Saurav Dasa2d37502016-03-25 17:50:40 -070093import org.projectfloodlight.openflow.protocol.OFPortMod;
Ayaka Koshibe5460d622015-05-14 12:19:19 -070094import org.projectfloodlight.openflow.protocol.OFPortOptical;
Yafit Hadara9a73de2015-09-06 13:52:52 +030095import org.projectfloodlight.openflow.protocol.OFPortOpticalTransportLayerClass;
96import org.projectfloodlight.openflow.protocol.OFPortOpticalTransportSignalType;
alshabibafc514a2014-12-01 14:44:05 -080097import org.projectfloodlight.openflow.protocol.OFPortReason;
alshabib4680bb62014-09-04 17:15:08 -070098import org.projectfloodlight.openflow.protocol.OFPortState;
sangho538108b2015-04-08 14:29:20 -070099import org.projectfloodlight.openflow.protocol.OFPortStatsEntry;
Laszlo Papp7cf60372018-01-11 00:06:43 +0000100import org.projectfloodlight.openflow.protocol.OFPortStatsPropOptical;
sangho538108b2015-04-08 14:29:20 -0700101import org.projectfloodlight.openflow.protocol.OFPortStatsReply;
alshabiba14f3642014-09-05 09:31:31 -0700102import org.projectfloodlight.openflow.protocol.OFPortStatus;
sangho538108b2015-04-08 14:29:20 -0700103import org.projectfloodlight.openflow.protocol.OFStatsReply;
Thomas Vachuska893bf4b2015-05-29 18:13:18 -0700104import org.projectfloodlight.openflow.protocol.OFStatsReplyFlags;
sangho538108b2015-04-08 14:29:20 -0700105import org.projectfloodlight.openflow.protocol.OFStatsType;
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700106import org.projectfloodlight.openflow.protocol.OFVersion;
Yuta HIGUCHIf42a2cc2017-05-02 17:04:31 -0700107import org.projectfloodlight.openflow.protocol.ver14.OFOpticalPortFeaturesSerializerVer14;
Laszlo Papp7cf60372018-01-11 00:06:43 +0000108import org.projectfloodlight.openflow.protocol.ver14.OFPortStatsOpticalFlagsSerializerVer14;
Saurav Dasa2d37502016-03-25 17:50:40 -0700109import org.projectfloodlight.openflow.types.OFPort;
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700110import org.projectfloodlight.openflow.types.PortSpeed;
tomb5a46e62014-08-26 14:20:00 -0700111import org.slf4j.Logger;
tom5f38b3a2014-08-27 23:50:54 -0700112
Marc De Leenheer8aba62f2017-04-25 14:33:37 -0700113import java.util.ArrayList;
114import java.util.Collection;
115import java.util.Collections;
116import java.util.Comparator;
117import java.util.Dictionary;
Jimmy Jine9b7a022016-08-12 16:56:48 -0700118import java.util.EnumSet;
Marc De Leenheer8aba62f2017-04-25 14:33:37 -0700119import java.util.HashMap;
120import java.util.HashSet;
121import java.util.List;
Yuta HIGUCHI2341e602017-03-08 20:10:08 -0800122import java.util.Optional;
Marc De Leenheer8aba62f2017-04-25 14:33:37 -0700123import java.util.Properties;
124import java.util.Set;
125import java.util.Timer;
126
127import static com.google.common.base.Preconditions.checkArgument;
128import static com.google.common.base.Strings.isNullOrEmpty;
129import static org.onlab.util.Tools.get;
130import static org.onosproject.net.DeviceId.deviceId;
131import static org.onosproject.net.Port.Type.COPPER;
132import static org.onosproject.net.Port.Type.FIBER;
133import static org.onosproject.net.optical.device.OchPortHelper.ochPortDescription;
134import static org.onosproject.net.optical.device.OduCltPortHelper.oduCltPortDescription;
135import static org.onosproject.net.optical.device.OmsPortHelper.omsPortDescription;
136import static org.onosproject.net.optical.device.OtuPortHelper.otuPortDescription;
137import static org.onosproject.openflow.controller.Dpid.dpid;
138import static org.onosproject.openflow.controller.Dpid.uri;
139import static org.slf4j.LoggerFactory.getLogger;
tom782a7cf2014-09-11 23:58:38 -0700140
tomb5a46e62014-08-26 14:20:00 -0700141/**
tomb1260e42014-08-26 18:39:57 -0700142 * Provider which uses an OpenFlow controller to detect network
tome06f8552014-08-26 16:58:42 -0700143 * infrastructure devices.
tomb5a46e62014-08-26 14:20:00 -0700144 */
tomb1260e42014-08-26 18:39:57 -0700145@Component(immediate = true)
tomab21e7c2014-08-26 15:23:08 -0700146public class OpenFlowDeviceProvider extends AbstractProvider implements DeviceProvider {
tomb5a46e62014-08-26 14:20:00 -0700147
alshabiba89cc582014-09-09 16:43:00 -0700148 private static final Logger LOG = getLogger(OpenFlowDeviceProvider.class);
Thomas Vachuskad07c0922015-10-06 14:48:06 -0700149
Yuta HIGUCHI2341e602017-03-08 20:10:08 -0800150 // TODO Some duplicate with one defined in OpticalAnnotations
151 // slice out optical specific handling and consolidate.
152 /**
153 * Annotation key for minimum frequency in Hz.
154 * Value is expected to be an integer.
155 */
156 public static final String AK_MIN_FREQ_HZ = "minFrequency";
157
158 /**
159 * Annotation key for maximum frequency in Hz.
160 * Value is expected be an integer.
161 */
162 public static final String AK_MAX_FREQ_HZ = "maxFrequency";
163
164 /**
165 * Annotation key for grid in Hz.
166 * Value is expected to be an integer.
167 */
168 public static final String AK_GRID_HZ = "grid";
169
Laszlo Papp20cfb6f2018-01-08 16:06:53 +0000170 /**
Yuta HIGUCHI2341e602017-03-08 20:10:08 -0800171 * Annotation key for minimum frequency in Hz.
172 * Value is expected to be an integer.
173 */
174 public static final String AK_TX_MIN_FREQ_HZ = "txMinFrequency";
175
176 /**
177 * Annotation key for maximum frequency in Hz.
178 * Value is expected be an integer.
179 */
180 public static final String AK_TX_MAX_FREQ_HZ = "txMaxFrequency";
181
182 /**
183 * Annotation key for grid in Hz.
184 * Value is expected to be an integer.
185 */
186 public static final String AK_TX_GRID_HZ = "txGrid";
187
188 /**
189 * Annotation key for minimum frequency in Hz.
190 * Value is expected to be an integer.
191 */
192 public static final String AK_RX_MIN_FREQ_HZ = "rxMinFrequency";
193
194 /**
195 * Annotation key for maximum frequency in Hz.
196 * Value is expected be an integer.
197 */
198 public static final String AK_RX_MAX_FREQ_HZ = "rxMaxFrequency";
199
200 /**
201 * Annotation key for grid in Hz.
202 * Value is expected to be an integer.
203 */
204 public static final String AK_RX_GRID_HZ = "rxGrid";
205
Laszlo Papp20cfb6f2018-01-08 16:06:53 +0000206 /**
207 * Annotation key for minimum transmit power in dBm*10.
208 * Value is expected to be an integer.
209 */
210 public static final String AK_TX_PWR_MIN = "txPowerMin";
211
212 /**
213 * Annotation key for maximum transmit power in dBm*10.
214 * Value is expected to be an integer.
215 */
216 public static final String AK_TX_PWR_MAX = "txPowerMax";
217
Laszlo Papp7cf60372018-01-11 00:06:43 +0000218
219
220 // Port Stats annotations
221
222 /**
223 * Annotation key for transmit frequency in Hz.
224 * Value is expected be an integer.
225 */
226 public static final String AK_TX_FREQ_HZ = "txFrequency";
227
228 /**
229 * Annotation key for transmit offset in Hz.
230 * Value is expected be an integer.
231 */
232 public static final String AK_TX_OFFSET_HZ = "txOffset";
233
234 /**
235 * Annotation key for transmit grid spacing in Hz.
236 * Value is expected be an integer.
237 */
238 public static final String AK_TX_GRID_SPAN_HZ = "txGridSpan";
239
240 /**
241 * Annotation key for receive frequency in Hz.
242 * Value is expected be an integer.
243 */
244 public static final String AK_RX_FREQ_HZ = "rxFrequency";
245
246 /**
247 * Annotation key for receive offset in Hz.
248 * Value is expected be an integer.
249 */
250 public static final String AK_RX_OFFSET_HZ = "rxOffset";
251
252 /**
253 * Annotation key for receive grid spacing in Hz.
254 * Value is expected be an integer.
255 */
256 public static final String AK_RX_GRID_SPAN_HZ = "rxGridSpan";
257
258 /**
259 * Annotation key for transmit power in dBm*10.
260 * Value is expected to be an integer.
261 */
262 public static final String AK_TX_PWR = "txPower";
263
264 /**
265 * Annotation key for receive power feature.
266 * Value is expected to be "enabled" or "disabled"
267 */
268 public static final String AK_RX_PWR_FEATURE = "rxPwrFeature";
269
270 /**
271 * Annotation key for receive power in dBm*10.
272 * Value is expected to be an integer.
273 */
274 public static final String AK_RX_PWR = "rxPower";
275
276 /**
277 * Annotation key for transmit bias feature.
278 * Value is expected to be "enabled" or "disabled"
279 */
280 public static final String AK_TX_BIAS_FEATURE = "txBiasFeature";
281
282 /**
283 * Annotation key for transmit bias current in mA*10.
284 * Value is expected to be an integer.
285 */
286 public static final String AK_BIAS_CURRENT = "biasCurrent";
287
288 /**
289 * Annotation key for transmit temperature feature.
290 * Value is expected to be "enabled" or "disabled"
291 */
292 public static final String AK_TX_TEMP_FEATURE = "txTempFeature";
293
294 /**
295 * Annotation key for transmit laser temperature in C*10.
296 * Value is expected to be an integer.
297 */
298 public static final String AK_TEMPERATURE = "temperature";
299
300
301 // Common feature annotations
302
303 /**
304 * Annotation key for transmit tune feature.
305 * Value is expected to be "enabled" or "disabled"
306 */
307 public static final String AK_TX_TUNE_FEATURE = "txTuneFeature";
308
309 /**
310 * Annotation key for receive tune feature.
311 * Value is expected to be "enabled" or "disabled"
312 */
313 public static final String AK_RX_TUNE_FEATURE = "rxTuneFeature";
314
315 /**
316 * Annotation key for transmit power feature.
317 * Value is expected to be "enabled" or "disabled"
318 */
319 public static final String AK_TX_PWR_FEATURE = "txPwrFeature";
320
321
Charles Chan25b77322015-12-13 01:00:56 -0800322 //TODO consider renaming KBPS and MBPS (as they are used to convert by division)
323 private static final long KBPS = 1_000;
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700324 private static final long MBPS = 1_000 * 1_000;
Rimon Ashkenazye3201032016-01-11 14:27:30 +0200325 private static final Frequency FREQ50 = Frequency.ofGHz(50);
326 private static final Frequency FREQ191_7 = Frequency.ofGHz(191_700);
Sho SHIMIZU00762ee2016-01-05 16:32:24 -0800327 private static final Frequency FREQ4_4 = Frequency.ofGHz(4_400);
tomb5a46e62014-08-26 14:20:00 -0700328
329 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
tom96dfcab2014-08-28 09:26:03 -0700330 protected DeviceProviderRegistry providerRegistry;
tomab21e7c2014-08-26 15:23:08 -0700331
tom5f38b3a2014-08-27 23:50:54 -0700332 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
333 protected OpenFlowController controller;
334
Dusan Pajinbab8a5e2015-07-24 17:37:19 +0200335 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
336 protected ComponentConfigService cfgService;
337
Victor Silvaac8dab02016-11-28 16:33:08 -0300338 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
339 protected DriverService driverService;
340
tomab21e7c2014-08-26 15:23:08 -0700341 private DeviceProviderService providerService;
tomb5a46e62014-08-26 14:20:00 -0700342
sangho538108b2015-04-08 14:29:20 -0700343 private final InternalDeviceProvider listener = new InternalDeviceProvider();
344
Simon Hunt7a307652016-06-23 01:17:29 -0700345 private static final String POLL_PROP_NAME = "portStatsPollFrequency";
346 private static final int POLL_INTERVAL = 5;
347 @Property(name = POLL_PROP_NAME, intValue = POLL_INTERVAL,
Dusan Pajinbab8a5e2015-07-24 17:37:19 +0200348 label = "Frequency (in seconds) for polling switch Port statistics")
349 private int portStatsPollFrequency = POLL_INTERVAL;
sangho538108b2015-04-08 14:29:20 -0700350
Victor Silvaff5871b2016-10-04 18:08:47 -0300351 private final Timer timer = new Timer("onos-openflow-portstats-collector");
sangyun-han07a572f2016-07-29 21:14:27 +0900352
sangho538108b2015-04-08 14:29:20 -0700353 private HashMap<Dpid, PortStatsCollector> collectors = Maps.newHashMap();
tomd40fc7a2014-09-04 16:41:10 -0700354
tomab21e7c2014-08-26 15:23:08 -0700355 /**
356 * Creates an OpenFlow device provider.
357 */
358 public OpenFlowDeviceProvider() {
Brian O'Connorabafb502014-12-02 22:26:20 -0800359 super(new ProviderId("of", "org.onosproject.provider.openflow"));
tomab21e7c2014-08-26 15:23:08 -0700360 }
361
tomb5a46e62014-08-26 14:20:00 -0700362 @Activate
Dusan Pajinbab8a5e2015-07-24 17:37:19 +0200363 public void activate(ComponentContext context) {
364 cfgService.registerProperties(getClass());
tom96dfcab2014-08-28 09:26:03 -0700365 providerService = providerRegistry.register(this);
tomd40fc7a2014-09-04 16:41:10 -0700366 controller.addListener(listener);
sangho538108b2015-04-08 14:29:20 -0700367 controller.addEventListener(listener);
Jian Li152b8852015-12-07 14:47:25 -0800368
Victor Silva7944d142016-07-08 00:14:55 -0300369 modified(context);
370
Thomas Vachuskad07c0922015-10-06 14:48:06 -0700371 connectInitialDevices();
alshabiba89cc582014-09-09 16:43:00 -0700372 LOG.info("Started");
tomb5a46e62014-08-26 14:20:00 -0700373 }
374
375 @Deactivate
Dusan Pajinbab8a5e2015-07-24 17:37:19 +0200376 public void deactivate(ComponentContext context) {
377 cfgService.unregisterProperties(getClass(), false);
Ray Milkey7abe50b2016-04-13 11:57:44 -0700378 listener.disable();
tomd40fc7a2014-09-04 16:41:10 -0700379 controller.removeListener(listener);
Thomas Vachuskad07c0922015-10-06 14:48:06 -0700380 providerRegistry.unregister(this);
Thomas Vachuska74bcd922015-06-02 13:08:57 -0700381 collectors.values().forEach(PortStatsCollector::stop);
Thomas Vachuskaa394b952016-06-14 15:02:09 -0700382 collectors.clear();
tomab21e7c2014-08-26 15:23:08 -0700383 providerService = null;
alshabiba89cc582014-09-09 16:43:00 -0700384 LOG.info("Stopped");
tomb5a46e62014-08-26 14:20:00 -0700385 }
386
Dusan Pajinbab8a5e2015-07-24 17:37:19 +0200387 @Modified
388 public void modified(ComponentContext context) {
Victor Silva7944d142016-07-08 00:14:55 -0300389 Dictionary<?, ?> properties = context != null ? context.getProperties() : new Properties();
Dusan Pajinbab8a5e2015-07-24 17:37:19 +0200390 int newPortStatsPollFrequency;
391 try {
Simon Hunt7a307652016-06-23 01:17:29 -0700392 String s = get(properties, POLL_PROP_NAME);
Dusan Pajinbab8a5e2015-07-24 17:37:19 +0200393 newPortStatsPollFrequency = isNullOrEmpty(s) ? portStatsPollFrequency : Integer.parseInt(s.trim());
394
395 } catch (NumberFormatException | ClassCastException e) {
396 newPortStatsPollFrequency = portStatsPollFrequency;
397 }
398
399 if (newPortStatsPollFrequency != portStatsPollFrequency) {
400 portStatsPollFrequency = newPortStatsPollFrequency;
401 collectors.values().forEach(psc -> psc.adjustPollInterval(portStatsPollFrequency));
402 }
403
404 LOG.info("Settings: portStatsPollFrequency={}", portStatsPollFrequency);
405 }
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700406
Thomas Vachuskad07c0922015-10-06 14:48:06 -0700407 private void connectInitialDevices() {
408 for (OpenFlowSwitch sw : controller.getSwitches()) {
409 try {
410 listener.switchAdded(new Dpid(sw.getId()));
411 } catch (Exception e) {
412 LOG.warn("Failed initially adding {} : {}", sw.getStringId(), e.getMessage());
413 LOG.debug("Error details:", e);
414 // disconnect to trigger switch-add later
415 sw.disconnectSwitch();
416 }
Thomas Vachuskad07c0922015-10-06 14:48:06 -0700417 }
418 }
419
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700420 @Override
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700421 public boolean isReachable(DeviceId deviceId) {
422 OpenFlowSwitch sw = controller.getSwitch(dpid(deviceId.uri()));
Thomas Vachuskad07c0922015-10-06 14:48:06 -0700423 return sw != null && sw.isConnected();
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700424 }
425
tomab21e7c2014-08-26 15:23:08 -0700426 @Override
Ayaka Koshibe78bcbc12014-11-19 14:28:58 -0800427 public void triggerProbe(DeviceId deviceId) {
Madan Jampanic3328762015-05-31 12:39:27 -0700428 LOG.debug("Triggering probe on device {}", deviceId);
Ayaka Koshibee8708e32014-10-22 13:40:18 -0700429
Yuta HIGUCHI802d0e82014-10-31 16:07:37 -0700430 final Dpid dpid = dpid(deviceId.uri());
431 OpenFlowSwitch sw = controller.getSwitch(dpid);
432 if (sw == null || !sw.isConnected()) {
Ayaka Koshibe78bcbc12014-11-19 14:28:58 -0800433 LOG.error("Failed to probe device {} on sw={}", deviceId, sw);
Yuta HIGUCHI802d0e82014-10-31 16:07:37 -0700434 providerService.deviceDisconnected(deviceId);
Thomas Vachuska74bcd922015-06-02 13:08:57 -0700435 return;
Yuta HIGUCHI802d0e82014-10-31 16:07:37 -0700436 } else {
Ayaka Koshibe78bcbc12014-11-19 14:28:58 -0800437 LOG.trace("Confirmed device {} connection", deviceId);
Yuta HIGUCHI802d0e82014-10-31 16:07:37 -0700438 }
Ayaka Koshibee8708e32014-10-22 13:40:18 -0700439
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700440 // Prompt an update of port information. We can use any XID for this.
Ayaka Koshibee8708e32014-10-22 13:40:18 -0700441 OFFactory fact = sw.factory();
442 switch (fact.getVersion()) {
443 case OF_10:
444 sw.sendMsg(fact.buildFeaturesRequest().setXid(0).build());
445 break;
446 case OF_13:
Yuta HIGUCHI6512f3e2017-05-18 17:21:24 -0700447 case OF_14:
448 case OF_15:
Ayaka Koshibee8708e32014-10-22 13:40:18 -0700449 sw.sendMsg(fact.buildPortDescStatsRequest().setXid(0).build());
450 break;
451 default:
452 LOG.warn("Unhandled protocol version");
453 }
tomab21e7c2014-08-26 15:23:08 -0700454 }
455
456 @Override
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700457 public void roleChanged(DeviceId deviceId, MastershipRole newRole) {
alshabibf1216ed2014-09-03 11:53:54 -0700458 switch (newRole) {
tom782a7cf2014-09-11 23:58:38 -0700459 case MASTER:
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700460 controller.setRole(dpid(deviceId.uri()), RoleState.MASTER);
tom782a7cf2014-09-11 23:58:38 -0700461 break;
462 case STANDBY:
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700463 controller.setRole(dpid(deviceId.uri()), RoleState.EQUAL);
tom782a7cf2014-09-11 23:58:38 -0700464 break;
465 case NONE:
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700466 controller.setRole(dpid(deviceId.uri()), RoleState.SLAVE);
tom782a7cf2014-09-11 23:58:38 -0700467 break;
468 default:
469 LOG.error("Unknown Mastership state : {}", newRole);
alshabibf1216ed2014-09-03 11:53:54 -0700470
471 }
HIGUCHI Yuta1979f552015-12-28 21:24:26 -0800472 LOG.debug("Accepting mastership role change to {} for device {}", newRole, deviceId);
tomab21e7c2014-08-26 15:23:08 -0700473 }
474
Saurav Dasa2d37502016-03-25 17:50:40 -0700475 @Override
476 public void changePortState(DeviceId deviceId, PortNumber portNumber,
477 boolean enable) {
478 final Dpid dpid = dpid(deviceId.uri());
479 OpenFlowSwitch sw = controller.getSwitch(dpid);
480 if (sw == null || !sw.isConnected()) {
481 LOG.error("Failed to change portState on device {}", deviceId);
482 return;
483 }
484 OFPortMod.Builder pmb = sw.factory().buildPortMod();
485 OFPort port = OFPort.of((int) portNumber.toLong());
486 pmb.setPortNo(port);
Jimmy Jine9b7a022016-08-12 16:56:48 -0700487 Set<OFPortConfig> portConfig = EnumSet.noneOf(OFPortConfig.class);
488 if (!enable) {
489 portConfig.add(OFPortConfig.PORT_DOWN);
Saurav Dasa2d37502016-03-25 17:50:40 -0700490 }
Jimmy Jine9b7a022016-08-12 16:56:48 -0700491 pmb.setConfig(portConfig);
492 Set<OFPortConfig> portMask = EnumSet.noneOf(OFPortConfig.class);
493 portMask.add(OFPortConfig.PORT_DOWN);
494 pmb.setMask(portMask);
Saurav Dasa2d37502016-03-25 17:50:40 -0700495 pmb.setAdvertise(0x0);
496 for (OFPortDesc pd : sw.getPorts()) {
497 if (pd.getPortNo().equals(port)) {
498 pmb.setHwAddr(pd.getHwAddr());
499 break;
500 }
501 }
502 sw.sendMsg(Collections.singletonList(pmb.build()));
503 }
504
Thomas Vachuska893bf4b2015-05-29 18:13:18 -0700505 private void pushPortMetrics(Dpid dpid, List<OFPortStatsEntry> portStatsEntries) {
HIGUCHI Yuta1979f552015-12-28 21:24:26 -0800506 DeviceId deviceId = DeviceId.deviceId(Dpid.uri(dpid));
Ray Milkey3c4124a2016-08-31 12:09:06 -0700507 Collection<PortStatistics> stats =
508 buildPortStatistics(deviceId, ImmutableList.copyOf(portStatsEntries));
sangho538108b2015-04-08 14:29:20 -0700509 providerService.updatePortStatistics(deviceId, stats);
510 }
511
Laszlo Papp7cf60372018-01-11 00:06:43 +0000512
513 private static String mhzToAnnotation(long freqMhz) {
514 return Long.toString(Frequency.ofMHz(freqMhz).asHz());
515 }
516
Thomas Vachuska893bf4b2015-05-29 18:13:18 -0700517 private Collection<PortStatistics> buildPortStatistics(DeviceId deviceId,
518 List<OFPortStatsEntry> entries) {
sangho538108b2015-04-08 14:29:20 -0700519 HashSet<PortStatistics> stats = Sets.newHashSet();
520
Thomas Vachuska893bf4b2015-05-29 18:13:18 -0700521 for (OFPortStatsEntry entry : entries) {
Thomas Vachuskaf0397b52015-05-29 13:50:17 -0700522 try {
Thomas Vachuska4e6025e2015-06-03 11:25:21 -0700523 if (entry == null || entry.getPortNo() == null || entry.getPortNo().getPortNumber() < 0) {
Thomas Vachuskaf0397b52015-05-29 13:50:17 -0700524 continue;
525 }
Laszlo Papp7cf60372018-01-11 00:06:43 +0000526 DefaultAnnotations.Builder annotations = DefaultAnnotations.builder();
Laszlo Papp9dfb5122018-01-12 21:10:13 +0000527 boolean propSupported = entry.getVersion().getWireVersion() >= OFVersion.OF_14.getWireVersion();
528 Optional<OFPortStatsPropOptical> optical = propSupported ?
529 entry.getProperties().stream()
Laszlo Papp7cf60372018-01-11 00:06:43 +0000530 .filter(OFPortStatsPropOptical.class::isInstance)
531 .map(OFPortStatsPropOptical.class::cast)
Laszlo Papp9dfb5122018-01-12 21:10:13 +0000532 .findAny() : Optional.empty();
Laszlo Papp7cf60372018-01-11 00:06:43 +0000533 if (optical.isPresent()) {
534 long flags = optical.get().getFlags();
535
536 int txTune = OFPortStatsOpticalFlagsSerializerVer14.TX_TUNE_VAL;
537 long txFreq = optical.get().getTxFreqLmda();
538 long txOffset = optical.get().getTxOffset();
539 long txGridSpan = optical.get().getTxGridSpan();
540 annotations.set(AK_TX_TUNE_FEATURE, ((flags & txTune) != 0) ? "enabled" : "disabled");
541 annotations.set(AK_TX_FREQ_HZ, mhzToAnnotation(txFreq));
542 annotations.set(AK_TX_OFFSET_HZ, mhzToAnnotation(txOffset));
543 annotations.set(AK_TX_GRID_SPAN_HZ, mhzToAnnotation(txGridSpan));
544
545 int rxTune = OFPortStatsOpticalFlagsSerializerVer14.RX_TUNE_VAL;
546 long rxFreq = optical.get().getRxFreqLmda();
547 long rxOffset = optical.get().getRxOffset();
548 long rxGridSpan = optical.get().getRxGridSpan();
549 annotations.set(AK_RX_TUNE_FEATURE, ((flags & rxTune) != 0) ? "enabled" : "disabled");
550 annotations.set(AK_RX_FREQ_HZ, mhzToAnnotation(rxFreq));
551 annotations.set(AK_RX_OFFSET_HZ, mhzToAnnotation(rxOffset));
552 annotations.set(AK_RX_GRID_SPAN_HZ, mhzToAnnotation(rxGridSpan));
553
554 int txPwrVal = OFPortStatsOpticalFlagsSerializerVer14.TX_PWR_VAL;
555 int txPwr = optical.get().getTxPwr();
556 annotations.set(AK_TX_PWR_FEATURE, ((flags & txPwrVal) != 0) ? "enabled" : "disabled");
557 annotations.set(AK_TX_PWR, Integer.toString(txPwr));
558
559 int rxPwrVal = OFPortStatsOpticalFlagsSerializerVer14.RX_PWR_VAL;
560 int rxPwr = optical.get().getRxPwr();
561 annotations.set(AK_RX_PWR_FEATURE, ((flags & rxPwrVal) != 0) ? "enabled" : "disabled");
562 annotations.set(AK_RX_PWR, Integer.toString(rxPwr));
563
564 int txBias = OFPortStatsOpticalFlagsSerializerVer14.TX_BIAS_VAL;
565 int biasCurrent = optical.get().getBiasCurrent();
566 annotations.set(AK_TX_BIAS_FEATURE, ((flags & txBias) != 0) ? "enabled" : "disabled");
567 annotations.set(AK_BIAS_CURRENT, Integer.toString(biasCurrent));
568
569 int txTemp = OFPortStatsOpticalFlagsSerializerVer14.TX_TEMP_VAL;
570 int temperature = optical.get().getTemperature();
571 annotations.set(AK_TX_TEMP_FEATURE, ((flags & txTemp) != 0) ? "enabled" : "disabled");
572 annotations.set(AK_TEMPERATURE, Integer.toString(temperature));
573 }
Thomas Vachuskaf0397b52015-05-29 13:50:17 -0700574 DefaultPortStatistics.Builder builder = DefaultPortStatistics.builder();
575 DefaultPortStatistics stat = builder.setDeviceId(deviceId)
576 .setPort(entry.getPortNo().getPortNumber())
577 .setPacketsReceived(entry.getRxPackets().getValue())
578 .setPacketsSent(entry.getTxPackets().getValue())
579 .setBytesReceived(entry.getRxBytes().getValue())
580 .setBytesSent(entry.getTxBytes().getValue())
581 .setPacketsRxDropped(entry.getRxDropped().getValue())
582 .setPacketsTxDropped(entry.getTxDropped().getValue())
583 .setPacketsRxErrors(entry.getRxErrors().getValue())
584 .setPacketsTxErrors(entry.getTxErrors().getValue())
585 .setDurationSec(entry.getVersion() == OFVersion.OF_10 ? 0 : entry.getDurationSec())
586 .setDurationNano(entry.getVersion() == OFVersion.OF_10 ? 0 : entry.getDurationNsec())
Laszlo Papp7cf60372018-01-11 00:06:43 +0000587 .setAnnotations(annotations.build())
Thomas Vachuskaf0397b52015-05-29 13:50:17 -0700588 .build();
sangho538108b2015-04-08 14:29:20 -0700589
Thomas Vachuskaf0397b52015-05-29 13:50:17 -0700590 stats.add(stat);
591 } catch (Exception e) {
592 LOG.warn("Unable to process port stats", e);
593 }
sangho538108b2015-04-08 14:29:20 -0700594 }
595
596 return Collections.unmodifiableSet(stats);
sangho538108b2015-04-08 14:29:20 -0700597 }
598
599 private class InternalDeviceProvider implements OpenFlowSwitchListener, OpenFlowEventListener {
Thomas Vachuska893bf4b2015-05-29 18:13:18 -0700600
Srikanth Vavilapalli78baf582015-06-05 11:40:14 -0700601 private HashMap<Dpid, List<OFPortStatsEntry>> portStatsReplies = new HashMap<>();
Ray Milkey7abe50b2016-04-13 11:57:44 -0700602 private boolean isDisabled = false;
Thomas Vachuska893bf4b2015-05-29 18:13:18 -0700603
alshabibf1216ed2014-09-03 11:53:54 -0700604 @Override
tomd1900f32014-09-03 14:08:16 -0700605 public void switchAdded(Dpid dpid) {
alshabib6f5460b2014-09-03 14:46:17 -0700606 if (providerService == null) {
607 return;
608 }
tom782a7cf2014-09-11 23:58:38 -0700609 DeviceId did = deviceId(uri(dpid));
alshabib6f5460b2014-09-03 14:46:17 -0700610 OpenFlowSwitch sw = controller.getSwitch(dpid);
Lei Xudee1aff2015-10-16 00:45:10 -0500611 if (sw == null) {
sangyun-han147e6f42016-07-26 17:14:41 +0900612 LOG.error("Switch {} is not found", dpid);
Lei Xudee1aff2015-10-16 00:45:10 -0500613 return;
614 }
alshabib6f5460b2014-09-03 14:46:17 -0700615
alshabib7911a052014-10-16 17:49:37 -0700616 ChassisId cId = new ChassisId(dpid.value());
Ray Milkeye53f1712015-01-16 09:17:16 -0800617
Victor Silvaac8dab02016-11-28 16:33:08 -0300618 DefaultAnnotations.Builder annotationsBuilder = DefaultAnnotations.builder()
andreafe3308f2015-10-06 15:51:25 -0700619 .set(AnnotationKeys.PROTOCOL, sw.factory().getVersion().toString())
620 .set(AnnotationKeys.CHANNEL_ID, sw.channelId())
Victor Silvaac8dab02016-11-28 16:33:08 -0300621 .set(AnnotationKeys.MANAGEMENT_ADDRESS, sw.channelId().split(":")[0]);
622
Yuta HIGUCHI2341e602017-03-08 20:10:08 -0800623 // FIXME following ignores driver specified by name
Victor Silvaac8dab02016-11-28 16:33:08 -0300624 Driver driver = driverService.getDriver(sw.manufacturerDescription(),
625 sw.hardwareDescription(),
626 sw.softwareDescription());
Thomas Vachuskaa79cbf72017-01-13 15:07:04 -0800627 // FIXME: The following breaks the STC tests and will require to be revisited.
628// if (driver != null) {
629// annotationsBuilder.set(AnnotationKeys.DRIVER, driver.name());
630// }
Victor Silvaac8dab02016-11-28 16:33:08 -0300631
632 SparseAnnotations annotations = annotationsBuilder.build();
Ray Milkeye53f1712015-01-16 09:17:16 -0800633
tomd1900f32014-09-03 14:08:16 -0700634 DeviceDescription description =
Marc De Leenheerb9311372015-07-09 11:36:49 -0700635 new DefaultDeviceDescription(did.uri(), sw.deviceType(),
Ray Milkeyd3edd032015-01-16 11:38:58 -0800636 sw.manufacturerDescription(),
tom782a7cf2014-09-11 23:58:38 -0700637 sw.hardwareDescription(),
638 sw.softwareDescription(),
alshabib7911a052014-10-16 17:49:37 -0700639 sw.serialNumber(),
Thomas Vachuska82041f52014-11-30 22:14:02 -0800640 cId, annotations);
tom782a7cf2014-09-11 23:58:38 -0700641 providerService.deviceConnected(did, description);
Ayaka Koshibe5460d622015-05-14 12:19:19 -0700642 providerService.updatePorts(did, buildPortDescriptions(sw));
sangho538108b2015-04-08 14:29:20 -0700643
Laszlo Papp58174812018-01-16 13:29:47 +0000644 if (sw.features().getCapabilities().contains(OFCapabilities.PORT_STATS)) {
645 PortStatsCollector psc = new PortStatsCollector(timer, sw, portStatsPollFrequency);
646 stopCollectorIfNeeded(collectors.put(dpid, psc));
647 psc.start();
648 }
Lei Xudee1aff2015-10-16 00:45:10 -0500649
650 //figure out race condition for collectors.remove() and collectors.put()
651 if (controller.getSwitch(dpid) == null) {
652 switchRemoved(dpid);
653 }
alshabib25c8eec2014-09-04 16:41:31 -0700654 }
655
Thomas Vachuskaa394b952016-06-14 15:02:09 -0700656 private void stopCollectorIfNeeded(PortStatsCollector collector) {
657 if (collector != null) {
658 collector.stop();
659 }
660 }
661
alshabibf1216ed2014-09-03 11:53:54 -0700662 @Override
663 public void switchRemoved(Dpid dpid) {
Victor Silva03265dd2016-12-08 11:20:36 -0300664 stopCollectorIfNeeded(collectors.remove(dpid));
alshabib6f5460b2014-09-03 14:46:17 -0700665 if (providerService == null) {
666 return;
667 }
tom782a7cf2014-09-11 23:58:38 -0700668 providerService.deviceDisconnected(deviceId(uri(dpid)));
sangho538108b2015-04-08 14:29:20 -0700669 }
Ayaka Koshibe38594c22014-10-22 13:36:12 -0700670
671 @Override
672 public void switchChanged(Dpid dpid) {
HIGUCHI Yuta1979f552015-12-28 21:24:26 -0800673 LOG.debug("switchChanged({})", dpid);
Ayaka Koshibe38594c22014-10-22 13:36:12 -0700674 if (providerService == null) {
675 return;
676 }
677 DeviceId did = deviceId(uri(dpid));
678 OpenFlowSwitch sw = controller.getSwitch(dpid);
Lei Xudee1aff2015-10-16 00:45:10 -0500679 if (sw == null) {
sangyun-han147e6f42016-07-26 17:14:41 +0900680 LOG.error("Switch {} is not found", dpid);
Lei Xudee1aff2015-10-16 00:45:10 -0500681 return;
682 }
HIGUCHI Yuta1979f552015-12-28 21:24:26 -0800683 final List<PortDescription> ports = buildPortDescriptions(sw);
684 LOG.debug("switchChanged({}) {}", did, ports);
685 providerService.updatePorts(did, ports);
Ayaka Koshibe38594c22014-10-22 13:36:12 -0700686 }
687
alshabiba14f3642014-09-05 09:31:31 -0700688 @Override
689 public void portChanged(Dpid dpid, OFPortStatus status) {
HIGUCHI Yuta1979f552015-12-28 21:24:26 -0800690 LOG.debug("portChanged({},{})", dpid, status);
alshabibafc514a2014-12-01 14:44:05 -0800691 PortDescription portDescription = buildPortDescription(status);
Michal Machce774332017-01-25 11:02:55 +0100692 if (status.getReason() != OFPortReason.DELETE) {
693 providerService.portStatusChanged(deviceId(uri(dpid)), portDescription);
694 } else {
695 providerService.deletePort(deviceId(uri(dpid)), portDescription);
696 }
alshabibf1216ed2014-09-03 11:53:54 -0700697 }
698
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700699 @Override
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700700 public void receivedRoleReply(Dpid dpid, RoleState requested, RoleState response) {
HIGUCHI Yuta1979f552015-12-28 21:24:26 -0800701 LOG.debug("receivedRoleReply({},{},{})", dpid, requested, response);
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700702 MastershipRole request = roleOf(requested);
703 MastershipRole reply = roleOf(response);
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700704 providerService.receivedRoleReply(deviceId(uri(dpid)), request, reply);
705 }
706
707 /**
708 * Translates a RoleState to the corresponding MastershipRole.
709 *
Thomas Vachuskafc52fec2015-05-18 19:13:56 -0700710 * @param response role state
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700711 * @return a MastershipRole
712 */
713 private MastershipRole roleOf(RoleState response) {
714 switch (response) {
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700715 case MASTER:
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700716 return MastershipRole.MASTER;
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700717 case EQUAL:
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700718 return MastershipRole.STANDBY;
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700719 case SLAVE:
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700720 return MastershipRole.NONE;
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700721 default:
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700722 LOG.warn("unknown role {}", response);
723 return null;
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700724 }
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700725 }
726
alshabiba14f3642014-09-05 09:31:31 -0700727 /**
728 * Builds a list of port descriptions for a given list of ports.
tomff7eb7c2014-09-08 12:49:03 -0700729 *
alshabiba14f3642014-09-05 09:31:31 -0700730 * @return list of portdescriptions
731 */
Ayaka Koshibe5460d622015-05-14 12:19:19 -0700732 private List<PortDescription> buildPortDescriptions(OpenFlowSwitch sw) {
Yuta HIGUCHIf83c8cf2017-12-17 14:33:49 -0800733 List<OFPortDesc> ofPorts = sw.getPorts();
734 final List<PortDescription> portDescs = new ArrayList<>(ofPorts.size());
Rimon Ashkenazy8ebfff02016-02-01 11:56:36 +0200735 if (!((Device.Type.ROADM.equals(sw.deviceType())) ||
MaoLuc201ae42017-02-06 17:57:01 -0800736 (Device.Type.OTN.equals(sw.deviceType())) ||
737 (Device.Type.OPTICAL_AMPLIFIER.equals(sw.deviceType())))) {
Yuta HIGUCHI2341e602017-03-08 20:10:08 -0800738 // build regular (=non-optical) Device ports
Yuta HIGUCHIf83c8cf2017-12-17 14:33:49 -0800739 ofPorts.forEach(port -> portDescs.add(buildPortDescription(port)));
Yafit Hadara9a73de2015-09-06 13:52:52 +0300740 }
Marc De Leenheerb9311372015-07-09 11:36:49 -0700741
Yuta HIGUCHI2341e602017-03-08 20:10:08 -0800742 // TODO handle Optical Device, but plain OF devices(1.4 and later)
743
Marc De Leenheerb9311372015-07-09 11:36:49 -0700744 OpenFlowOpticalSwitch opsw;
745 switch (sw.deviceType()) {
746 case ROADM:
Rimon Ashkenazy8ebfff02016-02-01 11:56:36 +0200747 case OTN:
MaoLuc201ae42017-02-06 17:57:01 -0800748 case OPTICAL_AMPLIFIER:
Marc De Leenheerb9311372015-07-09 11:36:49 -0700749 opsw = (OpenFlowOpticalSwitch) sw;
Yafit Hadara9a73de2015-09-06 13:52:52 +0300750 List<OFPortDesc> ports = opsw.getPorts();
751 LOG.debug("SW ID {} , ETH- ODU CLT Ports {}", opsw.getId(), ports);
752 // ODU client ports are reported as ETH
753 ports.forEach(port -> portDescs.add(buildOduCltPortDescription(port)));
754
Marc De Leenheerb9311372015-07-09 11:36:49 -0700755 opsw.getPortTypes().forEach(type -> {
Yafit Hadara9a73de2015-09-06 13:52:52 +0300756 List<? extends OFObject> portsOf = opsw.getPortsOf(type);
757 LOG.debug("Ports Of{}", portsOf);
758 portsOf.forEach(
759 op -> {
Jimmy Yan4deb03b2016-06-24 10:53:54 -0700760 portDescs.add(buildPortDescription(type, op, opsw));
Yafit Hadara9a73de2015-09-06 13:52:52 +0300761 }
762 );
Marc De Leenheerb9311372015-07-09 11:36:49 -0700763 });
764 break;
765 case FIBER_SWITCH:
766 opsw = (OpenFlowOpticalSwitch) sw;
767 opsw.getPortTypes().forEach(type -> {
Yuta HIGUCHIf42a2cc2017-05-02 17:04:31 -0700768 opsw.getPortsOf(type).forEach(op -> {
769 if (op instanceof OFPortDesc) {
770 // ports using standard optical extension
771 // TODO OFMessage -> PortDescription should
772 // probably be a Behaviour
773 portDescs.add(oms(buildPortDescription((OFPortDesc) op)));
774 } else if (op instanceof OFCalientPortDescStatsEntry) {
775 // calient extension
776 portDescs.add(buildPortDescription((OFCalientPortDescStatsEntry) op));
777 } else {
778 LOG.warn("Unexpected FIBER_SWITCH port {} on {}",
779 op, sw.getStringId());
780 }
781 });
Marc De Leenheerb9311372015-07-09 11:36:49 -0700782 });
783 break;
784 default:
785 break;
alshabib4680bb62014-09-04 17:15:08 -0700786 }
Marc De Leenheerb9311372015-07-09 11:36:49 -0700787
alshabib4680bb62014-09-04 17:15:08 -0700788 return portDescs;
789 }
790
Yuta HIGUCHIf42a2cc2017-05-02 17:04:31 -0700791 /**
792 * Ensures returned PortDescription is an OMS port.
793 *
794 * @param descr input PortDescription
795 * @return OMS PortDescription
796 */
797 private PortDescription oms(PortDescription descr) {
798 // Hack until OFMessage -> PortDescription transformation
799 // becomes a Behaviour
800 if (descr.type() == Type.OMS) {
801 return descr;
802 }
803
804 Builder builder = DefaultAnnotations.builder();
805 builder.putAll(descr.annotations());
806
807 // set reasonable default when mandatory key is missing
808 if (Strings.isNullOrEmpty(descr.annotations().value(AK_MIN_FREQ_HZ))) {
809 builder.set(AK_MIN_FREQ_HZ, String.valueOf(Spectrum.O_BAND_MIN.asHz()));
810 }
811
812 if (Strings.isNullOrEmpty(descr.annotations().value(AK_MAX_FREQ_HZ))) {
813 builder.set(AK_MAX_FREQ_HZ, String.valueOf(Spectrum.O_BAND_MAX.asHz()));
814 }
815
816 if (Strings.isNullOrEmpty(descr.annotations().value(AK_GRID_HZ))) {
817 builder.set(AK_GRID_HZ, String.valueOf(Frequency.ofGHz(50).asHz()));
818 }
819
820 return DefaultPortDescription.builder(descr)
821 .type(Type.OMS)
822 .annotations(builder.build())
823 .build();
824 }
825
Yafit Hadara9a73de2015-09-06 13:52:52 +0300826 private PortDescription buildOduCltPortDescription(OFPortDesc port) {
827 PortNumber portNo = PortNumber.portNumber(port.getPortNo().getPortNumber());
828 boolean enabled = !port.getState().contains(OFPortState.LINK_DOWN) &&
829 !port.getConfig().contains(OFPortConfig.PORT_DOWN);
Rimon Ashkenazyd15ebf52016-01-06 14:36:13 +0200830 Long portSpeedInMbps = portSpeed(port);
Toru Furusawa72ee30c2016-01-08 13:29:04 -0800831 CltSignalType sigType = null;
Yafit Hadara9a73de2015-09-06 13:52:52 +0300832
Rimon Ashkenazyd15ebf52016-01-06 14:36:13 +0200833 switch (portSpeedInMbps.toString()) {
834 case "1000":
Toru Furusawa72ee30c2016-01-08 13:29:04 -0800835 sigType = CltSignalType.CLT_1GBE;
Yafit Hadara9a73de2015-09-06 13:52:52 +0300836 break;
Rimon Ashkenazyd15ebf52016-01-06 14:36:13 +0200837 case "10000":
Toru Furusawa72ee30c2016-01-08 13:29:04 -0800838 sigType = CltSignalType.CLT_10GBE;
Yafit Hadara9a73de2015-09-06 13:52:52 +0300839 break;
Rimon Ashkenazyd15ebf52016-01-06 14:36:13 +0200840 case "40000":
Toru Furusawa72ee30c2016-01-08 13:29:04 -0800841 sigType = CltSignalType.CLT_40GBE;
Yafit Hadara9a73de2015-09-06 13:52:52 +0300842 break;
Rimon Ashkenazyd15ebf52016-01-06 14:36:13 +0200843 case "100000":
Toru Furusawa72ee30c2016-01-08 13:29:04 -0800844 sigType = CltSignalType.CLT_100GBE;
Yafit Hadara9a73de2015-09-06 13:52:52 +0300845 break;
846 default:
Rimon Ashkenazyd15ebf52016-01-06 14:36:13 +0200847 throw new RuntimeException("Un recognize OduClt speed: " + portSpeedInMbps.toString());
Yafit Hadara9a73de2015-09-06 13:52:52 +0300848 }
849
850 SparseAnnotations annotations = buildOduCltAnnotation(port);
HIGUCHI Yuta4c0ef6b2016-05-02 19:45:41 -0700851 return oduCltPortDescription(portNo, enabled, sigType, annotations);
Yafit Hadara9a73de2015-09-06 13:52:52 +0300852 }
853
854 private SparseAnnotations buildOduCltAnnotation(OFPortDesc port) {
855 SparseAnnotations annotations = null;
856 String portName = Strings.emptyToNull(port.getName());
857 if (portName != null) {
858 annotations = DefaultAnnotations.builder()
859 .set(AnnotationKeys.PORT_NAME, portName)
860 .set(AnnotationKeys.STATIC_PORT, Boolean.TRUE.toString()).build();
861 }
862 return annotations;
863 }
864
Jimmy Yan4deb03b2016-06-24 10:53:54 -0700865 private PortDescription buildPortDescription(PortDescPropertyType ptype, OFObject port,
866 OpenFlowOpticalSwitch opsw) {
yjimmyy646aa022016-07-05 12:09:50 -0700867 if (port instanceof OFPortOptical) {
Jimmy Yan4deb03b2016-06-24 10:53:54 -0700868 return buildPortDescription(ptype, (OFPortOptical) port, opsw);
Yafit Hadara9a73de2015-09-06 13:52:52 +0300869 }
870 return buildPortDescription(ptype, (OFExpPort) port);
871 }
872
Rimon Ashkenazy8ebfff02016-02-01 11:56:36 +0200873 private boolean matchingOtuPortSignalTypes(OFPortOpticalTransportSignalType sigType,
874 OduSignalType oduSignalType) {
875 switch (sigType) {
876 case OTU2:
877 if (oduSignalType == OduSignalType.ODU2) {
878 return true;
879 }
880 break;
881 case OTU4:
882 if (oduSignalType == OduSignalType.ODU4) {
883 return true;
884 }
885 break;
886 default:
887 break;
888 }
889 return false;
890 }
Yafit Hadara9a73de2015-09-06 13:52:52 +0300891 /**
892 * Build a portDescription from a given a port description describing some
893 * Optical port.
894 *
895 * @param ptype description property type.
896 * @param port the port to build from.
897 * @return portDescription for the port.
898 */
899 private PortDescription buildPortDescription(PortDescPropertyType ptype, OFExpPort port) {
900 PortNumber portNo = PortNumber.portNumber(port.getPortNo().getPortNumber());
901 boolean enabled = !port.getState().contains(OFPortState.LINK_DOWN)
902 && !port.getConfig().contains(OFPortConfig.PORT_DOWN);
mskalac584bd92017-11-28 15:51:17 +0100903 boolean adminDown = port.getConfig().contains(OFPortConfig.PORT_DOWN);
904 SparseAnnotations annotations = makePortAnnotation(port.getName(),
905 port.getHwAddr().toString(),
906 adminDown).build();
Yafit Hadara9a73de2015-09-06 13:52:52 +0300907
908 OFExpPortDescPropOpticalTransport firstProp = port.getProperties().get(0);
909 OFPortOpticalTransportSignalType sigType = firstProp.getPortSignalType();
910
HIGUCHI Yuta34a3f692016-01-09 21:08:57 -0800911 PortDescription portDes = null;
Yafit Hadara9a73de2015-09-06 13:52:52 +0300912 switch (sigType) {
913 case OMSN:
HIGUCHI Yuta95d83e82016-04-26 12:13:48 -0700914 portDes = omsPortDescription(portNo, enabled,
Rimon Ashkenazye3201032016-01-11 14:27:30 +0200915 FREQ191_7, FREQ191_7.add(FREQ4_4), FREQ50, annotations);
Yafit Hadara9a73de2015-09-06 13:52:52 +0300916 break;
917 case OCH:
918 OFExpPortOpticalTransportLayerEntry entry = firstProp.getFeatures().get(0).getValue().get(0);
919 OFPortOpticalTransportLayerClass layerClass = entry.getLayerClass();
920 if (!OFPortOpticalTransportLayerClass.ODU.equals(layerClass)) {
921 LOG.error("Unsupported layer Class {} ", layerClass);
922 return null;
923 }
924
925 // convert to ONOS OduSignalType
926 OduSignalType oduSignalType = OpenFlowDeviceValueMapper.
927 lookupOduSignalType((byte) entry.getSignalType());
928 //OchSignal is needed for OchPortDescription constructor,
929 //yet not relevant for tunable OCH port, creating with default parameters
930 OchSignal signalId = new OchSignal(GridType.DWDM, ChannelSpacing.CHL_50GHZ, 1, 1);
931
HIGUCHI Yuta34a3f692016-01-09 21:08:57 -0800932 portDes = ochPortDescription(portNo, enabled,
933 oduSignalType, true,
934 signalId, annotations);
Yafit Hadara9a73de2015-09-06 13:52:52 +0300935
936 break;
937 case OTU2:
938 case OTU4:
Rimon Ashkenazy8ebfff02016-02-01 11:56:36 +0200939 entry = firstProp.getFeatures().get(0).getValue().get(0);
940 layerClass = entry.getLayerClass();
941 if (!OFPortOpticalTransportLayerClass.ODU.equals(layerClass)) {
942 LOG.error("Unsupported layer Class {} ", layerClass);
943 return null;
944 }
945
946 // convert to ONOS OduSignalType
947 OduSignalType oduSignalTypeOtuPort = OpenFlowDeviceValueMapper.
948 lookupOduSignalType((byte) entry.getSignalType());
949 if (!matchingOtuPortSignalTypes(sigType, oduSignalTypeOtuPort)) {
950 LOG.error("Wrong oduSignalType {} for OTU Port sigType {} ", oduSignalTypeOtuPort, sigType);
951 return null;
952 }
953 OtuSignalType otuSignalType =
954 ((sigType == OFPortOpticalTransportSignalType.OTU2) ? OtuSignalType.OTU2 :
955 OtuSignalType.OTU4);
HIGUCHI Yuta5be3e822016-05-03 13:51:42 -0700956 portDes = otuPortDescription(portNo, enabled, otuSignalType, annotations);
Rimon Ashkenazy8ebfff02016-02-01 11:56:36 +0200957 break;
Yafit Hadara9a73de2015-09-06 13:52:52 +0300958 default:
959 break;
960 }
961
962 return portDes;
963 }
964
alshabiba14f3642014-09-05 09:31:31 -0700965 /**
Yuta HIGUCHI2341e602017-03-08 20:10:08 -0800966 * Creates an annotation builder for the port name if one is available.
Ray Milkeye0fade72015-01-15 13:29:47 -0800967 *
lishuai4ee42042015-11-30 17:19:21 +0800968 * @param portName the port name
969 * @param portMac the port mac
mskalac584bd92017-11-28 15:51:17 +0100970 * @param adminDown the port admin state
971 * @return annotation builder containing port admin state, port name
972 * and/or port MAC if any of the two is found
Ray Milkeye0fade72015-01-15 13:29:47 -0800973 */
mskalac584bd92017-11-28 15:51:17 +0100974 private DefaultAnnotations.Builder makePortAnnotation(String portName, String portMac, boolean adminDown) {
Andrea Campanellac8b871a2017-01-19 13:53:05 -0800975 DefaultAnnotations.Builder builder = DefaultAnnotations.builder();
lishuai4ee42042015-11-30 17:19:21 +0800976 String pName = Strings.emptyToNull(portName);
977 String pMac = Strings.emptyToNull(portMac);
Andrea Campanellac8b871a2017-01-19 13:53:05 -0800978 if (pName != null) {
979 builder.set(AnnotationKeys.PORT_NAME, pName);
Ray Milkeye0fade72015-01-15 13:29:47 -0800980 }
Andrea Campanellac8b871a2017-01-19 13:53:05 -0800981 if (pMac != null) {
982 builder.set(AnnotationKeys.PORT_MAC, pMac);
983 }
mskalac584bd92017-11-28 15:51:17 +0100984 String adminState = adminDown ? "disabled" : "enabled";
985 builder.set(AnnotationKeys.ADMIN_STATE, adminState);
Yuta HIGUCHI2341e602017-03-08 20:10:08 -0800986 return builder;
987 }
988
989 private String mhzToAnnotation(long freqMhz) {
Laszlo Papp7cf60372018-01-11 00:06:43 +0000990 return OpenFlowDeviceProvider.mhzToAnnotation(freqMhz);
Yuta HIGUCHI2341e602017-03-08 20:10:08 -0800991 }
992
Yuta HIGUCHIf42a2cc2017-05-02 17:04:31 -0700993 private String lambdaToAnnotationHz(long lambda) {
994 // ref. OF1.5: wavelength (lambda) as nm * 100
995
996 long c = 299792458; // speed of light in m/s
997 // f = c / λ
998 // (m/s) * (nm/m) / (nm * 100) * 100
999 // annotations is in Hz
Laszlo Papp73f9aea2018-01-15 19:23:45 +00001000 return Long.toString(lambda == 0 ? lambda : (c * 1_000_000_000 / lambda * 100));
Yuta HIGUCHIf42a2cc2017-05-02 17:04:31 -07001001 }
1002
Yuta HIGUCHI2341e602017-03-08 20:10:08 -08001003 private PortDescription buildPortDescription14(OFPortDesc port) {
1004 PortNumber portNo = PortNumber.portNumber(port.getPortNo().getPortNumber());
1005 boolean enabled =
1006 !port.getState().contains(OFPortState.LINK_DOWN) &&
1007 !port.getConfig().contains(OFPortConfig.PORT_DOWN);
mskalac584bd92017-11-28 15:51:17 +01001008 boolean adminDown = port.getConfig().contains(OFPortConfig.PORT_DOWN);
Yuta HIGUCHI2341e602017-03-08 20:10:08 -08001009 Builder annotations = makePortAnnotation(port.getName(),
mskalac584bd92017-11-28 15:51:17 +01001010 port.getHwAddr().toString(),
1011 adminDown);
Yuta HIGUCHI2341e602017-03-08 20:10:08 -08001012
1013 Optional<OFPortDescPropEthernet> ether = port.getProperties().stream()
1014 .filter(OFPortDescPropEthernet.class::isInstance)
1015 .map(OFPortDescPropEthernet.class::cast)
1016 .findAny();
1017 if (ether.isPresent()) {
1018 // ethernet port
1019 // TODO parse other part of OFPortDescPropEthernet if necessary
1020 return new DefaultPortDescription(portNo,
1021 enabled,
1022 COPPER,
1023 portSpeed(port),
1024 annotations.build());
1025 }
1026
1027 Optional<OFPortDescPropOptical> optical = port.getProperties().stream()
1028 .filter(OFPortDescPropOptical.class::isInstance)
1029 .map(OFPortDescPropOptical.class::cast)
1030 .findAny();
1031 if (optical.isPresent()) {
1032 // optical port
1033
Yuta HIGUCHIf42a2cc2017-05-02 17:04:31 -07001034 // FIXME is there a OF version neutral way to access
1035 // OFOpticalPortFeaturesSerializerVer14
Yuta HIGUCHI2341e602017-03-08 20:10:08 -08001036
Laszlo Papp20cfb6f2018-01-08 16:06:53 +00001037 long supported = optical.get().getSupported();
1038 long rxMin = optical.get().getRxMinFreqLmda();
1039 long rxMax = optical.get().getRxMaxFreqLmda();
1040 long rxGrid = optical.get().getRxGridFreqLmda();
1041 long txMin = optical.get().getTxMinFreqLmda();
1042 long txMax = optical.get().getTxMaxFreqLmda();
1043 long txGrid = optical.get().getTxGridFreqLmda();
1044
1045 int txTune = OFOpticalPortFeaturesSerializerVer14.TX_TUNE_VAL;
1046 int rxTune = OFOpticalPortFeaturesSerializerVer14.RX_TUNE_VAL;
1047 annotations.set(AK_TX_TUNE_FEATURE,
1048 ((supported & txTune) != 0) ? "enabled" : "disabled");
1049 annotations.set(AK_RX_TUNE_FEATURE,
1050 ((supported & rxTune) != 0) ? "enabled" : "disabled");
1051
Yuta HIGUCHI2341e602017-03-08 20:10:08 -08001052 // wire value for OFOpticalPortFeatures.USE_FREQ
Yuta HIGUCHIf42a2cc2017-05-02 17:04:31 -07001053 long useFreq = OFOpticalPortFeaturesSerializerVer14.USE_FREQ_VAL;
Laszlo Papp20cfb6f2018-01-08 16:06:53 +00001054 if ((supported & useFreq) != 0) {
Yuta HIGUCHI2341e602017-03-08 20:10:08 -08001055 // unit is in Frequency Mhz
Laszlo Papp20cfb6f2018-01-08 16:06:53 +00001056 annotations.set(AK_RX_MIN_FREQ_HZ, mhzToAnnotation(rxMin));
1057 annotations.set(AK_RX_MAX_FREQ_HZ, mhzToAnnotation(rxMax));
1058 annotations.set(AK_RX_GRID_HZ, mhzToAnnotation(rxGrid));
Yuta HIGUCHI2341e602017-03-08 20:10:08 -08001059
Laszlo Papp20cfb6f2018-01-08 16:06:53 +00001060 annotations.set(AK_TX_MIN_FREQ_HZ, mhzToAnnotation(txMin));
1061 annotations.set(AK_TX_MAX_FREQ_HZ, mhzToAnnotation(txMax));
1062 annotations.set(AK_TX_GRID_HZ, mhzToAnnotation(txGrid));
Yuta HIGUCHI2341e602017-03-08 20:10:08 -08001063
1064 // FIXME pretty confident this is not going to happen
1065 // unless Device models Tx/Rx ports as separate port
Laszlo Papp20cfb6f2018-01-08 16:06:53 +00001066 if (rxMin == txMin) {
Yuta HIGUCHI2341e602017-03-08 20:10:08 -08001067 annotations.set(AK_MIN_FREQ_HZ,
Laszlo Papp20cfb6f2018-01-08 16:06:53 +00001068 mhzToAnnotation(rxMin));
Yuta HIGUCHI2341e602017-03-08 20:10:08 -08001069 }
Laszlo Papp20cfb6f2018-01-08 16:06:53 +00001070 if (rxMax == txMax) {
Yuta HIGUCHI2341e602017-03-08 20:10:08 -08001071 annotations.set(AK_MAX_FREQ_HZ,
Laszlo Papp20cfb6f2018-01-08 16:06:53 +00001072 mhzToAnnotation(rxMax));
1073 }
1074 if (rxGrid == txGrid) {
1075 annotations.set(AK_GRID_HZ,
1076 mhzToAnnotation(rxGrid));
Yuta HIGUCHI2341e602017-03-08 20:10:08 -08001077 }
1078
1079 } else {
1080 // unit is in Lambda nm * 100
Yuta HIGUCHIf42a2cc2017-05-02 17:04:31 -07001081 annotations.set(AK_RX_MIN_FREQ_HZ, lambdaToAnnotationHz(rxMin));
1082 annotations.set(AK_RX_MAX_FREQ_HZ, lambdaToAnnotationHz(rxMax));
Laszlo Papp20cfb6f2018-01-08 16:06:53 +00001083 annotations.set(AK_RX_GRID_HZ, lambdaToAnnotationHz(rxGrid));
Yuta HIGUCHIf42a2cc2017-05-02 17:04:31 -07001084
1085 annotations.set(AK_TX_MIN_FREQ_HZ, lambdaToAnnotationHz(txMin));
1086 annotations.set(AK_TX_MAX_FREQ_HZ, lambdaToAnnotationHz(txMax));
Laszlo Papp20cfb6f2018-01-08 16:06:53 +00001087 annotations.set(AK_TX_GRID_HZ, lambdaToAnnotationHz(txGrid));
Yuta HIGUCHIf42a2cc2017-05-02 17:04:31 -07001088
1089 // FIXME pretty confident this is not going to happen
1090 // unless Device models Tx/Rx ports as separate port
1091 if (rxMin == txMin) {
1092 annotations.set(AK_MIN_FREQ_HZ,
1093 lambdaToAnnotationHz(rxMin));
1094 }
1095 if (rxMax == txMax) {
1096 annotations.set(AK_MAX_FREQ_HZ,
1097 lambdaToAnnotationHz(rxMax));
1098 }
Laszlo Papp20cfb6f2018-01-08 16:06:53 +00001099 if (rxGrid == txGrid) {
1100 annotations.set(AK_GRID_HZ,
1101 lambdaToAnnotationHz(rxGrid));
1102 }
Yuta HIGUCHIf42a2cc2017-05-02 17:04:31 -07001103
Yuta HIGUCHI2341e602017-03-08 20:10:08 -08001104 }
Laszlo Papp20cfb6f2018-01-08 16:06:53 +00001105
1106 int txPwr = OFOpticalPortFeaturesSerializerVer14.TX_PWR_VAL;
1107 long txPwrMin = optical.get().getTxPwrMin();
1108 long txPwrMax = optical.get().getTxPwrMax();
1109 annotations.set(AK_TX_PWR_FEATURE, ((supported & txPwr) != 0) ? "enabled" : "disabled");
1110 annotations.set(AK_TX_PWR_MIN, Long.toString(txPwrMin));
1111 annotations.set(AK_TX_PWR_MAX, Long.toString(txPwrMax));
Yuta HIGUCHI2341e602017-03-08 20:10:08 -08001112
1113 // TODO How to determine appropriate port type?
1114
1115 return new DefaultPortDescription(portNo,
1116 enabled,
1117 FIBER,
1118 portSpeed(port),
1119 annotations.build());
1120 }
1121
1122 // fall back default
1123 return new DefaultPortDescription(portNo,
1124 enabled,
1125 COPPER,
1126 portSpeed(port),
1127 annotations.build());
1128
Ray Milkeye0fade72015-01-15 13:29:47 -08001129 }
1130
1131 /**
Ayaka Koshibe5460d622015-05-14 12:19:19 -07001132 * Build a portDescription from a given Ethernet port description.
tomff7eb7c2014-09-08 12:49:03 -07001133 *
alshabiba14f3642014-09-05 09:31:31 -07001134 * @param port the port to build from.
1135 * @return portDescription for the port.
1136 */
1137 private PortDescription buildPortDescription(OFPortDesc port) {
Yuta HIGUCHI2341e602017-03-08 20:10:08 -08001138 if (port.getVersion().wireVersion >= OFVersion.OF_14.getWireVersion()) {
1139 return buildPortDescription14(port);
1140 }
Thomas Vachuskad16ce182014-10-29 17:25:29 -07001141 PortNumber portNo = PortNumber.portNumber(port.getPortNo().getPortNumber());
1142 boolean enabled =
1143 !port.getState().contains(OFPortState.LINK_DOWN) &&
1144 !port.getConfig().contains(OFPortConfig.PORT_DOWN);
1145 Port.Type type = port.getCurr().contains(OFPortFeatures.PF_FIBER) ? FIBER : COPPER;
mskalac584bd92017-11-28 15:51:17 +01001146 boolean adminDown = port.getConfig().contains(OFPortConfig.PORT_DOWN);
1147 SparseAnnotations annotations = makePortAnnotation(port.getName(),
1148 port.getHwAddr().toString(),
1149 adminDown).build();
Ray Milkeye0fade72015-01-15 13:29:47 -08001150 return new DefaultPortDescription(portNo, enabled, type,
1151 portSpeed(port), annotations);
alshabiba14f3642014-09-05 09:31:31 -07001152 }
Ayaka Koshibeab91cc42014-09-25 10:20:52 -07001153
Ayaka Koshibe5460d622015-05-14 12:19:19 -07001154 /**
1155 * Build a portDescription from a given a port description describing some
1156 * Optical port.
1157 *
1158 * @param port description property type.
1159 * @param port the port to build from.
1160 * @return portDescription for the port.
1161 */
Jimmy Yan4deb03b2016-06-24 10:53:54 -07001162 private PortDescription buildPortDescription(PortDescPropertyType ptype, OFPortOptical port,
1163 OpenFlowOpticalSwitch opsw) {
Jon Hallcbd1b392017-01-18 20:15:44 -08001164 checkArgument(!port.getDesc().isEmpty());
Marc De Leenheerfc913dd2015-07-30 16:04:55 -07001165
Ayaka Koshibe5460d622015-05-14 12:19:19 -07001166 // Minimally functional fixture. This needs to be fixed as we add better support.
1167 PortNumber portNo = PortNumber.portNumber(port.getPortNo().getPortNumber());
Ayaka Koshibe74b55272015-05-28 15:16:04 -07001168
Ayaka Koshibe5460d622015-05-14 12:19:19 -07001169 boolean enabled = !port.getState().contains(OFPortState.LINK_DOWN)
1170 && !port.getConfig().contains(OFPortConfig.PORT_DOWN);
mskalac584bd92017-11-28 15:51:17 +01001171 boolean adminDown = port.getConfig().contains(OFPortConfig.PORT_DOWN);
1172 SparseAnnotations annotations = makePortAnnotation(port.getName(),
1173 port.getHwAddr().toString(),
1174 adminDown).build();
Ayaka Koshibe5460d622015-05-14 12:19:19 -07001175
1176 if (port.getVersion() == OFVersion.OF_13
1177 && ptype == PortDescPropertyType.OPTICAL_TRANSPORT) {
1178 // At this point, not much is carried in the optical port message.
Ayaka Koshibeae541732015-05-19 13:37:27 -07001179 LOG.debug("Optical transport port message {}", port.toString());
Ayaka Koshibe5460d622015-05-14 12:19:19 -07001180 } else {
1181 // removable once 1.4+ support complete.
Ayaka Koshibeae541732015-05-19 13:37:27 -07001182 LOG.debug("Unsupported optical port properties");
Ayaka Koshibe5460d622015-05-14 12:19:19 -07001183 }
Marc De Leenheerfc913dd2015-07-30 16:04:55 -07001184
1185 OFPortDescPropOpticalTransport desc = port.getDesc().get(0);
1186 switch (desc.getPortSignalType()) {
1187 // FIXME: use constants once loxi has full optical extensions
1188 case 2: // OMS port
1189 // Assume complete optical spectrum and 50 GHz grid
Jimmy Yan4deb03b2016-06-24 10:53:54 -07001190 Set<OchSignal> signals = null;
1191 if (opsw instanceof HandlerBehaviour) {
1192 DriverHandler driverHandler = ((HandlerBehaviour) opsw).handler();
1193 if (driverHandler != null && driverHandler.hasBehaviour(LambdaQuery.class)) {
1194 try {
1195 signals = driverHandler.behaviour(LambdaQuery.class).queryLambdas(portNo);
1196 } catch (NullPointerException e) {
1197 signals = null;
1198 }
1199 }
1200 }
1201 Frequency minFreq;
1202 Frequency maxFreq;
1203 Frequency channelSpacing;
1204 if (signals == null || signals.isEmpty()) {
1205 minFreq = Spectrum.U_BAND_MIN;
1206 maxFreq = Spectrum.O_BAND_MAX;
1207 channelSpacing = Frequency.ofGHz(50);
1208 } else {
1209 Comparator<OchSignal> compare =
1210 (OchSignal a, OchSignal b) -> a.spacingMultiplier() - b.spacingMultiplier();
1211 OchSignal minOch = Collections.min(signals, compare);
1212 OchSignal maxOch = Collections.max(signals, compare);
1213 minFreq = minOch.centralFrequency();
1214 maxFreq = maxOch.centralFrequency();
1215 channelSpacing = minOch.channelSpacing().frequency();
1216 }
1217 return omsPortDescription(portNo, enabled, minFreq,
1218 maxFreq, channelSpacing, annotations);
Marc De Leenheerfc913dd2015-07-30 16:04:55 -07001219 case 5: // OCH port
1220 OchSignal signal = new OchSignal(GridType.DWDM, ChannelSpacing.CHL_50GHZ, 0, 4);
HIGUCHI Yuta34a3f692016-01-09 21:08:57 -08001221 return ochPortDescription(portNo, enabled, OduSignalType.ODU4,
Marc De Leenheerfc913dd2015-07-30 16:04:55 -07001222 true, signal, annotations);
1223 default:
1224 break;
1225 }
1226
Ayaka Koshibe74b55272015-05-28 15:16:04 -07001227 return new DefaultPortDescription(portNo, enabled, FIBER, 0, annotations);
Ayaka Koshibe5460d622015-05-14 12:19:19 -07001228 }
1229
Marc De Leenheerb9311372015-07-09 11:36:49 -07001230 /**
1231 * Build a portDescription from a given port description describing a fiber switch optical port.
1232 *
1233 * @param port description property type.
1234 * @param port the port to build from.
1235 * @return portDescription for the port.
1236 */
1237 private PortDescription buildPortDescription(OFCalientPortDescStatsEntry port) {
1238 PortNumber portNo = PortNumber.portNumber(port.getPortNo().getPortNumber());
1239
Marc De Leenheerc662d322016-02-18 16:05:10 -08001240 // Use the alias name if it's available
1241 String name = port.getName();
1242 List<OFCalientPortDescProp> props = port.getProperties();
Jon Hallcbd1b392017-01-18 20:15:44 -08001243 if (props != null && !props.isEmpty()) {
Marc De Leenheerc662d322016-02-18 16:05:10 -08001244 OFCalientPortDescPropOptical propOptical = (OFCalientPortDescPropOptical) props.get(0);
1245 if (propOptical != null) {
1246 name = propOptical.getInAlias();
1247 }
1248 }
1249
Marc De Leenheerb9311372015-07-09 11:36:49 -07001250 // FIXME when Calient OF agent reports port status
1251 boolean enabled = true;
mskalac584bd92017-11-28 15:51:17 +01001252 boolean adminDown = false;
1253 SparseAnnotations annotations = makePortAnnotation(name, port.getHwAddr().toString(), adminDown).build();
Marc De Leenheerb9311372015-07-09 11:36:49 -07001254
Marc De Leenheerfc913dd2015-07-30 16:04:55 -07001255 // S160 data sheet
1256 // Wavelength range: 1260 - 1630 nm, grid is irrelevant for this type of switch
HIGUCHI Yuta95d83e82016-04-26 12:13:48 -07001257 return omsPortDescription(portNo, enabled,
Yuta HIGUCHIf42a2cc2017-05-02 17:04:31 -07001258 Spectrum.O_BAND_MIN, Spectrum.O_BAND_MAX, Frequency.ofGHz(50), annotations);
Marc De Leenheerb9311372015-07-09 11:36:49 -07001259 }
1260
alshabibafc514a2014-12-01 14:44:05 -08001261 private PortDescription buildPortDescription(OFPortStatus status) {
1262 OFPortDesc port = status.getDesc();
1263 if (status.getReason() != OFPortReason.DELETE) {
1264 return buildPortDescription(port);
1265 } else {
Yuta HIGUCHI6512f3e2017-05-18 17:21:24 -07001266 PortDescription desc = buildPortDescription(port);
1267 if (desc.isEnabled()) {
1268 return DefaultPortDescription.builder(desc)
1269 .isEnabled(false)
1270 .build();
1271 }
1272 return desc;
alshabibafc514a2014-12-01 14:44:05 -08001273 }
1274 }
1275
Yuta HIGUCHI2341e602017-03-08 20:10:08 -08001276 /**
1277 * Returns port speed in Mbps.
1278 *
1279 * @param port description to parse
1280 * @return port speed in Mbps
1281 */
Thomas Vachuskad16ce182014-10-29 17:25:29 -07001282 private long portSpeed(OFPortDesc port) {
Yuta HIGUCHI2341e602017-03-08 20:10:08 -08001283 if (port.getVersion().getWireVersion() >= OFVersion.OF_14.getWireVersion()) {
1284 // OFPortDescPropEthernet
1285 return port.getProperties().stream()
1286 .filter(OFPortDescPropEthernet.class::isInstance)
1287 .map(OFPortDescPropEthernet.class::cast)
1288 .mapToLong(OFPortDescPropEthernet::getCurrSpeed)
1289 .map(kbps -> kbps / KBPS)
1290 .findAny()
1291 .orElse(PortSpeed.SPEED_NONE.getSpeedBps() / MBPS);
1292 }
Thomas Vachuskad16ce182014-10-29 17:25:29 -07001293 if (port.getVersion() == OFVersion.OF_13) {
Charles Chan25b77322015-12-13 01:00:56 -08001294 // Note: getCurrSpeed() returns a value in kbps (this also applies to OF_11 and OF_12)
1295 return port.getCurrSpeed() / KBPS;
Thomas Vachuskad16ce182014-10-29 17:25:29 -07001296 }
Yuta HIGUCHI2341e602017-03-08 20:10:08 -08001297 // < OF1.3
Thomas Vachuskad16ce182014-10-29 17:25:29 -07001298 PortSpeed portSpeed = PortSpeed.SPEED_NONE;
1299 for (OFPortFeatures feat : port.getCurr()) {
1300 portSpeed = PortSpeed.max(portSpeed, feat.getPortSpeed());
1301 }
Thomas Vachuska98eda532014-10-29 17:31:02 -07001302 return portSpeed.getSpeedBps() / MBPS;
Thomas Vachuskad16ce182014-10-29 17:25:29 -07001303 }
sangho538108b2015-04-08 14:29:20 -07001304
1305 @Override
1306 public void handleMessage(Dpid dpid, OFMessage msg) {
Ray Milkey7abe50b2016-04-13 11:57:44 -07001307 if (isDisabled) {
1308 return;
sangho538108b2015-04-08 14:29:20 -07001309 }
Ray Milkey7abe50b2016-04-13 11:57:44 -07001310
1311 try {
1312 switch (msg.getType()) {
1313 case STATS_REPLY:
1314 if (((OFStatsReply) msg).getStatsType() == OFStatsType.PORT) {
1315 OFPortStatsReply portStatsReply = (OFPortStatsReply) msg;
1316 List<OFPortStatsEntry> portStatsReplyList = portStatsReplies.get(dpid);
1317 if (portStatsReplyList == null) {
Madan Jampani84382b92016-06-22 08:26:49 -07001318 portStatsReplyList = Lists.newCopyOnWriteArrayList();
Ray Milkey7abe50b2016-04-13 11:57:44 -07001319 }
1320 portStatsReplyList.addAll(portStatsReply.getEntries());
1321 portStatsReplies.put(dpid, portStatsReplyList);
1322 if (!portStatsReply.getFlags().contains(OFStatsReplyFlags.REPLY_MORE)) {
Charles Chanb3ef0db2016-05-03 11:11:00 -07001323 List<OFPortStatsEntry> statsEntries = portStatsReplies.get(dpid);
1324 if (statsEntries != null) {
1325 pushPortMetrics(dpid, statsEntries);
1326 statsEntries.clear();
1327 }
Ray Milkey7abe50b2016-04-13 11:57:44 -07001328 }
yjimmyy646aa022016-07-05 12:09:50 -07001329 } else if (((OFStatsReply) msg).getStatsType() == OFStatsType.EXPERIMENTER) {
1330 OpenFlowSwitch sw = controller.getSwitch(dpid);
sangyun-han147e6f42016-07-26 17:14:41 +09001331 if (sw == null) {
1332 LOG.error("Switch {} is not found", dpid);
1333 break;
1334 }
yjimmyy646aa022016-07-05 12:09:50 -07001335 if (sw instanceof OpenFlowOpticalSwitch) {
1336 // Optical switch uses experimenter stats message to update power
1337 List<PortDescription> portDescs =
1338 ((OpenFlowOpticalSwitch) sw).processExpPortStats(msg);
1339 if (!portDescs.isEmpty()) {
1340 providerService.updatePorts(DeviceId.deviceId(Dpid.uri(dpid)), portDescs);
1341 }
1342 }
Ray Milkey7abe50b2016-04-13 11:57:44 -07001343 }
1344 break;
1345 case ERROR:
1346 if (((OFErrorMsg) msg).getErrType() == OFErrorType.PORT_MOD_FAILED) {
1347 LOG.error("port mod failed");
1348 }
1349 default:
1350 break;
1351 }
1352 } catch (IllegalStateException e) {
1353 // system is shutting down and the providerService is no longer
1354 // valid. Messages cannot be processed.
1355 }
1356 }
1357
1358 private void disable() {
1359 isDisabled = true;
sangho538108b2015-04-08 14:29:20 -07001360 }
alshabibf1216ed2014-09-03 11:53:54 -07001361 }
Saurav Dasa2d37502016-03-25 17:50:40 -07001362
1363
tomb5a46e62014-08-26 14:20:00 -07001364}