blob: d23817fd467932dd669026c6303b8603923ac421 [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 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
sangho538108b2015-04-08 14:29:20 -070018import com.google.common.collect.Maps;
19import com.google.common.collect.Sets;
tomb5a46e62014-08-26 14:20:00 -070020import org.apache.felix.scr.annotations.Activate;
21import org.apache.felix.scr.annotations.Component;
22import org.apache.felix.scr.annotations.Deactivate;
23import org.apache.felix.scr.annotations.Reference;
24import org.apache.felix.scr.annotations.ReferenceCardinality;
Brian O'Connorabafb502014-12-02 22:26:20 -080025import org.onosproject.net.DefaultAnnotations;
26import org.onosproject.net.Device;
27import org.onosproject.net.DeviceId;
28import org.onosproject.net.MastershipRole;
29import org.onosproject.net.Port;
30import org.onosproject.net.PortNumber;
31import org.onosproject.net.SparseAnnotations;
32import org.onosproject.net.device.DefaultDeviceDescription;
33import org.onosproject.net.device.DefaultPortDescription;
sangho538108b2015-04-08 14:29:20 -070034import org.onosproject.net.device.DefaultPortStatistics;
Brian O'Connorabafb502014-12-02 22:26:20 -080035import org.onosproject.net.device.DeviceDescription;
36import org.onosproject.net.device.DeviceProvider;
37import org.onosproject.net.device.DeviceProviderRegistry;
38import org.onosproject.net.device.DeviceProviderService;
39import org.onosproject.net.device.PortDescription;
sangho538108b2015-04-08 14:29:20 -070040import org.onosproject.net.device.PortStatistics;
Brian O'Connorabafb502014-12-02 22:26:20 -080041import org.onosproject.net.provider.AbstractProvider;
42import org.onosproject.net.provider.ProviderId;
43import org.onosproject.openflow.controller.Dpid;
44import org.onosproject.openflow.controller.OpenFlowController;
sangho538108b2015-04-08 14:29:20 -070045import org.onosproject.openflow.controller.OpenFlowEventListener;
Brian O'Connorabafb502014-12-02 22:26:20 -080046import org.onosproject.openflow.controller.OpenFlowSwitch;
47import org.onosproject.openflow.controller.OpenFlowSwitchListener;
48import org.onosproject.openflow.controller.RoleState;
alshabib7911a052014-10-16 17:49:37 -070049import org.onlab.packet.ChassisId;
Ayaka Koshibee8708e32014-10-22 13:40:18 -070050import org.projectfloodlight.openflow.protocol.OFFactory;
sangho538108b2015-04-08 14:29:20 -070051import org.projectfloodlight.openflow.protocol.OFMessage;
alshabib4680bb62014-09-04 17:15:08 -070052import org.projectfloodlight.openflow.protocol.OFPortConfig;
alshabib25c8eec2014-09-04 16:41:31 -070053import org.projectfloodlight.openflow.protocol.OFPortDesc;
Thomas Vachuskad16ce182014-10-29 17:25:29 -070054import org.projectfloodlight.openflow.protocol.OFPortFeatures;
alshabibafc514a2014-12-01 14:44:05 -080055import org.projectfloodlight.openflow.protocol.OFPortReason;
alshabib4680bb62014-09-04 17:15:08 -070056import org.projectfloodlight.openflow.protocol.OFPortState;
sangho538108b2015-04-08 14:29:20 -070057import org.projectfloodlight.openflow.protocol.OFPortStatsEntry;
58import org.projectfloodlight.openflow.protocol.OFPortStatsReply;
alshabiba14f3642014-09-05 09:31:31 -070059import org.projectfloodlight.openflow.protocol.OFPortStatus;
sangho538108b2015-04-08 14:29:20 -070060import org.projectfloodlight.openflow.protocol.OFStatsReply;
61import org.projectfloodlight.openflow.protocol.OFStatsType;
Thomas Vachuskad16ce182014-10-29 17:25:29 -070062import org.projectfloodlight.openflow.protocol.OFVersion;
63import org.projectfloodlight.openflow.types.PortSpeed;
tomb5a46e62014-08-26 14:20:00 -070064import org.slf4j.Logger;
tom5f38b3a2014-08-27 23:50:54 -070065
tom782a7cf2014-09-11 23:58:38 -070066import java.util.ArrayList;
sangho538108b2015-04-08 14:29:20 -070067import java.util.Collection;
68import java.util.Collections;
69import java.util.HashMap;
70import java.util.HashSet;
tom782a7cf2014-09-11 23:58:38 -070071import java.util.List;
72
Ray Milkeye0fade72015-01-15 13:29:47 -080073import com.google.common.base.Strings;
74
Brian O'Connorabafb502014-12-02 22:26:20 -080075import static org.onosproject.net.DeviceId.deviceId;
76import static org.onosproject.net.Port.Type.COPPER;
77import static org.onosproject.net.Port.Type.FIBER;
78import static org.onosproject.openflow.controller.Dpid.dpid;
79import static org.onosproject.openflow.controller.Dpid.uri;
tom782a7cf2014-09-11 23:58:38 -070080import static org.slf4j.LoggerFactory.getLogger;
81
tomb5a46e62014-08-26 14:20:00 -070082/**
tomb1260e42014-08-26 18:39:57 -070083 * Provider which uses an OpenFlow controller to detect network
tome06f8552014-08-26 16:58:42 -070084 * infrastructure devices.
tomb5a46e62014-08-26 14:20:00 -070085 */
tomb1260e42014-08-26 18:39:57 -070086@Component(immediate = true)
tomab21e7c2014-08-26 15:23:08 -070087public class OpenFlowDeviceProvider extends AbstractProvider implements DeviceProvider {
tomb5a46e62014-08-26 14:20:00 -070088
alshabiba89cc582014-09-09 16:43:00 -070089 private static final Logger LOG = getLogger(OpenFlowDeviceProvider.class);
Thomas Vachuskad16ce182014-10-29 17:25:29 -070090 private static final long MBPS = 1_000 * 1_000;
tomb5a46e62014-08-26 14:20:00 -070091
92 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
tom96dfcab2014-08-28 09:26:03 -070093 protected DeviceProviderRegistry providerRegistry;
tomab21e7c2014-08-26 15:23:08 -070094
tom5f38b3a2014-08-27 23:50:54 -070095 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
96 protected OpenFlowController controller;
97
tomab21e7c2014-08-26 15:23:08 -070098 private DeviceProviderService providerService;
tomb5a46e62014-08-26 14:20:00 -070099
sangho538108b2015-04-08 14:29:20 -0700100 private final InternalDeviceProvider listener = new InternalDeviceProvider();
101
102 // TODO: We need to make the poll interval configurable.
103 static final int POLL_INTERVAL = 10;
104
105 private HashMap<Dpid, PortStatsCollector> collectors = Maps.newHashMap();
tomd40fc7a2014-09-04 16:41:10 -0700106
tomab21e7c2014-08-26 15:23:08 -0700107 /**
108 * Creates an OpenFlow device provider.
109 */
110 public OpenFlowDeviceProvider() {
Brian O'Connorabafb502014-12-02 22:26:20 -0800111 super(new ProviderId("of", "org.onosproject.provider.openflow"));
tomab21e7c2014-08-26 15:23:08 -0700112 }
113
tomb5a46e62014-08-26 14:20:00 -0700114 @Activate
115 public void activate() {
tom96dfcab2014-08-28 09:26:03 -0700116 providerService = providerRegistry.register(this);
tomd40fc7a2014-09-04 16:41:10 -0700117 controller.addListener(listener);
sangho538108b2015-04-08 14:29:20 -0700118 controller.addEventListener(listener);
alshabibc944fd02014-09-10 17:55:17 -0700119 for (OpenFlowSwitch sw : controller.getSwitches()) {
Yuta HIGUCHIac2972a2014-11-18 12:58:52 -0800120 try {
121 listener.switchAdded(new Dpid(sw.getId()));
122 } catch (Exception e) {
123 LOG.warn("Failed initially adding {} : {}", sw.getStringId(), e.getMessage());
124 LOG.debug("Error details:", e);
125 // disconnect to trigger switch-add later
126 sw.disconnectSwitch();
127 }
sangho538108b2015-04-08 14:29:20 -0700128 PortStatsCollector psc = new PortStatsCollector(sw, POLL_INTERVAL);
129 psc.start();
130 collectors.put(new Dpid(sw.getId()), psc);
alshabibc944fd02014-09-10 17:55:17 -0700131 }
alshabiba89cc582014-09-09 16:43:00 -0700132 LOG.info("Started");
tomb5a46e62014-08-26 14:20:00 -0700133 }
134
135 @Deactivate
136 public void deactivate() {
tom96dfcab2014-08-28 09:26:03 -0700137 providerRegistry.unregister(this);
tomd40fc7a2014-09-04 16:41:10 -0700138 controller.removeListener(listener);
tomab21e7c2014-08-26 15:23:08 -0700139 providerService = null;
alshabibc944fd02014-09-10 17:55:17 -0700140
alshabiba89cc582014-09-09 16:43:00 -0700141 LOG.info("Stopped");
tomb5a46e62014-08-26 14:20:00 -0700142 }
143
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700144
145 @Override
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700146 public boolean isReachable(DeviceId deviceId) {
147 OpenFlowSwitch sw = controller.getSwitch(dpid(deviceId.uri()));
Yuta HIGUCHI69a27352014-10-31 15:48:37 -0700148 if (sw == null || !sw.isConnected()) {
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700149 return false;
150 }
151 return true;
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700152 }
153
tomab21e7c2014-08-26 15:23:08 -0700154 @Override
Ayaka Koshibe78bcbc12014-11-19 14:28:58 -0800155 public void triggerProbe(DeviceId deviceId) {
Yuta HIGUCHI802d0e82014-10-31 16:07:37 -0700156 LOG.info("Triggering probe on device {}", deviceId);
Ayaka Koshibee8708e32014-10-22 13:40:18 -0700157
Yuta HIGUCHI802d0e82014-10-31 16:07:37 -0700158 final Dpid dpid = dpid(deviceId.uri());
159 OpenFlowSwitch sw = controller.getSwitch(dpid);
160 if (sw == null || !sw.isConnected()) {
Ayaka Koshibe78bcbc12014-11-19 14:28:58 -0800161 LOG.error("Failed to probe device {} on sw={}", deviceId, sw);
Yuta HIGUCHI802d0e82014-10-31 16:07:37 -0700162 providerService.deviceDisconnected(deviceId);
163 } else {
Ayaka Koshibe78bcbc12014-11-19 14:28:58 -0800164 LOG.trace("Confirmed device {} connection", deviceId);
Yuta HIGUCHI802d0e82014-10-31 16:07:37 -0700165 }
Ayaka Koshibee8708e32014-10-22 13:40:18 -0700166
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700167 // Prompt an update of port information. We can use any XID for this.
Ayaka Koshibee8708e32014-10-22 13:40:18 -0700168 OFFactory fact = sw.factory();
169 switch (fact.getVersion()) {
170 case OF_10:
171 sw.sendMsg(fact.buildFeaturesRequest().setXid(0).build());
172 break;
173 case OF_13:
174 sw.sendMsg(fact.buildPortDescStatsRequest().setXid(0).build());
175 break;
176 default:
177 LOG.warn("Unhandled protocol version");
178 }
tomab21e7c2014-08-26 15:23:08 -0700179 }
180
181 @Override
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700182 public void roleChanged(DeviceId deviceId, MastershipRole newRole) {
alshabibf1216ed2014-09-03 11:53:54 -0700183 switch (newRole) {
tom782a7cf2014-09-11 23:58:38 -0700184 case MASTER:
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700185 controller.setRole(dpid(deviceId.uri()), RoleState.MASTER);
tom782a7cf2014-09-11 23:58:38 -0700186 break;
187 case STANDBY:
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700188 controller.setRole(dpid(deviceId.uri()), RoleState.EQUAL);
tom782a7cf2014-09-11 23:58:38 -0700189 break;
190 case NONE:
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700191 controller.setRole(dpid(deviceId.uri()), RoleState.SLAVE);
tom782a7cf2014-09-11 23:58:38 -0700192 break;
193 default:
194 LOG.error("Unknown Mastership state : {}", newRole);
alshabibf1216ed2014-09-03 11:53:54 -0700195
196 }
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700197 LOG.info("Accepting mastership role change for device {}", deviceId);
tomab21e7c2014-08-26 15:23:08 -0700198 }
199
sangho538108b2015-04-08 14:29:20 -0700200 private void pushPortMetrics(Dpid dpid, OFPortStatsReply msg) {
201 DeviceId deviceId = DeviceId.deviceId(dpid.uri(dpid));
202
203 Collection<PortStatistics> stats = buildPortStatistics(deviceId, msg);
204
205 providerService.updatePortStatistics(deviceId, stats);
206 }
207
208 private Collection<PortStatistics> buildPortStatistics(DeviceId deviceId, OFPortStatsReply msg) {
209
210 HashSet<PortStatistics> stats = Sets.newHashSet();
211
212 for (OFPortStatsEntry entry: msg.getEntries()) {
213 if (entry.getPortNo().getPortNumber() < 0) {
214 continue;
215 }
216 DefaultPortStatistics.Builder builder = DefaultPortStatistics.builder();
217 DefaultPortStatistics stat = builder.setDeviceId(deviceId)
218 .setPort(entry.getPortNo().getPortNumber())
219 .setPacketsReceived(entry.getRxPackets().getValue())
220 .setPacketsSent(entry.getTxPackets().getValue())
221 .setBytesReceived(entry.getRxBytes().getValue())
222 .setBytesSent(entry.getTxBytes().getValue())
223 .setPacketsRxDropped(entry.getRxDropped().getValue())
224 .setPacketsTxDropped(entry.getTxDropped().getValue())
225 .setPacketsRxErrors(entry.getRxErrors().getValue())
226 .setPacketsTxErrors(entry.getTxErrors().getValue())
227 .setDurationSec(entry.getDurationSec())
228 .setDurationNano(entry.getDurationNsec())
229 .build();
230
231 stats.add(stat);
232 }
233
234 return Collections.unmodifiableSet(stats);
235
236 }
237
238 private class InternalDeviceProvider implements OpenFlowSwitchListener, OpenFlowEventListener {
alshabibf1216ed2014-09-03 11:53:54 -0700239 @Override
tomd1900f32014-09-03 14:08:16 -0700240 public void switchAdded(Dpid dpid) {
alshabib6f5460b2014-09-03 14:46:17 -0700241 if (providerService == null) {
242 return;
243 }
tom782a7cf2014-09-11 23:58:38 -0700244 DeviceId did = deviceId(uri(dpid));
alshabib6f5460b2014-09-03 14:46:17 -0700245 OpenFlowSwitch sw = controller.getSwitch(dpid);
246
Praseed Balakrishnana22eadf2014-10-20 14:21:45 -0700247 Device.Type deviceType = sw.isOptical() ? Device.Type.ROADM :
248 Device.Type.SWITCH;
alshabib7911a052014-10-16 17:49:37 -0700249 ChassisId cId = new ChassisId(dpid.value());
Ray Milkeye53f1712015-01-16 09:17:16 -0800250
Thomas Vachuska82041f52014-11-30 22:14:02 -0800251 SparseAnnotations annotations = DefaultAnnotations.builder()
Ray Milkeye53f1712015-01-16 09:17:16 -0800252 .set("protocol", sw.factory().getVersion().toString())
253 .set("channelId", sw.channelId())
254 .build();
255
tomd1900f32014-09-03 14:08:16 -0700256 DeviceDescription description =
Praseed Balakrishnana22eadf2014-10-20 14:21:45 -0700257 new DefaultDeviceDescription(did.uri(), deviceType,
Ray Milkeyd3edd032015-01-16 11:38:58 -0800258 sw.manufacturerDescription(),
tom782a7cf2014-09-11 23:58:38 -0700259 sw.hardwareDescription(),
260 sw.softwareDescription(),
alshabib7911a052014-10-16 17:49:37 -0700261 sw.serialNumber(),
Thomas Vachuska82041f52014-11-30 22:14:02 -0800262 cId, annotations);
tom782a7cf2014-09-11 23:58:38 -0700263 providerService.deviceConnected(did, description);
264 providerService.updatePorts(did, buildPortDescriptions(sw.getPorts()));
sangho538108b2015-04-08 14:29:20 -0700265
266 PortStatsCollector psc = new PortStatsCollector(
267 controller.getSwitch(dpid), POLL_INTERVAL);
268 psc.start();
269 collectors.put(dpid, psc);
alshabib25c8eec2014-09-04 16:41:31 -0700270 }
271
alshabibf1216ed2014-09-03 11:53:54 -0700272 @Override
273 public void switchRemoved(Dpid dpid) {
alshabib6f5460b2014-09-03 14:46:17 -0700274 if (providerService == null) {
275 return;
276 }
tom782a7cf2014-09-11 23:58:38 -0700277 providerService.deviceDisconnected(deviceId(uri(dpid)));
alshabibf1216ed2014-09-03 11:53:54 -0700278
sangho538108b2015-04-08 14:29:20 -0700279 PortStatsCollector collector = collectors.remove(dpid);
280 if (collector != null) {
281 collector.stop();
282 }
283 }
Ayaka Koshibe38594c22014-10-22 13:36:12 -0700284
285 @Override
286 public void switchChanged(Dpid dpid) {
287 if (providerService == null) {
288 return;
289 }
290 DeviceId did = deviceId(uri(dpid));
291 OpenFlowSwitch sw = controller.getSwitch(dpid);
292 providerService.updatePorts(did, buildPortDescriptions(sw.getPorts()));
293 }
294
alshabiba14f3642014-09-05 09:31:31 -0700295 @Override
296 public void portChanged(Dpid dpid, OFPortStatus status) {
alshabibafc514a2014-12-01 14:44:05 -0800297 PortDescription portDescription = buildPortDescription(status);
tom782a7cf2014-09-11 23:58:38 -0700298 providerService.portStatusChanged(deviceId(uri(dpid)), portDescription);
alshabibf1216ed2014-09-03 11:53:54 -0700299 }
300
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700301 @Override
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700302 public void receivedRoleReply(Dpid dpid, RoleState requested, RoleState response) {
303 MastershipRole request = roleOf(requested);
304 MastershipRole reply = roleOf(response);
305
306 providerService.receivedRoleReply(deviceId(uri(dpid)), request, reply);
307 }
308
309 /**
310 * Translates a RoleState to the corresponding MastershipRole.
311 *
312 * @param response
313 * @return a MastershipRole
314 */
315 private MastershipRole roleOf(RoleState response) {
316 switch (response) {
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700317 case MASTER:
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700318 return MastershipRole.MASTER;
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700319 case EQUAL:
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700320 return MastershipRole.STANDBY;
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700321 case SLAVE:
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700322 return MastershipRole.NONE;
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700323 default:
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700324 LOG.warn("unknown role {}", response);
325 return null;
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700326 }
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700327 }
328
alshabiba14f3642014-09-05 09:31:31 -0700329 /**
330 * Builds a list of port descriptions for a given list of ports.
tomff7eb7c2014-09-08 12:49:03 -0700331 *
alshabiba14f3642014-09-05 09:31:31 -0700332 * @param ports the list of ports
333 * @return list of portdescriptions
334 */
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700335 private List<PortDescription> buildPortDescriptions(List<OFPortDesc> ports) {
Yuta HIGUCHI38294a92014-10-16 18:09:49 -0700336 final List<PortDescription> portDescs = new ArrayList<>(ports.size());
alshabib4680bb62014-09-04 17:15:08 -0700337 for (OFPortDesc port : ports) {
alshabiba14f3642014-09-05 09:31:31 -0700338 portDescs.add(buildPortDescription(port));
alshabib4680bb62014-09-04 17:15:08 -0700339 }
340 return portDescs;
341 }
342
alshabiba14f3642014-09-05 09:31:31 -0700343 /**
Ray Milkeye0fade72015-01-15 13:29:47 -0800344 * Creates an annotation for the port name if one is available.
345 *
346 * @param port description of the port
347 * @return annotation containing the port name if one is found,
348 * null otherwise
349 */
350 private SparseAnnotations makePortNameAnnotation(OFPortDesc port) {
351 SparseAnnotations annotations = null;
352 String portName = Strings.emptyToNull(port.getName());
353 if (portName != null) {
354 annotations = DefaultAnnotations.builder()
355 .set("portName", portName).build();
356 }
357 return annotations;
358 }
359
360 /**
alshabiba14f3642014-09-05 09:31:31 -0700361 * Build a portDescription from a given port.
tomff7eb7c2014-09-08 12:49:03 -0700362 *
alshabiba14f3642014-09-05 09:31:31 -0700363 * @param port the port to build from.
364 * @return portDescription for the port.
365 */
366 private PortDescription buildPortDescription(OFPortDesc port) {
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700367 PortNumber portNo = PortNumber.portNumber(port.getPortNo().getPortNumber());
368 boolean enabled =
369 !port.getState().contains(OFPortState.LINK_DOWN) &&
370 !port.getConfig().contains(OFPortConfig.PORT_DOWN);
371 Port.Type type = port.getCurr().contains(OFPortFeatures.PF_FIBER) ? FIBER : COPPER;
Ray Milkeye0fade72015-01-15 13:29:47 -0800372 SparseAnnotations annotations = makePortNameAnnotation(port);
373 return new DefaultPortDescription(portNo, enabled, type,
374 portSpeed(port), annotations);
alshabiba14f3642014-09-05 09:31:31 -0700375 }
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700376
alshabibafc514a2014-12-01 14:44:05 -0800377 private PortDescription buildPortDescription(OFPortStatus status) {
378 OFPortDesc port = status.getDesc();
379 if (status.getReason() != OFPortReason.DELETE) {
380 return buildPortDescription(port);
381 } else {
382 PortNumber portNo = PortNumber.portNumber(port.getPortNo().getPortNumber());
383 Port.Type type = port.getCurr().contains(OFPortFeatures.PF_FIBER) ? FIBER : COPPER;
Ray Milkeye0fade72015-01-15 13:29:47 -0800384 SparseAnnotations annotations = makePortNameAnnotation(port);
385 return new DefaultPortDescription(portNo, false, type,
386 portSpeed(port), annotations);
alshabibafc514a2014-12-01 14:44:05 -0800387 }
388 }
389
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700390 private long portSpeed(OFPortDesc port) {
391 if (port.getVersion() == OFVersion.OF_13) {
392 return port.getCurrSpeed() / MBPS;
393 }
394
395 PortSpeed portSpeed = PortSpeed.SPEED_NONE;
396 for (OFPortFeatures feat : port.getCurr()) {
397 portSpeed = PortSpeed.max(portSpeed, feat.getPortSpeed());
398 }
Thomas Vachuska98eda532014-10-29 17:31:02 -0700399 return portSpeed.getSpeedBps() / MBPS;
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700400 }
sangho538108b2015-04-08 14:29:20 -0700401
402 @Override
403 public void handleMessage(Dpid dpid, OFMessage msg) {
404 switch (msg.getType()) {
405 case STATS_REPLY:
406 if (((OFStatsReply) msg).getStatsType() == OFStatsType.PORT) {
407 pushPortMetrics(dpid, (OFPortStatsReply) msg);
408 }
409 break;
410 default:
411 break;
412 }
413 }
alshabibf1216ed2014-09-03 11:53:54 -0700414 }
415
tomb5a46e62014-08-26 14:20:00 -0700416}