blob: 18a714d5cc63cc616371b461e64b9b325208e1e4 [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 Open Networking Laboratory
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
Yafit Hadara9a73de2015-09-06 13:52:52 +030018import static com.google.common.base.Preconditions.checkArgument;
19import static com.google.common.base.Strings.isNullOrEmpty;
20import static org.onlab.util.Tools.get;
21import static org.onosproject.net.DeviceId.deviceId;
22import static org.onosproject.net.Port.Type.COPPER;
23import static org.onosproject.net.Port.Type.FIBER;
24import static org.onosproject.openflow.controller.Dpid.dpid;
25import static org.onosproject.openflow.controller.Dpid.uri;
26import static org.slf4j.LoggerFactory.getLogger;
27
28import java.util.ArrayList;
29import java.util.Collection;
30import java.util.Collections;
31import java.util.Dictionary;
32import java.util.HashMap;
33import java.util.HashSet;
34import java.util.List;
35
tomb5a46e62014-08-26 14:20:00 -070036import org.apache.felix.scr.annotations.Activate;
37import org.apache.felix.scr.annotations.Component;
38import org.apache.felix.scr.annotations.Deactivate;
Dusan Pajinbab8a5e2015-07-24 17:37:19 +020039import org.apache.felix.scr.annotations.Modified;
40import org.apache.felix.scr.annotations.Property;
tomb5a46e62014-08-26 14:20:00 -070041import org.apache.felix.scr.annotations.Reference;
42import org.apache.felix.scr.annotations.ReferenceCardinality;
Thomas Vachuskaf0397b52015-05-29 13:50:17 -070043import org.onlab.packet.ChassisId;
Marc De Leenheerb9311372015-07-09 11:36:49 -070044import org.onlab.util.Frequency;
45import org.onlab.util.Spectrum;
Yafit Hadara9a73de2015-09-06 13:52:52 +030046import org.onosproject.cfg.ComponentConfigService;
Thomas Vachuskab52a0142015-04-21 17:48:15 -070047import org.onosproject.net.AnnotationKeys;
Marc De Leenheerfc913dd2015-07-30 16:04:55 -070048import org.onosproject.net.ChannelSpacing;
Brian O'Connorabafb502014-12-02 22:26:20 -080049import org.onosproject.net.DefaultAnnotations;
Yafit Hadara9a73de2015-09-06 13:52:52 +030050import org.onosproject.net.Device;
Brian O'Connorabafb502014-12-02 22:26:20 -080051import org.onosproject.net.DeviceId;
Marc De Leenheerfc913dd2015-07-30 16:04:55 -070052import org.onosproject.net.GridType;
Brian O'Connorabafb502014-12-02 22:26:20 -080053import org.onosproject.net.MastershipRole;
Marc De Leenheerfc913dd2015-07-30 16:04:55 -070054import org.onosproject.net.OchSignal;
Yafit Hadara9a73de2015-09-06 13:52:52 +030055import org.onosproject.net.OduCltPort;
Marc De Leenheerfc913dd2015-07-30 16:04:55 -070056import org.onosproject.net.OduSignalType;
Brian O'Connorabafb502014-12-02 22:26:20 -080057import org.onosproject.net.Port;
58import org.onosproject.net.PortNumber;
59import org.onosproject.net.SparseAnnotations;
60import org.onosproject.net.device.DefaultDeviceDescription;
61import org.onosproject.net.device.DefaultPortDescription;
sangho538108b2015-04-08 14:29:20 -070062import org.onosproject.net.device.DefaultPortStatistics;
Brian O'Connorabafb502014-12-02 22:26:20 -080063import org.onosproject.net.device.DeviceDescription;
64import org.onosproject.net.device.DeviceProvider;
65import org.onosproject.net.device.DeviceProviderRegistry;
66import org.onosproject.net.device.DeviceProviderService;
Marc De Leenheerfc913dd2015-07-30 16:04:55 -070067import org.onosproject.net.device.OchPortDescription;
Yafit Hadara9a73de2015-09-06 13:52:52 +030068import org.onosproject.net.device.OduCltPortDescription;
Marc De Leenheerb9311372015-07-09 11:36:49 -070069import org.onosproject.net.device.OmsPortDescription;
Brian O'Connorabafb502014-12-02 22:26:20 -080070import org.onosproject.net.device.PortDescription;
sangho538108b2015-04-08 14:29:20 -070071import org.onosproject.net.device.PortStatistics;
Brian O'Connorabafb502014-12-02 22:26:20 -080072import org.onosproject.net.provider.AbstractProvider;
73import org.onosproject.net.provider.ProviderId;
74import org.onosproject.openflow.controller.Dpid;
75import org.onosproject.openflow.controller.OpenFlowController;
sangho538108b2015-04-08 14:29:20 -070076import org.onosproject.openflow.controller.OpenFlowEventListener;
Ayaka Koshibe5460d622015-05-14 12:19:19 -070077import org.onosproject.openflow.controller.OpenFlowOpticalSwitch;
Brian O'Connorabafb502014-12-02 22:26:20 -080078import org.onosproject.openflow.controller.OpenFlowSwitch;
79import org.onosproject.openflow.controller.OpenFlowSwitchListener;
Ayaka Koshibe5460d622015-05-14 12:19:19 -070080import org.onosproject.openflow.controller.PortDescPropertyType;
Brian O'Connorabafb502014-12-02 22:26:20 -080081import org.onosproject.openflow.controller.RoleState;
Dusan Pajinbab8a5e2015-07-24 17:37:19 +020082import org.osgi.service.component.ComponentContext;
Marc De Leenheerb9311372015-07-09 11:36:49 -070083import org.projectfloodlight.openflow.protocol.OFCalientPortDescStatsEntry;
Yafit Hadara9a73de2015-09-06 13:52:52 +030084import org.projectfloodlight.openflow.protocol.OFExpPort;
85import org.projectfloodlight.openflow.protocol.OFExpPortDescPropOpticalTransport;
86import org.projectfloodlight.openflow.protocol.OFExpPortOpticalTransportLayerEntry;
Ayaka Koshibee8708e32014-10-22 13:40:18 -070087import org.projectfloodlight.openflow.protocol.OFFactory;
sangho538108b2015-04-08 14:29:20 -070088import org.projectfloodlight.openflow.protocol.OFMessage;
Yafit Hadara9a73de2015-09-06 13:52:52 +030089import org.projectfloodlight.openflow.protocol.OFObject;
alshabib4680bb62014-09-04 17:15:08 -070090import org.projectfloodlight.openflow.protocol.OFPortConfig;
alshabib25c8eec2014-09-04 16:41:31 -070091import org.projectfloodlight.openflow.protocol.OFPortDesc;
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;
Ayaka Koshibe5460d622015-05-14 12:19:19 -070094import org.projectfloodlight.openflow.protocol.OFPortOptical;
Yafit Hadara9a73de2015-09-06 13:52:52 +030095import org.projectfloodlight.openflow.protocol.OFPortOpticalTransportLayerClass;
96import org.projectfloodlight.openflow.protocol.OFPortOpticalTransportSignalType;
alshabibafc514a2014-12-01 14:44:05 -080097import org.projectfloodlight.openflow.protocol.OFPortReason;
alshabib4680bb62014-09-04 17:15:08 -070098import org.projectfloodlight.openflow.protocol.OFPortState;
sangho538108b2015-04-08 14:29:20 -070099import org.projectfloodlight.openflow.protocol.OFPortStatsEntry;
100import 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;
106import org.projectfloodlight.openflow.types.PortSpeed;
tomb5a46e62014-08-26 14:20:00 -0700107import org.slf4j.Logger;
tom5f38b3a2014-08-27 23:50:54 -0700108
Yafit Hadara9a73de2015-09-06 13:52:52 +0300109import com.google.common.base.Strings;
110import com.google.common.collect.Lists;
111import com.google.common.collect.Maps;
112import com.google.common.collect.Sets;
tom782a7cf2014-09-11 23:58:38 -0700113
tomb5a46e62014-08-26 14:20:00 -0700114/**
tomb1260e42014-08-26 18:39:57 -0700115 * Provider which uses an OpenFlow controller to detect network
tome06f8552014-08-26 16:58:42 -0700116 * infrastructure devices.
tomb5a46e62014-08-26 14:20:00 -0700117 */
tomb1260e42014-08-26 18:39:57 -0700118@Component(immediate = true)
tomab21e7c2014-08-26 15:23:08 -0700119public class OpenFlowDeviceProvider extends AbstractProvider implements DeviceProvider {
tomb5a46e62014-08-26 14:20:00 -0700120
alshabiba89cc582014-09-09 16:43:00 -0700121 private static final Logger LOG = getLogger(OpenFlowDeviceProvider.class);
Thomas Vachuskad07c0922015-10-06 14:48:06 -0700122
Charles Chan25b77322015-12-13 01:00:56 -0800123 //TODO consider renaming KBPS and MBPS (as they are used to convert by division)
124 private static final long KBPS = 1_000;
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700125 private static final long MBPS = 1_000 * 1_000;
Yafit Hadara9a73de2015-09-06 13:52:52 +0300126 private static final Frequency FREQ100 = Frequency.ofGHz(100);
Sho SHIMIZU00762ee2016-01-05 16:32:24 -0800127 private static final Frequency FREQ4_4 = Frequency.ofGHz(4_400);
tomb5a46e62014-08-26 14:20:00 -0700128
129 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
tom96dfcab2014-08-28 09:26:03 -0700130 protected DeviceProviderRegistry providerRegistry;
tomab21e7c2014-08-26 15:23:08 -0700131
tom5f38b3a2014-08-27 23:50:54 -0700132 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
133 protected OpenFlowController controller;
134
Dusan Pajinbab8a5e2015-07-24 17:37:19 +0200135 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
136 protected ComponentConfigService cfgService;
137
tomab21e7c2014-08-26 15:23:08 -0700138 private DeviceProviderService providerService;
tomb5a46e62014-08-26 14:20:00 -0700139
sangho538108b2015-04-08 14:29:20 -0700140 private final InternalDeviceProvider listener = new InternalDeviceProvider();
141
Jian Li152b8852015-12-07 14:47:25 -0800142 private final IncomingMessageProvider inMsgListener = new IncomingMessageProvider();
143
144 private final OutgoingMessageProvider outMsgListener = new OutgoingMessageProvider();
145
146 private boolean isCtrlMsgMonitor;
147
sangho538108b2015-04-08 14:29:20 -0700148 // TODO: We need to make the poll interval configurable.
Srikanth Vavilapalli78baf582015-06-05 11:40:14 -0700149 static final int POLL_INTERVAL = 5;
Dusan Pajinbab8a5e2015-07-24 17:37:19 +0200150 @Property(name = "PortStatsPollFrequency", intValue = POLL_INTERVAL,
151 label = "Frequency (in seconds) for polling switch Port statistics")
152 private int portStatsPollFrequency = POLL_INTERVAL;
sangho538108b2015-04-08 14:29:20 -0700153
154 private HashMap<Dpid, PortStatsCollector> collectors = Maps.newHashMap();
tomd40fc7a2014-09-04 16:41:10 -0700155
tomab21e7c2014-08-26 15:23:08 -0700156 /**
157 * Creates an OpenFlow device provider.
158 */
159 public OpenFlowDeviceProvider() {
Brian O'Connorabafb502014-12-02 22:26:20 -0800160 super(new ProviderId("of", "org.onosproject.provider.openflow"));
tomab21e7c2014-08-26 15:23:08 -0700161 }
162
tomb5a46e62014-08-26 14:20:00 -0700163 @Activate
Dusan Pajinbab8a5e2015-07-24 17:37:19 +0200164 public void activate(ComponentContext context) {
165 cfgService.registerProperties(getClass());
tom96dfcab2014-08-28 09:26:03 -0700166 providerService = providerRegistry.register(this);
tomd40fc7a2014-09-04 16:41:10 -0700167 controller.addListener(listener);
sangho538108b2015-04-08 14:29:20 -0700168 controller.addEventListener(listener);
Jian Li152b8852015-12-07 14:47:25 -0800169
Thomas Vachuskad07c0922015-10-06 14:48:06 -0700170 connectInitialDevices();
alshabiba89cc582014-09-09 16:43:00 -0700171 LOG.info("Started");
tomb5a46e62014-08-26 14:20:00 -0700172 }
173
174 @Deactivate
Dusan Pajinbab8a5e2015-07-24 17:37:19 +0200175 public void deactivate(ComponentContext context) {
176 cfgService.unregisterProperties(getClass(), false);
tomd40fc7a2014-09-04 16:41:10 -0700177 controller.removeListener(listener);
Thomas Vachuskad07c0922015-10-06 14:48:06 -0700178 providerRegistry.unregister(this);
Thomas Vachuska74bcd922015-06-02 13:08:57 -0700179 collectors.values().forEach(PortStatsCollector::stop);
tomab21e7c2014-08-26 15:23:08 -0700180 providerService = null;
alshabiba89cc582014-09-09 16:43:00 -0700181 LOG.info("Stopped");
tomb5a46e62014-08-26 14:20:00 -0700182 }
183
Dusan Pajinbab8a5e2015-07-24 17:37:19 +0200184 @Modified
185 public void modified(ComponentContext context) {
186 Dictionary<?, ?> properties = context.getProperties();
187 int newPortStatsPollFrequency;
188 try {
189 String s = get(properties, "PortStatsPollFrequency");
190 newPortStatsPollFrequency = isNullOrEmpty(s) ? portStatsPollFrequency : Integer.parseInt(s.trim());
191
192 } catch (NumberFormatException | ClassCastException e) {
193 newPortStatsPollFrequency = portStatsPollFrequency;
194 }
195
196 if (newPortStatsPollFrequency != portStatsPollFrequency) {
197 portStatsPollFrequency = newPortStatsPollFrequency;
198 collectors.values().forEach(psc -> psc.adjustPollInterval(portStatsPollFrequency));
199 }
200
201 LOG.info("Settings: portStatsPollFrequency={}", portStatsPollFrequency);
202 }
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700203
Thomas Vachuskad07c0922015-10-06 14:48:06 -0700204 private void connectInitialDevices() {
205 for (OpenFlowSwitch sw : controller.getSwitches()) {
206 try {
207 listener.switchAdded(new Dpid(sw.getId()));
208 } catch (Exception e) {
209 LOG.warn("Failed initially adding {} : {}", sw.getStringId(), e.getMessage());
210 LOG.debug("Error details:", e);
211 // disconnect to trigger switch-add later
212 sw.disconnectSwitch();
213 }
214 PortStatsCollector psc = new PortStatsCollector(sw, portStatsPollFrequency);
215 psc.start();
216 collectors.put(new Dpid(sw.getId()), psc);
217 }
218 }
219
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700220 @Override
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700221 public boolean isReachable(DeviceId deviceId) {
222 OpenFlowSwitch sw = controller.getSwitch(dpid(deviceId.uri()));
Thomas Vachuskad07c0922015-10-06 14:48:06 -0700223 return sw != null && sw.isConnected();
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700224 }
225
tomab21e7c2014-08-26 15:23:08 -0700226 @Override
Ayaka Koshibe78bcbc12014-11-19 14:28:58 -0800227 public void triggerProbe(DeviceId deviceId) {
Madan Jampanic3328762015-05-31 12:39:27 -0700228 LOG.debug("Triggering probe on device {}", deviceId);
Ayaka Koshibee8708e32014-10-22 13:40:18 -0700229
Yuta HIGUCHI802d0e82014-10-31 16:07:37 -0700230 final Dpid dpid = dpid(deviceId.uri());
231 OpenFlowSwitch sw = controller.getSwitch(dpid);
232 if (sw == null || !sw.isConnected()) {
Ayaka Koshibe78bcbc12014-11-19 14:28:58 -0800233 LOG.error("Failed to probe device {} on sw={}", deviceId, sw);
Yuta HIGUCHI802d0e82014-10-31 16:07:37 -0700234 providerService.deviceDisconnected(deviceId);
Thomas Vachuska74bcd922015-06-02 13:08:57 -0700235 return;
Yuta HIGUCHI802d0e82014-10-31 16:07:37 -0700236 } else {
Ayaka Koshibe78bcbc12014-11-19 14:28:58 -0800237 LOG.trace("Confirmed device {} connection", deviceId);
Yuta HIGUCHI802d0e82014-10-31 16:07:37 -0700238 }
Ayaka Koshibee8708e32014-10-22 13:40:18 -0700239
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700240 // Prompt an update of port information. We can use any XID for this.
Ayaka Koshibee8708e32014-10-22 13:40:18 -0700241 OFFactory fact = sw.factory();
242 switch (fact.getVersion()) {
243 case OF_10:
244 sw.sendMsg(fact.buildFeaturesRequest().setXid(0).build());
245 break;
246 case OF_13:
247 sw.sendMsg(fact.buildPortDescStatsRequest().setXid(0).build());
248 break;
249 default:
250 LOG.warn("Unhandled protocol version");
251 }
tomab21e7c2014-08-26 15:23:08 -0700252 }
253
254 @Override
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700255 public void roleChanged(DeviceId deviceId, MastershipRole newRole) {
alshabibf1216ed2014-09-03 11:53:54 -0700256 switch (newRole) {
tom782a7cf2014-09-11 23:58:38 -0700257 case MASTER:
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700258 controller.setRole(dpid(deviceId.uri()), RoleState.MASTER);
tom782a7cf2014-09-11 23:58:38 -0700259 break;
260 case STANDBY:
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700261 controller.setRole(dpid(deviceId.uri()), RoleState.EQUAL);
tom782a7cf2014-09-11 23:58:38 -0700262 break;
263 case NONE:
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700264 controller.setRole(dpid(deviceId.uri()), RoleState.SLAVE);
tom782a7cf2014-09-11 23:58:38 -0700265 break;
266 default:
267 LOG.error("Unknown Mastership state : {}", newRole);
alshabibf1216ed2014-09-03 11:53:54 -0700268
269 }
HIGUCHI Yuta1979f552015-12-28 21:24:26 -0800270 LOG.debug("Accepting mastership role change to {} for device {}", newRole, deviceId);
tomab21e7c2014-08-26 15:23:08 -0700271 }
272
Jian Li152b8852015-12-07 14:47:25 -0800273
274 /**
275 * Enable OpenFlow control message monitoring.
276 */
277 public void enableCtrlMsgMonitor() {
278 isCtrlMsgMonitor = true;
279 controller.addEventListener(inMsgListener);
280 for (OpenFlowSwitch sw : controller.getSwitches()) {
281 sw.addEventListener(outMsgListener);
282 }
283 LOG.info("Enable control message monitoring.");
284 }
285
286 /**
287 * Disable OpenFlow control message monitoring.
288 */
289 public void disableCtrlMsgMonitor() {
290 isCtrlMsgMonitor = false;
291 controller.removeEventListener(inMsgListener);
292 for (OpenFlowSwitch sw: controller.getSwitches()) {
293 sw.removeEventListener(outMsgListener);
294 }
295 LOG.info("Disable control message monitoring");
296 }
297
Thomas Vachuska893bf4b2015-05-29 18:13:18 -0700298 private void pushPortMetrics(Dpid dpid, List<OFPortStatsEntry> portStatsEntries) {
HIGUCHI Yuta1979f552015-12-28 21:24:26 -0800299 DeviceId deviceId = DeviceId.deviceId(Dpid.uri(dpid));
Thomas Vachuska893bf4b2015-05-29 18:13:18 -0700300 Collection<PortStatistics> stats = buildPortStatistics(deviceId, portStatsEntries);
sangho538108b2015-04-08 14:29:20 -0700301 providerService.updatePortStatistics(deviceId, stats);
302 }
303
Thomas Vachuska893bf4b2015-05-29 18:13:18 -0700304 private Collection<PortStatistics> buildPortStatistics(DeviceId deviceId,
305 List<OFPortStatsEntry> entries) {
sangho538108b2015-04-08 14:29:20 -0700306 HashSet<PortStatistics> stats = Sets.newHashSet();
307
Thomas Vachuska893bf4b2015-05-29 18:13:18 -0700308 for (OFPortStatsEntry entry : entries) {
Thomas Vachuskaf0397b52015-05-29 13:50:17 -0700309 try {
Thomas Vachuska4e6025e2015-06-03 11:25:21 -0700310 if (entry == null || entry.getPortNo() == null || entry.getPortNo().getPortNumber() < 0) {
Thomas Vachuskaf0397b52015-05-29 13:50:17 -0700311 continue;
312 }
313 DefaultPortStatistics.Builder builder = DefaultPortStatistics.builder();
314 DefaultPortStatistics stat = builder.setDeviceId(deviceId)
315 .setPort(entry.getPortNo().getPortNumber())
316 .setPacketsReceived(entry.getRxPackets().getValue())
317 .setPacketsSent(entry.getTxPackets().getValue())
318 .setBytesReceived(entry.getRxBytes().getValue())
319 .setBytesSent(entry.getTxBytes().getValue())
320 .setPacketsRxDropped(entry.getRxDropped().getValue())
321 .setPacketsTxDropped(entry.getTxDropped().getValue())
322 .setPacketsRxErrors(entry.getRxErrors().getValue())
323 .setPacketsTxErrors(entry.getTxErrors().getValue())
324 .setDurationSec(entry.getVersion() == OFVersion.OF_10 ? 0 : entry.getDurationSec())
325 .setDurationNano(entry.getVersion() == OFVersion.OF_10 ? 0 : entry.getDurationNsec())
326 .build();
sangho538108b2015-04-08 14:29:20 -0700327
Thomas Vachuskaf0397b52015-05-29 13:50:17 -0700328 stats.add(stat);
329 } catch (Exception e) {
330 LOG.warn("Unable to process port stats", e);
331 }
sangho538108b2015-04-08 14:29:20 -0700332 }
333
334 return Collections.unmodifiableSet(stats);
335
336 }
337
Jian Li152b8852015-12-07 14:47:25 -0800338 /**
339 * A listener for incoming OpenFlow messages.
340 */
341 private class IncomingMessageProvider implements OpenFlowEventListener {
342
343 @Override
344 public void handleMessage(Dpid dpid, OFMessage msg) {
345 if (isCtrlMsgMonitor) {
346 // TODO: feed the control message stats via ControlMetricsServiceFactory
347 }
348 }
349 }
350
351 /**
352 * A listener for outgoing OpenFlow messages.
353 */
354 private class OutgoingMessageProvider implements OpenFlowEventListener {
355
356 @Override
357 public void handleMessage(Dpid dpid, OFMessage msg) {
358 if (isCtrlMsgMonitor) {
359 // TODO: feed the control message stats via ControlMetricsServiceFactory
360 }
361 }
362 }
363
sangho538108b2015-04-08 14:29:20 -0700364 private class InternalDeviceProvider implements OpenFlowSwitchListener, OpenFlowEventListener {
Thomas Vachuska893bf4b2015-05-29 18:13:18 -0700365
Srikanth Vavilapalli78baf582015-06-05 11:40:14 -0700366 private HashMap<Dpid, List<OFPortStatsEntry>> portStatsReplies = new HashMap<>();
Thomas Vachuska893bf4b2015-05-29 18:13:18 -0700367
alshabibf1216ed2014-09-03 11:53:54 -0700368 @Override
tomd1900f32014-09-03 14:08:16 -0700369 public void switchAdded(Dpid dpid) {
alshabib6f5460b2014-09-03 14:46:17 -0700370 if (providerService == null) {
371 return;
372 }
tom782a7cf2014-09-11 23:58:38 -0700373 DeviceId did = deviceId(uri(dpid));
alshabib6f5460b2014-09-03 14:46:17 -0700374 OpenFlowSwitch sw = controller.getSwitch(dpid);
Lei Xudee1aff2015-10-16 00:45:10 -0500375 if (sw == null) {
376 return;
377 }
alshabib6f5460b2014-09-03 14:46:17 -0700378
Jian Li152b8852015-12-07 14:47:25 -0800379 if (isCtrlMsgMonitor) {
380 // start to monitor the outgoing control messages
381 sw.addEventListener(outMsgListener);
382 }
383
alshabib7911a052014-10-16 17:49:37 -0700384 ChassisId cId = new ChassisId(dpid.value());
Ray Milkeye53f1712015-01-16 09:17:16 -0800385
Thomas Vachuska82041f52014-11-30 22:14:02 -0800386 SparseAnnotations annotations = DefaultAnnotations.builder()
andreafe3308f2015-10-06 15:51:25 -0700387 .set(AnnotationKeys.PROTOCOL, sw.factory().getVersion().toString())
388 .set(AnnotationKeys.CHANNEL_ID, sw.channelId())
389 .set(AnnotationKeys.MANAGEMENT_ADDRESS, sw.channelId().split(":")[0])
Ray Milkeye53f1712015-01-16 09:17:16 -0800390 .build();
391
tomd1900f32014-09-03 14:08:16 -0700392 DeviceDescription description =
Marc De Leenheerb9311372015-07-09 11:36:49 -0700393 new DefaultDeviceDescription(did.uri(), sw.deviceType(),
Ray Milkeyd3edd032015-01-16 11:38:58 -0800394 sw.manufacturerDescription(),
tom782a7cf2014-09-11 23:58:38 -0700395 sw.hardwareDescription(),
396 sw.softwareDescription(),
alshabib7911a052014-10-16 17:49:37 -0700397 sw.serialNumber(),
Thomas Vachuska82041f52014-11-30 22:14:02 -0800398 cId, annotations);
tom782a7cf2014-09-11 23:58:38 -0700399 providerService.deviceConnected(did, description);
Ayaka Koshibe5460d622015-05-14 12:19:19 -0700400 providerService.updatePorts(did, buildPortDescriptions(sw));
sangho538108b2015-04-08 14:29:20 -0700401
Thomas Vachuskafc52fec2015-05-18 19:13:56 -0700402 PortStatsCollector psc =
Lei Xudee1aff2015-10-16 00:45:10 -0500403 new PortStatsCollector(sw, portStatsPollFrequency);
sangho538108b2015-04-08 14:29:20 -0700404 psc.start();
405 collectors.put(dpid, psc);
Lei Xudee1aff2015-10-16 00:45:10 -0500406
407 //figure out race condition for collectors.remove() and collectors.put()
408 if (controller.getSwitch(dpid) == null) {
409 switchRemoved(dpid);
410 }
alshabib25c8eec2014-09-04 16:41:31 -0700411 }
412
alshabibf1216ed2014-09-03 11:53:54 -0700413 @Override
414 public void switchRemoved(Dpid dpid) {
alshabib6f5460b2014-09-03 14:46:17 -0700415 if (providerService == null) {
416 return;
417 }
tom782a7cf2014-09-11 23:58:38 -0700418 providerService.deviceDisconnected(deviceId(uri(dpid)));
alshabibf1216ed2014-09-03 11:53:54 -0700419
sangho538108b2015-04-08 14:29:20 -0700420 PortStatsCollector collector = collectors.remove(dpid);
421 if (collector != null) {
422 collector.stop();
423 }
Jian Li152b8852015-12-07 14:47:25 -0800424
425 OpenFlowSwitch sw = controller.getSwitch(dpid);
426 if (sw != null) {
427 if (isCtrlMsgMonitor) {
428 // stop monitoring the outgoing control messages
429 sw.removeEventListener(outMsgListener);
430 }
431 }
sangho538108b2015-04-08 14:29:20 -0700432 }
Ayaka Koshibe38594c22014-10-22 13:36:12 -0700433
434 @Override
435 public void switchChanged(Dpid dpid) {
HIGUCHI Yuta1979f552015-12-28 21:24:26 -0800436 LOG.debug("switchChanged({})", dpid);
Ayaka Koshibe38594c22014-10-22 13:36:12 -0700437 if (providerService == null) {
438 return;
439 }
440 DeviceId did = deviceId(uri(dpid));
441 OpenFlowSwitch sw = controller.getSwitch(dpid);
Lei Xudee1aff2015-10-16 00:45:10 -0500442 if (sw == null) {
443 return;
444 }
HIGUCHI Yuta1979f552015-12-28 21:24:26 -0800445 final List<PortDescription> ports = buildPortDescriptions(sw);
446 LOG.debug("switchChanged({}) {}", did, ports);
447 providerService.updatePorts(did, ports);
Ayaka Koshibe38594c22014-10-22 13:36:12 -0700448 }
449
alshabiba14f3642014-09-05 09:31:31 -0700450 @Override
451 public void portChanged(Dpid dpid, OFPortStatus status) {
HIGUCHI Yuta1979f552015-12-28 21:24:26 -0800452 LOG.debug("portChanged({},{})", dpid, status);
alshabibafc514a2014-12-01 14:44:05 -0800453 PortDescription portDescription = buildPortDescription(status);
tom782a7cf2014-09-11 23:58:38 -0700454 providerService.portStatusChanged(deviceId(uri(dpid)), portDescription);
alshabibf1216ed2014-09-03 11:53:54 -0700455 }
456
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700457 @Override
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700458 public void receivedRoleReply(Dpid dpid, RoleState requested, RoleState response) {
HIGUCHI Yuta1979f552015-12-28 21:24:26 -0800459 LOG.debug("receivedRoleReply({},{},{})", dpid, requested, response);
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700460 MastershipRole request = roleOf(requested);
461 MastershipRole reply = roleOf(response);
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700462 providerService.receivedRoleReply(deviceId(uri(dpid)), request, reply);
463 }
464
465 /**
466 * Translates a RoleState to the corresponding MastershipRole.
467 *
Thomas Vachuskafc52fec2015-05-18 19:13:56 -0700468 * @param response role state
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700469 * @return a MastershipRole
470 */
471 private MastershipRole roleOf(RoleState response) {
472 switch (response) {
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700473 case MASTER:
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700474 return MastershipRole.MASTER;
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700475 case EQUAL:
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700476 return MastershipRole.STANDBY;
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700477 case SLAVE:
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700478 return MastershipRole.NONE;
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700479 default:
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700480 LOG.warn("unknown role {}", response);
481 return null;
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700482 }
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700483 }
484
alshabiba14f3642014-09-05 09:31:31 -0700485 /**
486 * Builds a list of port descriptions for a given list of ports.
tomff7eb7c2014-09-08 12:49:03 -0700487 *
alshabiba14f3642014-09-05 09:31:31 -0700488 * @return list of portdescriptions
489 */
Ayaka Koshibe5460d622015-05-14 12:19:19 -0700490 private List<PortDescription> buildPortDescriptions(OpenFlowSwitch sw) {
491 final List<PortDescription> portDescs = new ArrayList<>(sw.getPorts().size());
Yafit Hadara9a73de2015-09-06 13:52:52 +0300492 if (!(Device.Type.ROADM.equals(sw.deviceType()))) {
493 sw.getPorts().forEach(port -> portDescs.add(buildPortDescription(port)));
494 }
Marc De Leenheerb9311372015-07-09 11:36:49 -0700495
496 OpenFlowOpticalSwitch opsw;
497 switch (sw.deviceType()) {
498 case ROADM:
499 opsw = (OpenFlowOpticalSwitch) sw;
Yafit Hadara9a73de2015-09-06 13:52:52 +0300500 List<OFPortDesc> ports = opsw.getPorts();
501 LOG.debug("SW ID {} , ETH- ODU CLT Ports {}", opsw.getId(), ports);
502 // ODU client ports are reported as ETH
503 ports.forEach(port -> portDescs.add(buildOduCltPortDescription(port)));
504
Marc De Leenheerb9311372015-07-09 11:36:49 -0700505 opsw.getPortTypes().forEach(type -> {
Yafit Hadara9a73de2015-09-06 13:52:52 +0300506 List<? extends OFObject> portsOf = opsw.getPortsOf(type);
507 LOG.debug("Ports Of{}", portsOf);
508 portsOf.forEach(
509 op -> {
HIGUCHI Yuta1979f552015-12-28 21:24:26 -0800510 portDescs.add(buildPortDescription(type, op));
Yafit Hadara9a73de2015-09-06 13:52:52 +0300511 }
512 );
Marc De Leenheerb9311372015-07-09 11:36:49 -0700513 });
514 break;
515 case FIBER_SWITCH:
516 opsw = (OpenFlowOpticalSwitch) sw;
517 opsw.getPortTypes().forEach(type -> {
518 opsw.getPortsOf(type).forEach(
519 op -> {
520 portDescs.add(buildPortDescription((OFCalientPortDescStatsEntry) op));
521 }
522 );
523 });
524 break;
525 default:
526 break;
alshabib4680bb62014-09-04 17:15:08 -0700527 }
Marc De Leenheerb9311372015-07-09 11:36:49 -0700528
alshabib4680bb62014-09-04 17:15:08 -0700529 return portDescs;
530 }
531
Yafit Hadara9a73de2015-09-06 13:52:52 +0300532 private PortDescription buildOduCltPortDescription(OFPortDesc port) {
533 PortNumber portNo = PortNumber.portNumber(port.getPortNo().getPortNumber());
534 boolean enabled = !port.getState().contains(OFPortState.LINK_DOWN) &&
535 !port.getConfig().contains(OFPortConfig.PORT_DOWN);
Rimon Ashkenazyd15ebf52016-01-06 14:36:13 +0200536 Long portSpeedInMbps = portSpeed(port);
Yafit Hadara9a73de2015-09-06 13:52:52 +0300537 OduCltPort.SignalType sigType = null;
538
Rimon Ashkenazyd15ebf52016-01-06 14:36:13 +0200539 switch (portSpeedInMbps.toString()) {
540 case "1000":
Yafit Hadara9a73de2015-09-06 13:52:52 +0300541 sigType = OduCltPort.SignalType.CLT_1GBE;
542 break;
Rimon Ashkenazyd15ebf52016-01-06 14:36:13 +0200543 case "10000":
Yafit Hadara9a73de2015-09-06 13:52:52 +0300544 sigType = OduCltPort.SignalType.CLT_10GBE;
545 break;
Rimon Ashkenazyd15ebf52016-01-06 14:36:13 +0200546 case "40000":
Yafit Hadara9a73de2015-09-06 13:52:52 +0300547 sigType = OduCltPort.SignalType.CLT_40GBE;
548 break;
Rimon Ashkenazyd15ebf52016-01-06 14:36:13 +0200549 case "100000":
Yafit Hadara9a73de2015-09-06 13:52:52 +0300550 sigType = OduCltPort.SignalType.CLT_100GBE;
551 break;
552 default:
Rimon Ashkenazyd15ebf52016-01-06 14:36:13 +0200553 throw new RuntimeException("Un recognize OduClt speed: " + portSpeedInMbps.toString());
Yafit Hadara9a73de2015-09-06 13:52:52 +0300554 }
555
556 SparseAnnotations annotations = buildOduCltAnnotation(port);
557 return new OduCltPortDescription(portNo, enabled, sigType, annotations);
558 }
559
560 private SparseAnnotations buildOduCltAnnotation(OFPortDesc port) {
561 SparseAnnotations annotations = null;
562 String portName = Strings.emptyToNull(port.getName());
563 if (portName != null) {
564 annotations = DefaultAnnotations.builder()
565 .set(AnnotationKeys.PORT_NAME, portName)
566 .set(AnnotationKeys.STATIC_PORT, Boolean.TRUE.toString()).build();
567 }
568 return annotations;
569 }
570
571 private PortDescription buildPortDescription(PortDescPropertyType ptype, OFObject port) {
572 if (port instanceof OFPortOptical) {
573 return buildPortDescription(ptype, (OFPortOptical) port);
574 }
575 return buildPortDescription(ptype, (OFExpPort) port);
576 }
577
578 /**
579 * Build a portDescription from a given a port description describing some
580 * Optical port.
581 *
582 * @param ptype description property type.
583 * @param port the port to build from.
584 * @return portDescription for the port.
585 */
586 private PortDescription buildPortDescription(PortDescPropertyType ptype, OFExpPort port) {
587 PortNumber portNo = PortNumber.portNumber(port.getPortNo().getPortNumber());
588 boolean enabled = !port.getState().contains(OFPortState.LINK_DOWN)
589 && !port.getConfig().contains(OFPortConfig.PORT_DOWN);
lishuai4ee42042015-11-30 17:19:21 +0800590 SparseAnnotations annotations = makePortAnnotation(port.getName(), port.getHwAddr().toString());
Yafit Hadara9a73de2015-09-06 13:52:52 +0300591
592 OFExpPortDescPropOpticalTransport firstProp = port.getProperties().get(0);
593 OFPortOpticalTransportSignalType sigType = firstProp.getPortSignalType();
594
595 DefaultPortDescription portDes = null;
596 switch (sigType) {
597 case OMSN:
Sho SHIMIZU70e76d82016-01-05 16:26:26 -0800598 portDes = new OmsPortDescription(portNo, enabled,
599 Spectrum.CENTER_FREQUENCY, Spectrum.CENTER_FREQUENCY.add(FREQ4_4), FREQ100, annotations);
Yafit Hadara9a73de2015-09-06 13:52:52 +0300600 break;
601 case OCH:
602 OFExpPortOpticalTransportLayerEntry entry = firstProp.getFeatures().get(0).getValue().get(0);
603 OFPortOpticalTransportLayerClass layerClass = entry.getLayerClass();
604 if (!OFPortOpticalTransportLayerClass.ODU.equals(layerClass)) {
605 LOG.error("Unsupported layer Class {} ", layerClass);
606 return null;
607 }
608
609 // convert to ONOS OduSignalType
610 OduSignalType oduSignalType = OpenFlowDeviceValueMapper.
611 lookupOduSignalType((byte) entry.getSignalType());
612 //OchSignal is needed for OchPortDescription constructor,
613 //yet not relevant for tunable OCH port, creating with default parameters
614 OchSignal signalId = new OchSignal(GridType.DWDM, ChannelSpacing.CHL_50GHZ, 1, 1);
615
616 portDes = new OchPortDescription(portNo, enabled,
617 oduSignalType, true, signalId, annotations);
618
619 break;
620 case OTU2:
621 case OTU4:
622 LOG.error("Signal tpye OTU2/4 not supported yet ", port.toString());
623 break;
624 default:
625 break;
626 }
627
628 return portDes;
629 }
630
alshabiba14f3642014-09-05 09:31:31 -0700631 /**
Ray Milkeye0fade72015-01-15 13:29:47 -0800632 * Creates an annotation for the port name if one is available.
633 *
lishuai4ee42042015-11-30 17:19:21 +0800634 * @param portName the port name
635 * @param portMac the port mac
Ray Milkeye0fade72015-01-15 13:29:47 -0800636 * @return annotation containing the port name if one is found,
637 * null otherwise
638 */
lishuai4ee42042015-11-30 17:19:21 +0800639 private SparseAnnotations makePortAnnotation(String portName, String portMac) {
Ray Milkeye0fade72015-01-15 13:29:47 -0800640 SparseAnnotations annotations = null;
lishuai4ee42042015-11-30 17:19:21 +0800641 String pName = Strings.emptyToNull(portName);
642 String pMac = Strings.emptyToNull(portMac);
Ray Milkeye0fade72015-01-15 13:29:47 -0800643 if (portName != null) {
644 annotations = DefaultAnnotations.builder()
lishuai4ee42042015-11-30 17:19:21 +0800645 .set(AnnotationKeys.PORT_NAME, pName)
646 .set(AnnotationKeys.PORT_MAC, pMac).build();
Ray Milkeye0fade72015-01-15 13:29:47 -0800647 }
648 return annotations;
649 }
650
651 /**
Ayaka Koshibe5460d622015-05-14 12:19:19 -0700652 * Build a portDescription from a given Ethernet port description.
tomff7eb7c2014-09-08 12:49:03 -0700653 *
alshabiba14f3642014-09-05 09:31:31 -0700654 * @param port the port to build from.
655 * @return portDescription for the port.
656 */
657 private PortDescription buildPortDescription(OFPortDesc port) {
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700658 PortNumber portNo = PortNumber.portNumber(port.getPortNo().getPortNumber());
659 boolean enabled =
660 !port.getState().contains(OFPortState.LINK_DOWN) &&
661 !port.getConfig().contains(OFPortConfig.PORT_DOWN);
662 Port.Type type = port.getCurr().contains(OFPortFeatures.PF_FIBER) ? FIBER : COPPER;
lishuai4ee42042015-11-30 17:19:21 +0800663 SparseAnnotations annotations = makePortAnnotation(port.getName(), port.getHwAddr().toString());
Ray Milkeye0fade72015-01-15 13:29:47 -0800664 return new DefaultPortDescription(portNo, enabled, type,
665 portSpeed(port), annotations);
alshabiba14f3642014-09-05 09:31:31 -0700666 }
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700667
Ayaka Koshibe5460d622015-05-14 12:19:19 -0700668 /**
669 * Build a portDescription from a given a port description describing some
670 * Optical port.
671 *
672 * @param port description property type.
673 * @param port the port to build from.
674 * @return portDescription for the port.
675 */
676 private PortDescription buildPortDescription(PortDescPropertyType ptype, OFPortOptical port) {
Marc De Leenheerfc913dd2015-07-30 16:04:55 -0700677 checkArgument(port.getDesc().size() >= 1);
678
Ayaka Koshibe5460d622015-05-14 12:19:19 -0700679 // Minimally functional fixture. This needs to be fixed as we add better support.
680 PortNumber portNo = PortNumber.portNumber(port.getPortNo().getPortNumber());
Ayaka Koshibe74b55272015-05-28 15:16:04 -0700681
Ayaka Koshibe5460d622015-05-14 12:19:19 -0700682 boolean enabled = !port.getState().contains(OFPortState.LINK_DOWN)
683 && !port.getConfig().contains(OFPortConfig.PORT_DOWN);
lishuai4ee42042015-11-30 17:19:21 +0800684 SparseAnnotations annotations = makePortAnnotation(port.getName(), port.getHwAddr().toString());
Ayaka Koshibe5460d622015-05-14 12:19:19 -0700685
686 if (port.getVersion() == OFVersion.OF_13
687 && ptype == PortDescPropertyType.OPTICAL_TRANSPORT) {
688 // At this point, not much is carried in the optical port message.
Ayaka Koshibeae541732015-05-19 13:37:27 -0700689 LOG.debug("Optical transport port message {}", port.toString());
Ayaka Koshibe5460d622015-05-14 12:19:19 -0700690 } else {
691 // removable once 1.4+ support complete.
Ayaka Koshibeae541732015-05-19 13:37:27 -0700692 LOG.debug("Unsupported optical port properties");
Ayaka Koshibe5460d622015-05-14 12:19:19 -0700693 }
Marc De Leenheerfc913dd2015-07-30 16:04:55 -0700694
695 OFPortDescPropOpticalTransport desc = port.getDesc().get(0);
696 switch (desc.getPortSignalType()) {
697 // FIXME: use constants once loxi has full optical extensions
698 case 2: // OMS port
699 // Assume complete optical spectrum and 50 GHz grid
700 // LINC-OE is only supported optical OF device for now
701 return new OmsPortDescription(portNo, enabled,
702 Spectrum.U_BAND_MIN, Spectrum.O_BAND_MAX, Frequency.ofGHz(50), annotations);
703 case 5: // OCH port
704 OchSignal signal = new OchSignal(GridType.DWDM, ChannelSpacing.CHL_50GHZ, 0, 4);
705 return new OchPortDescription(portNo, enabled, OduSignalType.ODU4,
706 true, signal, annotations);
707 default:
708 break;
709 }
710
Ayaka Koshibe74b55272015-05-28 15:16:04 -0700711 return new DefaultPortDescription(portNo, enabled, FIBER, 0, annotations);
Ayaka Koshibe5460d622015-05-14 12:19:19 -0700712 }
713
Marc De Leenheerb9311372015-07-09 11:36:49 -0700714 /**
715 * Build a portDescription from a given port description describing a fiber switch optical port.
716 *
717 * @param port description property type.
718 * @param port the port to build from.
719 * @return portDescription for the port.
720 */
721 private PortDescription buildPortDescription(OFCalientPortDescStatsEntry port) {
722 PortNumber portNo = PortNumber.portNumber(port.getPortNo().getPortNumber());
723
724 // FIXME when Calient OF agent reports port status
725 boolean enabled = true;
lishuai4ee42042015-11-30 17:19:21 +0800726 SparseAnnotations annotations = makePortAnnotation(port.getName(), port.getHwAddr().toString());
Marc De Leenheerb9311372015-07-09 11:36:49 -0700727
Marc De Leenheerfc913dd2015-07-30 16:04:55 -0700728 // S160 data sheet
729 // Wavelength range: 1260 - 1630 nm, grid is irrelevant for this type of switch
730 return new OmsPortDescription(portNo, enabled,
731 Spectrum.U_BAND_MIN, Spectrum.O_BAND_MAX, Frequency.ofGHz(100), annotations);
Marc De Leenheerb9311372015-07-09 11:36:49 -0700732 }
733
alshabibafc514a2014-12-01 14:44:05 -0800734 private PortDescription buildPortDescription(OFPortStatus status) {
735 OFPortDesc port = status.getDesc();
736 if (status.getReason() != OFPortReason.DELETE) {
737 return buildPortDescription(port);
738 } else {
739 PortNumber portNo = PortNumber.portNumber(port.getPortNo().getPortNumber());
740 Port.Type type = port.getCurr().contains(OFPortFeatures.PF_FIBER) ? FIBER : COPPER;
lishuai4ee42042015-11-30 17:19:21 +0800741 SparseAnnotations annotations = makePortAnnotation(port.getName(), port.getHwAddr().toString());
Ray Milkeye0fade72015-01-15 13:29:47 -0800742 return new DefaultPortDescription(portNo, false, type,
743 portSpeed(port), annotations);
alshabibafc514a2014-12-01 14:44:05 -0800744 }
745 }
746
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700747 private long portSpeed(OFPortDesc port) {
748 if (port.getVersion() == OFVersion.OF_13) {
Charles Chan25b77322015-12-13 01:00:56 -0800749 // Note: getCurrSpeed() returns a value in kbps (this also applies to OF_11 and OF_12)
750 return port.getCurrSpeed() / KBPS;
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700751 }
752
753 PortSpeed portSpeed = PortSpeed.SPEED_NONE;
754 for (OFPortFeatures feat : port.getCurr()) {
755 portSpeed = PortSpeed.max(portSpeed, feat.getPortSpeed());
756 }
Thomas Vachuska98eda532014-10-29 17:31:02 -0700757 return portSpeed.getSpeedBps() / MBPS;
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700758 }
sangho538108b2015-04-08 14:29:20 -0700759
760 @Override
761 public void handleMessage(Dpid dpid, OFMessage msg) {
762 switch (msg.getType()) {
763 case STATS_REPLY:
764 if (((OFStatsReply) msg).getStatsType() == OFStatsType.PORT) {
Thomas Vachuska893bf4b2015-05-29 18:13:18 -0700765 OFPortStatsReply portStatsReply = (OFPortStatsReply) msg;
Srikanth Vavilapalli78baf582015-06-05 11:40:14 -0700766 List<OFPortStatsEntry> portStatsReplyList = portStatsReplies.get(dpid);
767 if (portStatsReplyList == null) {
768 portStatsReplyList = Lists.newArrayList();
769 }
770 portStatsReplyList.addAll(portStatsReply.getEntries());
771 portStatsReplies.put(dpid, portStatsReplyList);
Thomas Vachuska893bf4b2015-05-29 18:13:18 -0700772 if (!portStatsReply.getFlags().contains(OFStatsReplyFlags.REPLY_MORE)) {
Srikanth Vavilapalli78baf582015-06-05 11:40:14 -0700773 pushPortMetrics(dpid, portStatsReplies.get(dpid));
774 portStatsReplies.get(dpid).clear();
Thomas Vachuska893bf4b2015-05-29 18:13:18 -0700775 }
sangho538108b2015-04-08 14:29:20 -0700776 }
777 break;
778 default:
779 break;
780 }
781 }
alshabibf1216ed2014-09-03 11:53:54 -0700782 }
tomb5a46e62014-08-26 14:20:00 -0700783}