blob: 4f58bb19e5893c737b5e8592484fe6cc8c49cd7e [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();
526 Optional<OFPortStatsPropOptical> optical = entry.getProperties().stream()
527 .filter(OFPortStatsPropOptical.class::isInstance)
528 .map(OFPortStatsPropOptical.class::cast)
529 .findAny();
530 if (optical.isPresent()) {
531 long flags = optical.get().getFlags();
532
533 int txTune = OFPortStatsOpticalFlagsSerializerVer14.TX_TUNE_VAL;
534 long txFreq = optical.get().getTxFreqLmda();
535 long txOffset = optical.get().getTxOffset();
536 long txGridSpan = optical.get().getTxGridSpan();
537 annotations.set(AK_TX_TUNE_FEATURE, ((flags & txTune) != 0) ? "enabled" : "disabled");
538 annotations.set(AK_TX_FREQ_HZ, mhzToAnnotation(txFreq));
539 annotations.set(AK_TX_OFFSET_HZ, mhzToAnnotation(txOffset));
540 annotations.set(AK_TX_GRID_SPAN_HZ, mhzToAnnotation(txGridSpan));
541
542 int rxTune = OFPortStatsOpticalFlagsSerializerVer14.RX_TUNE_VAL;
543 long rxFreq = optical.get().getRxFreqLmda();
544 long rxOffset = optical.get().getRxOffset();
545 long rxGridSpan = optical.get().getRxGridSpan();
546 annotations.set(AK_RX_TUNE_FEATURE, ((flags & rxTune) != 0) ? "enabled" : "disabled");
547 annotations.set(AK_RX_FREQ_HZ, mhzToAnnotation(rxFreq));
548 annotations.set(AK_RX_OFFSET_HZ, mhzToAnnotation(rxOffset));
549 annotations.set(AK_RX_GRID_SPAN_HZ, mhzToAnnotation(rxGridSpan));
550
551 int txPwrVal = OFPortStatsOpticalFlagsSerializerVer14.TX_PWR_VAL;
552 int txPwr = optical.get().getTxPwr();
553 annotations.set(AK_TX_PWR_FEATURE, ((flags & txPwrVal) != 0) ? "enabled" : "disabled");
554 annotations.set(AK_TX_PWR, Integer.toString(txPwr));
555
556 int rxPwrVal = OFPortStatsOpticalFlagsSerializerVer14.RX_PWR_VAL;
557 int rxPwr = optical.get().getRxPwr();
558 annotations.set(AK_RX_PWR_FEATURE, ((flags & rxPwrVal) != 0) ? "enabled" : "disabled");
559 annotations.set(AK_RX_PWR, Integer.toString(rxPwr));
560
561 int txBias = OFPortStatsOpticalFlagsSerializerVer14.TX_BIAS_VAL;
562 int biasCurrent = optical.get().getBiasCurrent();
563 annotations.set(AK_TX_BIAS_FEATURE, ((flags & txBias) != 0) ? "enabled" : "disabled");
564 annotations.set(AK_BIAS_CURRENT, Integer.toString(biasCurrent));
565
566 int txTemp = OFPortStatsOpticalFlagsSerializerVer14.TX_TEMP_VAL;
567 int temperature = optical.get().getTemperature();
568 annotations.set(AK_TX_TEMP_FEATURE, ((flags & txTemp) != 0) ? "enabled" : "disabled");
569 annotations.set(AK_TEMPERATURE, Integer.toString(temperature));
570 }
Thomas Vachuskaf0397b52015-05-29 13:50:17 -0700571 DefaultPortStatistics.Builder builder = DefaultPortStatistics.builder();
572 DefaultPortStatistics stat = builder.setDeviceId(deviceId)
573 .setPort(entry.getPortNo().getPortNumber())
574 .setPacketsReceived(entry.getRxPackets().getValue())
575 .setPacketsSent(entry.getTxPackets().getValue())
576 .setBytesReceived(entry.getRxBytes().getValue())
577 .setBytesSent(entry.getTxBytes().getValue())
578 .setPacketsRxDropped(entry.getRxDropped().getValue())
579 .setPacketsTxDropped(entry.getTxDropped().getValue())
580 .setPacketsRxErrors(entry.getRxErrors().getValue())
581 .setPacketsTxErrors(entry.getTxErrors().getValue())
582 .setDurationSec(entry.getVersion() == OFVersion.OF_10 ? 0 : entry.getDurationSec())
583 .setDurationNano(entry.getVersion() == OFVersion.OF_10 ? 0 : entry.getDurationNsec())
Laszlo Papp7cf60372018-01-11 00:06:43 +0000584 .setAnnotations(annotations.build())
Thomas Vachuskaf0397b52015-05-29 13:50:17 -0700585 .build();
sangho538108b2015-04-08 14:29:20 -0700586
Thomas Vachuskaf0397b52015-05-29 13:50:17 -0700587 stats.add(stat);
588 } catch (Exception e) {
589 LOG.warn("Unable to process port stats", e);
590 }
sangho538108b2015-04-08 14:29:20 -0700591 }
592
593 return Collections.unmodifiableSet(stats);
sangho538108b2015-04-08 14:29:20 -0700594 }
595
596 private class InternalDeviceProvider implements OpenFlowSwitchListener, OpenFlowEventListener {
Thomas Vachuska893bf4b2015-05-29 18:13:18 -0700597
Srikanth Vavilapalli78baf582015-06-05 11:40:14 -0700598 private HashMap<Dpid, List<OFPortStatsEntry>> portStatsReplies = new HashMap<>();
Ray Milkey7abe50b2016-04-13 11:57:44 -0700599 private boolean isDisabled = false;
Thomas Vachuska893bf4b2015-05-29 18:13:18 -0700600
alshabibf1216ed2014-09-03 11:53:54 -0700601 @Override
tomd1900f32014-09-03 14:08:16 -0700602 public void switchAdded(Dpid dpid) {
alshabib6f5460b2014-09-03 14:46:17 -0700603 if (providerService == null) {
604 return;
605 }
tom782a7cf2014-09-11 23:58:38 -0700606 DeviceId did = deviceId(uri(dpid));
alshabib6f5460b2014-09-03 14:46:17 -0700607 OpenFlowSwitch sw = controller.getSwitch(dpid);
Lei Xudee1aff2015-10-16 00:45:10 -0500608 if (sw == null) {
sangyun-han147e6f42016-07-26 17:14:41 +0900609 LOG.error("Switch {} is not found", dpid);
Lei Xudee1aff2015-10-16 00:45:10 -0500610 return;
611 }
alshabib6f5460b2014-09-03 14:46:17 -0700612
alshabib7911a052014-10-16 17:49:37 -0700613 ChassisId cId = new ChassisId(dpid.value());
Ray Milkeye53f1712015-01-16 09:17:16 -0800614
Victor Silvaac8dab02016-11-28 16:33:08 -0300615 DefaultAnnotations.Builder annotationsBuilder = DefaultAnnotations.builder()
andreafe3308f2015-10-06 15:51:25 -0700616 .set(AnnotationKeys.PROTOCOL, sw.factory().getVersion().toString())
617 .set(AnnotationKeys.CHANNEL_ID, sw.channelId())
Victor Silvaac8dab02016-11-28 16:33:08 -0300618 .set(AnnotationKeys.MANAGEMENT_ADDRESS, sw.channelId().split(":")[0]);
619
Yuta HIGUCHI2341e602017-03-08 20:10:08 -0800620 // FIXME following ignores driver specified by name
Victor Silvaac8dab02016-11-28 16:33:08 -0300621 Driver driver = driverService.getDriver(sw.manufacturerDescription(),
622 sw.hardwareDescription(),
623 sw.softwareDescription());
Thomas Vachuskaa79cbf72017-01-13 15:07:04 -0800624 // FIXME: The following breaks the STC tests and will require to be revisited.
625// if (driver != null) {
626// annotationsBuilder.set(AnnotationKeys.DRIVER, driver.name());
627// }
Victor Silvaac8dab02016-11-28 16:33:08 -0300628
629 SparseAnnotations annotations = annotationsBuilder.build();
Ray Milkeye53f1712015-01-16 09:17:16 -0800630
tomd1900f32014-09-03 14:08:16 -0700631 DeviceDescription description =
Marc De Leenheerb9311372015-07-09 11:36:49 -0700632 new DefaultDeviceDescription(did.uri(), sw.deviceType(),
Ray Milkeyd3edd032015-01-16 11:38:58 -0800633 sw.manufacturerDescription(),
tom782a7cf2014-09-11 23:58:38 -0700634 sw.hardwareDescription(),
635 sw.softwareDescription(),
alshabib7911a052014-10-16 17:49:37 -0700636 sw.serialNumber(),
Thomas Vachuska82041f52014-11-30 22:14:02 -0800637 cId, annotations);
tom782a7cf2014-09-11 23:58:38 -0700638 providerService.deviceConnected(did, description);
Ayaka Koshibe5460d622015-05-14 12:19:19 -0700639 providerService.updatePorts(did, buildPortDescriptions(sw));
sangho538108b2015-04-08 14:29:20 -0700640
sangyun-han07a572f2016-07-29 21:14:27 +0900641 PortStatsCollector psc = new PortStatsCollector(timer, sw, portStatsPollFrequency);
Thomas Vachuskaa394b952016-06-14 15:02:09 -0700642 stopCollectorIfNeeded(collectors.put(dpid, psc));
sangho538108b2015-04-08 14:29:20 -0700643 psc.start();
Lei Xudee1aff2015-10-16 00:45:10 -0500644
645 //figure out race condition for collectors.remove() and collectors.put()
646 if (controller.getSwitch(dpid) == null) {
647 switchRemoved(dpid);
648 }
alshabib25c8eec2014-09-04 16:41:31 -0700649 }
650
Thomas Vachuskaa394b952016-06-14 15:02:09 -0700651 private void stopCollectorIfNeeded(PortStatsCollector collector) {
652 if (collector != null) {
653 collector.stop();
654 }
655 }
656
alshabibf1216ed2014-09-03 11:53:54 -0700657 @Override
658 public void switchRemoved(Dpid dpid) {
Victor Silva03265dd2016-12-08 11:20:36 -0300659 stopCollectorIfNeeded(collectors.remove(dpid));
alshabib6f5460b2014-09-03 14:46:17 -0700660 if (providerService == null) {
661 return;
662 }
tom782a7cf2014-09-11 23:58:38 -0700663 providerService.deviceDisconnected(deviceId(uri(dpid)));
sangho538108b2015-04-08 14:29:20 -0700664 }
Ayaka Koshibe38594c22014-10-22 13:36:12 -0700665
666 @Override
667 public void switchChanged(Dpid dpid) {
HIGUCHI Yuta1979f552015-12-28 21:24:26 -0800668 LOG.debug("switchChanged({})", dpid);
Ayaka Koshibe38594c22014-10-22 13:36:12 -0700669 if (providerService == null) {
670 return;
671 }
672 DeviceId did = deviceId(uri(dpid));
673 OpenFlowSwitch sw = controller.getSwitch(dpid);
Lei Xudee1aff2015-10-16 00:45:10 -0500674 if (sw == null) {
sangyun-han147e6f42016-07-26 17:14:41 +0900675 LOG.error("Switch {} is not found", dpid);
Lei Xudee1aff2015-10-16 00:45:10 -0500676 return;
677 }
HIGUCHI Yuta1979f552015-12-28 21:24:26 -0800678 final List<PortDescription> ports = buildPortDescriptions(sw);
679 LOG.debug("switchChanged({}) {}", did, ports);
680 providerService.updatePorts(did, ports);
Ayaka Koshibe38594c22014-10-22 13:36:12 -0700681 }
682
alshabiba14f3642014-09-05 09:31:31 -0700683 @Override
684 public void portChanged(Dpid dpid, OFPortStatus status) {
HIGUCHI Yuta1979f552015-12-28 21:24:26 -0800685 LOG.debug("portChanged({},{})", dpid, status);
alshabibafc514a2014-12-01 14:44:05 -0800686 PortDescription portDescription = buildPortDescription(status);
Michal Machce774332017-01-25 11:02:55 +0100687 if (status.getReason() != OFPortReason.DELETE) {
688 providerService.portStatusChanged(deviceId(uri(dpid)), portDescription);
689 } else {
690 providerService.deletePort(deviceId(uri(dpid)), portDescription);
691 }
alshabibf1216ed2014-09-03 11:53:54 -0700692 }
693
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700694 @Override
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700695 public void receivedRoleReply(Dpid dpid, RoleState requested, RoleState response) {
HIGUCHI Yuta1979f552015-12-28 21:24:26 -0800696 LOG.debug("receivedRoleReply({},{},{})", dpid, requested, response);
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700697 MastershipRole request = roleOf(requested);
698 MastershipRole reply = roleOf(response);
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700699 providerService.receivedRoleReply(deviceId(uri(dpid)), request, reply);
700 }
701
702 /**
703 * Translates a RoleState to the corresponding MastershipRole.
704 *
Thomas Vachuskafc52fec2015-05-18 19:13:56 -0700705 * @param response role state
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700706 * @return a MastershipRole
707 */
708 private MastershipRole roleOf(RoleState response) {
709 switch (response) {
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700710 case MASTER:
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700711 return MastershipRole.MASTER;
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700712 case EQUAL:
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700713 return MastershipRole.STANDBY;
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700714 case SLAVE:
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700715 return MastershipRole.NONE;
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700716 default:
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700717 LOG.warn("unknown role {}", response);
718 return null;
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700719 }
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700720 }
721
alshabiba14f3642014-09-05 09:31:31 -0700722 /**
723 * Builds a list of port descriptions for a given list of ports.
tomff7eb7c2014-09-08 12:49:03 -0700724 *
alshabiba14f3642014-09-05 09:31:31 -0700725 * @return list of portdescriptions
726 */
Ayaka Koshibe5460d622015-05-14 12:19:19 -0700727 private List<PortDescription> buildPortDescriptions(OpenFlowSwitch sw) {
Yuta HIGUCHIf83c8cf2017-12-17 14:33:49 -0800728 List<OFPortDesc> ofPorts = sw.getPorts();
729 final List<PortDescription> portDescs = new ArrayList<>(ofPorts.size());
Rimon Ashkenazy8ebfff02016-02-01 11:56:36 +0200730 if (!((Device.Type.ROADM.equals(sw.deviceType())) ||
MaoLuc201ae42017-02-06 17:57:01 -0800731 (Device.Type.OTN.equals(sw.deviceType())) ||
732 (Device.Type.OPTICAL_AMPLIFIER.equals(sw.deviceType())))) {
Yuta HIGUCHI2341e602017-03-08 20:10:08 -0800733 // build regular (=non-optical) Device ports
Yuta HIGUCHIf83c8cf2017-12-17 14:33:49 -0800734 ofPorts.forEach(port -> portDescs.add(buildPortDescription(port)));
Yafit Hadara9a73de2015-09-06 13:52:52 +0300735 }
Marc De Leenheerb9311372015-07-09 11:36:49 -0700736
Yuta HIGUCHI2341e602017-03-08 20:10:08 -0800737 // TODO handle Optical Device, but plain OF devices(1.4 and later)
738
Marc De Leenheerb9311372015-07-09 11:36:49 -0700739 OpenFlowOpticalSwitch opsw;
740 switch (sw.deviceType()) {
741 case ROADM:
Rimon Ashkenazy8ebfff02016-02-01 11:56:36 +0200742 case OTN:
MaoLuc201ae42017-02-06 17:57:01 -0800743 case OPTICAL_AMPLIFIER:
Marc De Leenheerb9311372015-07-09 11:36:49 -0700744 opsw = (OpenFlowOpticalSwitch) sw;
Yafit Hadara9a73de2015-09-06 13:52:52 +0300745 List<OFPortDesc> ports = opsw.getPorts();
746 LOG.debug("SW ID {} , ETH- ODU CLT Ports {}", opsw.getId(), ports);
747 // ODU client ports are reported as ETH
748 ports.forEach(port -> portDescs.add(buildOduCltPortDescription(port)));
749
Marc De Leenheerb9311372015-07-09 11:36:49 -0700750 opsw.getPortTypes().forEach(type -> {
Yafit Hadara9a73de2015-09-06 13:52:52 +0300751 List<? extends OFObject> portsOf = opsw.getPortsOf(type);
752 LOG.debug("Ports Of{}", portsOf);
753 portsOf.forEach(
754 op -> {
Jimmy Yan4deb03b2016-06-24 10:53:54 -0700755 portDescs.add(buildPortDescription(type, op, opsw));
Yafit Hadara9a73de2015-09-06 13:52:52 +0300756 }
757 );
Marc De Leenheerb9311372015-07-09 11:36:49 -0700758 });
759 break;
760 case FIBER_SWITCH:
761 opsw = (OpenFlowOpticalSwitch) sw;
762 opsw.getPortTypes().forEach(type -> {
Yuta HIGUCHIf42a2cc2017-05-02 17:04:31 -0700763 opsw.getPortsOf(type).forEach(op -> {
764 if (op instanceof OFPortDesc) {
765 // ports using standard optical extension
766 // TODO OFMessage -> PortDescription should
767 // probably be a Behaviour
768 portDescs.add(oms(buildPortDescription((OFPortDesc) op)));
769 } else if (op instanceof OFCalientPortDescStatsEntry) {
770 // calient extension
771 portDescs.add(buildPortDescription((OFCalientPortDescStatsEntry) op));
772 } else {
773 LOG.warn("Unexpected FIBER_SWITCH port {} on {}",
774 op, sw.getStringId());
775 }
776 });
Marc De Leenheerb9311372015-07-09 11:36:49 -0700777 });
778 break;
779 default:
780 break;
alshabib4680bb62014-09-04 17:15:08 -0700781 }
Marc De Leenheerb9311372015-07-09 11:36:49 -0700782
alshabib4680bb62014-09-04 17:15:08 -0700783 return portDescs;
784 }
785
Yuta HIGUCHIf42a2cc2017-05-02 17:04:31 -0700786 /**
787 * Ensures returned PortDescription is an OMS port.
788 *
789 * @param descr input PortDescription
790 * @return OMS PortDescription
791 */
792 private PortDescription oms(PortDescription descr) {
793 // Hack until OFMessage -> PortDescription transformation
794 // becomes a Behaviour
795 if (descr.type() == Type.OMS) {
796 return descr;
797 }
798
799 Builder builder = DefaultAnnotations.builder();
800 builder.putAll(descr.annotations());
801
802 // set reasonable default when mandatory key is missing
803 if (Strings.isNullOrEmpty(descr.annotations().value(AK_MIN_FREQ_HZ))) {
804 builder.set(AK_MIN_FREQ_HZ, String.valueOf(Spectrum.O_BAND_MIN.asHz()));
805 }
806
807 if (Strings.isNullOrEmpty(descr.annotations().value(AK_MAX_FREQ_HZ))) {
808 builder.set(AK_MAX_FREQ_HZ, String.valueOf(Spectrum.O_BAND_MAX.asHz()));
809 }
810
811 if (Strings.isNullOrEmpty(descr.annotations().value(AK_GRID_HZ))) {
812 builder.set(AK_GRID_HZ, String.valueOf(Frequency.ofGHz(50).asHz()));
813 }
814
815 return DefaultPortDescription.builder(descr)
816 .type(Type.OMS)
817 .annotations(builder.build())
818 .build();
819 }
820
Yafit Hadara9a73de2015-09-06 13:52:52 +0300821 private PortDescription buildOduCltPortDescription(OFPortDesc port) {
822 PortNumber portNo = PortNumber.portNumber(port.getPortNo().getPortNumber());
823 boolean enabled = !port.getState().contains(OFPortState.LINK_DOWN) &&
824 !port.getConfig().contains(OFPortConfig.PORT_DOWN);
Rimon Ashkenazyd15ebf52016-01-06 14:36:13 +0200825 Long portSpeedInMbps = portSpeed(port);
Toru Furusawa72ee30c2016-01-08 13:29:04 -0800826 CltSignalType sigType = null;
Yafit Hadara9a73de2015-09-06 13:52:52 +0300827
Rimon Ashkenazyd15ebf52016-01-06 14:36:13 +0200828 switch (portSpeedInMbps.toString()) {
829 case "1000":
Toru Furusawa72ee30c2016-01-08 13:29:04 -0800830 sigType = CltSignalType.CLT_1GBE;
Yafit Hadara9a73de2015-09-06 13:52:52 +0300831 break;
Rimon Ashkenazyd15ebf52016-01-06 14:36:13 +0200832 case "10000":
Toru Furusawa72ee30c2016-01-08 13:29:04 -0800833 sigType = CltSignalType.CLT_10GBE;
Yafit Hadara9a73de2015-09-06 13:52:52 +0300834 break;
Rimon Ashkenazyd15ebf52016-01-06 14:36:13 +0200835 case "40000":
Toru Furusawa72ee30c2016-01-08 13:29:04 -0800836 sigType = CltSignalType.CLT_40GBE;
Yafit Hadara9a73de2015-09-06 13:52:52 +0300837 break;
Rimon Ashkenazyd15ebf52016-01-06 14:36:13 +0200838 case "100000":
Toru Furusawa72ee30c2016-01-08 13:29:04 -0800839 sigType = CltSignalType.CLT_100GBE;
Yafit Hadara9a73de2015-09-06 13:52:52 +0300840 break;
841 default:
Rimon Ashkenazyd15ebf52016-01-06 14:36:13 +0200842 throw new RuntimeException("Un recognize OduClt speed: " + portSpeedInMbps.toString());
Yafit Hadara9a73de2015-09-06 13:52:52 +0300843 }
844
845 SparseAnnotations annotations = buildOduCltAnnotation(port);
HIGUCHI Yuta4c0ef6b2016-05-02 19:45:41 -0700846 return oduCltPortDescription(portNo, enabled, sigType, annotations);
Yafit Hadara9a73de2015-09-06 13:52:52 +0300847 }
848
849 private SparseAnnotations buildOduCltAnnotation(OFPortDesc port) {
850 SparseAnnotations annotations = null;
851 String portName = Strings.emptyToNull(port.getName());
852 if (portName != null) {
853 annotations = DefaultAnnotations.builder()
854 .set(AnnotationKeys.PORT_NAME, portName)
855 .set(AnnotationKeys.STATIC_PORT, Boolean.TRUE.toString()).build();
856 }
857 return annotations;
858 }
859
Jimmy Yan4deb03b2016-06-24 10:53:54 -0700860 private PortDescription buildPortDescription(PortDescPropertyType ptype, OFObject port,
861 OpenFlowOpticalSwitch opsw) {
yjimmyy646aa022016-07-05 12:09:50 -0700862 if (port instanceof OFPortOptical) {
Jimmy Yan4deb03b2016-06-24 10:53:54 -0700863 return buildPortDescription(ptype, (OFPortOptical) port, opsw);
Yafit Hadara9a73de2015-09-06 13:52:52 +0300864 }
865 return buildPortDescription(ptype, (OFExpPort) port);
866 }
867
Rimon Ashkenazy8ebfff02016-02-01 11:56:36 +0200868 private boolean matchingOtuPortSignalTypes(OFPortOpticalTransportSignalType sigType,
869 OduSignalType oduSignalType) {
870 switch (sigType) {
871 case OTU2:
872 if (oduSignalType == OduSignalType.ODU2) {
873 return true;
874 }
875 break;
876 case OTU4:
877 if (oduSignalType == OduSignalType.ODU4) {
878 return true;
879 }
880 break;
881 default:
882 break;
883 }
884 return false;
885 }
Yafit Hadara9a73de2015-09-06 13:52:52 +0300886 /**
887 * Build a portDescription from a given a port description describing some
888 * Optical port.
889 *
890 * @param ptype description property type.
891 * @param port the port to build from.
892 * @return portDescription for the port.
893 */
894 private PortDescription buildPortDescription(PortDescPropertyType ptype, OFExpPort port) {
895 PortNumber portNo = PortNumber.portNumber(port.getPortNo().getPortNumber());
896 boolean enabled = !port.getState().contains(OFPortState.LINK_DOWN)
897 && !port.getConfig().contains(OFPortConfig.PORT_DOWN);
mskalac584bd92017-11-28 15:51:17 +0100898 boolean adminDown = port.getConfig().contains(OFPortConfig.PORT_DOWN);
899 SparseAnnotations annotations = makePortAnnotation(port.getName(),
900 port.getHwAddr().toString(),
901 adminDown).build();
Yafit Hadara9a73de2015-09-06 13:52:52 +0300902
903 OFExpPortDescPropOpticalTransport firstProp = port.getProperties().get(0);
904 OFPortOpticalTransportSignalType sigType = firstProp.getPortSignalType();
905
HIGUCHI Yuta34a3f692016-01-09 21:08:57 -0800906 PortDescription portDes = null;
Yafit Hadara9a73de2015-09-06 13:52:52 +0300907 switch (sigType) {
908 case OMSN:
HIGUCHI Yuta95d83e82016-04-26 12:13:48 -0700909 portDes = omsPortDescription(portNo, enabled,
Rimon Ashkenazye3201032016-01-11 14:27:30 +0200910 FREQ191_7, FREQ191_7.add(FREQ4_4), FREQ50, annotations);
Yafit Hadara9a73de2015-09-06 13:52:52 +0300911 break;
912 case OCH:
913 OFExpPortOpticalTransportLayerEntry entry = firstProp.getFeatures().get(0).getValue().get(0);
914 OFPortOpticalTransportLayerClass layerClass = entry.getLayerClass();
915 if (!OFPortOpticalTransportLayerClass.ODU.equals(layerClass)) {
916 LOG.error("Unsupported layer Class {} ", layerClass);
917 return null;
918 }
919
920 // convert to ONOS OduSignalType
921 OduSignalType oduSignalType = OpenFlowDeviceValueMapper.
922 lookupOduSignalType((byte) entry.getSignalType());
923 //OchSignal is needed for OchPortDescription constructor,
924 //yet not relevant for tunable OCH port, creating with default parameters
925 OchSignal signalId = new OchSignal(GridType.DWDM, ChannelSpacing.CHL_50GHZ, 1, 1);
926
HIGUCHI Yuta34a3f692016-01-09 21:08:57 -0800927 portDes = ochPortDescription(portNo, enabled,
928 oduSignalType, true,
929 signalId, annotations);
Yafit Hadara9a73de2015-09-06 13:52:52 +0300930
931 break;
932 case OTU2:
933 case OTU4:
Rimon Ashkenazy8ebfff02016-02-01 11:56:36 +0200934 entry = firstProp.getFeatures().get(0).getValue().get(0);
935 layerClass = entry.getLayerClass();
936 if (!OFPortOpticalTransportLayerClass.ODU.equals(layerClass)) {
937 LOG.error("Unsupported layer Class {} ", layerClass);
938 return null;
939 }
940
941 // convert to ONOS OduSignalType
942 OduSignalType oduSignalTypeOtuPort = OpenFlowDeviceValueMapper.
943 lookupOduSignalType((byte) entry.getSignalType());
944 if (!matchingOtuPortSignalTypes(sigType, oduSignalTypeOtuPort)) {
945 LOG.error("Wrong oduSignalType {} for OTU Port sigType {} ", oduSignalTypeOtuPort, sigType);
946 return null;
947 }
948 OtuSignalType otuSignalType =
949 ((sigType == OFPortOpticalTransportSignalType.OTU2) ? OtuSignalType.OTU2 :
950 OtuSignalType.OTU4);
HIGUCHI Yuta5be3e822016-05-03 13:51:42 -0700951 portDes = otuPortDescription(portNo, enabled, otuSignalType, annotations);
Rimon Ashkenazy8ebfff02016-02-01 11:56:36 +0200952 break;
Yafit Hadara9a73de2015-09-06 13:52:52 +0300953 default:
954 break;
955 }
956
957 return portDes;
958 }
959
alshabiba14f3642014-09-05 09:31:31 -0700960 /**
Yuta HIGUCHI2341e602017-03-08 20:10:08 -0800961 * Creates an annotation builder for the port name if one is available.
Ray Milkeye0fade72015-01-15 13:29:47 -0800962 *
lishuai4ee42042015-11-30 17:19:21 +0800963 * @param portName the port name
964 * @param portMac the port mac
mskalac584bd92017-11-28 15:51:17 +0100965 * @param adminDown the port admin state
966 * @return annotation builder containing port admin state, port name
967 * and/or port MAC if any of the two is found
Ray Milkeye0fade72015-01-15 13:29:47 -0800968 */
mskalac584bd92017-11-28 15:51:17 +0100969 private DefaultAnnotations.Builder makePortAnnotation(String portName, String portMac, boolean adminDown) {
Andrea Campanellac8b871a2017-01-19 13:53:05 -0800970 DefaultAnnotations.Builder builder = DefaultAnnotations.builder();
lishuai4ee42042015-11-30 17:19:21 +0800971 String pName = Strings.emptyToNull(portName);
972 String pMac = Strings.emptyToNull(portMac);
Andrea Campanellac8b871a2017-01-19 13:53:05 -0800973 if (pName != null) {
974 builder.set(AnnotationKeys.PORT_NAME, pName);
Ray Milkeye0fade72015-01-15 13:29:47 -0800975 }
Andrea Campanellac8b871a2017-01-19 13:53:05 -0800976 if (pMac != null) {
977 builder.set(AnnotationKeys.PORT_MAC, pMac);
978 }
mskalac584bd92017-11-28 15:51:17 +0100979 String adminState = adminDown ? "disabled" : "enabled";
980 builder.set(AnnotationKeys.ADMIN_STATE, adminState);
Yuta HIGUCHI2341e602017-03-08 20:10:08 -0800981 return builder;
982 }
983
984 private String mhzToAnnotation(long freqMhz) {
Laszlo Papp7cf60372018-01-11 00:06:43 +0000985 return OpenFlowDeviceProvider.mhzToAnnotation(freqMhz);
Yuta HIGUCHI2341e602017-03-08 20:10:08 -0800986 }
987
Yuta HIGUCHIf42a2cc2017-05-02 17:04:31 -0700988 private String lambdaToAnnotationHz(long lambda) {
989 // ref. OF1.5: wavelength (lambda) as nm * 100
990
991 long c = 299792458; // speed of light in m/s
992 // f = c / λ
993 // (m/s) * (nm/m) / (nm * 100) * 100
994 // annotations is in Hz
995 return Long.toString(c * 1_000_000_000 / lambda * 100);
996 }
997
Yuta HIGUCHI2341e602017-03-08 20:10:08 -0800998 private PortDescription buildPortDescription14(OFPortDesc port) {
999 PortNumber portNo = PortNumber.portNumber(port.getPortNo().getPortNumber());
1000 boolean enabled =
1001 !port.getState().contains(OFPortState.LINK_DOWN) &&
1002 !port.getConfig().contains(OFPortConfig.PORT_DOWN);
mskalac584bd92017-11-28 15:51:17 +01001003 boolean adminDown = port.getConfig().contains(OFPortConfig.PORT_DOWN);
Yuta HIGUCHI2341e602017-03-08 20:10:08 -08001004 Builder annotations = makePortAnnotation(port.getName(),
mskalac584bd92017-11-28 15:51:17 +01001005 port.getHwAddr().toString(),
1006 adminDown);
Yuta HIGUCHI2341e602017-03-08 20:10:08 -08001007
1008 Optional<OFPortDescPropEthernet> ether = port.getProperties().stream()
1009 .filter(OFPortDescPropEthernet.class::isInstance)
1010 .map(OFPortDescPropEthernet.class::cast)
1011 .findAny();
1012 if (ether.isPresent()) {
1013 // ethernet port
1014 // TODO parse other part of OFPortDescPropEthernet if necessary
1015 return new DefaultPortDescription(portNo,
1016 enabled,
1017 COPPER,
1018 portSpeed(port),
1019 annotations.build());
1020 }
1021
1022 Optional<OFPortDescPropOptical> optical = port.getProperties().stream()
1023 .filter(OFPortDescPropOptical.class::isInstance)
1024 .map(OFPortDescPropOptical.class::cast)
1025 .findAny();
1026 if (optical.isPresent()) {
1027 // optical port
1028
Yuta HIGUCHIf42a2cc2017-05-02 17:04:31 -07001029 // FIXME is there a OF version neutral way to access
1030 // OFOpticalPortFeaturesSerializerVer14
Yuta HIGUCHI2341e602017-03-08 20:10:08 -08001031
Laszlo Papp20cfb6f2018-01-08 16:06:53 +00001032 long supported = optical.get().getSupported();
1033 long rxMin = optical.get().getRxMinFreqLmda();
1034 long rxMax = optical.get().getRxMaxFreqLmda();
1035 long rxGrid = optical.get().getRxGridFreqLmda();
1036 long txMin = optical.get().getTxMinFreqLmda();
1037 long txMax = optical.get().getTxMaxFreqLmda();
1038 long txGrid = optical.get().getTxGridFreqLmda();
1039
1040 int txTune = OFOpticalPortFeaturesSerializerVer14.TX_TUNE_VAL;
1041 int rxTune = OFOpticalPortFeaturesSerializerVer14.RX_TUNE_VAL;
1042 annotations.set(AK_TX_TUNE_FEATURE,
1043 ((supported & txTune) != 0) ? "enabled" : "disabled");
1044 annotations.set(AK_RX_TUNE_FEATURE,
1045 ((supported & rxTune) != 0) ? "enabled" : "disabled");
1046
Yuta HIGUCHI2341e602017-03-08 20:10:08 -08001047 // wire value for OFOpticalPortFeatures.USE_FREQ
Yuta HIGUCHIf42a2cc2017-05-02 17:04:31 -07001048 long useFreq = OFOpticalPortFeaturesSerializerVer14.USE_FREQ_VAL;
Laszlo Papp20cfb6f2018-01-08 16:06:53 +00001049 if ((supported & useFreq) != 0) {
Yuta HIGUCHI2341e602017-03-08 20:10:08 -08001050 // unit is in Frequency Mhz
Laszlo Papp20cfb6f2018-01-08 16:06:53 +00001051 annotations.set(AK_RX_MIN_FREQ_HZ, mhzToAnnotation(rxMin));
1052 annotations.set(AK_RX_MAX_FREQ_HZ, mhzToAnnotation(rxMax));
1053 annotations.set(AK_RX_GRID_HZ, mhzToAnnotation(rxGrid));
Yuta HIGUCHI2341e602017-03-08 20:10:08 -08001054
Laszlo Papp20cfb6f2018-01-08 16:06:53 +00001055 annotations.set(AK_TX_MIN_FREQ_HZ, mhzToAnnotation(txMin));
1056 annotations.set(AK_TX_MAX_FREQ_HZ, mhzToAnnotation(txMax));
1057 annotations.set(AK_TX_GRID_HZ, mhzToAnnotation(txGrid));
Yuta HIGUCHI2341e602017-03-08 20:10:08 -08001058
1059 // FIXME pretty confident this is not going to happen
1060 // unless Device models Tx/Rx ports as separate port
Laszlo Papp20cfb6f2018-01-08 16:06:53 +00001061 if (rxMin == txMin) {
Yuta HIGUCHI2341e602017-03-08 20:10:08 -08001062 annotations.set(AK_MIN_FREQ_HZ,
Laszlo Papp20cfb6f2018-01-08 16:06:53 +00001063 mhzToAnnotation(rxMin));
Yuta HIGUCHI2341e602017-03-08 20:10:08 -08001064 }
Laszlo Papp20cfb6f2018-01-08 16:06:53 +00001065 if (rxMax == txMax) {
Yuta HIGUCHI2341e602017-03-08 20:10:08 -08001066 annotations.set(AK_MAX_FREQ_HZ,
Laszlo Papp20cfb6f2018-01-08 16:06:53 +00001067 mhzToAnnotation(rxMax));
1068 }
1069 if (rxGrid == txGrid) {
1070 annotations.set(AK_GRID_HZ,
1071 mhzToAnnotation(rxGrid));
Yuta HIGUCHI2341e602017-03-08 20:10:08 -08001072 }
1073
1074 } else {
1075 // unit is in Lambda nm * 100
Yuta HIGUCHIf42a2cc2017-05-02 17:04:31 -07001076 annotations.set(AK_RX_MIN_FREQ_HZ, lambdaToAnnotationHz(rxMin));
1077 annotations.set(AK_RX_MAX_FREQ_HZ, lambdaToAnnotationHz(rxMax));
Laszlo Papp20cfb6f2018-01-08 16:06:53 +00001078 annotations.set(AK_RX_GRID_HZ, lambdaToAnnotationHz(rxGrid));
Yuta HIGUCHIf42a2cc2017-05-02 17:04:31 -07001079
1080 annotations.set(AK_TX_MIN_FREQ_HZ, lambdaToAnnotationHz(txMin));
1081 annotations.set(AK_TX_MAX_FREQ_HZ, lambdaToAnnotationHz(txMax));
Laszlo Papp20cfb6f2018-01-08 16:06:53 +00001082 annotations.set(AK_TX_GRID_HZ, lambdaToAnnotationHz(txGrid));
Yuta HIGUCHIf42a2cc2017-05-02 17:04:31 -07001083
1084 // FIXME pretty confident this is not going to happen
1085 // unless Device models Tx/Rx ports as separate port
1086 if (rxMin == txMin) {
1087 annotations.set(AK_MIN_FREQ_HZ,
1088 lambdaToAnnotationHz(rxMin));
1089 }
1090 if (rxMax == txMax) {
1091 annotations.set(AK_MAX_FREQ_HZ,
1092 lambdaToAnnotationHz(rxMax));
1093 }
Laszlo Papp20cfb6f2018-01-08 16:06:53 +00001094 if (rxGrid == txGrid) {
1095 annotations.set(AK_GRID_HZ,
1096 lambdaToAnnotationHz(rxGrid));
1097 }
Yuta HIGUCHIf42a2cc2017-05-02 17:04:31 -07001098
Yuta HIGUCHI2341e602017-03-08 20:10:08 -08001099 }
Laszlo Papp20cfb6f2018-01-08 16:06:53 +00001100
1101 int txPwr = OFOpticalPortFeaturesSerializerVer14.TX_PWR_VAL;
1102 long txPwrMin = optical.get().getTxPwrMin();
1103 long txPwrMax = optical.get().getTxPwrMax();
1104 annotations.set(AK_TX_PWR_FEATURE, ((supported & txPwr) != 0) ? "enabled" : "disabled");
1105 annotations.set(AK_TX_PWR_MIN, Long.toString(txPwrMin));
1106 annotations.set(AK_TX_PWR_MAX, Long.toString(txPwrMax));
Yuta HIGUCHI2341e602017-03-08 20:10:08 -08001107
1108 // TODO How to determine appropriate port type?
1109
1110 return new DefaultPortDescription(portNo,
1111 enabled,
1112 FIBER,
1113 portSpeed(port),
1114 annotations.build());
1115 }
1116
1117 // fall back default
1118 return new DefaultPortDescription(portNo,
1119 enabled,
1120 COPPER,
1121 portSpeed(port),
1122 annotations.build());
1123
Ray Milkeye0fade72015-01-15 13:29:47 -08001124 }
1125
1126 /**
Ayaka Koshibe5460d622015-05-14 12:19:19 -07001127 * Build a portDescription from a given Ethernet port description.
tomff7eb7c2014-09-08 12:49:03 -07001128 *
alshabiba14f3642014-09-05 09:31:31 -07001129 * @param port the port to build from.
1130 * @return portDescription for the port.
1131 */
1132 private PortDescription buildPortDescription(OFPortDesc port) {
Yuta HIGUCHI2341e602017-03-08 20:10:08 -08001133 if (port.getVersion().wireVersion >= OFVersion.OF_14.getWireVersion()) {
1134 return buildPortDescription14(port);
1135 }
Thomas Vachuskad16ce182014-10-29 17:25:29 -07001136 PortNumber portNo = PortNumber.portNumber(port.getPortNo().getPortNumber());
1137 boolean enabled =
1138 !port.getState().contains(OFPortState.LINK_DOWN) &&
1139 !port.getConfig().contains(OFPortConfig.PORT_DOWN);
1140 Port.Type type = port.getCurr().contains(OFPortFeatures.PF_FIBER) ? FIBER : COPPER;
mskalac584bd92017-11-28 15:51:17 +01001141 boolean adminDown = port.getConfig().contains(OFPortConfig.PORT_DOWN);
1142 SparseAnnotations annotations = makePortAnnotation(port.getName(),
1143 port.getHwAddr().toString(),
1144 adminDown).build();
Ray Milkeye0fade72015-01-15 13:29:47 -08001145 return new DefaultPortDescription(portNo, enabled, type,
1146 portSpeed(port), annotations);
alshabiba14f3642014-09-05 09:31:31 -07001147 }
Ayaka Koshibeab91cc42014-09-25 10:20:52 -07001148
Ayaka Koshibe5460d622015-05-14 12:19:19 -07001149 /**
1150 * Build a portDescription from a given a port description describing some
1151 * Optical port.
1152 *
1153 * @param port description property type.
1154 * @param port the port to build from.
1155 * @return portDescription for the port.
1156 */
Jimmy Yan4deb03b2016-06-24 10:53:54 -07001157 private PortDescription buildPortDescription(PortDescPropertyType ptype, OFPortOptical port,
1158 OpenFlowOpticalSwitch opsw) {
Jon Hallcbd1b392017-01-18 20:15:44 -08001159 checkArgument(!port.getDesc().isEmpty());
Marc De Leenheerfc913dd2015-07-30 16:04:55 -07001160
Ayaka Koshibe5460d622015-05-14 12:19:19 -07001161 // Minimally functional fixture. This needs to be fixed as we add better support.
1162 PortNumber portNo = PortNumber.portNumber(port.getPortNo().getPortNumber());
Ayaka Koshibe74b55272015-05-28 15:16:04 -07001163
Ayaka Koshibe5460d622015-05-14 12:19:19 -07001164 boolean enabled = !port.getState().contains(OFPortState.LINK_DOWN)
1165 && !port.getConfig().contains(OFPortConfig.PORT_DOWN);
mskalac584bd92017-11-28 15:51:17 +01001166 boolean adminDown = port.getConfig().contains(OFPortConfig.PORT_DOWN);
1167 SparseAnnotations annotations = makePortAnnotation(port.getName(),
1168 port.getHwAddr().toString(),
1169 adminDown).build();
Ayaka Koshibe5460d622015-05-14 12:19:19 -07001170
1171 if (port.getVersion() == OFVersion.OF_13
1172 && ptype == PortDescPropertyType.OPTICAL_TRANSPORT) {
1173 // At this point, not much is carried in the optical port message.
Ayaka Koshibeae541732015-05-19 13:37:27 -07001174 LOG.debug("Optical transport port message {}", port.toString());
Ayaka Koshibe5460d622015-05-14 12:19:19 -07001175 } else {
1176 // removable once 1.4+ support complete.
Ayaka Koshibeae541732015-05-19 13:37:27 -07001177 LOG.debug("Unsupported optical port properties");
Ayaka Koshibe5460d622015-05-14 12:19:19 -07001178 }
Marc De Leenheerfc913dd2015-07-30 16:04:55 -07001179
1180 OFPortDescPropOpticalTransport desc = port.getDesc().get(0);
1181 switch (desc.getPortSignalType()) {
1182 // FIXME: use constants once loxi has full optical extensions
1183 case 2: // OMS port
1184 // Assume complete optical spectrum and 50 GHz grid
Jimmy Yan4deb03b2016-06-24 10:53:54 -07001185 Set<OchSignal> signals = null;
1186 if (opsw instanceof HandlerBehaviour) {
1187 DriverHandler driverHandler = ((HandlerBehaviour) opsw).handler();
1188 if (driverHandler != null && driverHandler.hasBehaviour(LambdaQuery.class)) {
1189 try {
1190 signals = driverHandler.behaviour(LambdaQuery.class).queryLambdas(portNo);
1191 } catch (NullPointerException e) {
1192 signals = null;
1193 }
1194 }
1195 }
1196 Frequency minFreq;
1197 Frequency maxFreq;
1198 Frequency channelSpacing;
1199 if (signals == null || signals.isEmpty()) {
1200 minFreq = Spectrum.U_BAND_MIN;
1201 maxFreq = Spectrum.O_BAND_MAX;
1202 channelSpacing = Frequency.ofGHz(50);
1203 } else {
1204 Comparator<OchSignal> compare =
1205 (OchSignal a, OchSignal b) -> a.spacingMultiplier() - b.spacingMultiplier();
1206 OchSignal minOch = Collections.min(signals, compare);
1207 OchSignal maxOch = Collections.max(signals, compare);
1208 minFreq = minOch.centralFrequency();
1209 maxFreq = maxOch.centralFrequency();
1210 channelSpacing = minOch.channelSpacing().frequency();
1211 }
1212 return omsPortDescription(portNo, enabled, minFreq,
1213 maxFreq, channelSpacing, annotations);
Marc De Leenheerfc913dd2015-07-30 16:04:55 -07001214 case 5: // OCH port
1215 OchSignal signal = new OchSignal(GridType.DWDM, ChannelSpacing.CHL_50GHZ, 0, 4);
HIGUCHI Yuta34a3f692016-01-09 21:08:57 -08001216 return ochPortDescription(portNo, enabled, OduSignalType.ODU4,
Marc De Leenheerfc913dd2015-07-30 16:04:55 -07001217 true, signal, annotations);
1218 default:
1219 break;
1220 }
1221
Ayaka Koshibe74b55272015-05-28 15:16:04 -07001222 return new DefaultPortDescription(portNo, enabled, FIBER, 0, annotations);
Ayaka Koshibe5460d622015-05-14 12:19:19 -07001223 }
1224
Marc De Leenheerb9311372015-07-09 11:36:49 -07001225 /**
1226 * Build a portDescription from a given port description describing a fiber switch optical port.
1227 *
1228 * @param port description property type.
1229 * @param port the port to build from.
1230 * @return portDescription for the port.
1231 */
1232 private PortDescription buildPortDescription(OFCalientPortDescStatsEntry port) {
1233 PortNumber portNo = PortNumber.portNumber(port.getPortNo().getPortNumber());
1234
Marc De Leenheerc662d322016-02-18 16:05:10 -08001235 // Use the alias name if it's available
1236 String name = port.getName();
1237 List<OFCalientPortDescProp> props = port.getProperties();
Jon Hallcbd1b392017-01-18 20:15:44 -08001238 if (props != null && !props.isEmpty()) {
Marc De Leenheerc662d322016-02-18 16:05:10 -08001239 OFCalientPortDescPropOptical propOptical = (OFCalientPortDescPropOptical) props.get(0);
1240 if (propOptical != null) {
1241 name = propOptical.getInAlias();
1242 }
1243 }
1244
Marc De Leenheerb9311372015-07-09 11:36:49 -07001245 // FIXME when Calient OF agent reports port status
1246 boolean enabled = true;
mskalac584bd92017-11-28 15:51:17 +01001247 boolean adminDown = false;
1248 SparseAnnotations annotations = makePortAnnotation(name, port.getHwAddr().toString(), adminDown).build();
Marc De Leenheerb9311372015-07-09 11:36:49 -07001249
Marc De Leenheerfc913dd2015-07-30 16:04:55 -07001250 // S160 data sheet
1251 // Wavelength range: 1260 - 1630 nm, grid is irrelevant for this type of switch
HIGUCHI Yuta95d83e82016-04-26 12:13:48 -07001252 return omsPortDescription(portNo, enabled,
Yuta HIGUCHIf42a2cc2017-05-02 17:04:31 -07001253 Spectrum.O_BAND_MIN, Spectrum.O_BAND_MAX, Frequency.ofGHz(50), annotations);
Marc De Leenheerb9311372015-07-09 11:36:49 -07001254 }
1255
alshabibafc514a2014-12-01 14:44:05 -08001256 private PortDescription buildPortDescription(OFPortStatus status) {
1257 OFPortDesc port = status.getDesc();
1258 if (status.getReason() != OFPortReason.DELETE) {
1259 return buildPortDescription(port);
1260 } else {
Yuta HIGUCHI6512f3e2017-05-18 17:21:24 -07001261 PortDescription desc = buildPortDescription(port);
1262 if (desc.isEnabled()) {
1263 return DefaultPortDescription.builder(desc)
1264 .isEnabled(false)
1265 .build();
1266 }
1267 return desc;
alshabibafc514a2014-12-01 14:44:05 -08001268 }
1269 }
1270
Yuta HIGUCHI2341e602017-03-08 20:10:08 -08001271 /**
1272 * Returns port speed in Mbps.
1273 *
1274 * @param port description to parse
1275 * @return port speed in Mbps
1276 */
Thomas Vachuskad16ce182014-10-29 17:25:29 -07001277 private long portSpeed(OFPortDesc port) {
Yuta HIGUCHI2341e602017-03-08 20:10:08 -08001278 if (port.getVersion().getWireVersion() >= OFVersion.OF_14.getWireVersion()) {
1279 // OFPortDescPropEthernet
1280 return port.getProperties().stream()
1281 .filter(OFPortDescPropEthernet.class::isInstance)
1282 .map(OFPortDescPropEthernet.class::cast)
1283 .mapToLong(OFPortDescPropEthernet::getCurrSpeed)
1284 .map(kbps -> kbps / KBPS)
1285 .findAny()
1286 .orElse(PortSpeed.SPEED_NONE.getSpeedBps() / MBPS);
1287 }
Thomas Vachuskad16ce182014-10-29 17:25:29 -07001288 if (port.getVersion() == OFVersion.OF_13) {
Charles Chan25b77322015-12-13 01:00:56 -08001289 // Note: getCurrSpeed() returns a value in kbps (this also applies to OF_11 and OF_12)
1290 return port.getCurrSpeed() / KBPS;
Thomas Vachuskad16ce182014-10-29 17:25:29 -07001291 }
Yuta HIGUCHI2341e602017-03-08 20:10:08 -08001292 // < OF1.3
Thomas Vachuskad16ce182014-10-29 17:25:29 -07001293 PortSpeed portSpeed = PortSpeed.SPEED_NONE;
1294 for (OFPortFeatures feat : port.getCurr()) {
1295 portSpeed = PortSpeed.max(portSpeed, feat.getPortSpeed());
1296 }
Thomas Vachuska98eda532014-10-29 17:31:02 -07001297 return portSpeed.getSpeedBps() / MBPS;
Thomas Vachuskad16ce182014-10-29 17:25:29 -07001298 }
sangho538108b2015-04-08 14:29:20 -07001299
1300 @Override
1301 public void handleMessage(Dpid dpid, OFMessage msg) {
Ray Milkey7abe50b2016-04-13 11:57:44 -07001302 if (isDisabled) {
1303 return;
sangho538108b2015-04-08 14:29:20 -07001304 }
Ray Milkey7abe50b2016-04-13 11:57:44 -07001305
1306 try {
1307 switch (msg.getType()) {
1308 case STATS_REPLY:
1309 if (((OFStatsReply) msg).getStatsType() == OFStatsType.PORT) {
1310 OFPortStatsReply portStatsReply = (OFPortStatsReply) msg;
1311 List<OFPortStatsEntry> portStatsReplyList = portStatsReplies.get(dpid);
1312 if (portStatsReplyList == null) {
Madan Jampani84382b92016-06-22 08:26:49 -07001313 portStatsReplyList = Lists.newCopyOnWriteArrayList();
Ray Milkey7abe50b2016-04-13 11:57:44 -07001314 }
1315 portStatsReplyList.addAll(portStatsReply.getEntries());
1316 portStatsReplies.put(dpid, portStatsReplyList);
1317 if (!portStatsReply.getFlags().contains(OFStatsReplyFlags.REPLY_MORE)) {
Charles Chanb3ef0db2016-05-03 11:11:00 -07001318 List<OFPortStatsEntry> statsEntries = portStatsReplies.get(dpid);
1319 if (statsEntries != null) {
1320 pushPortMetrics(dpid, statsEntries);
1321 statsEntries.clear();
1322 }
Ray Milkey7abe50b2016-04-13 11:57:44 -07001323 }
yjimmyy646aa022016-07-05 12:09:50 -07001324 } else if (((OFStatsReply) msg).getStatsType() == OFStatsType.EXPERIMENTER) {
1325 OpenFlowSwitch sw = controller.getSwitch(dpid);
sangyun-han147e6f42016-07-26 17:14:41 +09001326 if (sw == null) {
1327 LOG.error("Switch {} is not found", dpid);
1328 break;
1329 }
yjimmyy646aa022016-07-05 12:09:50 -07001330 if (sw instanceof OpenFlowOpticalSwitch) {
1331 // Optical switch uses experimenter stats message to update power
1332 List<PortDescription> portDescs =
1333 ((OpenFlowOpticalSwitch) sw).processExpPortStats(msg);
1334 if (!portDescs.isEmpty()) {
1335 providerService.updatePorts(DeviceId.deviceId(Dpid.uri(dpid)), portDescs);
1336 }
1337 }
Ray Milkey7abe50b2016-04-13 11:57:44 -07001338 }
1339 break;
1340 case ERROR:
1341 if (((OFErrorMsg) msg).getErrType() == OFErrorType.PORT_MOD_FAILED) {
1342 LOG.error("port mod failed");
1343 }
1344 default:
1345 break;
1346 }
1347 } catch (IllegalStateException e) {
1348 // system is shutting down and the providerService is no longer
1349 // valid. Messages cannot be processed.
1350 }
1351 }
1352
1353 private void disable() {
1354 isDisabled = true;
sangho538108b2015-04-08 14:29:20 -07001355 }
alshabibf1216ed2014-09-03 11:53:54 -07001356 }
Saurav Dasa2d37502016-03-25 17:50:40 -07001357
1358
tomb5a46e62014-08-26 14:20:00 -07001359}