blob: e23c7d604b2715c2e5566e027a8575bd73c1986e [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
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;
Thomas Vachuskab52a0142015-04-21 17:48:15 -070025import org.onosproject.net.AnnotationKeys;
Brian O'Connorabafb502014-12-02 22:26:20 -080026import org.onosproject.net.DefaultAnnotations;
27import org.onosproject.net.Device;
28import org.onosproject.net.DeviceId;
29import org.onosproject.net.MastershipRole;
30import org.onosproject.net.Port;
31import org.onosproject.net.PortNumber;
32import org.onosproject.net.SparseAnnotations;
33import org.onosproject.net.device.DefaultDeviceDescription;
34import org.onosproject.net.device.DefaultPortDescription;
sangho538108b2015-04-08 14:29:20 -070035import org.onosproject.net.device.DefaultPortStatistics;
Brian O'Connorabafb502014-12-02 22:26:20 -080036import org.onosproject.net.device.DeviceDescription;
37import org.onosproject.net.device.DeviceProvider;
38import org.onosproject.net.device.DeviceProviderRegistry;
39import org.onosproject.net.device.DeviceProviderService;
40import org.onosproject.net.device.PortDescription;
sangho538108b2015-04-08 14:29:20 -070041import org.onosproject.net.device.PortStatistics;
Brian O'Connorabafb502014-12-02 22:26:20 -080042import org.onosproject.net.provider.AbstractProvider;
43import org.onosproject.net.provider.ProviderId;
44import org.onosproject.openflow.controller.Dpid;
45import org.onosproject.openflow.controller.OpenFlowController;
sangho538108b2015-04-08 14:29:20 -070046import org.onosproject.openflow.controller.OpenFlowEventListener;
Brian O'Connorabafb502014-12-02 22:26:20 -080047import org.onosproject.openflow.controller.OpenFlowSwitch;
48import org.onosproject.openflow.controller.OpenFlowSwitchListener;
49import org.onosproject.openflow.controller.RoleState;
alshabib7911a052014-10-16 17:49:37 -070050import org.onlab.packet.ChassisId;
Ayaka Koshibee8708e32014-10-22 13:40:18 -070051import org.projectfloodlight.openflow.protocol.OFFactory;
sangho538108b2015-04-08 14:29:20 -070052import org.projectfloodlight.openflow.protocol.OFMessage;
alshabib4680bb62014-09-04 17:15:08 -070053import org.projectfloodlight.openflow.protocol.OFPortConfig;
alshabib25c8eec2014-09-04 16:41:31 -070054import org.projectfloodlight.openflow.protocol.OFPortDesc;
Thomas Vachuskad16ce182014-10-29 17:25:29 -070055import org.projectfloodlight.openflow.protocol.OFPortFeatures;
alshabibafc514a2014-12-01 14:44:05 -080056import org.projectfloodlight.openflow.protocol.OFPortReason;
alshabib4680bb62014-09-04 17:15:08 -070057import org.projectfloodlight.openflow.protocol.OFPortState;
sangho538108b2015-04-08 14:29:20 -070058import org.projectfloodlight.openflow.protocol.OFPortStatsEntry;
59import org.projectfloodlight.openflow.protocol.OFPortStatsReply;
alshabiba14f3642014-09-05 09:31:31 -070060import org.projectfloodlight.openflow.protocol.OFPortStatus;
sangho538108b2015-04-08 14:29:20 -070061import org.projectfloodlight.openflow.protocol.OFStatsReply;
62import org.projectfloodlight.openflow.protocol.OFStatsType;
Thomas Vachuskad16ce182014-10-29 17:25:29 -070063import org.projectfloodlight.openflow.protocol.OFVersion;
64import org.projectfloodlight.openflow.types.PortSpeed;
tomb5a46e62014-08-26 14:20:00 -070065import org.slf4j.Logger;
tom5f38b3a2014-08-27 23:50:54 -070066
tom782a7cf2014-09-11 23:58:38 -070067import java.util.ArrayList;
sangho538108b2015-04-08 14:29:20 -070068import java.util.Collection;
69import java.util.Collections;
70import java.util.HashMap;
71import java.util.HashSet;
tom782a7cf2014-09-11 23:58:38 -070072import java.util.List;
73
Ray Milkeye0fade72015-01-15 13:29:47 -080074import com.google.common.base.Strings;
75
Brian O'Connorabafb502014-12-02 22:26:20 -080076import static org.onosproject.net.DeviceId.deviceId;
77import static org.onosproject.net.Port.Type.COPPER;
78import static org.onosproject.net.Port.Type.FIBER;
79import static org.onosproject.openflow.controller.Dpid.dpid;
80import static org.onosproject.openflow.controller.Dpid.uri;
tom782a7cf2014-09-11 23:58:38 -070081import static org.slf4j.LoggerFactory.getLogger;
82
tomb5a46e62014-08-26 14:20:00 -070083/**
tomb1260e42014-08-26 18:39:57 -070084 * Provider which uses an OpenFlow controller to detect network
tome06f8552014-08-26 16:58:42 -070085 * infrastructure devices.
tomb5a46e62014-08-26 14:20:00 -070086 */
tomb1260e42014-08-26 18:39:57 -070087@Component(immediate = true)
tomab21e7c2014-08-26 15:23:08 -070088public class OpenFlowDeviceProvider extends AbstractProvider implements DeviceProvider {
tomb5a46e62014-08-26 14:20:00 -070089
alshabiba89cc582014-09-09 16:43:00 -070090 private static final Logger LOG = getLogger(OpenFlowDeviceProvider.class);
Thomas Vachuskad16ce182014-10-29 17:25:29 -070091 private static final long MBPS = 1_000 * 1_000;
tomb5a46e62014-08-26 14:20:00 -070092
93 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
tom96dfcab2014-08-28 09:26:03 -070094 protected DeviceProviderRegistry providerRegistry;
tomab21e7c2014-08-26 15:23:08 -070095
tom5f38b3a2014-08-27 23:50:54 -070096 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
97 protected OpenFlowController controller;
98
tomab21e7c2014-08-26 15:23:08 -070099 private DeviceProviderService providerService;
tomb5a46e62014-08-26 14:20:00 -0700100
sangho538108b2015-04-08 14:29:20 -0700101 private final InternalDeviceProvider listener = new InternalDeviceProvider();
102
103 // TODO: We need to make the poll interval configurable.
104 static final int POLL_INTERVAL = 10;
105
106 private HashMap<Dpid, PortStatsCollector> collectors = Maps.newHashMap();
tomd40fc7a2014-09-04 16:41:10 -0700107
tomab21e7c2014-08-26 15:23:08 -0700108 /**
109 * Creates an OpenFlow device provider.
110 */
111 public OpenFlowDeviceProvider() {
Brian O'Connorabafb502014-12-02 22:26:20 -0800112 super(new ProviderId("of", "org.onosproject.provider.openflow"));
tomab21e7c2014-08-26 15:23:08 -0700113 }
114
tomb5a46e62014-08-26 14:20:00 -0700115 @Activate
116 public void activate() {
tom96dfcab2014-08-28 09:26:03 -0700117 providerService = providerRegistry.register(this);
tomd40fc7a2014-09-04 16:41:10 -0700118 controller.addListener(listener);
sangho538108b2015-04-08 14:29:20 -0700119 controller.addEventListener(listener);
alshabibc944fd02014-09-10 17:55:17 -0700120 for (OpenFlowSwitch sw : controller.getSwitches()) {
Yuta HIGUCHIac2972a2014-11-18 12:58:52 -0800121 try {
122 listener.switchAdded(new Dpid(sw.getId()));
123 } catch (Exception e) {
124 LOG.warn("Failed initially adding {} : {}", sw.getStringId(), e.getMessage());
125 LOG.debug("Error details:", e);
126 // disconnect to trigger switch-add later
127 sw.disconnectSwitch();
128 }
sangho538108b2015-04-08 14:29:20 -0700129 PortStatsCollector psc = new PortStatsCollector(sw, POLL_INTERVAL);
130 psc.start();
131 collectors.put(new Dpid(sw.getId()), psc);
alshabibc944fd02014-09-10 17:55:17 -0700132 }
alshabiba89cc582014-09-09 16:43:00 -0700133 LOG.info("Started");
tomb5a46e62014-08-26 14:20:00 -0700134 }
135
136 @Deactivate
137 public void deactivate() {
tom96dfcab2014-08-28 09:26:03 -0700138 providerRegistry.unregister(this);
tomd40fc7a2014-09-04 16:41:10 -0700139 controller.removeListener(listener);
tomab21e7c2014-08-26 15:23:08 -0700140 providerService = null;
alshabibc944fd02014-09-10 17:55:17 -0700141
alshabiba89cc582014-09-09 16:43:00 -0700142 LOG.info("Stopped");
tomb5a46e62014-08-26 14:20:00 -0700143 }
144
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700145
146 @Override
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700147 public boolean isReachable(DeviceId deviceId) {
148 OpenFlowSwitch sw = controller.getSwitch(dpid(deviceId.uri()));
Yuta HIGUCHI69a27352014-10-31 15:48:37 -0700149 if (sw == null || !sw.isConnected()) {
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700150 return false;
151 }
152 return true;
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700153 }
154
tomab21e7c2014-08-26 15:23:08 -0700155 @Override
Ayaka Koshibe78bcbc12014-11-19 14:28:58 -0800156 public void triggerProbe(DeviceId deviceId) {
Yuta HIGUCHI802d0e82014-10-31 16:07:37 -0700157 LOG.info("Triggering probe on device {}", deviceId);
Ayaka Koshibee8708e32014-10-22 13:40:18 -0700158
Yuta HIGUCHI802d0e82014-10-31 16:07:37 -0700159 final Dpid dpid = dpid(deviceId.uri());
160 OpenFlowSwitch sw = controller.getSwitch(dpid);
161 if (sw == null || !sw.isConnected()) {
Ayaka Koshibe78bcbc12014-11-19 14:28:58 -0800162 LOG.error("Failed to probe device {} on sw={}", deviceId, sw);
Yuta HIGUCHI802d0e82014-10-31 16:07:37 -0700163 providerService.deviceDisconnected(deviceId);
164 } else {
Ayaka Koshibe78bcbc12014-11-19 14:28:58 -0800165 LOG.trace("Confirmed device {} connection", deviceId);
Yuta HIGUCHI802d0e82014-10-31 16:07:37 -0700166 }
Ayaka Koshibee8708e32014-10-22 13:40:18 -0700167
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700168 // Prompt an update of port information. We can use any XID for this.
Ayaka Koshibee8708e32014-10-22 13:40:18 -0700169 OFFactory fact = sw.factory();
170 switch (fact.getVersion()) {
171 case OF_10:
172 sw.sendMsg(fact.buildFeaturesRequest().setXid(0).build());
173 break;
174 case OF_13:
175 sw.sendMsg(fact.buildPortDescStatsRequest().setXid(0).build());
176 break;
177 default:
178 LOG.warn("Unhandled protocol version");
179 }
tomab21e7c2014-08-26 15:23:08 -0700180 }
181
182 @Override
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700183 public void roleChanged(DeviceId deviceId, MastershipRole newRole) {
alshabibf1216ed2014-09-03 11:53:54 -0700184 switch (newRole) {
tom782a7cf2014-09-11 23:58:38 -0700185 case MASTER:
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700186 controller.setRole(dpid(deviceId.uri()), RoleState.MASTER);
tom782a7cf2014-09-11 23:58:38 -0700187 break;
188 case STANDBY:
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700189 controller.setRole(dpid(deviceId.uri()), RoleState.EQUAL);
tom782a7cf2014-09-11 23:58:38 -0700190 break;
191 case NONE:
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700192 controller.setRole(dpid(deviceId.uri()), RoleState.SLAVE);
tom782a7cf2014-09-11 23:58:38 -0700193 break;
194 default:
195 LOG.error("Unknown Mastership state : {}", newRole);
alshabibf1216ed2014-09-03 11:53:54 -0700196
197 }
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700198 LOG.info("Accepting mastership role change for device {}", deviceId);
tomab21e7c2014-08-26 15:23:08 -0700199 }
200
sangho538108b2015-04-08 14:29:20 -0700201 private void pushPortMetrics(Dpid dpid, OFPortStatsReply msg) {
202 DeviceId deviceId = DeviceId.deviceId(dpid.uri(dpid));
203
204 Collection<PortStatistics> stats = buildPortStatistics(deviceId, msg);
205
206 providerService.updatePortStatistics(deviceId, stats);
207 }
208
209 private Collection<PortStatistics> buildPortStatistics(DeviceId deviceId, OFPortStatsReply msg) {
210
211 HashSet<PortStatistics> stats = Sets.newHashSet();
212
213 for (OFPortStatsEntry entry: msg.getEntries()) {
214 if (entry.getPortNo().getPortNumber() < 0) {
215 continue;
216 }
217 DefaultPortStatistics.Builder builder = DefaultPortStatistics.builder();
218 DefaultPortStatistics stat = builder.setDeviceId(deviceId)
219 .setPort(entry.getPortNo().getPortNumber())
220 .setPacketsReceived(entry.getRxPackets().getValue())
221 .setPacketsSent(entry.getTxPackets().getValue())
222 .setBytesReceived(entry.getRxBytes().getValue())
223 .setBytesSent(entry.getTxBytes().getValue())
224 .setPacketsRxDropped(entry.getRxDropped().getValue())
225 .setPacketsTxDropped(entry.getTxDropped().getValue())
226 .setPacketsRxErrors(entry.getRxErrors().getValue())
227 .setPacketsTxErrors(entry.getTxErrors().getValue())
228 .setDurationSec(entry.getDurationSec())
229 .setDurationNano(entry.getDurationNsec())
230 .build();
231
232 stats.add(stat);
233 }
234
235 return Collections.unmodifiableSet(stats);
236
237 }
238
239 private class InternalDeviceProvider implements OpenFlowSwitchListener, OpenFlowEventListener {
alshabibf1216ed2014-09-03 11:53:54 -0700240 @Override
tomd1900f32014-09-03 14:08:16 -0700241 public void switchAdded(Dpid dpid) {
alshabib6f5460b2014-09-03 14:46:17 -0700242 if (providerService == null) {
243 return;
244 }
tom782a7cf2014-09-11 23:58:38 -0700245 DeviceId did = deviceId(uri(dpid));
alshabib6f5460b2014-09-03 14:46:17 -0700246 OpenFlowSwitch sw = controller.getSwitch(dpid);
247
Praseed Balakrishnana22eadf2014-10-20 14:21:45 -0700248 Device.Type deviceType = sw.isOptical() ? Device.Type.ROADM :
249 Device.Type.SWITCH;
alshabib7911a052014-10-16 17:49:37 -0700250 ChassisId cId = new ChassisId(dpid.value());
Ray Milkeye53f1712015-01-16 09:17:16 -0800251
Thomas Vachuska82041f52014-11-30 22:14:02 -0800252 SparseAnnotations annotations = DefaultAnnotations.builder()
Ray Milkeye53f1712015-01-16 09:17:16 -0800253 .set("protocol", sw.factory().getVersion().toString())
254 .set("channelId", sw.channelId())
255 .build();
256
tomd1900f32014-09-03 14:08:16 -0700257 DeviceDescription description =
Praseed Balakrishnana22eadf2014-10-20 14:21:45 -0700258 new DefaultDeviceDescription(did.uri(), deviceType,
Ray Milkeyd3edd032015-01-16 11:38:58 -0800259 sw.manufacturerDescription(),
tom782a7cf2014-09-11 23:58:38 -0700260 sw.hardwareDescription(),
261 sw.softwareDescription(),
alshabib7911a052014-10-16 17:49:37 -0700262 sw.serialNumber(),
Thomas Vachuska82041f52014-11-30 22:14:02 -0800263 cId, annotations);
tom782a7cf2014-09-11 23:58:38 -0700264 providerService.deviceConnected(did, description);
265 providerService.updatePorts(did, buildPortDescriptions(sw.getPorts()));
sangho538108b2015-04-08 14:29:20 -0700266
267 PortStatsCollector psc = new PortStatsCollector(
268 controller.getSwitch(dpid), POLL_INTERVAL);
269 psc.start();
270 collectors.put(dpid, psc);
alshabib25c8eec2014-09-04 16:41:31 -0700271 }
272
alshabibf1216ed2014-09-03 11:53:54 -0700273 @Override
274 public void switchRemoved(Dpid dpid) {
alshabib6f5460b2014-09-03 14:46:17 -0700275 if (providerService == null) {
276 return;
277 }
tom782a7cf2014-09-11 23:58:38 -0700278 providerService.deviceDisconnected(deviceId(uri(dpid)));
alshabibf1216ed2014-09-03 11:53:54 -0700279
sangho538108b2015-04-08 14:29:20 -0700280 PortStatsCollector collector = collectors.remove(dpid);
281 if (collector != null) {
282 collector.stop();
283 }
284 }
Ayaka Koshibe38594c22014-10-22 13:36:12 -0700285
286 @Override
287 public void switchChanged(Dpid dpid) {
288 if (providerService == null) {
289 return;
290 }
291 DeviceId did = deviceId(uri(dpid));
292 OpenFlowSwitch sw = controller.getSwitch(dpid);
293 providerService.updatePorts(did, buildPortDescriptions(sw.getPorts()));
294 }
295
alshabiba14f3642014-09-05 09:31:31 -0700296 @Override
297 public void portChanged(Dpid dpid, OFPortStatus status) {
alshabibafc514a2014-12-01 14:44:05 -0800298 PortDescription portDescription = buildPortDescription(status);
tom782a7cf2014-09-11 23:58:38 -0700299 providerService.portStatusChanged(deviceId(uri(dpid)), portDescription);
alshabibf1216ed2014-09-03 11:53:54 -0700300 }
301
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700302 @Override
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700303 public void receivedRoleReply(Dpid dpid, RoleState requested, RoleState response) {
304 MastershipRole request = roleOf(requested);
305 MastershipRole reply = roleOf(response);
306
307 providerService.receivedRoleReply(deviceId(uri(dpid)), request, reply);
308 }
309
310 /**
311 * Translates a RoleState to the corresponding MastershipRole.
312 *
313 * @param response
314 * @return a MastershipRole
315 */
316 private MastershipRole roleOf(RoleState response) {
317 switch (response) {
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700318 case MASTER:
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700319 return MastershipRole.MASTER;
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700320 case EQUAL:
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700321 return MastershipRole.STANDBY;
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700322 case SLAVE:
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700323 return MastershipRole.NONE;
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700324 default:
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700325 LOG.warn("unknown role {}", response);
326 return null;
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700327 }
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700328 }
329
alshabiba14f3642014-09-05 09:31:31 -0700330 /**
331 * Builds a list of port descriptions for a given list of ports.
tomff7eb7c2014-09-08 12:49:03 -0700332 *
alshabiba14f3642014-09-05 09:31:31 -0700333 * @param ports the list of ports
334 * @return list of portdescriptions
335 */
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700336 private List<PortDescription> buildPortDescriptions(List<OFPortDesc> ports) {
Yuta HIGUCHI38294a92014-10-16 18:09:49 -0700337 final List<PortDescription> portDescs = new ArrayList<>(ports.size());
alshabib4680bb62014-09-04 17:15:08 -0700338 for (OFPortDesc port : ports) {
alshabiba14f3642014-09-05 09:31:31 -0700339 portDescs.add(buildPortDescription(port));
alshabib4680bb62014-09-04 17:15:08 -0700340 }
341 return portDescs;
342 }
343
alshabiba14f3642014-09-05 09:31:31 -0700344 /**
Ray Milkeye0fade72015-01-15 13:29:47 -0800345 * Creates an annotation for the port name if one is available.
346 *
347 * @param port description of the port
348 * @return annotation containing the port name if one is found,
349 * null otherwise
350 */
351 private SparseAnnotations makePortNameAnnotation(OFPortDesc port) {
352 SparseAnnotations annotations = null;
353 String portName = Strings.emptyToNull(port.getName());
354 if (portName != null) {
355 annotations = DefaultAnnotations.builder()
Thomas Vachuskab52a0142015-04-21 17:48:15 -0700356 .set(AnnotationKeys.PORT_NAME, portName).build();
Ray Milkeye0fade72015-01-15 13:29:47 -0800357 }
358 return annotations;
359 }
360
361 /**
alshabiba14f3642014-09-05 09:31:31 -0700362 * Build a portDescription from a given port.
tomff7eb7c2014-09-08 12:49:03 -0700363 *
alshabiba14f3642014-09-05 09:31:31 -0700364 * @param port the port to build from.
365 * @return portDescription for the port.
366 */
367 private PortDescription buildPortDescription(OFPortDesc port) {
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700368 PortNumber portNo = PortNumber.portNumber(port.getPortNo().getPortNumber());
369 boolean enabled =
370 !port.getState().contains(OFPortState.LINK_DOWN) &&
371 !port.getConfig().contains(OFPortConfig.PORT_DOWN);
372 Port.Type type = port.getCurr().contains(OFPortFeatures.PF_FIBER) ? FIBER : COPPER;
Ray Milkeye0fade72015-01-15 13:29:47 -0800373 SparseAnnotations annotations = makePortNameAnnotation(port);
374 return new DefaultPortDescription(portNo, enabled, type,
375 portSpeed(port), annotations);
alshabiba14f3642014-09-05 09:31:31 -0700376 }
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700377
alshabibafc514a2014-12-01 14:44:05 -0800378 private PortDescription buildPortDescription(OFPortStatus status) {
379 OFPortDesc port = status.getDesc();
380 if (status.getReason() != OFPortReason.DELETE) {
381 return buildPortDescription(port);
382 } else {
383 PortNumber portNo = PortNumber.portNumber(port.getPortNo().getPortNumber());
384 Port.Type type = port.getCurr().contains(OFPortFeatures.PF_FIBER) ? FIBER : COPPER;
Ray Milkeye0fade72015-01-15 13:29:47 -0800385 SparseAnnotations annotations = makePortNameAnnotation(port);
386 return new DefaultPortDescription(portNo, false, type,
387 portSpeed(port), annotations);
alshabibafc514a2014-12-01 14:44:05 -0800388 }
389 }
390
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700391 private long portSpeed(OFPortDesc port) {
392 if (port.getVersion() == OFVersion.OF_13) {
393 return port.getCurrSpeed() / MBPS;
394 }
395
396 PortSpeed portSpeed = PortSpeed.SPEED_NONE;
397 for (OFPortFeatures feat : port.getCurr()) {
398 portSpeed = PortSpeed.max(portSpeed, feat.getPortSpeed());
399 }
Thomas Vachuska98eda532014-10-29 17:31:02 -0700400 return portSpeed.getSpeedBps() / MBPS;
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700401 }
sangho538108b2015-04-08 14:29:20 -0700402
403 @Override
404 public void handleMessage(Dpid dpid, OFMessage msg) {
405 switch (msg.getType()) {
406 case STATS_REPLY:
407 if (((OFStatsReply) msg).getStatsType() == OFStatsType.PORT) {
408 pushPortMetrics(dpid, (OFPortStatsReply) msg);
409 }
410 break;
411 default:
412 break;
413 }
414 }
alshabibf1216ed2014-09-03 11:53:54 -0700415 }
416
tomb5a46e62014-08-26 14:20:00 -0700417}