blob: 3e88dcf34b92f5db7f410eac7af62cfbba1b6b8c [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2014-present Open Networking Foundation
Thomas Vachuska781d18b2014-10-27 10:31:25 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
Thomas Vachuska781d18b2014-10-27 10:31:25 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
Thomas Vachuska781d18b2014-10-27 10:31:25 -070015 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.provider.of.device.impl;
tomb5a46e62014-08-26 14:20:00 -070017
Marc De Leenheer8aba62f2017-04-25 14:33:37 -070018import com.google.common.base.Strings;
19import com.google.common.collect.ImmutableList;
20import com.google.common.collect.Lists;
21import com.google.common.collect.Maps;
22import com.google.common.collect.Sets;
tomb5a46e62014-08-26 14:20:00 -070023import org.apache.felix.scr.annotations.Activate;
24import org.apache.felix.scr.annotations.Component;
25import org.apache.felix.scr.annotations.Deactivate;
Dusan Pajinbab8a5e2015-07-24 17:37:19 +020026import org.apache.felix.scr.annotations.Modified;
27import org.apache.felix.scr.annotations.Property;
tomb5a46e62014-08-26 14:20:00 -070028import org.apache.felix.scr.annotations.Reference;
29import org.apache.felix.scr.annotations.ReferenceCardinality;
Thomas Vachuskaf0397b52015-05-29 13:50:17 -070030import org.onlab.packet.ChassisId;
Marc De Leenheerb9311372015-07-09 11:36:49 -070031import org.onlab.util.Frequency;
32import org.onlab.util.Spectrum;
Yafit Hadara9a73de2015-09-06 13:52:52 +030033import org.onosproject.cfg.ComponentConfigService;
Thomas Vachuskab52a0142015-04-21 17:48:15 -070034import org.onosproject.net.AnnotationKeys;
Marc De Leenheerfc913dd2015-07-30 16:04:55 -070035import org.onosproject.net.ChannelSpacing;
Toru Furusawa72ee30c2016-01-08 13:29:04 -080036import org.onosproject.net.CltSignalType;
Brian O'Connorabafb502014-12-02 22:26:20 -080037import org.onosproject.net.DefaultAnnotations;
Yuta HIGUCHI2341e602017-03-08 20:10:08 -080038import org.onosproject.net.DefaultAnnotations.Builder;
Yafit Hadara9a73de2015-09-06 13:52:52 +030039import org.onosproject.net.Device;
Brian O'Connorabafb502014-12-02 22:26:20 -080040import org.onosproject.net.DeviceId;
Marc De Leenheerfc913dd2015-07-30 16:04:55 -070041import org.onosproject.net.GridType;
Brian O'Connorabafb502014-12-02 22:26:20 -080042import org.onosproject.net.MastershipRole;
Marc De Leenheerfc913dd2015-07-30 16:04:55 -070043import org.onosproject.net.OchSignal;
44import org.onosproject.net.OduSignalType;
Rimon Ashkenazy8ebfff02016-02-01 11:56:36 +020045import org.onosproject.net.OtuSignalType;
Brian O'Connorabafb502014-12-02 22:26:20 -080046import org.onosproject.net.Port;
Yuta HIGUCHIf42a2cc2017-05-02 17:04:31 -070047import org.onosproject.net.Port.Type;
Brian O'Connorabafb502014-12-02 22:26:20 -080048import org.onosproject.net.PortNumber;
49import org.onosproject.net.SparseAnnotations;
Jimmy Yan4deb03b2016-06-24 10:53:54 -070050import org.onosproject.net.behaviour.LambdaQuery;
Brian O'Connorabafb502014-12-02 22:26:20 -080051import org.onosproject.net.device.DefaultDeviceDescription;
52import org.onosproject.net.device.DefaultPortDescription;
sangho538108b2015-04-08 14:29:20 -070053import org.onosproject.net.device.DefaultPortStatistics;
Brian O'Connorabafb502014-12-02 22:26:20 -080054import org.onosproject.net.device.DeviceDescription;
55import org.onosproject.net.device.DeviceProvider;
56import org.onosproject.net.device.DeviceProviderRegistry;
57import org.onosproject.net.device.DeviceProviderService;
58import org.onosproject.net.device.PortDescription;
sangho538108b2015-04-08 14:29:20 -070059import org.onosproject.net.device.PortStatistics;
Victor Silvaac8dab02016-11-28 16:33:08 -030060import org.onosproject.net.driver.Driver;
Jimmy Yan4deb03b2016-06-24 10:53:54 -070061import org.onosproject.net.driver.DriverHandler;
Victor Silvaac8dab02016-11-28 16:33:08 -030062import org.onosproject.net.driver.DriverService;
Jimmy Yan4deb03b2016-06-24 10:53:54 -070063import org.onosproject.net.driver.HandlerBehaviour;
Brian O'Connorabafb502014-12-02 22:26:20 -080064import org.onosproject.net.provider.AbstractProvider;
65import org.onosproject.net.provider.ProviderId;
66import org.onosproject.openflow.controller.Dpid;
67import org.onosproject.openflow.controller.OpenFlowController;
sangho538108b2015-04-08 14:29:20 -070068import org.onosproject.openflow.controller.OpenFlowEventListener;
Ayaka Koshibe5460d622015-05-14 12:19:19 -070069import org.onosproject.openflow.controller.OpenFlowOpticalSwitch;
Brian O'Connorabafb502014-12-02 22:26:20 -080070import org.onosproject.openflow.controller.OpenFlowSwitch;
71import org.onosproject.openflow.controller.OpenFlowSwitchListener;
Ayaka Koshibe5460d622015-05-14 12:19:19 -070072import org.onosproject.openflow.controller.PortDescPropertyType;
Brian O'Connorabafb502014-12-02 22:26:20 -080073import org.onosproject.openflow.controller.RoleState;
Dusan Pajinbab8a5e2015-07-24 17:37:19 +020074import org.osgi.service.component.ComponentContext;
Marc De Leenheerc662d322016-02-18 16:05:10 -080075import org.projectfloodlight.openflow.protocol.OFCalientPortDescProp;
76import org.projectfloodlight.openflow.protocol.OFCalientPortDescPropOptical;
Marc De Leenheerb9311372015-07-09 11:36:49 -070077import org.projectfloodlight.openflow.protocol.OFCalientPortDescStatsEntry;
Laszlo Papp58174812018-01-16 13:29:47 +000078import org.projectfloodlight.openflow.protocol.OFCapabilities;
Saurav Dasa2d37502016-03-25 17:50:40 -070079import org.projectfloodlight.openflow.protocol.OFErrorMsg;
80import org.projectfloodlight.openflow.protocol.OFErrorType;
Yafit Hadara9a73de2015-09-06 13:52:52 +030081import org.projectfloodlight.openflow.protocol.OFExpPort;
82import org.projectfloodlight.openflow.protocol.OFExpPortDescPropOpticalTransport;
83import org.projectfloodlight.openflow.protocol.OFExpPortOpticalTransportLayerEntry;
Ayaka Koshibee8708e32014-10-22 13:40:18 -070084import org.projectfloodlight.openflow.protocol.OFFactory;
sangho538108b2015-04-08 14:29:20 -070085import org.projectfloodlight.openflow.protocol.OFMessage;
Yafit Hadara9a73de2015-09-06 13:52:52 +030086import org.projectfloodlight.openflow.protocol.OFObject;
alshabib4680bb62014-09-04 17:15:08 -070087import org.projectfloodlight.openflow.protocol.OFPortConfig;
alshabib25c8eec2014-09-04 16:41:31 -070088import org.projectfloodlight.openflow.protocol.OFPortDesc;
Laszlo Papp8e8ff052018-01-16 09:55:45 +000089import org.projectfloodlight.openflow.protocol.OFPortDescProp;
Yuta HIGUCHI2341e602017-03-08 20:10:08 -080090import org.projectfloodlight.openflow.protocol.OFPortDescPropEthernet;
91import org.projectfloodlight.openflow.protocol.OFPortDescPropOptical;
Marc De Leenheerfc913dd2015-07-30 16:04:55 -070092import org.projectfloodlight.openflow.protocol.OFPortDescPropOpticalTransport;
Thomas Vachuskad16ce182014-10-29 17:25:29 -070093import org.projectfloodlight.openflow.protocol.OFPortFeatures;
Saurav Dasa2d37502016-03-25 17:50:40 -070094import org.projectfloodlight.openflow.protocol.OFPortMod;
Ayaka Koshibe5460d622015-05-14 12:19:19 -070095import org.projectfloodlight.openflow.protocol.OFPortOptical;
Yafit Hadara9a73de2015-09-06 13:52:52 +030096import org.projectfloodlight.openflow.protocol.OFPortOpticalTransportLayerClass;
97import org.projectfloodlight.openflow.protocol.OFPortOpticalTransportSignalType;
alshabibafc514a2014-12-01 14:44:05 -080098import org.projectfloodlight.openflow.protocol.OFPortReason;
alshabib4680bb62014-09-04 17:15:08 -070099import org.projectfloodlight.openflow.protocol.OFPortState;
sangho538108b2015-04-08 14:29:20 -0700100import org.projectfloodlight.openflow.protocol.OFPortStatsEntry;
Laszlo Papp7cf60372018-01-11 00:06:43 +0000101import org.projectfloodlight.openflow.protocol.OFPortStatsPropOptical;
sangho538108b2015-04-08 14:29:20 -0700102import org.projectfloodlight.openflow.protocol.OFPortStatsReply;
alshabiba14f3642014-09-05 09:31:31 -0700103import org.projectfloodlight.openflow.protocol.OFPortStatus;
sangho538108b2015-04-08 14:29:20 -0700104import org.projectfloodlight.openflow.protocol.OFStatsReply;
Thomas Vachuska893bf4b2015-05-29 18:13:18 -0700105import org.projectfloodlight.openflow.protocol.OFStatsReplyFlags;
sangho538108b2015-04-08 14:29:20 -0700106import org.projectfloodlight.openflow.protocol.OFStatsType;
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700107import org.projectfloodlight.openflow.protocol.OFVersion;
Yuta HIGUCHIf42a2cc2017-05-02 17:04:31 -0700108import org.projectfloodlight.openflow.protocol.ver14.OFOpticalPortFeaturesSerializerVer14;
Laszlo Papp7cf60372018-01-11 00:06:43 +0000109import org.projectfloodlight.openflow.protocol.ver14.OFPortStatsOpticalFlagsSerializerVer14;
Saurav Dasa2d37502016-03-25 17:50:40 -0700110import org.projectfloodlight.openflow.types.OFPort;
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700111import org.projectfloodlight.openflow.types.PortSpeed;
tomb5a46e62014-08-26 14:20:00 -0700112import org.slf4j.Logger;
tom5f38b3a2014-08-27 23:50:54 -0700113
Marc De Leenheer8aba62f2017-04-25 14:33:37 -0700114import java.util.ArrayList;
115import java.util.Collection;
116import java.util.Collections;
117import java.util.Comparator;
118import java.util.Dictionary;
Jimmy Jine9b7a022016-08-12 16:56:48 -0700119import java.util.EnumSet;
Marc De Leenheer8aba62f2017-04-25 14:33:37 -0700120import java.util.HashMap;
121import java.util.HashSet;
122import java.util.List;
Yuta HIGUCHI2341e602017-03-08 20:10:08 -0800123import java.util.Optional;
Marc De Leenheer8aba62f2017-04-25 14:33:37 -0700124import java.util.Properties;
125import java.util.Set;
126import java.util.Timer;
127
128import static com.google.common.base.Preconditions.checkArgument;
129import static com.google.common.base.Strings.isNullOrEmpty;
130import static org.onlab.util.Tools.get;
131import static org.onosproject.net.DeviceId.deviceId;
132import static org.onosproject.net.Port.Type.COPPER;
133import static org.onosproject.net.Port.Type.FIBER;
134import static org.onosproject.net.optical.device.OchPortHelper.ochPortDescription;
135import static org.onosproject.net.optical.device.OduCltPortHelper.oduCltPortDescription;
136import static org.onosproject.net.optical.device.OmsPortHelper.omsPortDescription;
137import static org.onosproject.net.optical.device.OtuPortHelper.otuPortDescription;
138import static org.onosproject.openflow.controller.Dpid.dpid;
139import static org.onosproject.openflow.controller.Dpid.uri;
140import static org.slf4j.LoggerFactory.getLogger;
tom782a7cf2014-09-11 23:58:38 -0700141
tomb5a46e62014-08-26 14:20:00 -0700142/**
tomb1260e42014-08-26 18:39:57 -0700143 * Provider which uses an OpenFlow controller to detect network
tome06f8552014-08-26 16:58:42 -0700144 * infrastructure devices.
tomb5a46e62014-08-26 14:20:00 -0700145 */
tomb1260e42014-08-26 18:39:57 -0700146@Component(immediate = true)
tomab21e7c2014-08-26 15:23:08 -0700147public class OpenFlowDeviceProvider extends AbstractProvider implements DeviceProvider {
tomb5a46e62014-08-26 14:20:00 -0700148
alshabiba89cc582014-09-09 16:43:00 -0700149 private static final Logger LOG = getLogger(OpenFlowDeviceProvider.class);
Thomas Vachuskad07c0922015-10-06 14:48:06 -0700150
Yuta HIGUCHI2341e602017-03-08 20:10:08 -0800151 // TODO Some duplicate with one defined in OpticalAnnotations
152 // slice out optical specific handling and consolidate.
153 /**
154 * Annotation key for minimum frequency in Hz.
155 * Value is expected to be an integer.
156 */
157 public static final String AK_MIN_FREQ_HZ = "minFrequency";
158
159 /**
160 * Annotation key for maximum frequency in Hz.
161 * Value is expected be an integer.
162 */
163 public static final String AK_MAX_FREQ_HZ = "maxFrequency";
164
165 /**
166 * Annotation key for grid in Hz.
167 * Value is expected to be an integer.
168 */
169 public static final String AK_GRID_HZ = "grid";
170
Laszlo Papp20cfb6f2018-01-08 16:06:53 +0000171 /**
Yuta HIGUCHI2341e602017-03-08 20:10:08 -0800172 * Annotation key for minimum frequency in Hz.
173 * Value is expected to be an integer.
174 */
175 public static final String AK_TX_MIN_FREQ_HZ = "txMinFrequency";
176
177 /**
178 * Annotation key for maximum frequency in Hz.
179 * Value is expected be an integer.
180 */
181 public static final String AK_TX_MAX_FREQ_HZ = "txMaxFrequency";
182
183 /**
184 * Annotation key for grid in Hz.
185 * Value is expected to be an integer.
186 */
187 public static final String AK_TX_GRID_HZ = "txGrid";
188
189 /**
190 * Annotation key for minimum frequency in Hz.
191 * Value is expected to be an integer.
192 */
193 public static final String AK_RX_MIN_FREQ_HZ = "rxMinFrequency";
194
195 /**
196 * Annotation key for maximum frequency in Hz.
197 * Value is expected be an integer.
198 */
199 public static final String AK_RX_MAX_FREQ_HZ = "rxMaxFrequency";
200
201 /**
202 * Annotation key for grid in Hz.
203 * Value is expected to be an integer.
204 */
205 public static final String AK_RX_GRID_HZ = "rxGrid";
206
Laszlo Papp20cfb6f2018-01-08 16:06:53 +0000207 /**
Laszlo Papp8e8ff052018-01-16 09:55:45 +0000208 * Annotation key for indicating frequency must be used instead of
209 * wavelength for port tuning.
210 * Value is expected to be "enabled" or "disabled"
211 */
212 public static final String AK_USE_FREQ_FEATURE = "useFreqFeature";
213
214 /**
Laszlo Papp20cfb6f2018-01-08 16:06:53 +0000215 * Annotation key for minimum transmit power in dBm*10.
216 * Value is expected to be an integer.
217 */
218 public static final String AK_TX_PWR_MIN = "txPowerMin";
219
220 /**
221 * Annotation key for maximum transmit power in dBm*10.
222 * Value is expected to be an integer.
223 */
224 public static final String AK_TX_PWR_MAX = "txPowerMax";
225
Laszlo Papp7cf60372018-01-11 00:06:43 +0000226
227
228 // Port Stats annotations
229
230 /**
231 * Annotation key for transmit frequency in Hz.
232 * Value is expected be an integer.
233 */
234 public static final String AK_TX_FREQ_HZ = "txFrequency";
235
236 /**
237 * Annotation key for transmit offset in Hz.
238 * Value is expected be an integer.
239 */
240 public static final String AK_TX_OFFSET_HZ = "txOffset";
241
242 /**
243 * Annotation key for transmit grid spacing in Hz.
244 * Value is expected be an integer.
245 */
246 public static final String AK_TX_GRID_SPAN_HZ = "txGridSpan";
247
248 /**
249 * Annotation key for receive frequency in Hz.
250 * Value is expected be an integer.
251 */
252 public static final String AK_RX_FREQ_HZ = "rxFrequency";
253
254 /**
255 * Annotation key for receive offset in Hz.
256 * Value is expected be an integer.
257 */
258 public static final String AK_RX_OFFSET_HZ = "rxOffset";
259
260 /**
261 * Annotation key for receive grid spacing in Hz.
262 * Value is expected be an integer.
263 */
264 public static final String AK_RX_GRID_SPAN_HZ = "rxGridSpan";
265
266 /**
267 * Annotation key for transmit power in dBm*10.
268 * Value is expected to be an integer.
269 */
270 public static final String AK_TX_PWR = "txPower";
271
272 /**
273 * Annotation key for receive power feature.
274 * Value is expected to be "enabled" or "disabled"
275 */
276 public static final String AK_RX_PWR_FEATURE = "rxPwrFeature";
277
278 /**
279 * Annotation key for receive power in dBm*10.
280 * Value is expected to be an integer.
281 */
282 public static final String AK_RX_PWR = "rxPower";
283
284 /**
285 * Annotation key for transmit bias feature.
286 * Value is expected to be "enabled" or "disabled"
287 */
288 public static final String AK_TX_BIAS_FEATURE = "txBiasFeature";
289
290 /**
291 * Annotation key for transmit bias current in mA*10.
292 * Value is expected to be an integer.
293 */
294 public static final String AK_BIAS_CURRENT = "biasCurrent";
295
296 /**
297 * Annotation key for transmit temperature feature.
298 * Value is expected to be "enabled" or "disabled"
299 */
300 public static final String AK_TX_TEMP_FEATURE = "txTempFeature";
301
302 /**
303 * Annotation key for transmit laser temperature in C*10.
304 * Value is expected to be an integer.
305 */
306 public static final String AK_TEMPERATURE = "temperature";
307
308
309 // Common feature annotations
310
311 /**
312 * Annotation key for transmit tune feature.
313 * Value is expected to be "enabled" or "disabled"
314 */
315 public static final String AK_TX_TUNE_FEATURE = "txTuneFeature";
316
317 /**
318 * Annotation key for receive tune feature.
319 * Value is expected to be "enabled" or "disabled"
320 */
321 public static final String AK_RX_TUNE_FEATURE = "rxTuneFeature";
322
323 /**
324 * Annotation key for transmit power feature.
325 * Value is expected to be "enabled" or "disabled"
326 */
327 public static final String AK_TX_PWR_FEATURE = "txPwrFeature";
328
329
Charles Chan25b77322015-12-13 01:00:56 -0800330 //TODO consider renaming KBPS and MBPS (as they are used to convert by division)
331 private static final long KBPS = 1_000;
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700332 private static final long MBPS = 1_000 * 1_000;
Rimon Ashkenazye3201032016-01-11 14:27:30 +0200333 private static final Frequency FREQ50 = Frequency.ofGHz(50);
334 private static final Frequency FREQ191_7 = Frequency.ofGHz(191_700);
Sho SHIMIZU00762ee2016-01-05 16:32:24 -0800335 private static final Frequency FREQ4_4 = Frequency.ofGHz(4_400);
tomb5a46e62014-08-26 14:20:00 -0700336
337 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
tom96dfcab2014-08-28 09:26:03 -0700338 protected DeviceProviderRegistry providerRegistry;
tomab21e7c2014-08-26 15:23:08 -0700339
tom5f38b3a2014-08-27 23:50:54 -0700340 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
341 protected OpenFlowController controller;
342
Dusan Pajinbab8a5e2015-07-24 17:37:19 +0200343 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
344 protected ComponentConfigService cfgService;
345
Victor Silvaac8dab02016-11-28 16:33:08 -0300346 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
347 protected DriverService driverService;
348
tomab21e7c2014-08-26 15:23:08 -0700349 private DeviceProviderService providerService;
tomb5a46e62014-08-26 14:20:00 -0700350
sangho538108b2015-04-08 14:29:20 -0700351 private final InternalDeviceProvider listener = new InternalDeviceProvider();
352
Simon Hunt7a307652016-06-23 01:17:29 -0700353 private static final String POLL_PROP_NAME = "portStatsPollFrequency";
354 private static final int POLL_INTERVAL = 5;
355 @Property(name = POLL_PROP_NAME, intValue = POLL_INTERVAL,
Dusan Pajinbab8a5e2015-07-24 17:37:19 +0200356 label = "Frequency (in seconds) for polling switch Port statistics")
357 private int portStatsPollFrequency = POLL_INTERVAL;
sangho538108b2015-04-08 14:29:20 -0700358
Victor Silvaff5871b2016-10-04 18:08:47 -0300359 private final Timer timer = new Timer("onos-openflow-portstats-collector");
sangyun-han07a572f2016-07-29 21:14:27 +0900360
sangho538108b2015-04-08 14:29:20 -0700361 private HashMap<Dpid, PortStatsCollector> collectors = Maps.newHashMap();
tomd40fc7a2014-09-04 16:41:10 -0700362
tomab21e7c2014-08-26 15:23:08 -0700363 /**
364 * Creates an OpenFlow device provider.
365 */
366 public OpenFlowDeviceProvider() {
Brian O'Connorabafb502014-12-02 22:26:20 -0800367 super(new ProviderId("of", "org.onosproject.provider.openflow"));
tomab21e7c2014-08-26 15:23:08 -0700368 }
369
tomb5a46e62014-08-26 14:20:00 -0700370 @Activate
Dusan Pajinbab8a5e2015-07-24 17:37:19 +0200371 public void activate(ComponentContext context) {
372 cfgService.registerProperties(getClass());
tom96dfcab2014-08-28 09:26:03 -0700373 providerService = providerRegistry.register(this);
tomd40fc7a2014-09-04 16:41:10 -0700374 controller.addListener(listener);
sangho538108b2015-04-08 14:29:20 -0700375 controller.addEventListener(listener);
Jian Li152b8852015-12-07 14:47:25 -0800376
Victor Silva7944d142016-07-08 00:14:55 -0300377 modified(context);
378
Thomas Vachuskad07c0922015-10-06 14:48:06 -0700379 connectInitialDevices();
alshabiba89cc582014-09-09 16:43:00 -0700380 LOG.info("Started");
tomb5a46e62014-08-26 14:20:00 -0700381 }
382
383 @Deactivate
Dusan Pajinbab8a5e2015-07-24 17:37:19 +0200384 public void deactivate(ComponentContext context) {
385 cfgService.unregisterProperties(getClass(), false);
Ray Milkey7abe50b2016-04-13 11:57:44 -0700386 listener.disable();
tomd40fc7a2014-09-04 16:41:10 -0700387 controller.removeListener(listener);
Thomas Vachuskad07c0922015-10-06 14:48:06 -0700388 providerRegistry.unregister(this);
Thomas Vachuska74bcd922015-06-02 13:08:57 -0700389 collectors.values().forEach(PortStatsCollector::stop);
Thomas Vachuskaa394b952016-06-14 15:02:09 -0700390 collectors.clear();
tomab21e7c2014-08-26 15:23:08 -0700391 providerService = null;
alshabiba89cc582014-09-09 16:43:00 -0700392 LOG.info("Stopped");
tomb5a46e62014-08-26 14:20:00 -0700393 }
394
Dusan Pajinbab8a5e2015-07-24 17:37:19 +0200395 @Modified
396 public void modified(ComponentContext context) {
Victor Silva7944d142016-07-08 00:14:55 -0300397 Dictionary<?, ?> properties = context != null ? context.getProperties() : new Properties();
Dusan Pajinbab8a5e2015-07-24 17:37:19 +0200398 int newPortStatsPollFrequency;
399 try {
Simon Hunt7a307652016-06-23 01:17:29 -0700400 String s = get(properties, POLL_PROP_NAME);
Dusan Pajinbab8a5e2015-07-24 17:37:19 +0200401 newPortStatsPollFrequency = isNullOrEmpty(s) ? portStatsPollFrequency : Integer.parseInt(s.trim());
402
403 } catch (NumberFormatException | ClassCastException e) {
404 newPortStatsPollFrequency = portStatsPollFrequency;
405 }
406
407 if (newPortStatsPollFrequency != portStatsPollFrequency) {
408 portStatsPollFrequency = newPortStatsPollFrequency;
409 collectors.values().forEach(psc -> psc.adjustPollInterval(portStatsPollFrequency));
410 }
411
412 LOG.info("Settings: portStatsPollFrequency={}", portStatsPollFrequency);
413 }
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700414
Thomas Vachuskad07c0922015-10-06 14:48:06 -0700415 private void connectInitialDevices() {
416 for (OpenFlowSwitch sw : controller.getSwitches()) {
417 try {
418 listener.switchAdded(new Dpid(sw.getId()));
419 } catch (Exception e) {
420 LOG.warn("Failed initially adding {} : {}", sw.getStringId(), e.getMessage());
421 LOG.debug("Error details:", e);
422 // disconnect to trigger switch-add later
423 sw.disconnectSwitch();
424 }
Thomas Vachuskad07c0922015-10-06 14:48:06 -0700425 }
426 }
427
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700428 @Override
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700429 public boolean isReachable(DeviceId deviceId) {
430 OpenFlowSwitch sw = controller.getSwitch(dpid(deviceId.uri()));
Thomas Vachuskad07c0922015-10-06 14:48:06 -0700431 return sw != null && sw.isConnected();
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700432 }
433
tomab21e7c2014-08-26 15:23:08 -0700434 @Override
Ayaka Koshibe78bcbc12014-11-19 14:28:58 -0800435 public void triggerProbe(DeviceId deviceId) {
Madan Jampanic3328762015-05-31 12:39:27 -0700436 LOG.debug("Triggering probe on device {}", deviceId);
Ayaka Koshibee8708e32014-10-22 13:40:18 -0700437
Yuta HIGUCHI802d0e82014-10-31 16:07:37 -0700438 final Dpid dpid = dpid(deviceId.uri());
439 OpenFlowSwitch sw = controller.getSwitch(dpid);
440 if (sw == null || !sw.isConnected()) {
Ayaka Koshibe78bcbc12014-11-19 14:28:58 -0800441 LOG.error("Failed to probe device {} on sw={}", deviceId, sw);
Yuta HIGUCHI802d0e82014-10-31 16:07:37 -0700442 providerService.deviceDisconnected(deviceId);
Thomas Vachuska74bcd922015-06-02 13:08:57 -0700443 return;
Yuta HIGUCHI802d0e82014-10-31 16:07:37 -0700444 } else {
Ayaka Koshibe78bcbc12014-11-19 14:28:58 -0800445 LOG.trace("Confirmed device {} connection", deviceId);
Yuta HIGUCHI802d0e82014-10-31 16:07:37 -0700446 }
Ayaka Koshibee8708e32014-10-22 13:40:18 -0700447
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700448 // Prompt an update of port information. We can use any XID for this.
Ayaka Koshibee8708e32014-10-22 13:40:18 -0700449 OFFactory fact = sw.factory();
450 switch (fact.getVersion()) {
451 case OF_10:
452 sw.sendMsg(fact.buildFeaturesRequest().setXid(0).build());
453 break;
454 case OF_13:
Yuta HIGUCHI6512f3e2017-05-18 17:21:24 -0700455 case OF_14:
456 case OF_15:
Ayaka Koshibee8708e32014-10-22 13:40:18 -0700457 sw.sendMsg(fact.buildPortDescStatsRequest().setXid(0).build());
458 break;
459 default:
460 LOG.warn("Unhandled protocol version");
461 }
tomab21e7c2014-08-26 15:23:08 -0700462 }
463
464 @Override
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700465 public void roleChanged(DeviceId deviceId, MastershipRole newRole) {
alshabibf1216ed2014-09-03 11:53:54 -0700466 switch (newRole) {
tom782a7cf2014-09-11 23:58:38 -0700467 case MASTER:
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700468 controller.setRole(dpid(deviceId.uri()), RoleState.MASTER);
tom782a7cf2014-09-11 23:58:38 -0700469 break;
470 case STANDBY:
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700471 controller.setRole(dpid(deviceId.uri()), RoleState.EQUAL);
tom782a7cf2014-09-11 23:58:38 -0700472 break;
473 case NONE:
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700474 controller.setRole(dpid(deviceId.uri()), RoleState.SLAVE);
tom782a7cf2014-09-11 23:58:38 -0700475 break;
476 default:
477 LOG.error("Unknown Mastership state : {}", newRole);
alshabibf1216ed2014-09-03 11:53:54 -0700478
479 }
HIGUCHI Yuta1979f552015-12-28 21:24:26 -0800480 LOG.debug("Accepting mastership role change to {} for device {}", newRole, deviceId);
tomab21e7c2014-08-26 15:23:08 -0700481 }
482
Saurav Dasa2d37502016-03-25 17:50:40 -0700483 @Override
484 public void changePortState(DeviceId deviceId, PortNumber portNumber,
485 boolean enable) {
486 final Dpid dpid = dpid(deviceId.uri());
487 OpenFlowSwitch sw = controller.getSwitch(dpid);
488 if (sw == null || !sw.isConnected()) {
489 LOG.error("Failed to change portState on device {}", deviceId);
490 return;
491 }
492 OFPortMod.Builder pmb = sw.factory().buildPortMod();
493 OFPort port = OFPort.of((int) portNumber.toLong());
494 pmb.setPortNo(port);
Jimmy Jine9b7a022016-08-12 16:56:48 -0700495 Set<OFPortConfig> portConfig = EnumSet.noneOf(OFPortConfig.class);
496 if (!enable) {
497 portConfig.add(OFPortConfig.PORT_DOWN);
Saurav Dasa2d37502016-03-25 17:50:40 -0700498 }
Jimmy Jine9b7a022016-08-12 16:56:48 -0700499 pmb.setConfig(portConfig);
500 Set<OFPortConfig> portMask = EnumSet.noneOf(OFPortConfig.class);
501 portMask.add(OFPortConfig.PORT_DOWN);
502 pmb.setMask(portMask);
Saurav Dasa2d37502016-03-25 17:50:40 -0700503 pmb.setAdvertise(0x0);
504 for (OFPortDesc pd : sw.getPorts()) {
505 if (pd.getPortNo().equals(port)) {
506 pmb.setHwAddr(pd.getHwAddr());
507 break;
508 }
509 }
510 sw.sendMsg(Collections.singletonList(pmb.build()));
511 }
512
Thomas Vachuska893bf4b2015-05-29 18:13:18 -0700513 private void pushPortMetrics(Dpid dpid, List<OFPortStatsEntry> portStatsEntries) {
HIGUCHI Yuta1979f552015-12-28 21:24:26 -0800514 DeviceId deviceId = DeviceId.deviceId(Dpid.uri(dpid));
Ray Milkey3c4124a2016-08-31 12:09:06 -0700515 Collection<PortStatistics> stats =
516 buildPortStatistics(deviceId, ImmutableList.copyOf(portStatsEntries));
sangho538108b2015-04-08 14:29:20 -0700517 providerService.updatePortStatistics(deviceId, stats);
518 }
519
Laszlo Papp8e8ff052018-01-16 09:55:45 +0000520 private static String lambdaToAnnotationHz(long lambda) {
521 // ref. OF1.5: wavelength (lambda) as nm * 100
522
523 long c = 299792458; // speed of light in m/s
524 // f = c / λ
525 // (m/s) * (nm/m) / (nm * 100) * 100
526 // annotations is in Hz
527 return Long.toString(lambda == 0 ? lambda : (c * 1_000_000_000 / lambda * 100));
528 }
Laszlo Papp7cf60372018-01-11 00:06:43 +0000529
530 private static String mhzToAnnotation(long freqMhz) {
531 return Long.toString(Frequency.ofMHz(freqMhz).asHz());
532 }
533
Laszlo Papp8e8ff052018-01-16 09:55:45 +0000534 private static String freqLmdaToAnnotation(long freqLmda, boolean useFreq) {
535 return useFreq ? mhzToAnnotation(freqLmda) : lambdaToAnnotationHz(freqLmda);
536 }
537
Thomas Vachuska893bf4b2015-05-29 18:13:18 -0700538 private Collection<PortStatistics> buildPortStatistics(DeviceId deviceId,
539 List<OFPortStatsEntry> entries) {
sangho538108b2015-04-08 14:29:20 -0700540 HashSet<PortStatistics> stats = Sets.newHashSet();
Laszlo Papp8e8ff052018-01-16 09:55:45 +0000541 final Dpid dpid = dpid(deviceId.uri());
542 OpenFlowSwitch sw = controller.getSwitch(dpid);
sangho538108b2015-04-08 14:29:20 -0700543
Thomas Vachuska893bf4b2015-05-29 18:13:18 -0700544 for (OFPortStatsEntry entry : entries) {
Thomas Vachuskaf0397b52015-05-29 13:50:17 -0700545 try {
Thomas Vachuska4e6025e2015-06-03 11:25:21 -0700546 if (entry == null || entry.getPortNo() == null || entry.getPortNo().getPortNumber() < 0) {
Thomas Vachuskaf0397b52015-05-29 13:50:17 -0700547 continue;
548 }
Laszlo Papp7cf60372018-01-11 00:06:43 +0000549 DefaultAnnotations.Builder annotations = DefaultAnnotations.builder();
Laszlo Papp9dfb5122018-01-12 21:10:13 +0000550 boolean propSupported = entry.getVersion().getWireVersion() >= OFVersion.OF_14.getWireVersion();
551 Optional<OFPortStatsPropOptical> optical = propSupported ?
552 entry.getProperties().stream()
Laszlo Papp7cf60372018-01-11 00:06:43 +0000553 .filter(OFPortStatsPropOptical.class::isInstance)
554 .map(OFPortStatsPropOptical.class::cast)
Laszlo Papp9dfb5122018-01-12 21:10:13 +0000555 .findAny() : Optional.empty();
Laszlo Papp7cf60372018-01-11 00:06:43 +0000556 if (optical.isPresent()) {
557 long flags = optical.get().getFlags();
558
Laszlo Papp8e8ff052018-01-16 09:55:45 +0000559 boolean useFreq = false;
560 for (OFPortDesc pd : sw.getPorts()) {
561 if (pd.getPortNo().equals(entry.getPortNo())) {
562 for (OFPortDescProp prop : pd.getProperties()) {
563 if (prop instanceof OFPortDescPropOptical) {
564 OFPortDescPropOptical oprop = (OFPortDescPropOptical) prop;
565 long supported = oprop.getSupported();
566 int useFreqVal = OFOpticalPortFeaturesSerializerVer14.USE_FREQ_VAL;
567 if ((supported & useFreqVal) != 0) {
568 useFreq = true;
569 break;
570 }
571 }
572 }
573 }
574 }
575
Laszlo Papp7cf60372018-01-11 00:06:43 +0000576 int txTune = OFPortStatsOpticalFlagsSerializerVer14.TX_TUNE_VAL;
577 long txFreq = optical.get().getTxFreqLmda();
578 long txOffset = optical.get().getTxOffset();
579 long txGridSpan = optical.get().getTxGridSpan();
580 annotations.set(AK_TX_TUNE_FEATURE, ((flags & txTune) != 0) ? "enabled" : "disabled");
Laszlo Papp8e8ff052018-01-16 09:55:45 +0000581 annotations.set(AK_TX_FREQ_HZ, freqLmdaToAnnotation(txFreq, useFreq));
582 annotations.set(AK_TX_OFFSET_HZ, freqLmdaToAnnotation(txOffset, useFreq));
583 annotations.set(AK_TX_GRID_SPAN_HZ, freqLmdaToAnnotation(txGridSpan, useFreq));
Laszlo Papp7cf60372018-01-11 00:06:43 +0000584
585 int rxTune = OFPortStatsOpticalFlagsSerializerVer14.RX_TUNE_VAL;
586 long rxFreq = optical.get().getRxFreqLmda();
587 long rxOffset = optical.get().getRxOffset();
588 long rxGridSpan = optical.get().getRxGridSpan();
589 annotations.set(AK_RX_TUNE_FEATURE, ((flags & rxTune) != 0) ? "enabled" : "disabled");
Laszlo Papp8e8ff052018-01-16 09:55:45 +0000590 annotations.set(AK_RX_FREQ_HZ, freqLmdaToAnnotation(rxFreq, useFreq));
591 annotations.set(AK_RX_OFFSET_HZ, freqLmdaToAnnotation(rxOffset, useFreq));
592 annotations.set(AK_RX_GRID_SPAN_HZ, freqLmdaToAnnotation(rxGridSpan, useFreq));
Laszlo Papp7cf60372018-01-11 00:06:43 +0000593
594 int txPwrVal = OFPortStatsOpticalFlagsSerializerVer14.TX_PWR_VAL;
595 int txPwr = optical.get().getTxPwr();
596 annotations.set(AK_TX_PWR_FEATURE, ((flags & txPwrVal) != 0) ? "enabled" : "disabled");
597 annotations.set(AK_TX_PWR, Integer.toString(txPwr));
598
599 int rxPwrVal = OFPortStatsOpticalFlagsSerializerVer14.RX_PWR_VAL;
600 int rxPwr = optical.get().getRxPwr();
601 annotations.set(AK_RX_PWR_FEATURE, ((flags & rxPwrVal) != 0) ? "enabled" : "disabled");
602 annotations.set(AK_RX_PWR, Integer.toString(rxPwr));
603
604 int txBias = OFPortStatsOpticalFlagsSerializerVer14.TX_BIAS_VAL;
605 int biasCurrent = optical.get().getBiasCurrent();
606 annotations.set(AK_TX_BIAS_FEATURE, ((flags & txBias) != 0) ? "enabled" : "disabled");
607 annotations.set(AK_BIAS_CURRENT, Integer.toString(biasCurrent));
608
609 int txTemp = OFPortStatsOpticalFlagsSerializerVer14.TX_TEMP_VAL;
610 int temperature = optical.get().getTemperature();
611 annotations.set(AK_TX_TEMP_FEATURE, ((flags & txTemp) != 0) ? "enabled" : "disabled");
612 annotations.set(AK_TEMPERATURE, Integer.toString(temperature));
613 }
Thomas Vachuskaf0397b52015-05-29 13:50:17 -0700614 DefaultPortStatistics.Builder builder = DefaultPortStatistics.builder();
615 DefaultPortStatistics stat = builder.setDeviceId(deviceId)
616 .setPort(entry.getPortNo().getPortNumber())
617 .setPacketsReceived(entry.getRxPackets().getValue())
618 .setPacketsSent(entry.getTxPackets().getValue())
619 .setBytesReceived(entry.getRxBytes().getValue())
620 .setBytesSent(entry.getTxBytes().getValue())
621 .setPacketsRxDropped(entry.getRxDropped().getValue())
622 .setPacketsTxDropped(entry.getTxDropped().getValue())
623 .setPacketsRxErrors(entry.getRxErrors().getValue())
624 .setPacketsTxErrors(entry.getTxErrors().getValue())
625 .setDurationSec(entry.getVersion() == OFVersion.OF_10 ? 0 : entry.getDurationSec())
626 .setDurationNano(entry.getVersion() == OFVersion.OF_10 ? 0 : entry.getDurationNsec())
Laszlo Papp7cf60372018-01-11 00:06:43 +0000627 .setAnnotations(annotations.build())
Thomas Vachuskaf0397b52015-05-29 13:50:17 -0700628 .build();
sangho538108b2015-04-08 14:29:20 -0700629
Thomas Vachuskaf0397b52015-05-29 13:50:17 -0700630 stats.add(stat);
631 } catch (Exception e) {
632 LOG.warn("Unable to process port stats", e);
633 }
sangho538108b2015-04-08 14:29:20 -0700634 }
635
636 return Collections.unmodifiableSet(stats);
sangho538108b2015-04-08 14:29:20 -0700637 }
638
639 private class InternalDeviceProvider implements OpenFlowSwitchListener, OpenFlowEventListener {
Thomas Vachuska893bf4b2015-05-29 18:13:18 -0700640
Srikanth Vavilapalli78baf582015-06-05 11:40:14 -0700641 private HashMap<Dpid, List<OFPortStatsEntry>> portStatsReplies = new HashMap<>();
Ray Milkey7abe50b2016-04-13 11:57:44 -0700642 private boolean isDisabled = false;
Thomas Vachuska893bf4b2015-05-29 18:13:18 -0700643
alshabibf1216ed2014-09-03 11:53:54 -0700644 @Override
tomd1900f32014-09-03 14:08:16 -0700645 public void switchAdded(Dpid dpid) {
alshabib6f5460b2014-09-03 14:46:17 -0700646 if (providerService == null) {
647 return;
648 }
tom782a7cf2014-09-11 23:58:38 -0700649 DeviceId did = deviceId(uri(dpid));
alshabib6f5460b2014-09-03 14:46:17 -0700650 OpenFlowSwitch sw = controller.getSwitch(dpid);
Lei Xudee1aff2015-10-16 00:45:10 -0500651 if (sw == null) {
sangyun-han147e6f42016-07-26 17:14:41 +0900652 LOG.error("Switch {} is not found", dpid);
Lei Xudee1aff2015-10-16 00:45:10 -0500653 return;
654 }
alshabib6f5460b2014-09-03 14:46:17 -0700655
alshabib7911a052014-10-16 17:49:37 -0700656 ChassisId cId = new ChassisId(dpid.value());
Ray Milkeye53f1712015-01-16 09:17:16 -0800657
Victor Silvaac8dab02016-11-28 16:33:08 -0300658 DefaultAnnotations.Builder annotationsBuilder = DefaultAnnotations.builder()
andreafe3308f2015-10-06 15:51:25 -0700659 .set(AnnotationKeys.PROTOCOL, sw.factory().getVersion().toString())
660 .set(AnnotationKeys.CHANNEL_ID, sw.channelId())
Victor Silvaac8dab02016-11-28 16:33:08 -0300661 .set(AnnotationKeys.MANAGEMENT_ADDRESS, sw.channelId().split(":")[0]);
662
Yuta HIGUCHI2341e602017-03-08 20:10:08 -0800663 // FIXME following ignores driver specified by name
Victor Silvaac8dab02016-11-28 16:33:08 -0300664 Driver driver = driverService.getDriver(sw.manufacturerDescription(),
665 sw.hardwareDescription(),
666 sw.softwareDescription());
Thomas Vachuskaa79cbf72017-01-13 15:07:04 -0800667 // FIXME: The following breaks the STC tests and will require to be revisited.
668// if (driver != null) {
669// annotationsBuilder.set(AnnotationKeys.DRIVER, driver.name());
670// }
Victor Silvaac8dab02016-11-28 16:33:08 -0300671
672 SparseAnnotations annotations = annotationsBuilder.build();
Ray Milkeye53f1712015-01-16 09:17:16 -0800673
tomd1900f32014-09-03 14:08:16 -0700674 DeviceDescription description =
Marc De Leenheerb9311372015-07-09 11:36:49 -0700675 new DefaultDeviceDescription(did.uri(), sw.deviceType(),
Ray Milkeyd3edd032015-01-16 11:38:58 -0800676 sw.manufacturerDescription(),
tom782a7cf2014-09-11 23:58:38 -0700677 sw.hardwareDescription(),
678 sw.softwareDescription(),
alshabib7911a052014-10-16 17:49:37 -0700679 sw.serialNumber(),
Thomas Vachuska82041f52014-11-30 22:14:02 -0800680 cId, annotations);
tom782a7cf2014-09-11 23:58:38 -0700681 providerService.deviceConnected(did, description);
Ayaka Koshibe5460d622015-05-14 12:19:19 -0700682 providerService.updatePorts(did, buildPortDescriptions(sw));
sangho538108b2015-04-08 14:29:20 -0700683
Laszlo Papp58174812018-01-16 13:29:47 +0000684 if (sw.features().getCapabilities().contains(OFCapabilities.PORT_STATS)) {
685 PortStatsCollector psc = new PortStatsCollector(timer, sw, portStatsPollFrequency);
686 stopCollectorIfNeeded(collectors.put(dpid, psc));
687 psc.start();
688 }
Lei Xudee1aff2015-10-16 00:45:10 -0500689
690 //figure out race condition for collectors.remove() and collectors.put()
691 if (controller.getSwitch(dpid) == null) {
692 switchRemoved(dpid);
693 }
alshabib25c8eec2014-09-04 16:41:31 -0700694 }
695
Thomas Vachuskaa394b952016-06-14 15:02:09 -0700696 private void stopCollectorIfNeeded(PortStatsCollector collector) {
697 if (collector != null) {
698 collector.stop();
699 }
700 }
701
alshabibf1216ed2014-09-03 11:53:54 -0700702 @Override
703 public void switchRemoved(Dpid dpid) {
Victor Silva03265dd2016-12-08 11:20:36 -0300704 stopCollectorIfNeeded(collectors.remove(dpid));
alshabib6f5460b2014-09-03 14:46:17 -0700705 if (providerService == null) {
706 return;
707 }
tom782a7cf2014-09-11 23:58:38 -0700708 providerService.deviceDisconnected(deviceId(uri(dpid)));
sangho538108b2015-04-08 14:29:20 -0700709 }
Ayaka Koshibe38594c22014-10-22 13:36:12 -0700710
711 @Override
712 public void switchChanged(Dpid dpid) {
HIGUCHI Yuta1979f552015-12-28 21:24:26 -0800713 LOG.debug("switchChanged({})", dpid);
Ayaka Koshibe38594c22014-10-22 13:36:12 -0700714 if (providerService == null) {
715 return;
716 }
717 DeviceId did = deviceId(uri(dpid));
718 OpenFlowSwitch sw = controller.getSwitch(dpid);
Lei Xudee1aff2015-10-16 00:45:10 -0500719 if (sw == null) {
sangyun-han147e6f42016-07-26 17:14:41 +0900720 LOG.error("Switch {} is not found", dpid);
Lei Xudee1aff2015-10-16 00:45:10 -0500721 return;
722 }
HIGUCHI Yuta1979f552015-12-28 21:24:26 -0800723 final List<PortDescription> ports = buildPortDescriptions(sw);
724 LOG.debug("switchChanged({}) {}", did, ports);
725 providerService.updatePorts(did, ports);
Ayaka Koshibe38594c22014-10-22 13:36:12 -0700726 }
727
alshabiba14f3642014-09-05 09:31:31 -0700728 @Override
729 public void portChanged(Dpid dpid, OFPortStatus status) {
HIGUCHI Yuta1979f552015-12-28 21:24:26 -0800730 LOG.debug("portChanged({},{})", dpid, status);
alshabibafc514a2014-12-01 14:44:05 -0800731 PortDescription portDescription = buildPortDescription(status);
Michal Machce774332017-01-25 11:02:55 +0100732 if (status.getReason() != OFPortReason.DELETE) {
733 providerService.portStatusChanged(deviceId(uri(dpid)), portDescription);
734 } else {
735 providerService.deletePort(deviceId(uri(dpid)), portDescription);
736 }
alshabibf1216ed2014-09-03 11:53:54 -0700737 }
738
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700739 @Override
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700740 public void receivedRoleReply(Dpid dpid, RoleState requested, RoleState response) {
HIGUCHI Yuta1979f552015-12-28 21:24:26 -0800741 LOG.debug("receivedRoleReply({},{},{})", dpid, requested, response);
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700742 MastershipRole request = roleOf(requested);
743 MastershipRole reply = roleOf(response);
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700744 providerService.receivedRoleReply(deviceId(uri(dpid)), request, reply);
745 }
746
747 /**
748 * Translates a RoleState to the corresponding MastershipRole.
749 *
Thomas Vachuskafc52fec2015-05-18 19:13:56 -0700750 * @param response role state
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700751 * @return a MastershipRole
752 */
753 private MastershipRole roleOf(RoleState response) {
754 switch (response) {
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700755 case MASTER:
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700756 return MastershipRole.MASTER;
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700757 case EQUAL:
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700758 return MastershipRole.STANDBY;
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700759 case SLAVE:
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700760 return MastershipRole.NONE;
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700761 default:
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700762 LOG.warn("unknown role {}", response);
763 return null;
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700764 }
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700765 }
766
alshabiba14f3642014-09-05 09:31:31 -0700767 /**
768 * Builds a list of port descriptions for a given list of ports.
tomff7eb7c2014-09-08 12:49:03 -0700769 *
alshabiba14f3642014-09-05 09:31:31 -0700770 * @return list of portdescriptions
771 */
Ayaka Koshibe5460d622015-05-14 12:19:19 -0700772 private List<PortDescription> buildPortDescriptions(OpenFlowSwitch sw) {
Yuta HIGUCHIf83c8cf2017-12-17 14:33:49 -0800773 List<OFPortDesc> ofPorts = sw.getPorts();
774 final List<PortDescription> portDescs = new ArrayList<>(ofPorts.size());
Rimon Ashkenazy8ebfff02016-02-01 11:56:36 +0200775 if (!((Device.Type.ROADM.equals(sw.deviceType())) ||
MaoLuc201ae42017-02-06 17:57:01 -0800776 (Device.Type.OTN.equals(sw.deviceType())) ||
777 (Device.Type.OPTICAL_AMPLIFIER.equals(sw.deviceType())))) {
Yuta HIGUCHI2341e602017-03-08 20:10:08 -0800778 // build regular (=non-optical) Device ports
Yuta HIGUCHIf83c8cf2017-12-17 14:33:49 -0800779 ofPorts.forEach(port -> portDescs.add(buildPortDescription(port)));
Yafit Hadara9a73de2015-09-06 13:52:52 +0300780 }
Marc De Leenheerb9311372015-07-09 11:36:49 -0700781
Yuta HIGUCHI2341e602017-03-08 20:10:08 -0800782 // TODO handle Optical Device, but plain OF devices(1.4 and later)
783
Marc De Leenheerb9311372015-07-09 11:36:49 -0700784 OpenFlowOpticalSwitch opsw;
785 switch (sw.deviceType()) {
786 case ROADM:
Rimon Ashkenazy8ebfff02016-02-01 11:56:36 +0200787 case OTN:
MaoLuc201ae42017-02-06 17:57:01 -0800788 case OPTICAL_AMPLIFIER:
Marc De Leenheerb9311372015-07-09 11:36:49 -0700789 opsw = (OpenFlowOpticalSwitch) sw;
Yafit Hadara9a73de2015-09-06 13:52:52 +0300790 List<OFPortDesc> ports = opsw.getPorts();
791 LOG.debug("SW ID {} , ETH- ODU CLT Ports {}", opsw.getId(), ports);
792 // ODU client ports are reported as ETH
793 ports.forEach(port -> portDescs.add(buildOduCltPortDescription(port)));
794
Marc De Leenheerb9311372015-07-09 11:36:49 -0700795 opsw.getPortTypes().forEach(type -> {
Yafit Hadara9a73de2015-09-06 13:52:52 +0300796 List<? extends OFObject> portsOf = opsw.getPortsOf(type);
797 LOG.debug("Ports Of{}", portsOf);
798 portsOf.forEach(
799 op -> {
Jimmy Yan4deb03b2016-06-24 10:53:54 -0700800 portDescs.add(buildPortDescription(type, op, opsw));
Yafit Hadara9a73de2015-09-06 13:52:52 +0300801 }
802 );
Marc De Leenheerb9311372015-07-09 11:36:49 -0700803 });
804 break;
805 case FIBER_SWITCH:
806 opsw = (OpenFlowOpticalSwitch) sw;
807 opsw.getPortTypes().forEach(type -> {
Yuta HIGUCHIf42a2cc2017-05-02 17:04:31 -0700808 opsw.getPortsOf(type).forEach(op -> {
809 if (op instanceof OFPortDesc) {
810 // ports using standard optical extension
811 // TODO OFMessage -> PortDescription should
812 // probably be a Behaviour
813 portDescs.add(oms(buildPortDescription((OFPortDesc) op)));
814 } else if (op instanceof OFCalientPortDescStatsEntry) {
815 // calient extension
816 portDescs.add(buildPortDescription((OFCalientPortDescStatsEntry) op));
817 } else {
818 LOG.warn("Unexpected FIBER_SWITCH port {} on {}",
819 op, sw.getStringId());
820 }
821 });
Marc De Leenheerb9311372015-07-09 11:36:49 -0700822 });
823 break;
824 default:
825 break;
alshabib4680bb62014-09-04 17:15:08 -0700826 }
Marc De Leenheerb9311372015-07-09 11:36:49 -0700827
alshabib4680bb62014-09-04 17:15:08 -0700828 return portDescs;
829 }
830
Yuta HIGUCHIf42a2cc2017-05-02 17:04:31 -0700831 /**
832 * Ensures returned PortDescription is an OMS port.
833 *
834 * @param descr input PortDescription
835 * @return OMS PortDescription
836 */
837 private PortDescription oms(PortDescription descr) {
838 // Hack until OFMessage -> PortDescription transformation
839 // becomes a Behaviour
840 if (descr.type() == Type.OMS) {
841 return descr;
842 }
843
844 Builder builder = DefaultAnnotations.builder();
845 builder.putAll(descr.annotations());
846
847 // set reasonable default when mandatory key is missing
848 if (Strings.isNullOrEmpty(descr.annotations().value(AK_MIN_FREQ_HZ))) {
849 builder.set(AK_MIN_FREQ_HZ, String.valueOf(Spectrum.O_BAND_MIN.asHz()));
850 }
851
852 if (Strings.isNullOrEmpty(descr.annotations().value(AK_MAX_FREQ_HZ))) {
853 builder.set(AK_MAX_FREQ_HZ, String.valueOf(Spectrum.O_BAND_MAX.asHz()));
854 }
855
856 if (Strings.isNullOrEmpty(descr.annotations().value(AK_GRID_HZ))) {
857 builder.set(AK_GRID_HZ, String.valueOf(Frequency.ofGHz(50).asHz()));
858 }
859
860 return DefaultPortDescription.builder(descr)
861 .type(Type.OMS)
862 .annotations(builder.build())
863 .build();
864 }
865
Yafit Hadara9a73de2015-09-06 13:52:52 +0300866 private PortDescription buildOduCltPortDescription(OFPortDesc port) {
867 PortNumber portNo = PortNumber.portNumber(port.getPortNo().getPortNumber());
868 boolean enabled = !port.getState().contains(OFPortState.LINK_DOWN) &&
869 !port.getConfig().contains(OFPortConfig.PORT_DOWN);
Rimon Ashkenazyd15ebf52016-01-06 14:36:13 +0200870 Long portSpeedInMbps = portSpeed(port);
Toru Furusawa72ee30c2016-01-08 13:29:04 -0800871 CltSignalType sigType = null;
Yafit Hadara9a73de2015-09-06 13:52:52 +0300872
Rimon Ashkenazyd15ebf52016-01-06 14:36:13 +0200873 switch (portSpeedInMbps.toString()) {
874 case "1000":
Toru Furusawa72ee30c2016-01-08 13:29:04 -0800875 sigType = CltSignalType.CLT_1GBE;
Yafit Hadara9a73de2015-09-06 13:52:52 +0300876 break;
Rimon Ashkenazyd15ebf52016-01-06 14:36:13 +0200877 case "10000":
Toru Furusawa72ee30c2016-01-08 13:29:04 -0800878 sigType = CltSignalType.CLT_10GBE;
Yafit Hadara9a73de2015-09-06 13:52:52 +0300879 break;
Rimon Ashkenazyd15ebf52016-01-06 14:36:13 +0200880 case "40000":
Toru Furusawa72ee30c2016-01-08 13:29:04 -0800881 sigType = CltSignalType.CLT_40GBE;
Yafit Hadara9a73de2015-09-06 13:52:52 +0300882 break;
Rimon Ashkenazyd15ebf52016-01-06 14:36:13 +0200883 case "100000":
Toru Furusawa72ee30c2016-01-08 13:29:04 -0800884 sigType = CltSignalType.CLT_100GBE;
Yafit Hadara9a73de2015-09-06 13:52:52 +0300885 break;
886 default:
Rimon Ashkenazyd15ebf52016-01-06 14:36:13 +0200887 throw new RuntimeException("Un recognize OduClt speed: " + portSpeedInMbps.toString());
Yafit Hadara9a73de2015-09-06 13:52:52 +0300888 }
889
890 SparseAnnotations annotations = buildOduCltAnnotation(port);
HIGUCHI Yuta4c0ef6b2016-05-02 19:45:41 -0700891 return oduCltPortDescription(portNo, enabled, sigType, annotations);
Yafit Hadara9a73de2015-09-06 13:52:52 +0300892 }
893
894 private SparseAnnotations buildOduCltAnnotation(OFPortDesc port) {
895 SparseAnnotations annotations = null;
896 String portName = Strings.emptyToNull(port.getName());
897 if (portName != null) {
898 annotations = DefaultAnnotations.builder()
899 .set(AnnotationKeys.PORT_NAME, portName)
900 .set(AnnotationKeys.STATIC_PORT, Boolean.TRUE.toString()).build();
901 }
902 return annotations;
903 }
904
Jimmy Yan4deb03b2016-06-24 10:53:54 -0700905 private PortDescription buildPortDescription(PortDescPropertyType ptype, OFObject port,
906 OpenFlowOpticalSwitch opsw) {
yjimmyy646aa022016-07-05 12:09:50 -0700907 if (port instanceof OFPortOptical) {
Jimmy Yan4deb03b2016-06-24 10:53:54 -0700908 return buildPortDescription(ptype, (OFPortOptical) port, opsw);
Yafit Hadara9a73de2015-09-06 13:52:52 +0300909 }
910 return buildPortDescription(ptype, (OFExpPort) port);
911 }
912
Rimon Ashkenazy8ebfff02016-02-01 11:56:36 +0200913 private boolean matchingOtuPortSignalTypes(OFPortOpticalTransportSignalType sigType,
914 OduSignalType oduSignalType) {
915 switch (sigType) {
916 case OTU2:
917 if (oduSignalType == OduSignalType.ODU2) {
918 return true;
919 }
920 break;
921 case OTU4:
922 if (oduSignalType == OduSignalType.ODU4) {
923 return true;
924 }
925 break;
926 default:
927 break;
928 }
929 return false;
930 }
Yafit Hadara9a73de2015-09-06 13:52:52 +0300931 /**
932 * Build a portDescription from a given a port description describing some
933 * Optical port.
934 *
935 * @param ptype description property type.
936 * @param port the port to build from.
937 * @return portDescription for the port.
938 */
939 private PortDescription buildPortDescription(PortDescPropertyType ptype, OFExpPort port) {
940 PortNumber portNo = PortNumber.portNumber(port.getPortNo().getPortNumber());
941 boolean enabled = !port.getState().contains(OFPortState.LINK_DOWN)
942 && !port.getConfig().contains(OFPortConfig.PORT_DOWN);
mskalac584bd92017-11-28 15:51:17 +0100943 boolean adminDown = port.getConfig().contains(OFPortConfig.PORT_DOWN);
944 SparseAnnotations annotations = makePortAnnotation(port.getName(),
945 port.getHwAddr().toString(),
946 adminDown).build();
Yafit Hadara9a73de2015-09-06 13:52:52 +0300947
948 OFExpPortDescPropOpticalTransport firstProp = port.getProperties().get(0);
949 OFPortOpticalTransportSignalType sigType = firstProp.getPortSignalType();
950
HIGUCHI Yuta34a3f692016-01-09 21:08:57 -0800951 PortDescription portDes = null;
Yafit Hadara9a73de2015-09-06 13:52:52 +0300952 switch (sigType) {
953 case OMSN:
HIGUCHI Yuta95d83e82016-04-26 12:13:48 -0700954 portDes = omsPortDescription(portNo, enabled,
Rimon Ashkenazye3201032016-01-11 14:27:30 +0200955 FREQ191_7, FREQ191_7.add(FREQ4_4), FREQ50, annotations);
Yafit Hadara9a73de2015-09-06 13:52:52 +0300956 break;
957 case OCH:
958 OFExpPortOpticalTransportLayerEntry entry = firstProp.getFeatures().get(0).getValue().get(0);
959 OFPortOpticalTransportLayerClass layerClass = entry.getLayerClass();
960 if (!OFPortOpticalTransportLayerClass.ODU.equals(layerClass)) {
961 LOG.error("Unsupported layer Class {} ", layerClass);
962 return null;
963 }
964
965 // convert to ONOS OduSignalType
966 OduSignalType oduSignalType = OpenFlowDeviceValueMapper.
967 lookupOduSignalType((byte) entry.getSignalType());
968 //OchSignal is needed for OchPortDescription constructor,
969 //yet not relevant for tunable OCH port, creating with default parameters
970 OchSignal signalId = new OchSignal(GridType.DWDM, ChannelSpacing.CHL_50GHZ, 1, 1);
971
HIGUCHI Yuta34a3f692016-01-09 21:08:57 -0800972 portDes = ochPortDescription(portNo, enabled,
973 oduSignalType, true,
974 signalId, annotations);
Yafit Hadara9a73de2015-09-06 13:52:52 +0300975
976 break;
977 case OTU2:
978 case OTU4:
Rimon Ashkenazy8ebfff02016-02-01 11:56:36 +0200979 entry = firstProp.getFeatures().get(0).getValue().get(0);
980 layerClass = entry.getLayerClass();
981 if (!OFPortOpticalTransportLayerClass.ODU.equals(layerClass)) {
982 LOG.error("Unsupported layer Class {} ", layerClass);
983 return null;
984 }
985
986 // convert to ONOS OduSignalType
987 OduSignalType oduSignalTypeOtuPort = OpenFlowDeviceValueMapper.
988 lookupOduSignalType((byte) entry.getSignalType());
989 if (!matchingOtuPortSignalTypes(sigType, oduSignalTypeOtuPort)) {
990 LOG.error("Wrong oduSignalType {} for OTU Port sigType {} ", oduSignalTypeOtuPort, sigType);
991 return null;
992 }
993 OtuSignalType otuSignalType =
994 ((sigType == OFPortOpticalTransportSignalType.OTU2) ? OtuSignalType.OTU2 :
995 OtuSignalType.OTU4);
HIGUCHI Yuta5be3e822016-05-03 13:51:42 -0700996 portDes = otuPortDescription(portNo, enabled, otuSignalType, annotations);
Rimon Ashkenazy8ebfff02016-02-01 11:56:36 +0200997 break;
Yafit Hadara9a73de2015-09-06 13:52:52 +0300998 default:
999 break;
1000 }
1001
1002 return portDes;
1003 }
1004
alshabiba14f3642014-09-05 09:31:31 -07001005 /**
Yuta HIGUCHI2341e602017-03-08 20:10:08 -08001006 * Creates an annotation builder for the port name if one is available.
Ray Milkeye0fade72015-01-15 13:29:47 -08001007 *
lishuai4ee42042015-11-30 17:19:21 +08001008 * @param portName the port name
1009 * @param portMac the port mac
mskalac584bd92017-11-28 15:51:17 +01001010 * @param adminDown the port admin state
1011 * @return annotation builder containing port admin state, port name
1012 * and/or port MAC if any of the two is found
Ray Milkeye0fade72015-01-15 13:29:47 -08001013 */
mskalac584bd92017-11-28 15:51:17 +01001014 private DefaultAnnotations.Builder makePortAnnotation(String portName, String portMac, boolean adminDown) {
Andrea Campanellac8b871a2017-01-19 13:53:05 -08001015 DefaultAnnotations.Builder builder = DefaultAnnotations.builder();
lishuai4ee42042015-11-30 17:19:21 +08001016 String pName = Strings.emptyToNull(portName);
1017 String pMac = Strings.emptyToNull(portMac);
Andrea Campanellac8b871a2017-01-19 13:53:05 -08001018 if (pName != null) {
1019 builder.set(AnnotationKeys.PORT_NAME, pName);
Ray Milkeye0fade72015-01-15 13:29:47 -08001020 }
Andrea Campanellac8b871a2017-01-19 13:53:05 -08001021 if (pMac != null) {
1022 builder.set(AnnotationKeys.PORT_MAC, pMac);
1023 }
mskalac584bd92017-11-28 15:51:17 +01001024 String adminState = adminDown ? "disabled" : "enabled";
1025 builder.set(AnnotationKeys.ADMIN_STATE, adminState);
Yuta HIGUCHI2341e602017-03-08 20:10:08 -08001026 return builder;
1027 }
1028
Yuta HIGUCHI2341e602017-03-08 20:10:08 -08001029 private PortDescription buildPortDescription14(OFPortDesc port) {
1030 PortNumber portNo = PortNumber.portNumber(port.getPortNo().getPortNumber());
1031 boolean enabled =
1032 !port.getState().contains(OFPortState.LINK_DOWN) &&
1033 !port.getConfig().contains(OFPortConfig.PORT_DOWN);
mskalac584bd92017-11-28 15:51:17 +01001034 boolean adminDown = port.getConfig().contains(OFPortConfig.PORT_DOWN);
Yuta HIGUCHI2341e602017-03-08 20:10:08 -08001035 Builder annotations = makePortAnnotation(port.getName(),
mskalac584bd92017-11-28 15:51:17 +01001036 port.getHwAddr().toString(),
1037 adminDown);
Yuta HIGUCHI2341e602017-03-08 20:10:08 -08001038
1039 Optional<OFPortDescPropEthernet> ether = port.getProperties().stream()
1040 .filter(OFPortDescPropEthernet.class::isInstance)
1041 .map(OFPortDescPropEthernet.class::cast)
1042 .findAny();
1043 if (ether.isPresent()) {
1044 // ethernet port
1045 // TODO parse other part of OFPortDescPropEthernet if necessary
1046 return new DefaultPortDescription(portNo,
1047 enabled,
1048 COPPER,
1049 portSpeed(port),
1050 annotations.build());
1051 }
1052
1053 Optional<OFPortDescPropOptical> optical = port.getProperties().stream()
1054 .filter(OFPortDescPropOptical.class::isInstance)
1055 .map(OFPortDescPropOptical.class::cast)
1056 .findAny();
1057 if (optical.isPresent()) {
1058 // optical port
1059
Yuta HIGUCHIf42a2cc2017-05-02 17:04:31 -07001060 // FIXME is there a OF version neutral way to access
1061 // OFOpticalPortFeaturesSerializerVer14
Yuta HIGUCHI2341e602017-03-08 20:10:08 -08001062
Laszlo Papp20cfb6f2018-01-08 16:06:53 +00001063 long supported = optical.get().getSupported();
1064 long rxMin = optical.get().getRxMinFreqLmda();
1065 long rxMax = optical.get().getRxMaxFreqLmda();
1066 long rxGrid = optical.get().getRxGridFreqLmda();
1067 long txMin = optical.get().getTxMinFreqLmda();
1068 long txMax = optical.get().getTxMaxFreqLmda();
1069 long txGrid = optical.get().getTxGridFreqLmda();
1070
1071 int txTune = OFOpticalPortFeaturesSerializerVer14.TX_TUNE_VAL;
1072 int rxTune = OFOpticalPortFeaturesSerializerVer14.RX_TUNE_VAL;
1073 annotations.set(AK_TX_TUNE_FEATURE,
1074 ((supported & txTune) != 0) ? "enabled" : "disabled");
1075 annotations.set(AK_RX_TUNE_FEATURE,
1076 ((supported & rxTune) != 0) ? "enabled" : "disabled");
1077
Yuta HIGUCHI2341e602017-03-08 20:10:08 -08001078 // wire value for OFOpticalPortFeatures.USE_FREQ
Laszlo Papp8e8ff052018-01-16 09:55:45 +00001079 boolean useFreq = (supported & OFOpticalPortFeaturesSerializerVer14.USE_FREQ_VAL) != 0;
1080 annotations.set(AK_USE_FREQ_FEATURE, useFreq ? "enabled" : "disabled");
Yuta HIGUCHI2341e602017-03-08 20:10:08 -08001081
Laszlo Papp8e8ff052018-01-16 09:55:45 +00001082 annotations.set(AK_RX_MIN_FREQ_HZ, freqLmdaToAnnotation(rxMin, useFreq));
1083 annotations.set(AK_RX_MAX_FREQ_HZ, freqLmdaToAnnotation(rxMax, useFreq));
1084 annotations.set(AK_RX_GRID_HZ, freqLmdaToAnnotation(rxGrid, useFreq));
Yuta HIGUCHI2341e602017-03-08 20:10:08 -08001085
Laszlo Papp8e8ff052018-01-16 09:55:45 +00001086 annotations.set(AK_TX_MIN_FREQ_HZ, freqLmdaToAnnotation(txMin, useFreq));
1087 annotations.set(AK_TX_MAX_FREQ_HZ, freqLmdaToAnnotation(txMax, useFreq));
1088 annotations.set(AK_TX_GRID_HZ, freqLmdaToAnnotation(txGrid, useFreq));
Yuta HIGUCHI2341e602017-03-08 20:10:08 -08001089
Laszlo Papp8e8ff052018-01-16 09:55:45 +00001090 // FIXME pretty confident this is not going to happen
1091 // unless Device models Tx/Rx ports as separate port
1092 if (rxMin == txMin) {
1093 annotations.set(AK_MIN_FREQ_HZ,
1094 freqLmdaToAnnotation(rxMin, useFreq));
1095 }
1096 if (rxMax == txMax) {
1097 annotations.set(AK_MAX_FREQ_HZ,
1098 freqLmdaToAnnotation(rxMax, useFreq));
1099 }
1100 if (rxGrid == txGrid) {
1101 annotations.set(AK_GRID_HZ,
1102 freqLmdaToAnnotation(rxGrid, useFreq));
Yuta HIGUCHI2341e602017-03-08 20:10:08 -08001103 }
Laszlo Papp20cfb6f2018-01-08 16:06:53 +00001104
1105 int txPwr = OFOpticalPortFeaturesSerializerVer14.TX_PWR_VAL;
1106 long txPwrMin = optical.get().getTxPwrMin();
1107 long txPwrMax = optical.get().getTxPwrMax();
1108 annotations.set(AK_TX_PWR_FEATURE, ((supported & txPwr) != 0) ? "enabled" : "disabled");
1109 annotations.set(AK_TX_PWR_MIN, Long.toString(txPwrMin));
1110 annotations.set(AK_TX_PWR_MAX, Long.toString(txPwrMax));
Yuta HIGUCHI2341e602017-03-08 20:10:08 -08001111
1112 // TODO How to determine appropriate port type?
1113
1114 return new DefaultPortDescription(portNo,
1115 enabled,
1116 FIBER,
1117 portSpeed(port),
1118 annotations.build());
1119 }
1120
1121 // fall back default
1122 return new DefaultPortDescription(portNo,
1123 enabled,
1124 COPPER,
1125 portSpeed(port),
1126 annotations.build());
1127
Ray Milkeye0fade72015-01-15 13:29:47 -08001128 }
1129
1130 /**
Ayaka Koshibe5460d622015-05-14 12:19:19 -07001131 * Build a portDescription from a given Ethernet port description.
tomff7eb7c2014-09-08 12:49:03 -07001132 *
alshabiba14f3642014-09-05 09:31:31 -07001133 * @param port the port to build from.
1134 * @return portDescription for the port.
1135 */
1136 private PortDescription buildPortDescription(OFPortDesc port) {
Yuta HIGUCHI2341e602017-03-08 20:10:08 -08001137 if (port.getVersion().wireVersion >= OFVersion.OF_14.getWireVersion()) {
1138 return buildPortDescription14(port);
1139 }
Thomas Vachuskad16ce182014-10-29 17:25:29 -07001140 PortNumber portNo = PortNumber.portNumber(port.getPortNo().getPortNumber());
1141 boolean enabled =
1142 !port.getState().contains(OFPortState.LINK_DOWN) &&
1143 !port.getConfig().contains(OFPortConfig.PORT_DOWN);
1144 Port.Type type = port.getCurr().contains(OFPortFeatures.PF_FIBER) ? FIBER : COPPER;
mskalac584bd92017-11-28 15:51:17 +01001145 boolean adminDown = port.getConfig().contains(OFPortConfig.PORT_DOWN);
1146 SparseAnnotations annotations = makePortAnnotation(port.getName(),
1147 port.getHwAddr().toString(),
1148 adminDown).build();
Ray Milkeye0fade72015-01-15 13:29:47 -08001149 return new DefaultPortDescription(portNo, enabled, type,
1150 portSpeed(port), annotations);
alshabiba14f3642014-09-05 09:31:31 -07001151 }
Ayaka Koshibeab91cc42014-09-25 10:20:52 -07001152
Ayaka Koshibe5460d622015-05-14 12:19:19 -07001153 /**
1154 * Build a portDescription from a given a port description describing some
1155 * Optical port.
1156 *
1157 * @param port description property type.
1158 * @param port the port to build from.
1159 * @return portDescription for the port.
1160 */
Jimmy Yan4deb03b2016-06-24 10:53:54 -07001161 private PortDescription buildPortDescription(PortDescPropertyType ptype, OFPortOptical port,
1162 OpenFlowOpticalSwitch opsw) {
Jon Hallcbd1b392017-01-18 20:15:44 -08001163 checkArgument(!port.getDesc().isEmpty());
Marc De Leenheerfc913dd2015-07-30 16:04:55 -07001164
Ayaka Koshibe5460d622015-05-14 12:19:19 -07001165 // Minimally functional fixture. This needs to be fixed as we add better support.
1166 PortNumber portNo = PortNumber.portNumber(port.getPortNo().getPortNumber());
Ayaka Koshibe74b55272015-05-28 15:16:04 -07001167
Ayaka Koshibe5460d622015-05-14 12:19:19 -07001168 boolean enabled = !port.getState().contains(OFPortState.LINK_DOWN)
1169 && !port.getConfig().contains(OFPortConfig.PORT_DOWN);
mskalac584bd92017-11-28 15:51:17 +01001170 boolean adminDown = port.getConfig().contains(OFPortConfig.PORT_DOWN);
1171 SparseAnnotations annotations = makePortAnnotation(port.getName(),
1172 port.getHwAddr().toString(),
1173 adminDown).build();
Ayaka Koshibe5460d622015-05-14 12:19:19 -07001174
1175 if (port.getVersion() == OFVersion.OF_13
1176 && ptype == PortDescPropertyType.OPTICAL_TRANSPORT) {
1177 // At this point, not much is carried in the optical port message.
Ayaka Koshibeae541732015-05-19 13:37:27 -07001178 LOG.debug("Optical transport port message {}", port.toString());
Ayaka Koshibe5460d622015-05-14 12:19:19 -07001179 } else {
1180 // removable once 1.4+ support complete.
Ayaka Koshibeae541732015-05-19 13:37:27 -07001181 LOG.debug("Unsupported optical port properties");
Ayaka Koshibe5460d622015-05-14 12:19:19 -07001182 }
Marc De Leenheerfc913dd2015-07-30 16:04:55 -07001183
1184 OFPortDescPropOpticalTransport desc = port.getDesc().get(0);
1185 switch (desc.getPortSignalType()) {
1186 // FIXME: use constants once loxi has full optical extensions
1187 case 2: // OMS port
1188 // Assume complete optical spectrum and 50 GHz grid
Jimmy Yan4deb03b2016-06-24 10:53:54 -07001189 Set<OchSignal> signals = null;
1190 if (opsw instanceof HandlerBehaviour) {
1191 DriverHandler driverHandler = ((HandlerBehaviour) opsw).handler();
1192 if (driverHandler != null && driverHandler.hasBehaviour(LambdaQuery.class)) {
1193 try {
1194 signals = driverHandler.behaviour(LambdaQuery.class).queryLambdas(portNo);
1195 } catch (NullPointerException e) {
1196 signals = null;
1197 }
1198 }
1199 }
1200 Frequency minFreq;
1201 Frequency maxFreq;
1202 Frequency channelSpacing;
1203 if (signals == null || signals.isEmpty()) {
1204 minFreq = Spectrum.U_BAND_MIN;
1205 maxFreq = Spectrum.O_BAND_MAX;
1206 channelSpacing = Frequency.ofGHz(50);
1207 } else {
1208 Comparator<OchSignal> compare =
1209 (OchSignal a, OchSignal b) -> a.spacingMultiplier() - b.spacingMultiplier();
1210 OchSignal minOch = Collections.min(signals, compare);
1211 OchSignal maxOch = Collections.max(signals, compare);
1212 minFreq = minOch.centralFrequency();
1213 maxFreq = maxOch.centralFrequency();
1214 channelSpacing = minOch.channelSpacing().frequency();
1215 }
1216 return omsPortDescription(portNo, enabled, minFreq,
1217 maxFreq, channelSpacing, annotations);
Marc De Leenheerfc913dd2015-07-30 16:04:55 -07001218 case 5: // OCH port
1219 OchSignal signal = new OchSignal(GridType.DWDM, ChannelSpacing.CHL_50GHZ, 0, 4);
HIGUCHI Yuta34a3f692016-01-09 21:08:57 -08001220 return ochPortDescription(portNo, enabled, OduSignalType.ODU4,
Marc De Leenheerfc913dd2015-07-30 16:04:55 -07001221 true, signal, annotations);
1222 default:
1223 break;
1224 }
1225
Ayaka Koshibe74b55272015-05-28 15:16:04 -07001226 return new DefaultPortDescription(portNo, enabled, FIBER, 0, annotations);
Ayaka Koshibe5460d622015-05-14 12:19:19 -07001227 }
1228
Marc De Leenheerb9311372015-07-09 11:36:49 -07001229 /**
1230 * Build a portDescription from a given port description describing a fiber switch optical port.
1231 *
1232 * @param port description property type.
1233 * @param port the port to build from.
1234 * @return portDescription for the port.
1235 */
1236 private PortDescription buildPortDescription(OFCalientPortDescStatsEntry port) {
1237 PortNumber portNo = PortNumber.portNumber(port.getPortNo().getPortNumber());
1238
Marc De Leenheerc662d322016-02-18 16:05:10 -08001239 // Use the alias name if it's available
1240 String name = port.getName();
1241 List<OFCalientPortDescProp> props = port.getProperties();
Jon Hallcbd1b392017-01-18 20:15:44 -08001242 if (props != null && !props.isEmpty()) {
Marc De Leenheerc662d322016-02-18 16:05:10 -08001243 OFCalientPortDescPropOptical propOptical = (OFCalientPortDescPropOptical) props.get(0);
1244 if (propOptical != null) {
1245 name = propOptical.getInAlias();
1246 }
1247 }
1248
Marc De Leenheerb9311372015-07-09 11:36:49 -07001249 // FIXME when Calient OF agent reports port status
1250 boolean enabled = true;
mskalac584bd92017-11-28 15:51:17 +01001251 boolean adminDown = false;
1252 SparseAnnotations annotations = makePortAnnotation(name, port.getHwAddr().toString(), adminDown).build();
Marc De Leenheerb9311372015-07-09 11:36:49 -07001253
Marc De Leenheerfc913dd2015-07-30 16:04:55 -07001254 // S160 data sheet
1255 // Wavelength range: 1260 - 1630 nm, grid is irrelevant for this type of switch
HIGUCHI Yuta95d83e82016-04-26 12:13:48 -07001256 return omsPortDescription(portNo, enabled,
Yuta HIGUCHIf42a2cc2017-05-02 17:04:31 -07001257 Spectrum.O_BAND_MIN, Spectrum.O_BAND_MAX, Frequency.ofGHz(50), annotations);
Marc De Leenheerb9311372015-07-09 11:36:49 -07001258 }
1259
alshabibafc514a2014-12-01 14:44:05 -08001260 private PortDescription buildPortDescription(OFPortStatus status) {
1261 OFPortDesc port = status.getDesc();
1262 if (status.getReason() != OFPortReason.DELETE) {
1263 return buildPortDescription(port);
1264 } else {
Yuta HIGUCHI6512f3e2017-05-18 17:21:24 -07001265 PortDescription desc = buildPortDescription(port);
1266 if (desc.isEnabled()) {
1267 return DefaultPortDescription.builder(desc)
1268 .isEnabled(false)
1269 .build();
1270 }
1271 return desc;
alshabibafc514a2014-12-01 14:44:05 -08001272 }
1273 }
1274
Yuta HIGUCHI2341e602017-03-08 20:10:08 -08001275 /**
1276 * Returns port speed in Mbps.
1277 *
1278 * @param port description to parse
1279 * @return port speed in Mbps
1280 */
Thomas Vachuskad16ce182014-10-29 17:25:29 -07001281 private long portSpeed(OFPortDesc port) {
Yuta HIGUCHI2341e602017-03-08 20:10:08 -08001282 if (port.getVersion().getWireVersion() >= OFVersion.OF_14.getWireVersion()) {
1283 // OFPortDescPropEthernet
1284 return port.getProperties().stream()
1285 .filter(OFPortDescPropEthernet.class::isInstance)
1286 .map(OFPortDescPropEthernet.class::cast)
1287 .mapToLong(OFPortDescPropEthernet::getCurrSpeed)
1288 .map(kbps -> kbps / KBPS)
1289 .findAny()
1290 .orElse(PortSpeed.SPEED_NONE.getSpeedBps() / MBPS);
1291 }
Thomas Vachuskad16ce182014-10-29 17:25:29 -07001292 if (port.getVersion() == OFVersion.OF_13) {
Charles Chan25b77322015-12-13 01:00:56 -08001293 // Note: getCurrSpeed() returns a value in kbps (this also applies to OF_11 and OF_12)
1294 return port.getCurrSpeed() / KBPS;
Thomas Vachuskad16ce182014-10-29 17:25:29 -07001295 }
Yuta HIGUCHI2341e602017-03-08 20:10:08 -08001296 // < OF1.3
Thomas Vachuskad16ce182014-10-29 17:25:29 -07001297 PortSpeed portSpeed = PortSpeed.SPEED_NONE;
1298 for (OFPortFeatures feat : port.getCurr()) {
1299 portSpeed = PortSpeed.max(portSpeed, feat.getPortSpeed());
1300 }
Thomas Vachuska98eda532014-10-29 17:31:02 -07001301 return portSpeed.getSpeedBps() / MBPS;
Thomas Vachuskad16ce182014-10-29 17:25:29 -07001302 }
sangho538108b2015-04-08 14:29:20 -07001303
1304 @Override
1305 public void handleMessage(Dpid dpid, OFMessage msg) {
Ray Milkey7abe50b2016-04-13 11:57:44 -07001306 if (isDisabled) {
1307 return;
sangho538108b2015-04-08 14:29:20 -07001308 }
Ray Milkey7abe50b2016-04-13 11:57:44 -07001309
1310 try {
1311 switch (msg.getType()) {
1312 case STATS_REPLY:
1313 if (((OFStatsReply) msg).getStatsType() == OFStatsType.PORT) {
1314 OFPortStatsReply portStatsReply = (OFPortStatsReply) msg;
1315 List<OFPortStatsEntry> portStatsReplyList = portStatsReplies.get(dpid);
1316 if (portStatsReplyList == null) {
Madan Jampani84382b92016-06-22 08:26:49 -07001317 portStatsReplyList = Lists.newCopyOnWriteArrayList();
Ray Milkey7abe50b2016-04-13 11:57:44 -07001318 }
1319 portStatsReplyList.addAll(portStatsReply.getEntries());
1320 portStatsReplies.put(dpid, portStatsReplyList);
1321 if (!portStatsReply.getFlags().contains(OFStatsReplyFlags.REPLY_MORE)) {
Charles Chanb3ef0db2016-05-03 11:11:00 -07001322 List<OFPortStatsEntry> statsEntries = portStatsReplies.get(dpid);
1323 if (statsEntries != null) {
1324 pushPortMetrics(dpid, statsEntries);
1325 statsEntries.clear();
1326 }
Ray Milkey7abe50b2016-04-13 11:57:44 -07001327 }
yjimmyy646aa022016-07-05 12:09:50 -07001328 } else if (((OFStatsReply) msg).getStatsType() == OFStatsType.EXPERIMENTER) {
1329 OpenFlowSwitch sw = controller.getSwitch(dpid);
sangyun-han147e6f42016-07-26 17:14:41 +09001330 if (sw == null) {
1331 LOG.error("Switch {} is not found", dpid);
1332 break;
1333 }
yjimmyy646aa022016-07-05 12:09:50 -07001334 if (sw instanceof OpenFlowOpticalSwitch) {
1335 // Optical switch uses experimenter stats message to update power
1336 List<PortDescription> portDescs =
1337 ((OpenFlowOpticalSwitch) sw).processExpPortStats(msg);
1338 if (!portDescs.isEmpty()) {
1339 providerService.updatePorts(DeviceId.deviceId(Dpid.uri(dpid)), portDescs);
1340 }
1341 }
Ray Milkey7abe50b2016-04-13 11:57:44 -07001342 }
1343 break;
1344 case ERROR:
1345 if (((OFErrorMsg) msg).getErrType() == OFErrorType.PORT_MOD_FAILED) {
1346 LOG.error("port mod failed");
1347 }
1348 default:
1349 break;
1350 }
1351 } catch (IllegalStateException e) {
1352 // system is shutting down and the providerService is no longer
1353 // valid. Messages cannot be processed.
1354 }
1355 }
1356
1357 private void disable() {
1358 isDisabled = true;
sangho538108b2015-04-08 14:29:20 -07001359 }
alshabibf1216ed2014-09-03 11:53:54 -07001360 }
Saurav Dasa2d37502016-03-25 17:50:40 -07001361
1362
tomb5a46e62014-08-26 14:20:00 -07001363}