blob: ba691fdf106c5de2ebd473265c1cdb7e60cae4fd [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;
Saurav Dasa2d37502016-03-25 17:50:40 -070078import org.projectfloodlight.openflow.protocol.OFErrorMsg;
79import org.projectfloodlight.openflow.protocol.OFErrorType;
Yafit Hadara9a73de2015-09-06 13:52:52 +030080import org.projectfloodlight.openflow.protocol.OFExpPort;
81import org.projectfloodlight.openflow.protocol.OFExpPortDescPropOpticalTransport;
82import org.projectfloodlight.openflow.protocol.OFExpPortOpticalTransportLayerEntry;
Ayaka Koshibee8708e32014-10-22 13:40:18 -070083import org.projectfloodlight.openflow.protocol.OFFactory;
sangho538108b2015-04-08 14:29:20 -070084import org.projectfloodlight.openflow.protocol.OFMessage;
Yafit Hadara9a73de2015-09-06 13:52:52 +030085import org.projectfloodlight.openflow.protocol.OFObject;
alshabib4680bb62014-09-04 17:15:08 -070086import org.projectfloodlight.openflow.protocol.OFPortConfig;
alshabib25c8eec2014-09-04 16:41:31 -070087import org.projectfloodlight.openflow.protocol.OFPortDesc;
Yuta HIGUCHI2341e602017-03-08 20:10:08 -080088import org.projectfloodlight.openflow.protocol.OFPortDescPropEthernet;
89import org.projectfloodlight.openflow.protocol.OFPortDescPropOptical;
Marc De Leenheerfc913dd2015-07-30 16:04:55 -070090import org.projectfloodlight.openflow.protocol.OFPortDescPropOpticalTransport;
Thomas Vachuskad16ce182014-10-29 17:25:29 -070091import org.projectfloodlight.openflow.protocol.OFPortFeatures;
Saurav Dasa2d37502016-03-25 17:50:40 -070092import org.projectfloodlight.openflow.protocol.OFPortMod;
Ayaka Koshibe5460d622015-05-14 12:19:19 -070093import org.projectfloodlight.openflow.protocol.OFPortOptical;
Yafit Hadara9a73de2015-09-06 13:52:52 +030094import org.projectfloodlight.openflow.protocol.OFPortOpticalTransportLayerClass;
95import org.projectfloodlight.openflow.protocol.OFPortOpticalTransportSignalType;
alshabibafc514a2014-12-01 14:44:05 -080096import org.projectfloodlight.openflow.protocol.OFPortReason;
alshabib4680bb62014-09-04 17:15:08 -070097import org.projectfloodlight.openflow.protocol.OFPortState;
sangho538108b2015-04-08 14:29:20 -070098import org.projectfloodlight.openflow.protocol.OFPortStatsEntry;
Laszlo Papp7cf60372018-01-11 00:06:43 +000099import org.projectfloodlight.openflow.protocol.OFPortStatsPropOptical;
sangho538108b2015-04-08 14:29:20 -0700100import org.projectfloodlight.openflow.protocol.OFPortStatsReply;
alshabiba14f3642014-09-05 09:31:31 -0700101import org.projectfloodlight.openflow.protocol.OFPortStatus;
sangho538108b2015-04-08 14:29:20 -0700102import org.projectfloodlight.openflow.protocol.OFStatsReply;
Thomas Vachuska893bf4b2015-05-29 18:13:18 -0700103import org.projectfloodlight.openflow.protocol.OFStatsReplyFlags;
sangho538108b2015-04-08 14:29:20 -0700104import org.projectfloodlight.openflow.protocol.OFStatsType;
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700105import org.projectfloodlight.openflow.protocol.OFVersion;
Yuta HIGUCHIf42a2cc2017-05-02 17:04:31 -0700106import org.projectfloodlight.openflow.protocol.ver14.OFOpticalPortFeaturesSerializerVer14;
Laszlo Papp7cf60372018-01-11 00:06:43 +0000107import org.projectfloodlight.openflow.protocol.ver14.OFPortStatsOpticalFlagsSerializerVer14;
Saurav Dasa2d37502016-03-25 17:50:40 -0700108import org.projectfloodlight.openflow.types.OFPort;
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700109import org.projectfloodlight.openflow.types.PortSpeed;
tomb5a46e62014-08-26 14:20:00 -0700110import org.slf4j.Logger;
tom5f38b3a2014-08-27 23:50:54 -0700111
Marc De Leenheer8aba62f2017-04-25 14:33:37 -0700112import java.util.ArrayList;
113import java.util.Collection;
114import java.util.Collections;
115import java.util.Comparator;
116import java.util.Dictionary;
Jimmy Jine9b7a022016-08-12 16:56:48 -0700117import java.util.EnumSet;
Marc De Leenheer8aba62f2017-04-25 14:33:37 -0700118import java.util.HashMap;
119import java.util.HashSet;
120import java.util.List;
Yuta HIGUCHI2341e602017-03-08 20:10:08 -0800121import java.util.Optional;
Marc De Leenheer8aba62f2017-04-25 14:33:37 -0700122import java.util.Properties;
123import java.util.Set;
124import java.util.Timer;
125
126import static com.google.common.base.Preconditions.checkArgument;
127import static com.google.common.base.Strings.isNullOrEmpty;
128import static org.onlab.util.Tools.get;
129import static org.onosproject.net.DeviceId.deviceId;
130import static org.onosproject.net.Port.Type.COPPER;
131import static org.onosproject.net.Port.Type.FIBER;
132import static org.onosproject.net.optical.device.OchPortHelper.ochPortDescription;
133import static org.onosproject.net.optical.device.OduCltPortHelper.oduCltPortDescription;
134import static org.onosproject.net.optical.device.OmsPortHelper.omsPortDescription;
135import static org.onosproject.net.optical.device.OtuPortHelper.otuPortDescription;
136import static org.onosproject.openflow.controller.Dpid.dpid;
137import static org.onosproject.openflow.controller.Dpid.uri;
138import static org.slf4j.LoggerFactory.getLogger;
tom782a7cf2014-09-11 23:58:38 -0700139
tomb5a46e62014-08-26 14:20:00 -0700140/**
tomb1260e42014-08-26 18:39:57 -0700141 * Provider which uses an OpenFlow controller to detect network
tome06f8552014-08-26 16:58:42 -0700142 * infrastructure devices.
tomb5a46e62014-08-26 14:20:00 -0700143 */
tomb1260e42014-08-26 18:39:57 -0700144@Component(immediate = true)
tomab21e7c2014-08-26 15:23:08 -0700145public class OpenFlowDeviceProvider extends AbstractProvider implements DeviceProvider {
tomb5a46e62014-08-26 14:20:00 -0700146
alshabiba89cc582014-09-09 16:43:00 -0700147 private static final Logger LOG = getLogger(OpenFlowDeviceProvider.class);
Thomas Vachuskad07c0922015-10-06 14:48:06 -0700148
Yuta HIGUCHI2341e602017-03-08 20:10:08 -0800149 // TODO Some duplicate with one defined in OpticalAnnotations
150 // slice out optical specific handling and consolidate.
151 /**
152 * Annotation key for minimum frequency in Hz.
153 * Value is expected to be an integer.
154 */
155 public static final String AK_MIN_FREQ_HZ = "minFrequency";
156
157 /**
158 * Annotation key for maximum frequency in Hz.
159 * Value is expected be an integer.
160 */
161 public static final String AK_MAX_FREQ_HZ = "maxFrequency";
162
163 /**
164 * Annotation key for grid in Hz.
165 * Value is expected to be an integer.
166 */
167 public static final String AK_GRID_HZ = "grid";
168
Laszlo Papp20cfb6f2018-01-08 16:06:53 +0000169 /**
Yuta HIGUCHI2341e602017-03-08 20:10:08 -0800170 * Annotation key for minimum frequency in Hz.
171 * Value is expected to be an integer.
172 */
173 public static final String AK_TX_MIN_FREQ_HZ = "txMinFrequency";
174
175 /**
176 * Annotation key for maximum frequency in Hz.
177 * Value is expected be an integer.
178 */
179 public static final String AK_TX_MAX_FREQ_HZ = "txMaxFrequency";
180
181 /**
182 * Annotation key for grid in Hz.
183 * Value is expected to be an integer.
184 */
185 public static final String AK_TX_GRID_HZ = "txGrid";
186
187 /**
188 * Annotation key for minimum frequency in Hz.
189 * Value is expected to be an integer.
190 */
191 public static final String AK_RX_MIN_FREQ_HZ = "rxMinFrequency";
192
193 /**
194 * Annotation key for maximum frequency in Hz.
195 * Value is expected be an integer.
196 */
197 public static final String AK_RX_MAX_FREQ_HZ = "rxMaxFrequency";
198
199 /**
200 * Annotation key for grid in Hz.
201 * Value is expected to be an integer.
202 */
203 public static final String AK_RX_GRID_HZ = "rxGrid";
204
Laszlo Papp20cfb6f2018-01-08 16:06:53 +0000205 /**
206 * Annotation key for minimum transmit power in dBm*10.
207 * Value is expected to be an integer.
208 */
209 public static final String AK_TX_PWR_MIN = "txPowerMin";
210
211 /**
212 * Annotation key for maximum transmit power in dBm*10.
213 * Value is expected to be an integer.
214 */
215 public static final String AK_TX_PWR_MAX = "txPowerMax";
216
Laszlo Papp7cf60372018-01-11 00:06:43 +0000217
218
219 // Port Stats annotations
220
221 /**
222 * Annotation key for transmit frequency in Hz.
223 * Value is expected be an integer.
224 */
225 public static final String AK_TX_FREQ_HZ = "txFrequency";
226
227 /**
228 * Annotation key for transmit offset in Hz.
229 * Value is expected be an integer.
230 */
231 public static final String AK_TX_OFFSET_HZ = "txOffset";
232
233 /**
234 * Annotation key for transmit grid spacing in Hz.
235 * Value is expected be an integer.
236 */
237 public static final String AK_TX_GRID_SPAN_HZ = "txGridSpan";
238
239 /**
240 * Annotation key for receive frequency in Hz.
241 * Value is expected be an integer.
242 */
243 public static final String AK_RX_FREQ_HZ = "rxFrequency";
244
245 /**
246 * Annotation key for receive offset in Hz.
247 * Value is expected be an integer.
248 */
249 public static final String AK_RX_OFFSET_HZ = "rxOffset";
250
251 /**
252 * Annotation key for receive grid spacing in Hz.
253 * Value is expected be an integer.
254 */
255 public static final String AK_RX_GRID_SPAN_HZ = "rxGridSpan";
256
257 /**
258 * Annotation key for transmit power in dBm*10.
259 * Value is expected to be an integer.
260 */
261 public static final String AK_TX_PWR = "txPower";
262
263 /**
264 * Annotation key for receive power feature.
265 * Value is expected to be "enabled" or "disabled"
266 */
267 public static final String AK_RX_PWR_FEATURE = "rxPwrFeature";
268
269 /**
270 * Annotation key for receive power in dBm*10.
271 * Value is expected to be an integer.
272 */
273 public static final String AK_RX_PWR = "rxPower";
274
275 /**
276 * Annotation key for transmit bias feature.
277 * Value is expected to be "enabled" or "disabled"
278 */
279 public static final String AK_TX_BIAS_FEATURE = "txBiasFeature";
280
281 /**
282 * Annotation key for transmit bias current in mA*10.
283 * Value is expected to be an integer.
284 */
285 public static final String AK_BIAS_CURRENT = "biasCurrent";
286
287 /**
288 * Annotation key for transmit temperature feature.
289 * Value is expected to be "enabled" or "disabled"
290 */
291 public static final String AK_TX_TEMP_FEATURE = "txTempFeature";
292
293 /**
294 * Annotation key for transmit laser temperature in C*10.
295 * Value is expected to be an integer.
296 */
297 public static final String AK_TEMPERATURE = "temperature";
298
299
300 // Common feature annotations
301
302 /**
303 * Annotation key for transmit tune feature.
304 * Value is expected to be "enabled" or "disabled"
305 */
306 public static final String AK_TX_TUNE_FEATURE = "txTuneFeature";
307
308 /**
309 * Annotation key for receive tune feature.
310 * Value is expected to be "enabled" or "disabled"
311 */
312 public static final String AK_RX_TUNE_FEATURE = "rxTuneFeature";
313
314 /**
315 * Annotation key for transmit power feature.
316 * Value is expected to be "enabled" or "disabled"
317 */
318 public static final String AK_TX_PWR_FEATURE = "txPwrFeature";
319
320
Charles Chan25b77322015-12-13 01:00:56 -0800321 //TODO consider renaming KBPS and MBPS (as they are used to convert by division)
322 private static final long KBPS = 1_000;
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700323 private static final long MBPS = 1_000 * 1_000;
Rimon Ashkenazye3201032016-01-11 14:27:30 +0200324 private static final Frequency FREQ50 = Frequency.ofGHz(50);
325 private static final Frequency FREQ191_7 = Frequency.ofGHz(191_700);
Sho SHIMIZU00762ee2016-01-05 16:32:24 -0800326 private static final Frequency FREQ4_4 = Frequency.ofGHz(4_400);
tomb5a46e62014-08-26 14:20:00 -0700327
328 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
tom96dfcab2014-08-28 09:26:03 -0700329 protected DeviceProviderRegistry providerRegistry;
tomab21e7c2014-08-26 15:23:08 -0700330
tom5f38b3a2014-08-27 23:50:54 -0700331 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
332 protected OpenFlowController controller;
333
Dusan Pajinbab8a5e2015-07-24 17:37:19 +0200334 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
335 protected ComponentConfigService cfgService;
336
Victor Silvaac8dab02016-11-28 16:33:08 -0300337 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
338 protected DriverService driverService;
339
tomab21e7c2014-08-26 15:23:08 -0700340 private DeviceProviderService providerService;
tomb5a46e62014-08-26 14:20:00 -0700341
sangho538108b2015-04-08 14:29:20 -0700342 private final InternalDeviceProvider listener = new InternalDeviceProvider();
343
Simon Hunt7a307652016-06-23 01:17:29 -0700344 private static final String POLL_PROP_NAME = "portStatsPollFrequency";
345 private static final int POLL_INTERVAL = 5;
346 @Property(name = POLL_PROP_NAME, intValue = POLL_INTERVAL,
Dusan Pajinbab8a5e2015-07-24 17:37:19 +0200347 label = "Frequency (in seconds) for polling switch Port statistics")
348 private int portStatsPollFrequency = POLL_INTERVAL;
sangho538108b2015-04-08 14:29:20 -0700349
Victor Silvaff5871b2016-10-04 18:08:47 -0300350 private final Timer timer = new Timer("onos-openflow-portstats-collector");
sangyun-han07a572f2016-07-29 21:14:27 +0900351
sangho538108b2015-04-08 14:29:20 -0700352 private HashMap<Dpid, PortStatsCollector> collectors = Maps.newHashMap();
tomd40fc7a2014-09-04 16:41:10 -0700353
tomab21e7c2014-08-26 15:23:08 -0700354 /**
355 * Creates an OpenFlow device provider.
356 */
357 public OpenFlowDeviceProvider() {
Brian O'Connorabafb502014-12-02 22:26:20 -0800358 super(new ProviderId("of", "org.onosproject.provider.openflow"));
tomab21e7c2014-08-26 15:23:08 -0700359 }
360
tomb5a46e62014-08-26 14:20:00 -0700361 @Activate
Dusan Pajinbab8a5e2015-07-24 17:37:19 +0200362 public void activate(ComponentContext context) {
363 cfgService.registerProperties(getClass());
tom96dfcab2014-08-28 09:26:03 -0700364 providerService = providerRegistry.register(this);
tomd40fc7a2014-09-04 16:41:10 -0700365 controller.addListener(listener);
sangho538108b2015-04-08 14:29:20 -0700366 controller.addEventListener(listener);
Jian Li152b8852015-12-07 14:47:25 -0800367
Victor Silva7944d142016-07-08 00:14:55 -0300368 modified(context);
369
Thomas Vachuskad07c0922015-10-06 14:48:06 -0700370 connectInitialDevices();
alshabiba89cc582014-09-09 16:43:00 -0700371 LOG.info("Started");
tomb5a46e62014-08-26 14:20:00 -0700372 }
373
374 @Deactivate
Dusan Pajinbab8a5e2015-07-24 17:37:19 +0200375 public void deactivate(ComponentContext context) {
376 cfgService.unregisterProperties(getClass(), false);
Ray Milkey7abe50b2016-04-13 11:57:44 -0700377 listener.disable();
tomd40fc7a2014-09-04 16:41:10 -0700378 controller.removeListener(listener);
Thomas Vachuskad07c0922015-10-06 14:48:06 -0700379 providerRegistry.unregister(this);
Thomas Vachuska74bcd922015-06-02 13:08:57 -0700380 collectors.values().forEach(PortStatsCollector::stop);
Thomas Vachuskaa394b952016-06-14 15:02:09 -0700381 collectors.clear();
tomab21e7c2014-08-26 15:23:08 -0700382 providerService = null;
alshabiba89cc582014-09-09 16:43:00 -0700383 LOG.info("Stopped");
tomb5a46e62014-08-26 14:20:00 -0700384 }
385
Dusan Pajinbab8a5e2015-07-24 17:37:19 +0200386 @Modified
387 public void modified(ComponentContext context) {
Victor Silva7944d142016-07-08 00:14:55 -0300388 Dictionary<?, ?> properties = context != null ? context.getProperties() : new Properties();
Dusan Pajinbab8a5e2015-07-24 17:37:19 +0200389 int newPortStatsPollFrequency;
390 try {
Simon Hunt7a307652016-06-23 01:17:29 -0700391 String s = get(properties, POLL_PROP_NAME);
Dusan Pajinbab8a5e2015-07-24 17:37:19 +0200392 newPortStatsPollFrequency = isNullOrEmpty(s) ? portStatsPollFrequency : Integer.parseInt(s.trim());
393
394 } catch (NumberFormatException | ClassCastException e) {
395 newPortStatsPollFrequency = portStatsPollFrequency;
396 }
397
398 if (newPortStatsPollFrequency != portStatsPollFrequency) {
399 portStatsPollFrequency = newPortStatsPollFrequency;
400 collectors.values().forEach(psc -> psc.adjustPollInterval(portStatsPollFrequency));
401 }
402
403 LOG.info("Settings: portStatsPollFrequency={}", portStatsPollFrequency);
404 }
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700405
Thomas Vachuskad07c0922015-10-06 14:48:06 -0700406 private void connectInitialDevices() {
407 for (OpenFlowSwitch sw : controller.getSwitches()) {
408 try {
409 listener.switchAdded(new Dpid(sw.getId()));
410 } catch (Exception e) {
411 LOG.warn("Failed initially adding {} : {}", sw.getStringId(), e.getMessage());
412 LOG.debug("Error details:", e);
413 // disconnect to trigger switch-add later
414 sw.disconnectSwitch();
415 }
Thomas Vachuskad07c0922015-10-06 14:48:06 -0700416 }
417 }
418
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700419 @Override
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700420 public boolean isReachable(DeviceId deviceId) {
421 OpenFlowSwitch sw = controller.getSwitch(dpid(deviceId.uri()));
Thomas Vachuskad07c0922015-10-06 14:48:06 -0700422 return sw != null && sw.isConnected();
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700423 }
424
tomab21e7c2014-08-26 15:23:08 -0700425 @Override
Ayaka Koshibe78bcbc12014-11-19 14:28:58 -0800426 public void triggerProbe(DeviceId deviceId) {
Madan Jampanic3328762015-05-31 12:39:27 -0700427 LOG.debug("Triggering probe on device {}", deviceId);
Ayaka Koshibee8708e32014-10-22 13:40:18 -0700428
Yuta HIGUCHI802d0e82014-10-31 16:07:37 -0700429 final Dpid dpid = dpid(deviceId.uri());
430 OpenFlowSwitch sw = controller.getSwitch(dpid);
431 if (sw == null || !sw.isConnected()) {
Ayaka Koshibe78bcbc12014-11-19 14:28:58 -0800432 LOG.error("Failed to probe device {} on sw={}", deviceId, sw);
Yuta HIGUCHI802d0e82014-10-31 16:07:37 -0700433 providerService.deviceDisconnected(deviceId);
Thomas Vachuska74bcd922015-06-02 13:08:57 -0700434 return;
Yuta HIGUCHI802d0e82014-10-31 16:07:37 -0700435 } else {
Ayaka Koshibe78bcbc12014-11-19 14:28:58 -0800436 LOG.trace("Confirmed device {} connection", deviceId);
Yuta HIGUCHI802d0e82014-10-31 16:07:37 -0700437 }
Ayaka Koshibee8708e32014-10-22 13:40:18 -0700438
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700439 // Prompt an update of port information. We can use any XID for this.
Ayaka Koshibee8708e32014-10-22 13:40:18 -0700440 OFFactory fact = sw.factory();
441 switch (fact.getVersion()) {
442 case OF_10:
443 sw.sendMsg(fact.buildFeaturesRequest().setXid(0).build());
444 break;
445 case OF_13:
Yuta HIGUCHI6512f3e2017-05-18 17:21:24 -0700446 case OF_14:
447 case OF_15:
Ayaka Koshibee8708e32014-10-22 13:40:18 -0700448 sw.sendMsg(fact.buildPortDescStatsRequest().setXid(0).build());
449 break;
450 default:
451 LOG.warn("Unhandled protocol version");
452 }
tomab21e7c2014-08-26 15:23:08 -0700453 }
454
455 @Override
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700456 public void roleChanged(DeviceId deviceId, MastershipRole newRole) {
alshabibf1216ed2014-09-03 11:53:54 -0700457 switch (newRole) {
tom782a7cf2014-09-11 23:58:38 -0700458 case MASTER:
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700459 controller.setRole(dpid(deviceId.uri()), RoleState.MASTER);
tom782a7cf2014-09-11 23:58:38 -0700460 break;
461 case STANDBY:
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700462 controller.setRole(dpid(deviceId.uri()), RoleState.EQUAL);
tom782a7cf2014-09-11 23:58:38 -0700463 break;
464 case NONE:
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700465 controller.setRole(dpid(deviceId.uri()), RoleState.SLAVE);
tom782a7cf2014-09-11 23:58:38 -0700466 break;
467 default:
468 LOG.error("Unknown Mastership state : {}", newRole);
alshabibf1216ed2014-09-03 11:53:54 -0700469
470 }
HIGUCHI Yuta1979f552015-12-28 21:24:26 -0800471 LOG.debug("Accepting mastership role change to {} for device {}", newRole, deviceId);
tomab21e7c2014-08-26 15:23:08 -0700472 }
473
Saurav Dasa2d37502016-03-25 17:50:40 -0700474 @Override
475 public void changePortState(DeviceId deviceId, PortNumber portNumber,
476 boolean enable) {
477 final Dpid dpid = dpid(deviceId.uri());
478 OpenFlowSwitch sw = controller.getSwitch(dpid);
479 if (sw == null || !sw.isConnected()) {
480 LOG.error("Failed to change portState on device {}", deviceId);
481 return;
482 }
483 OFPortMod.Builder pmb = sw.factory().buildPortMod();
484 OFPort port = OFPort.of((int) portNumber.toLong());
485 pmb.setPortNo(port);
Jimmy Jine9b7a022016-08-12 16:56:48 -0700486 Set<OFPortConfig> portConfig = EnumSet.noneOf(OFPortConfig.class);
487 if (!enable) {
488 portConfig.add(OFPortConfig.PORT_DOWN);
Saurav Dasa2d37502016-03-25 17:50:40 -0700489 }
Jimmy Jine9b7a022016-08-12 16:56:48 -0700490 pmb.setConfig(portConfig);
491 Set<OFPortConfig> portMask = EnumSet.noneOf(OFPortConfig.class);
492 portMask.add(OFPortConfig.PORT_DOWN);
493 pmb.setMask(portMask);
Saurav Dasa2d37502016-03-25 17:50:40 -0700494 pmb.setAdvertise(0x0);
495 for (OFPortDesc pd : sw.getPorts()) {
496 if (pd.getPortNo().equals(port)) {
497 pmb.setHwAddr(pd.getHwAddr());
498 break;
499 }
500 }
501 sw.sendMsg(Collections.singletonList(pmb.build()));
502 }
503
Thomas Vachuska893bf4b2015-05-29 18:13:18 -0700504 private void pushPortMetrics(Dpid dpid, List<OFPortStatsEntry> portStatsEntries) {
HIGUCHI Yuta1979f552015-12-28 21:24:26 -0800505 DeviceId deviceId = DeviceId.deviceId(Dpid.uri(dpid));
Ray Milkey3c4124a2016-08-31 12:09:06 -0700506 Collection<PortStatistics> stats =
507 buildPortStatistics(deviceId, ImmutableList.copyOf(portStatsEntries));
sangho538108b2015-04-08 14:29:20 -0700508 providerService.updatePortStatistics(deviceId, stats);
509 }
510
Laszlo Papp7cf60372018-01-11 00:06:43 +0000511
512 private static String mhzToAnnotation(long freqMhz) {
513 return Long.toString(Frequency.ofMHz(freqMhz).asHz());
514 }
515
Thomas Vachuska893bf4b2015-05-29 18:13:18 -0700516 private Collection<PortStatistics> buildPortStatistics(DeviceId deviceId,
517 List<OFPortStatsEntry> entries) {
sangho538108b2015-04-08 14:29:20 -0700518 HashSet<PortStatistics> stats = Sets.newHashSet();
519
Thomas Vachuska893bf4b2015-05-29 18:13:18 -0700520 for (OFPortStatsEntry entry : entries) {
Thomas Vachuskaf0397b52015-05-29 13:50:17 -0700521 try {
Thomas Vachuska4e6025e2015-06-03 11:25:21 -0700522 if (entry == null || entry.getPortNo() == null || entry.getPortNo().getPortNumber() < 0) {
Thomas Vachuskaf0397b52015-05-29 13:50:17 -0700523 continue;
524 }
Laszlo Papp7cf60372018-01-11 00:06:43 +0000525 DefaultAnnotations.Builder annotations = DefaultAnnotations.builder();
Laszlo Papp9dfb5122018-01-12 21:10:13 +0000526 boolean propSupported = entry.getVersion().getWireVersion() >= OFVersion.OF_14.getWireVersion();
527 Optional<OFPortStatsPropOptical> optical = propSupported ?
528 entry.getProperties().stream()
Laszlo Papp7cf60372018-01-11 00:06:43 +0000529 .filter(OFPortStatsPropOptical.class::isInstance)
530 .map(OFPortStatsPropOptical.class::cast)
Laszlo Papp9dfb5122018-01-12 21:10:13 +0000531 .findAny() : Optional.empty();
Laszlo Papp7cf60372018-01-11 00:06:43 +0000532 if (optical.isPresent()) {
533 long flags = optical.get().getFlags();
534
535 int txTune = OFPortStatsOpticalFlagsSerializerVer14.TX_TUNE_VAL;
536 long txFreq = optical.get().getTxFreqLmda();
537 long txOffset = optical.get().getTxOffset();
538 long txGridSpan = optical.get().getTxGridSpan();
539 annotations.set(AK_TX_TUNE_FEATURE, ((flags & txTune) != 0) ? "enabled" : "disabled");
540 annotations.set(AK_TX_FREQ_HZ, mhzToAnnotation(txFreq));
541 annotations.set(AK_TX_OFFSET_HZ, mhzToAnnotation(txOffset));
542 annotations.set(AK_TX_GRID_SPAN_HZ, mhzToAnnotation(txGridSpan));
543
544 int rxTune = OFPortStatsOpticalFlagsSerializerVer14.RX_TUNE_VAL;
545 long rxFreq = optical.get().getRxFreqLmda();
546 long rxOffset = optical.get().getRxOffset();
547 long rxGridSpan = optical.get().getRxGridSpan();
548 annotations.set(AK_RX_TUNE_FEATURE, ((flags & rxTune) != 0) ? "enabled" : "disabled");
549 annotations.set(AK_RX_FREQ_HZ, mhzToAnnotation(rxFreq));
550 annotations.set(AK_RX_OFFSET_HZ, mhzToAnnotation(rxOffset));
551 annotations.set(AK_RX_GRID_SPAN_HZ, mhzToAnnotation(rxGridSpan));
552
553 int txPwrVal = OFPortStatsOpticalFlagsSerializerVer14.TX_PWR_VAL;
554 int txPwr = optical.get().getTxPwr();
555 annotations.set(AK_TX_PWR_FEATURE, ((flags & txPwrVal) != 0) ? "enabled" : "disabled");
556 annotations.set(AK_TX_PWR, Integer.toString(txPwr));
557
558 int rxPwrVal = OFPortStatsOpticalFlagsSerializerVer14.RX_PWR_VAL;
559 int rxPwr = optical.get().getRxPwr();
560 annotations.set(AK_RX_PWR_FEATURE, ((flags & rxPwrVal) != 0) ? "enabled" : "disabled");
561 annotations.set(AK_RX_PWR, Integer.toString(rxPwr));
562
563 int txBias = OFPortStatsOpticalFlagsSerializerVer14.TX_BIAS_VAL;
564 int biasCurrent = optical.get().getBiasCurrent();
565 annotations.set(AK_TX_BIAS_FEATURE, ((flags & txBias) != 0) ? "enabled" : "disabled");
566 annotations.set(AK_BIAS_CURRENT, Integer.toString(biasCurrent));
567
568 int txTemp = OFPortStatsOpticalFlagsSerializerVer14.TX_TEMP_VAL;
569 int temperature = optical.get().getTemperature();
570 annotations.set(AK_TX_TEMP_FEATURE, ((flags & txTemp) != 0) ? "enabled" : "disabled");
571 annotations.set(AK_TEMPERATURE, Integer.toString(temperature));
572 }
Thomas Vachuskaf0397b52015-05-29 13:50:17 -0700573 DefaultPortStatistics.Builder builder = DefaultPortStatistics.builder();
574 DefaultPortStatistics stat = builder.setDeviceId(deviceId)
575 .setPort(entry.getPortNo().getPortNumber())
576 .setPacketsReceived(entry.getRxPackets().getValue())
577 .setPacketsSent(entry.getTxPackets().getValue())
578 .setBytesReceived(entry.getRxBytes().getValue())
579 .setBytesSent(entry.getTxBytes().getValue())
580 .setPacketsRxDropped(entry.getRxDropped().getValue())
581 .setPacketsTxDropped(entry.getTxDropped().getValue())
582 .setPacketsRxErrors(entry.getRxErrors().getValue())
583 .setPacketsTxErrors(entry.getTxErrors().getValue())
584 .setDurationSec(entry.getVersion() == OFVersion.OF_10 ? 0 : entry.getDurationSec())
585 .setDurationNano(entry.getVersion() == OFVersion.OF_10 ? 0 : entry.getDurationNsec())
Laszlo Papp7cf60372018-01-11 00:06:43 +0000586 .setAnnotations(annotations.build())
Thomas Vachuskaf0397b52015-05-29 13:50:17 -0700587 .build();
sangho538108b2015-04-08 14:29:20 -0700588
Thomas Vachuskaf0397b52015-05-29 13:50:17 -0700589 stats.add(stat);
590 } catch (Exception e) {
591 LOG.warn("Unable to process port stats", e);
592 }
sangho538108b2015-04-08 14:29:20 -0700593 }
594
595 return Collections.unmodifiableSet(stats);
sangho538108b2015-04-08 14:29:20 -0700596 }
597
598 private class InternalDeviceProvider implements OpenFlowSwitchListener, OpenFlowEventListener {
Thomas Vachuska893bf4b2015-05-29 18:13:18 -0700599
Srikanth Vavilapalli78baf582015-06-05 11:40:14 -0700600 private HashMap<Dpid, List<OFPortStatsEntry>> portStatsReplies = new HashMap<>();
Ray Milkey7abe50b2016-04-13 11:57:44 -0700601 private boolean isDisabled = false;
Thomas Vachuska893bf4b2015-05-29 18:13:18 -0700602
alshabibf1216ed2014-09-03 11:53:54 -0700603 @Override
tomd1900f32014-09-03 14:08:16 -0700604 public void switchAdded(Dpid dpid) {
alshabib6f5460b2014-09-03 14:46:17 -0700605 if (providerService == null) {
606 return;
607 }
tom782a7cf2014-09-11 23:58:38 -0700608 DeviceId did = deviceId(uri(dpid));
alshabib6f5460b2014-09-03 14:46:17 -0700609 OpenFlowSwitch sw = controller.getSwitch(dpid);
Lei Xudee1aff2015-10-16 00:45:10 -0500610 if (sw == null) {
sangyun-han147e6f42016-07-26 17:14:41 +0900611 LOG.error("Switch {} is not found", dpid);
Lei Xudee1aff2015-10-16 00:45:10 -0500612 return;
613 }
alshabib6f5460b2014-09-03 14:46:17 -0700614
alshabib7911a052014-10-16 17:49:37 -0700615 ChassisId cId = new ChassisId(dpid.value());
Ray Milkeye53f1712015-01-16 09:17:16 -0800616
Victor Silvaac8dab02016-11-28 16:33:08 -0300617 DefaultAnnotations.Builder annotationsBuilder = DefaultAnnotations.builder()
andreafe3308f2015-10-06 15:51:25 -0700618 .set(AnnotationKeys.PROTOCOL, sw.factory().getVersion().toString())
619 .set(AnnotationKeys.CHANNEL_ID, sw.channelId())
Victor Silvaac8dab02016-11-28 16:33:08 -0300620 .set(AnnotationKeys.MANAGEMENT_ADDRESS, sw.channelId().split(":")[0]);
621
Yuta HIGUCHI2341e602017-03-08 20:10:08 -0800622 // FIXME following ignores driver specified by name
Victor Silvaac8dab02016-11-28 16:33:08 -0300623 Driver driver = driverService.getDriver(sw.manufacturerDescription(),
624 sw.hardwareDescription(),
625 sw.softwareDescription());
Thomas Vachuskaa79cbf72017-01-13 15:07:04 -0800626 // FIXME: The following breaks the STC tests and will require to be revisited.
627// if (driver != null) {
628// annotationsBuilder.set(AnnotationKeys.DRIVER, driver.name());
629// }
Victor Silvaac8dab02016-11-28 16:33:08 -0300630
631 SparseAnnotations annotations = annotationsBuilder.build();
Ray Milkeye53f1712015-01-16 09:17:16 -0800632
tomd1900f32014-09-03 14:08:16 -0700633 DeviceDescription description =
Marc De Leenheerb9311372015-07-09 11:36:49 -0700634 new DefaultDeviceDescription(did.uri(), sw.deviceType(),
Ray Milkeyd3edd032015-01-16 11:38:58 -0800635 sw.manufacturerDescription(),
tom782a7cf2014-09-11 23:58:38 -0700636 sw.hardwareDescription(),
637 sw.softwareDescription(),
alshabib7911a052014-10-16 17:49:37 -0700638 sw.serialNumber(),
Thomas Vachuska82041f52014-11-30 22:14:02 -0800639 cId, annotations);
tom782a7cf2014-09-11 23:58:38 -0700640 providerService.deviceConnected(did, description);
Ayaka Koshibe5460d622015-05-14 12:19:19 -0700641 providerService.updatePorts(did, buildPortDescriptions(sw));
sangho538108b2015-04-08 14:29:20 -0700642
sangyun-han07a572f2016-07-29 21:14:27 +0900643 PortStatsCollector psc = new PortStatsCollector(timer, sw, portStatsPollFrequency);
Thomas Vachuskaa394b952016-06-14 15:02:09 -0700644 stopCollectorIfNeeded(collectors.put(dpid, psc));
sangho538108b2015-04-08 14:29:20 -0700645 psc.start();
Lei Xudee1aff2015-10-16 00:45:10 -0500646
647 //figure out race condition for collectors.remove() and collectors.put()
648 if (controller.getSwitch(dpid) == null) {
649 switchRemoved(dpid);
650 }
alshabib25c8eec2014-09-04 16:41:31 -0700651 }
652
Thomas Vachuskaa394b952016-06-14 15:02:09 -0700653 private void stopCollectorIfNeeded(PortStatsCollector collector) {
654 if (collector != null) {
655 collector.stop();
656 }
657 }
658
alshabibf1216ed2014-09-03 11:53:54 -0700659 @Override
660 public void switchRemoved(Dpid dpid) {
Victor Silva03265dd2016-12-08 11:20:36 -0300661 stopCollectorIfNeeded(collectors.remove(dpid));
alshabib6f5460b2014-09-03 14:46:17 -0700662 if (providerService == null) {
663 return;
664 }
tom782a7cf2014-09-11 23:58:38 -0700665 providerService.deviceDisconnected(deviceId(uri(dpid)));
sangho538108b2015-04-08 14:29:20 -0700666 }
Ayaka Koshibe38594c22014-10-22 13:36:12 -0700667
668 @Override
669 public void switchChanged(Dpid dpid) {
HIGUCHI Yuta1979f552015-12-28 21:24:26 -0800670 LOG.debug("switchChanged({})", dpid);
Ayaka Koshibe38594c22014-10-22 13:36:12 -0700671 if (providerService == null) {
672 return;
673 }
674 DeviceId did = deviceId(uri(dpid));
675 OpenFlowSwitch sw = controller.getSwitch(dpid);
Lei Xudee1aff2015-10-16 00:45:10 -0500676 if (sw == null) {
sangyun-han147e6f42016-07-26 17:14:41 +0900677 LOG.error("Switch {} is not found", dpid);
Lei Xudee1aff2015-10-16 00:45:10 -0500678 return;
679 }
HIGUCHI Yuta1979f552015-12-28 21:24:26 -0800680 final List<PortDescription> ports = buildPortDescriptions(sw);
681 LOG.debug("switchChanged({}) {}", did, ports);
682 providerService.updatePorts(did, ports);
Ayaka Koshibe38594c22014-10-22 13:36:12 -0700683 }
684
alshabiba14f3642014-09-05 09:31:31 -0700685 @Override
686 public void portChanged(Dpid dpid, OFPortStatus status) {
HIGUCHI Yuta1979f552015-12-28 21:24:26 -0800687 LOG.debug("portChanged({},{})", dpid, status);
alshabibafc514a2014-12-01 14:44:05 -0800688 PortDescription portDescription = buildPortDescription(status);
Michal Machce774332017-01-25 11:02:55 +0100689 if (status.getReason() != OFPortReason.DELETE) {
690 providerService.portStatusChanged(deviceId(uri(dpid)), portDescription);
691 } else {
692 providerService.deletePort(deviceId(uri(dpid)), portDescription);
693 }
alshabibf1216ed2014-09-03 11:53:54 -0700694 }
695
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700696 @Override
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700697 public void receivedRoleReply(Dpid dpid, RoleState requested, RoleState response) {
HIGUCHI Yuta1979f552015-12-28 21:24:26 -0800698 LOG.debug("receivedRoleReply({},{},{})", dpid, requested, response);
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700699 MastershipRole request = roleOf(requested);
700 MastershipRole reply = roleOf(response);
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700701 providerService.receivedRoleReply(deviceId(uri(dpid)), request, reply);
702 }
703
704 /**
705 * Translates a RoleState to the corresponding MastershipRole.
706 *
Thomas Vachuskafc52fec2015-05-18 19:13:56 -0700707 * @param response role state
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700708 * @return a MastershipRole
709 */
710 private MastershipRole roleOf(RoleState response) {
711 switch (response) {
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700712 case MASTER:
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700713 return MastershipRole.MASTER;
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700714 case EQUAL:
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700715 return MastershipRole.STANDBY;
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700716 case SLAVE:
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700717 return MastershipRole.NONE;
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700718 default:
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700719 LOG.warn("unknown role {}", response);
720 return null;
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700721 }
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700722 }
723
alshabiba14f3642014-09-05 09:31:31 -0700724 /**
725 * Builds a list of port descriptions for a given list of ports.
tomff7eb7c2014-09-08 12:49:03 -0700726 *
alshabiba14f3642014-09-05 09:31:31 -0700727 * @return list of portdescriptions
728 */
Ayaka Koshibe5460d622015-05-14 12:19:19 -0700729 private List<PortDescription> buildPortDescriptions(OpenFlowSwitch sw) {
Yuta HIGUCHIf83c8cf2017-12-17 14:33:49 -0800730 List<OFPortDesc> ofPorts = sw.getPorts();
731 final List<PortDescription> portDescs = new ArrayList<>(ofPorts.size());
Rimon Ashkenazy8ebfff02016-02-01 11:56:36 +0200732 if (!((Device.Type.ROADM.equals(sw.deviceType())) ||
MaoLuc201ae42017-02-06 17:57:01 -0800733 (Device.Type.OTN.equals(sw.deviceType())) ||
734 (Device.Type.OPTICAL_AMPLIFIER.equals(sw.deviceType())))) {
Yuta HIGUCHI2341e602017-03-08 20:10:08 -0800735 // build regular (=non-optical) Device ports
Yuta HIGUCHIf83c8cf2017-12-17 14:33:49 -0800736 ofPorts.forEach(port -> portDescs.add(buildPortDescription(port)));
Yafit Hadara9a73de2015-09-06 13:52:52 +0300737 }
Marc De Leenheerb9311372015-07-09 11:36:49 -0700738
Yuta HIGUCHI2341e602017-03-08 20:10:08 -0800739 // TODO handle Optical Device, but plain OF devices(1.4 and later)
740
Marc De Leenheerb9311372015-07-09 11:36:49 -0700741 OpenFlowOpticalSwitch opsw;
742 switch (sw.deviceType()) {
743 case ROADM:
Rimon Ashkenazy8ebfff02016-02-01 11:56:36 +0200744 case OTN:
MaoLuc201ae42017-02-06 17:57:01 -0800745 case OPTICAL_AMPLIFIER:
Marc De Leenheerb9311372015-07-09 11:36:49 -0700746 opsw = (OpenFlowOpticalSwitch) sw;
Yafit Hadara9a73de2015-09-06 13:52:52 +0300747 List<OFPortDesc> ports = opsw.getPorts();
748 LOG.debug("SW ID {} , ETH- ODU CLT Ports {}", opsw.getId(), ports);
749 // ODU client ports are reported as ETH
750 ports.forEach(port -> portDescs.add(buildOduCltPortDescription(port)));
751
Marc De Leenheerb9311372015-07-09 11:36:49 -0700752 opsw.getPortTypes().forEach(type -> {
Yafit Hadara9a73de2015-09-06 13:52:52 +0300753 List<? extends OFObject> portsOf = opsw.getPortsOf(type);
754 LOG.debug("Ports Of{}", portsOf);
755 portsOf.forEach(
756 op -> {
Jimmy Yan4deb03b2016-06-24 10:53:54 -0700757 portDescs.add(buildPortDescription(type, op, opsw));
Yafit Hadara9a73de2015-09-06 13:52:52 +0300758 }
759 );
Marc De Leenheerb9311372015-07-09 11:36:49 -0700760 });
761 break;
762 case FIBER_SWITCH:
763 opsw = (OpenFlowOpticalSwitch) sw;
764 opsw.getPortTypes().forEach(type -> {
Yuta HIGUCHIf42a2cc2017-05-02 17:04:31 -0700765 opsw.getPortsOf(type).forEach(op -> {
766 if (op instanceof OFPortDesc) {
767 // ports using standard optical extension
768 // TODO OFMessage -> PortDescription should
769 // probably be a Behaviour
770 portDescs.add(oms(buildPortDescription((OFPortDesc) op)));
771 } else if (op instanceof OFCalientPortDescStatsEntry) {
772 // calient extension
773 portDescs.add(buildPortDescription((OFCalientPortDescStatsEntry) op));
774 } else {
775 LOG.warn("Unexpected FIBER_SWITCH port {} on {}",
776 op, sw.getStringId());
777 }
778 });
Marc De Leenheerb9311372015-07-09 11:36:49 -0700779 });
780 break;
781 default:
782 break;
alshabib4680bb62014-09-04 17:15:08 -0700783 }
Marc De Leenheerb9311372015-07-09 11:36:49 -0700784
alshabib4680bb62014-09-04 17:15:08 -0700785 return portDescs;
786 }
787
Yuta HIGUCHIf42a2cc2017-05-02 17:04:31 -0700788 /**
789 * Ensures returned PortDescription is an OMS port.
790 *
791 * @param descr input PortDescription
792 * @return OMS PortDescription
793 */
794 private PortDescription oms(PortDescription descr) {
795 // Hack until OFMessage -> PortDescription transformation
796 // becomes a Behaviour
797 if (descr.type() == Type.OMS) {
798 return descr;
799 }
800
801 Builder builder = DefaultAnnotations.builder();
802 builder.putAll(descr.annotations());
803
804 // set reasonable default when mandatory key is missing
805 if (Strings.isNullOrEmpty(descr.annotations().value(AK_MIN_FREQ_HZ))) {
806 builder.set(AK_MIN_FREQ_HZ, String.valueOf(Spectrum.O_BAND_MIN.asHz()));
807 }
808
809 if (Strings.isNullOrEmpty(descr.annotations().value(AK_MAX_FREQ_HZ))) {
810 builder.set(AK_MAX_FREQ_HZ, String.valueOf(Spectrum.O_BAND_MAX.asHz()));
811 }
812
813 if (Strings.isNullOrEmpty(descr.annotations().value(AK_GRID_HZ))) {
814 builder.set(AK_GRID_HZ, String.valueOf(Frequency.ofGHz(50).asHz()));
815 }
816
817 return DefaultPortDescription.builder(descr)
818 .type(Type.OMS)
819 .annotations(builder.build())
820 .build();
821 }
822
Yafit Hadara9a73de2015-09-06 13:52:52 +0300823 private PortDescription buildOduCltPortDescription(OFPortDesc port) {
824 PortNumber portNo = PortNumber.portNumber(port.getPortNo().getPortNumber());
825 boolean enabled = !port.getState().contains(OFPortState.LINK_DOWN) &&
826 !port.getConfig().contains(OFPortConfig.PORT_DOWN);
Rimon Ashkenazyd15ebf52016-01-06 14:36:13 +0200827 Long portSpeedInMbps = portSpeed(port);
Toru Furusawa72ee30c2016-01-08 13:29:04 -0800828 CltSignalType sigType = null;
Yafit Hadara9a73de2015-09-06 13:52:52 +0300829
Rimon Ashkenazyd15ebf52016-01-06 14:36:13 +0200830 switch (portSpeedInMbps.toString()) {
831 case "1000":
Toru Furusawa72ee30c2016-01-08 13:29:04 -0800832 sigType = CltSignalType.CLT_1GBE;
Yafit Hadara9a73de2015-09-06 13:52:52 +0300833 break;
Rimon Ashkenazyd15ebf52016-01-06 14:36:13 +0200834 case "10000":
Toru Furusawa72ee30c2016-01-08 13:29:04 -0800835 sigType = CltSignalType.CLT_10GBE;
Yafit Hadara9a73de2015-09-06 13:52:52 +0300836 break;
Rimon Ashkenazyd15ebf52016-01-06 14:36:13 +0200837 case "40000":
Toru Furusawa72ee30c2016-01-08 13:29:04 -0800838 sigType = CltSignalType.CLT_40GBE;
Yafit Hadara9a73de2015-09-06 13:52:52 +0300839 break;
Rimon Ashkenazyd15ebf52016-01-06 14:36:13 +0200840 case "100000":
Toru Furusawa72ee30c2016-01-08 13:29:04 -0800841 sigType = CltSignalType.CLT_100GBE;
Yafit Hadara9a73de2015-09-06 13:52:52 +0300842 break;
843 default:
Rimon Ashkenazyd15ebf52016-01-06 14:36:13 +0200844 throw new RuntimeException("Un recognize OduClt speed: " + portSpeedInMbps.toString());
Yafit Hadara9a73de2015-09-06 13:52:52 +0300845 }
846
847 SparseAnnotations annotations = buildOduCltAnnotation(port);
HIGUCHI Yuta4c0ef6b2016-05-02 19:45:41 -0700848 return oduCltPortDescription(portNo, enabled, sigType, annotations);
Yafit Hadara9a73de2015-09-06 13:52:52 +0300849 }
850
851 private SparseAnnotations buildOduCltAnnotation(OFPortDesc port) {
852 SparseAnnotations annotations = null;
853 String portName = Strings.emptyToNull(port.getName());
854 if (portName != null) {
855 annotations = DefaultAnnotations.builder()
856 .set(AnnotationKeys.PORT_NAME, portName)
857 .set(AnnotationKeys.STATIC_PORT, Boolean.TRUE.toString()).build();
858 }
859 return annotations;
860 }
861
Jimmy Yan4deb03b2016-06-24 10:53:54 -0700862 private PortDescription buildPortDescription(PortDescPropertyType ptype, OFObject port,
863 OpenFlowOpticalSwitch opsw) {
yjimmyy646aa022016-07-05 12:09:50 -0700864 if (port instanceof OFPortOptical) {
Jimmy Yan4deb03b2016-06-24 10:53:54 -0700865 return buildPortDescription(ptype, (OFPortOptical) port, opsw);
Yafit Hadara9a73de2015-09-06 13:52:52 +0300866 }
867 return buildPortDescription(ptype, (OFExpPort) port);
868 }
869
Rimon Ashkenazy8ebfff02016-02-01 11:56:36 +0200870 private boolean matchingOtuPortSignalTypes(OFPortOpticalTransportSignalType sigType,
871 OduSignalType oduSignalType) {
872 switch (sigType) {
873 case OTU2:
874 if (oduSignalType == OduSignalType.ODU2) {
875 return true;
876 }
877 break;
878 case OTU4:
879 if (oduSignalType == OduSignalType.ODU4) {
880 return true;
881 }
882 break;
883 default:
884 break;
885 }
886 return false;
887 }
Yafit Hadara9a73de2015-09-06 13:52:52 +0300888 /**
889 * Build a portDescription from a given a port description describing some
890 * Optical port.
891 *
892 * @param ptype description property type.
893 * @param port the port to build from.
894 * @return portDescription for the port.
895 */
896 private PortDescription buildPortDescription(PortDescPropertyType ptype, OFExpPort port) {
897 PortNumber portNo = PortNumber.portNumber(port.getPortNo().getPortNumber());
898 boolean enabled = !port.getState().contains(OFPortState.LINK_DOWN)
899 && !port.getConfig().contains(OFPortConfig.PORT_DOWN);
mskalac584bd92017-11-28 15:51:17 +0100900 boolean adminDown = port.getConfig().contains(OFPortConfig.PORT_DOWN);
901 SparseAnnotations annotations = makePortAnnotation(port.getName(),
902 port.getHwAddr().toString(),
903 adminDown).build();
Yafit Hadara9a73de2015-09-06 13:52:52 +0300904
905 OFExpPortDescPropOpticalTransport firstProp = port.getProperties().get(0);
906 OFPortOpticalTransportSignalType sigType = firstProp.getPortSignalType();
907
HIGUCHI Yuta34a3f692016-01-09 21:08:57 -0800908 PortDescription portDes = null;
Yafit Hadara9a73de2015-09-06 13:52:52 +0300909 switch (sigType) {
910 case OMSN:
HIGUCHI Yuta95d83e82016-04-26 12:13:48 -0700911 portDes = omsPortDescription(portNo, enabled,
Rimon Ashkenazye3201032016-01-11 14:27:30 +0200912 FREQ191_7, FREQ191_7.add(FREQ4_4), FREQ50, annotations);
Yafit Hadara9a73de2015-09-06 13:52:52 +0300913 break;
914 case OCH:
915 OFExpPortOpticalTransportLayerEntry entry = firstProp.getFeatures().get(0).getValue().get(0);
916 OFPortOpticalTransportLayerClass layerClass = entry.getLayerClass();
917 if (!OFPortOpticalTransportLayerClass.ODU.equals(layerClass)) {
918 LOG.error("Unsupported layer Class {} ", layerClass);
919 return null;
920 }
921
922 // convert to ONOS OduSignalType
923 OduSignalType oduSignalType = OpenFlowDeviceValueMapper.
924 lookupOduSignalType((byte) entry.getSignalType());
925 //OchSignal is needed for OchPortDescription constructor,
926 //yet not relevant for tunable OCH port, creating with default parameters
927 OchSignal signalId = new OchSignal(GridType.DWDM, ChannelSpacing.CHL_50GHZ, 1, 1);
928
HIGUCHI Yuta34a3f692016-01-09 21:08:57 -0800929 portDes = ochPortDescription(portNo, enabled,
930 oduSignalType, true,
931 signalId, annotations);
Yafit Hadara9a73de2015-09-06 13:52:52 +0300932
933 break;
934 case OTU2:
935 case OTU4:
Rimon Ashkenazy8ebfff02016-02-01 11:56:36 +0200936 entry = firstProp.getFeatures().get(0).getValue().get(0);
937 layerClass = entry.getLayerClass();
938 if (!OFPortOpticalTransportLayerClass.ODU.equals(layerClass)) {
939 LOG.error("Unsupported layer Class {} ", layerClass);
940 return null;
941 }
942
943 // convert to ONOS OduSignalType
944 OduSignalType oduSignalTypeOtuPort = OpenFlowDeviceValueMapper.
945 lookupOduSignalType((byte) entry.getSignalType());
946 if (!matchingOtuPortSignalTypes(sigType, oduSignalTypeOtuPort)) {
947 LOG.error("Wrong oduSignalType {} for OTU Port sigType {} ", oduSignalTypeOtuPort, sigType);
948 return null;
949 }
950 OtuSignalType otuSignalType =
951 ((sigType == OFPortOpticalTransportSignalType.OTU2) ? OtuSignalType.OTU2 :
952 OtuSignalType.OTU4);
HIGUCHI Yuta5be3e822016-05-03 13:51:42 -0700953 portDes = otuPortDescription(portNo, enabled, otuSignalType, annotations);
Rimon Ashkenazy8ebfff02016-02-01 11:56:36 +0200954 break;
Yafit Hadara9a73de2015-09-06 13:52:52 +0300955 default:
956 break;
957 }
958
959 return portDes;
960 }
961
alshabiba14f3642014-09-05 09:31:31 -0700962 /**
Yuta HIGUCHI2341e602017-03-08 20:10:08 -0800963 * Creates an annotation builder for the port name if one is available.
Ray Milkeye0fade72015-01-15 13:29:47 -0800964 *
lishuai4ee42042015-11-30 17:19:21 +0800965 * @param portName the port name
966 * @param portMac the port mac
mskalac584bd92017-11-28 15:51:17 +0100967 * @param adminDown the port admin state
968 * @return annotation builder containing port admin state, port name
969 * and/or port MAC if any of the two is found
Ray Milkeye0fade72015-01-15 13:29:47 -0800970 */
mskalac584bd92017-11-28 15:51:17 +0100971 private DefaultAnnotations.Builder makePortAnnotation(String portName, String portMac, boolean adminDown) {
Andrea Campanellac8b871a2017-01-19 13:53:05 -0800972 DefaultAnnotations.Builder builder = DefaultAnnotations.builder();
lishuai4ee42042015-11-30 17:19:21 +0800973 String pName = Strings.emptyToNull(portName);
974 String pMac = Strings.emptyToNull(portMac);
Andrea Campanellac8b871a2017-01-19 13:53:05 -0800975 if (pName != null) {
976 builder.set(AnnotationKeys.PORT_NAME, pName);
Ray Milkeye0fade72015-01-15 13:29:47 -0800977 }
Andrea Campanellac8b871a2017-01-19 13:53:05 -0800978 if (pMac != null) {
979 builder.set(AnnotationKeys.PORT_MAC, pMac);
980 }
mskalac584bd92017-11-28 15:51:17 +0100981 String adminState = adminDown ? "disabled" : "enabled";
982 builder.set(AnnotationKeys.ADMIN_STATE, adminState);
Yuta HIGUCHI2341e602017-03-08 20:10:08 -0800983 return builder;
984 }
985
986 private String mhzToAnnotation(long freqMhz) {
Laszlo Papp7cf60372018-01-11 00:06:43 +0000987 return OpenFlowDeviceProvider.mhzToAnnotation(freqMhz);
Yuta HIGUCHI2341e602017-03-08 20:10:08 -0800988 }
989
Yuta HIGUCHIf42a2cc2017-05-02 17:04:31 -0700990 private String lambdaToAnnotationHz(long lambda) {
991 // ref. OF1.5: wavelength (lambda) as nm * 100
992
993 long c = 299792458; // speed of light in m/s
994 // f = c / λ
995 // (m/s) * (nm/m) / (nm * 100) * 100
996 // annotations is in Hz
997 return Long.toString(c * 1_000_000_000 / lambda * 100);
998 }
999
Yuta HIGUCHI2341e602017-03-08 20:10:08 -08001000 private PortDescription buildPortDescription14(OFPortDesc port) {
1001 PortNumber portNo = PortNumber.portNumber(port.getPortNo().getPortNumber());
1002 boolean enabled =
1003 !port.getState().contains(OFPortState.LINK_DOWN) &&
1004 !port.getConfig().contains(OFPortConfig.PORT_DOWN);
mskalac584bd92017-11-28 15:51:17 +01001005 boolean adminDown = port.getConfig().contains(OFPortConfig.PORT_DOWN);
Yuta HIGUCHI2341e602017-03-08 20:10:08 -08001006 Builder annotations = makePortAnnotation(port.getName(),
mskalac584bd92017-11-28 15:51:17 +01001007 port.getHwAddr().toString(),
1008 adminDown);
Yuta HIGUCHI2341e602017-03-08 20:10:08 -08001009
1010 Optional<OFPortDescPropEthernet> ether = port.getProperties().stream()
1011 .filter(OFPortDescPropEthernet.class::isInstance)
1012 .map(OFPortDescPropEthernet.class::cast)
1013 .findAny();
1014 if (ether.isPresent()) {
1015 // ethernet port
1016 // TODO parse other part of OFPortDescPropEthernet if necessary
1017 return new DefaultPortDescription(portNo,
1018 enabled,
1019 COPPER,
1020 portSpeed(port),
1021 annotations.build());
1022 }
1023
1024 Optional<OFPortDescPropOptical> optical = port.getProperties().stream()
1025 .filter(OFPortDescPropOptical.class::isInstance)
1026 .map(OFPortDescPropOptical.class::cast)
1027 .findAny();
1028 if (optical.isPresent()) {
1029 // optical port
1030
Yuta HIGUCHIf42a2cc2017-05-02 17:04:31 -07001031 // FIXME is there a OF version neutral way to access
1032 // OFOpticalPortFeaturesSerializerVer14
Yuta HIGUCHI2341e602017-03-08 20:10:08 -08001033
Laszlo Papp20cfb6f2018-01-08 16:06:53 +00001034 long supported = optical.get().getSupported();
1035 long rxMin = optical.get().getRxMinFreqLmda();
1036 long rxMax = optical.get().getRxMaxFreqLmda();
1037 long rxGrid = optical.get().getRxGridFreqLmda();
1038 long txMin = optical.get().getTxMinFreqLmda();
1039 long txMax = optical.get().getTxMaxFreqLmda();
1040 long txGrid = optical.get().getTxGridFreqLmda();
1041
1042 int txTune = OFOpticalPortFeaturesSerializerVer14.TX_TUNE_VAL;
1043 int rxTune = OFOpticalPortFeaturesSerializerVer14.RX_TUNE_VAL;
1044 annotations.set(AK_TX_TUNE_FEATURE,
1045 ((supported & txTune) != 0) ? "enabled" : "disabled");
1046 annotations.set(AK_RX_TUNE_FEATURE,
1047 ((supported & rxTune) != 0) ? "enabled" : "disabled");
1048
Yuta HIGUCHI2341e602017-03-08 20:10:08 -08001049 // wire value for OFOpticalPortFeatures.USE_FREQ
Yuta HIGUCHIf42a2cc2017-05-02 17:04:31 -07001050 long useFreq = OFOpticalPortFeaturesSerializerVer14.USE_FREQ_VAL;
Laszlo Papp20cfb6f2018-01-08 16:06:53 +00001051 if ((supported & useFreq) != 0) {
Yuta HIGUCHI2341e602017-03-08 20:10:08 -08001052 // unit is in Frequency Mhz
Laszlo Papp20cfb6f2018-01-08 16:06:53 +00001053 annotations.set(AK_RX_MIN_FREQ_HZ, mhzToAnnotation(rxMin));
1054 annotations.set(AK_RX_MAX_FREQ_HZ, mhzToAnnotation(rxMax));
1055 annotations.set(AK_RX_GRID_HZ, mhzToAnnotation(rxGrid));
Yuta HIGUCHI2341e602017-03-08 20:10:08 -08001056
Laszlo Papp20cfb6f2018-01-08 16:06:53 +00001057 annotations.set(AK_TX_MIN_FREQ_HZ, mhzToAnnotation(txMin));
1058 annotations.set(AK_TX_MAX_FREQ_HZ, mhzToAnnotation(txMax));
1059 annotations.set(AK_TX_GRID_HZ, mhzToAnnotation(txGrid));
Yuta HIGUCHI2341e602017-03-08 20:10:08 -08001060
1061 // FIXME pretty confident this is not going to happen
1062 // unless Device models Tx/Rx ports as separate port
Laszlo Papp20cfb6f2018-01-08 16:06:53 +00001063 if (rxMin == txMin) {
Yuta HIGUCHI2341e602017-03-08 20:10:08 -08001064 annotations.set(AK_MIN_FREQ_HZ,
Laszlo Papp20cfb6f2018-01-08 16:06:53 +00001065 mhzToAnnotation(rxMin));
Yuta HIGUCHI2341e602017-03-08 20:10:08 -08001066 }
Laszlo Papp20cfb6f2018-01-08 16:06:53 +00001067 if (rxMax == txMax) {
Yuta HIGUCHI2341e602017-03-08 20:10:08 -08001068 annotations.set(AK_MAX_FREQ_HZ,
Laszlo Papp20cfb6f2018-01-08 16:06:53 +00001069 mhzToAnnotation(rxMax));
1070 }
1071 if (rxGrid == txGrid) {
1072 annotations.set(AK_GRID_HZ,
1073 mhzToAnnotation(rxGrid));
Yuta HIGUCHI2341e602017-03-08 20:10:08 -08001074 }
1075
1076 } else {
1077 // unit is in Lambda nm * 100
Yuta HIGUCHIf42a2cc2017-05-02 17:04:31 -07001078 annotations.set(AK_RX_MIN_FREQ_HZ, lambdaToAnnotationHz(rxMin));
1079 annotations.set(AK_RX_MAX_FREQ_HZ, lambdaToAnnotationHz(rxMax));
Laszlo Papp20cfb6f2018-01-08 16:06:53 +00001080 annotations.set(AK_RX_GRID_HZ, lambdaToAnnotationHz(rxGrid));
Yuta HIGUCHIf42a2cc2017-05-02 17:04:31 -07001081
1082 annotations.set(AK_TX_MIN_FREQ_HZ, lambdaToAnnotationHz(txMin));
1083 annotations.set(AK_TX_MAX_FREQ_HZ, lambdaToAnnotationHz(txMax));
Laszlo Papp20cfb6f2018-01-08 16:06:53 +00001084 annotations.set(AK_TX_GRID_HZ, lambdaToAnnotationHz(txGrid));
Yuta HIGUCHIf42a2cc2017-05-02 17:04:31 -07001085
1086 // FIXME pretty confident this is not going to happen
1087 // unless Device models Tx/Rx ports as separate port
1088 if (rxMin == txMin) {
1089 annotations.set(AK_MIN_FREQ_HZ,
1090 lambdaToAnnotationHz(rxMin));
1091 }
1092 if (rxMax == txMax) {
1093 annotations.set(AK_MAX_FREQ_HZ,
1094 lambdaToAnnotationHz(rxMax));
1095 }
Laszlo Papp20cfb6f2018-01-08 16:06:53 +00001096 if (rxGrid == txGrid) {
1097 annotations.set(AK_GRID_HZ,
1098 lambdaToAnnotationHz(rxGrid));
1099 }
Yuta HIGUCHIf42a2cc2017-05-02 17:04:31 -07001100
Yuta HIGUCHI2341e602017-03-08 20:10:08 -08001101 }
Laszlo Papp20cfb6f2018-01-08 16:06:53 +00001102
1103 int txPwr = OFOpticalPortFeaturesSerializerVer14.TX_PWR_VAL;
1104 long txPwrMin = optical.get().getTxPwrMin();
1105 long txPwrMax = optical.get().getTxPwrMax();
1106 annotations.set(AK_TX_PWR_FEATURE, ((supported & txPwr) != 0) ? "enabled" : "disabled");
1107 annotations.set(AK_TX_PWR_MIN, Long.toString(txPwrMin));
1108 annotations.set(AK_TX_PWR_MAX, Long.toString(txPwrMax));
Yuta HIGUCHI2341e602017-03-08 20:10:08 -08001109
1110 // TODO How to determine appropriate port type?
1111
1112 return new DefaultPortDescription(portNo,
1113 enabled,
1114 FIBER,
1115 portSpeed(port),
1116 annotations.build());
1117 }
1118
1119 // fall back default
1120 return new DefaultPortDescription(portNo,
1121 enabled,
1122 COPPER,
1123 portSpeed(port),
1124 annotations.build());
1125
Ray Milkeye0fade72015-01-15 13:29:47 -08001126 }
1127
1128 /**
Ayaka Koshibe5460d622015-05-14 12:19:19 -07001129 * Build a portDescription from a given Ethernet port description.
tomff7eb7c2014-09-08 12:49:03 -07001130 *
alshabiba14f3642014-09-05 09:31:31 -07001131 * @param port the port to build from.
1132 * @return portDescription for the port.
1133 */
1134 private PortDescription buildPortDescription(OFPortDesc port) {
Yuta HIGUCHI2341e602017-03-08 20:10:08 -08001135 if (port.getVersion().wireVersion >= OFVersion.OF_14.getWireVersion()) {
1136 return buildPortDescription14(port);
1137 }
Thomas Vachuskad16ce182014-10-29 17:25:29 -07001138 PortNumber portNo = PortNumber.portNumber(port.getPortNo().getPortNumber());
1139 boolean enabled =
1140 !port.getState().contains(OFPortState.LINK_DOWN) &&
1141 !port.getConfig().contains(OFPortConfig.PORT_DOWN);
1142 Port.Type type = port.getCurr().contains(OFPortFeatures.PF_FIBER) ? FIBER : COPPER;
mskalac584bd92017-11-28 15:51:17 +01001143 boolean adminDown = port.getConfig().contains(OFPortConfig.PORT_DOWN);
1144 SparseAnnotations annotations = makePortAnnotation(port.getName(),
1145 port.getHwAddr().toString(),
1146 adminDown).build();
Ray Milkeye0fade72015-01-15 13:29:47 -08001147 return new DefaultPortDescription(portNo, enabled, type,
1148 portSpeed(port), annotations);
alshabiba14f3642014-09-05 09:31:31 -07001149 }
Ayaka Koshibeab91cc42014-09-25 10:20:52 -07001150
Ayaka Koshibe5460d622015-05-14 12:19:19 -07001151 /**
1152 * Build a portDescription from a given a port description describing some
1153 * Optical port.
1154 *
1155 * @param port description property type.
1156 * @param port the port to build from.
1157 * @return portDescription for the port.
1158 */
Jimmy Yan4deb03b2016-06-24 10:53:54 -07001159 private PortDescription buildPortDescription(PortDescPropertyType ptype, OFPortOptical port,
1160 OpenFlowOpticalSwitch opsw) {
Jon Hallcbd1b392017-01-18 20:15:44 -08001161 checkArgument(!port.getDesc().isEmpty());
Marc De Leenheerfc913dd2015-07-30 16:04:55 -07001162
Ayaka Koshibe5460d622015-05-14 12:19:19 -07001163 // Minimally functional fixture. This needs to be fixed as we add better support.
1164 PortNumber portNo = PortNumber.portNumber(port.getPortNo().getPortNumber());
Ayaka Koshibe74b55272015-05-28 15:16:04 -07001165
Ayaka Koshibe5460d622015-05-14 12:19:19 -07001166 boolean enabled = !port.getState().contains(OFPortState.LINK_DOWN)
1167 && !port.getConfig().contains(OFPortConfig.PORT_DOWN);
mskalac584bd92017-11-28 15:51:17 +01001168 boolean adminDown = port.getConfig().contains(OFPortConfig.PORT_DOWN);
1169 SparseAnnotations annotations = makePortAnnotation(port.getName(),
1170 port.getHwAddr().toString(),
1171 adminDown).build();
Ayaka Koshibe5460d622015-05-14 12:19:19 -07001172
1173 if (port.getVersion() == OFVersion.OF_13
1174 && ptype == PortDescPropertyType.OPTICAL_TRANSPORT) {
1175 // At this point, not much is carried in the optical port message.
Ayaka Koshibeae541732015-05-19 13:37:27 -07001176 LOG.debug("Optical transport port message {}", port.toString());
Ayaka Koshibe5460d622015-05-14 12:19:19 -07001177 } else {
1178 // removable once 1.4+ support complete.
Ayaka Koshibeae541732015-05-19 13:37:27 -07001179 LOG.debug("Unsupported optical port properties");
Ayaka Koshibe5460d622015-05-14 12:19:19 -07001180 }
Marc De Leenheerfc913dd2015-07-30 16:04:55 -07001181
1182 OFPortDescPropOpticalTransport desc = port.getDesc().get(0);
1183 switch (desc.getPortSignalType()) {
1184 // FIXME: use constants once loxi has full optical extensions
1185 case 2: // OMS port
1186 // Assume complete optical spectrum and 50 GHz grid
Jimmy Yan4deb03b2016-06-24 10:53:54 -07001187 Set<OchSignal> signals = null;
1188 if (opsw instanceof HandlerBehaviour) {
1189 DriverHandler driverHandler = ((HandlerBehaviour) opsw).handler();
1190 if (driverHandler != null && driverHandler.hasBehaviour(LambdaQuery.class)) {
1191 try {
1192 signals = driverHandler.behaviour(LambdaQuery.class).queryLambdas(portNo);
1193 } catch (NullPointerException e) {
1194 signals = null;
1195 }
1196 }
1197 }
1198 Frequency minFreq;
1199 Frequency maxFreq;
1200 Frequency channelSpacing;
1201 if (signals == null || signals.isEmpty()) {
1202 minFreq = Spectrum.U_BAND_MIN;
1203 maxFreq = Spectrum.O_BAND_MAX;
1204 channelSpacing = Frequency.ofGHz(50);
1205 } else {
1206 Comparator<OchSignal> compare =
1207 (OchSignal a, OchSignal b) -> a.spacingMultiplier() - b.spacingMultiplier();
1208 OchSignal minOch = Collections.min(signals, compare);
1209 OchSignal maxOch = Collections.max(signals, compare);
1210 minFreq = minOch.centralFrequency();
1211 maxFreq = maxOch.centralFrequency();
1212 channelSpacing = minOch.channelSpacing().frequency();
1213 }
1214 return omsPortDescription(portNo, enabled, minFreq,
1215 maxFreq, channelSpacing, annotations);
Marc De Leenheerfc913dd2015-07-30 16:04:55 -07001216 case 5: // OCH port
1217 OchSignal signal = new OchSignal(GridType.DWDM, ChannelSpacing.CHL_50GHZ, 0, 4);
HIGUCHI Yuta34a3f692016-01-09 21:08:57 -08001218 return ochPortDescription(portNo, enabled, OduSignalType.ODU4,
Marc De Leenheerfc913dd2015-07-30 16:04:55 -07001219 true, signal, annotations);
1220 default:
1221 break;
1222 }
1223
Ayaka Koshibe74b55272015-05-28 15:16:04 -07001224 return new DefaultPortDescription(portNo, enabled, FIBER, 0, annotations);
Ayaka Koshibe5460d622015-05-14 12:19:19 -07001225 }
1226
Marc De Leenheerb9311372015-07-09 11:36:49 -07001227 /**
1228 * Build a portDescription from a given port description describing a fiber switch optical port.
1229 *
1230 * @param port description property type.
1231 * @param port the port to build from.
1232 * @return portDescription for the port.
1233 */
1234 private PortDescription buildPortDescription(OFCalientPortDescStatsEntry port) {
1235 PortNumber portNo = PortNumber.portNumber(port.getPortNo().getPortNumber());
1236
Marc De Leenheerc662d322016-02-18 16:05:10 -08001237 // Use the alias name if it's available
1238 String name = port.getName();
1239 List<OFCalientPortDescProp> props = port.getProperties();
Jon Hallcbd1b392017-01-18 20:15:44 -08001240 if (props != null && !props.isEmpty()) {
Marc De Leenheerc662d322016-02-18 16:05:10 -08001241 OFCalientPortDescPropOptical propOptical = (OFCalientPortDescPropOptical) props.get(0);
1242 if (propOptical != null) {
1243 name = propOptical.getInAlias();
1244 }
1245 }
1246
Marc De Leenheerb9311372015-07-09 11:36:49 -07001247 // FIXME when Calient OF agent reports port status
1248 boolean enabled = true;
mskalac584bd92017-11-28 15:51:17 +01001249 boolean adminDown = false;
1250 SparseAnnotations annotations = makePortAnnotation(name, port.getHwAddr().toString(), adminDown).build();
Marc De Leenheerb9311372015-07-09 11:36:49 -07001251
Marc De Leenheerfc913dd2015-07-30 16:04:55 -07001252 // S160 data sheet
1253 // Wavelength range: 1260 - 1630 nm, grid is irrelevant for this type of switch
HIGUCHI Yuta95d83e82016-04-26 12:13:48 -07001254 return omsPortDescription(portNo, enabled,
Yuta HIGUCHIf42a2cc2017-05-02 17:04:31 -07001255 Spectrum.O_BAND_MIN, Spectrum.O_BAND_MAX, Frequency.ofGHz(50), annotations);
Marc De Leenheerb9311372015-07-09 11:36:49 -07001256 }
1257
alshabibafc514a2014-12-01 14:44:05 -08001258 private PortDescription buildPortDescription(OFPortStatus status) {
1259 OFPortDesc port = status.getDesc();
1260 if (status.getReason() != OFPortReason.DELETE) {
1261 return buildPortDescription(port);
1262 } else {
Yuta HIGUCHI6512f3e2017-05-18 17:21:24 -07001263 PortDescription desc = buildPortDescription(port);
1264 if (desc.isEnabled()) {
1265 return DefaultPortDescription.builder(desc)
1266 .isEnabled(false)
1267 .build();
1268 }
1269 return desc;
alshabibafc514a2014-12-01 14:44:05 -08001270 }
1271 }
1272
Yuta HIGUCHI2341e602017-03-08 20:10:08 -08001273 /**
1274 * Returns port speed in Mbps.
1275 *
1276 * @param port description to parse
1277 * @return port speed in Mbps
1278 */
Thomas Vachuskad16ce182014-10-29 17:25:29 -07001279 private long portSpeed(OFPortDesc port) {
Yuta HIGUCHI2341e602017-03-08 20:10:08 -08001280 if (port.getVersion().getWireVersion() >= OFVersion.OF_14.getWireVersion()) {
1281 // OFPortDescPropEthernet
1282 return port.getProperties().stream()
1283 .filter(OFPortDescPropEthernet.class::isInstance)
1284 .map(OFPortDescPropEthernet.class::cast)
1285 .mapToLong(OFPortDescPropEthernet::getCurrSpeed)
1286 .map(kbps -> kbps / KBPS)
1287 .findAny()
1288 .orElse(PortSpeed.SPEED_NONE.getSpeedBps() / MBPS);
1289 }
Thomas Vachuskad16ce182014-10-29 17:25:29 -07001290 if (port.getVersion() == OFVersion.OF_13) {
Charles Chan25b77322015-12-13 01:00:56 -08001291 // Note: getCurrSpeed() returns a value in kbps (this also applies to OF_11 and OF_12)
1292 return port.getCurrSpeed() / KBPS;
Thomas Vachuskad16ce182014-10-29 17:25:29 -07001293 }
Yuta HIGUCHI2341e602017-03-08 20:10:08 -08001294 // < OF1.3
Thomas Vachuskad16ce182014-10-29 17:25:29 -07001295 PortSpeed portSpeed = PortSpeed.SPEED_NONE;
1296 for (OFPortFeatures feat : port.getCurr()) {
1297 portSpeed = PortSpeed.max(portSpeed, feat.getPortSpeed());
1298 }
Thomas Vachuska98eda532014-10-29 17:31:02 -07001299 return portSpeed.getSpeedBps() / MBPS;
Thomas Vachuskad16ce182014-10-29 17:25:29 -07001300 }
sangho538108b2015-04-08 14:29:20 -07001301
1302 @Override
1303 public void handleMessage(Dpid dpid, OFMessage msg) {
Ray Milkey7abe50b2016-04-13 11:57:44 -07001304 if (isDisabled) {
1305 return;
sangho538108b2015-04-08 14:29:20 -07001306 }
Ray Milkey7abe50b2016-04-13 11:57:44 -07001307
1308 try {
1309 switch (msg.getType()) {
1310 case STATS_REPLY:
1311 if (((OFStatsReply) msg).getStatsType() == OFStatsType.PORT) {
1312 OFPortStatsReply portStatsReply = (OFPortStatsReply) msg;
1313 List<OFPortStatsEntry> portStatsReplyList = portStatsReplies.get(dpid);
1314 if (portStatsReplyList == null) {
Madan Jampani84382b92016-06-22 08:26:49 -07001315 portStatsReplyList = Lists.newCopyOnWriteArrayList();
Ray Milkey7abe50b2016-04-13 11:57:44 -07001316 }
1317 portStatsReplyList.addAll(portStatsReply.getEntries());
1318 portStatsReplies.put(dpid, portStatsReplyList);
1319 if (!portStatsReply.getFlags().contains(OFStatsReplyFlags.REPLY_MORE)) {
Charles Chanb3ef0db2016-05-03 11:11:00 -07001320 List<OFPortStatsEntry> statsEntries = portStatsReplies.get(dpid);
1321 if (statsEntries != null) {
1322 pushPortMetrics(dpid, statsEntries);
1323 statsEntries.clear();
1324 }
Ray Milkey7abe50b2016-04-13 11:57:44 -07001325 }
yjimmyy646aa022016-07-05 12:09:50 -07001326 } else if (((OFStatsReply) msg).getStatsType() == OFStatsType.EXPERIMENTER) {
1327 OpenFlowSwitch sw = controller.getSwitch(dpid);
sangyun-han147e6f42016-07-26 17:14:41 +09001328 if (sw == null) {
1329 LOG.error("Switch {} is not found", dpid);
1330 break;
1331 }
yjimmyy646aa022016-07-05 12:09:50 -07001332 if (sw instanceof OpenFlowOpticalSwitch) {
1333 // Optical switch uses experimenter stats message to update power
1334 List<PortDescription> portDescs =
1335 ((OpenFlowOpticalSwitch) sw).processExpPortStats(msg);
1336 if (!portDescs.isEmpty()) {
1337 providerService.updatePorts(DeviceId.deviceId(Dpid.uri(dpid)), portDescs);
1338 }
1339 }
Ray Milkey7abe50b2016-04-13 11:57:44 -07001340 }
1341 break;
1342 case ERROR:
1343 if (((OFErrorMsg) msg).getErrType() == OFErrorType.PORT_MOD_FAILED) {
1344 LOG.error("port mod failed");
1345 }
1346 default:
1347 break;
1348 }
1349 } catch (IllegalStateException e) {
1350 // system is shutting down and the providerService is no longer
1351 // valid. Messages cannot be processed.
1352 }
1353 }
1354
1355 private void disable() {
1356 isDisabled = true;
sangho538108b2015-04-08 14:29:20 -07001357 }
alshabibf1216ed2014-09-03 11:53:54 -07001358 }
Saurav Dasa2d37502016-03-25 17:50:40 -07001359
1360
tomb5a46e62014-08-26 14:20:00 -07001361}