blob: 6277e79b0e3070a772c822e983e4ada3a2b738ab [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
Thomas Vachuskaf0397b52015-05-29 13:50:17 -070018import com.google.common.base.Strings;
sangho538108b2015-04-08 14:29:20 -070019import com.google.common.collect.Maps;
20import com.google.common.collect.Sets;
tomb5a46e62014-08-26 14:20:00 -070021import org.apache.felix.scr.annotations.Activate;
22import org.apache.felix.scr.annotations.Component;
23import org.apache.felix.scr.annotations.Deactivate;
24import org.apache.felix.scr.annotations.Reference;
25import org.apache.felix.scr.annotations.ReferenceCardinality;
Thomas Vachuskaf0397b52015-05-29 13:50:17 -070026import org.onlab.packet.ChassisId;
Thomas Vachuskab52a0142015-04-21 17:48:15 -070027import org.onosproject.net.AnnotationKeys;
Brian O'Connorabafb502014-12-02 22:26:20 -080028import org.onosproject.net.DefaultAnnotations;
29import org.onosproject.net.Device;
30import org.onosproject.net.DeviceId;
31import org.onosproject.net.MastershipRole;
32import org.onosproject.net.Port;
33import org.onosproject.net.PortNumber;
34import org.onosproject.net.SparseAnnotations;
35import org.onosproject.net.device.DefaultDeviceDescription;
36import org.onosproject.net.device.DefaultPortDescription;
sangho538108b2015-04-08 14:29:20 -070037import org.onosproject.net.device.DefaultPortStatistics;
Brian O'Connorabafb502014-12-02 22:26:20 -080038import org.onosproject.net.device.DeviceDescription;
39import org.onosproject.net.device.DeviceProvider;
40import org.onosproject.net.device.DeviceProviderRegistry;
41import org.onosproject.net.device.DeviceProviderService;
42import org.onosproject.net.device.PortDescription;
sangho538108b2015-04-08 14:29:20 -070043import org.onosproject.net.device.PortStatistics;
Brian O'Connorabafb502014-12-02 22:26:20 -080044import org.onosproject.net.provider.AbstractProvider;
45import org.onosproject.net.provider.ProviderId;
46import org.onosproject.openflow.controller.Dpid;
47import org.onosproject.openflow.controller.OpenFlowController;
sangho538108b2015-04-08 14:29:20 -070048import org.onosproject.openflow.controller.OpenFlowEventListener;
Ayaka Koshibe5460d622015-05-14 12:19:19 -070049import org.onosproject.openflow.controller.OpenFlowOpticalSwitch;
Brian O'Connorabafb502014-12-02 22:26:20 -080050import org.onosproject.openflow.controller.OpenFlowSwitch;
51import org.onosproject.openflow.controller.OpenFlowSwitchListener;
Ayaka Koshibe5460d622015-05-14 12:19:19 -070052import org.onosproject.openflow.controller.PortDescPropertyType;
Brian O'Connorabafb502014-12-02 22:26:20 -080053import org.onosproject.openflow.controller.RoleState;
Ayaka Koshibee8708e32014-10-22 13:40:18 -070054import org.projectfloodlight.openflow.protocol.OFFactory;
sangho538108b2015-04-08 14:29:20 -070055import org.projectfloodlight.openflow.protocol.OFMessage;
alshabib4680bb62014-09-04 17:15:08 -070056import org.projectfloodlight.openflow.protocol.OFPortConfig;
alshabib25c8eec2014-09-04 16:41:31 -070057import org.projectfloodlight.openflow.protocol.OFPortDesc;
Thomas Vachuskad16ce182014-10-29 17:25:29 -070058import org.projectfloodlight.openflow.protocol.OFPortFeatures;
Ayaka Koshibe5460d622015-05-14 12:19:19 -070059import org.projectfloodlight.openflow.protocol.OFPortOptical;
alshabibafc514a2014-12-01 14:44:05 -080060import org.projectfloodlight.openflow.protocol.OFPortReason;
alshabib4680bb62014-09-04 17:15:08 -070061import org.projectfloodlight.openflow.protocol.OFPortState;
sangho538108b2015-04-08 14:29:20 -070062import org.projectfloodlight.openflow.protocol.OFPortStatsEntry;
63import org.projectfloodlight.openflow.protocol.OFPortStatsReply;
alshabiba14f3642014-09-05 09:31:31 -070064import org.projectfloodlight.openflow.protocol.OFPortStatus;
sangho538108b2015-04-08 14:29:20 -070065import org.projectfloodlight.openflow.protocol.OFStatsReply;
66import org.projectfloodlight.openflow.protocol.OFStatsType;
Thomas Vachuskad16ce182014-10-29 17:25:29 -070067import org.projectfloodlight.openflow.protocol.OFVersion;
68import org.projectfloodlight.openflow.types.PortSpeed;
tomb5a46e62014-08-26 14:20:00 -070069import org.slf4j.Logger;
tom5f38b3a2014-08-27 23:50:54 -070070
tom782a7cf2014-09-11 23:58:38 -070071import java.util.ArrayList;
sangho538108b2015-04-08 14:29:20 -070072import java.util.Collection;
73import java.util.Collections;
74import java.util.HashMap;
75import java.util.HashSet;
tom782a7cf2014-09-11 23:58:38 -070076import java.util.List;
77
Brian O'Connorabafb502014-12-02 22:26:20 -080078import static org.onosproject.net.DeviceId.deviceId;
79import static org.onosproject.net.Port.Type.COPPER;
80import static org.onosproject.net.Port.Type.FIBER;
81import static org.onosproject.openflow.controller.Dpid.dpid;
82import static org.onosproject.openflow.controller.Dpid.uri;
tom782a7cf2014-09-11 23:58:38 -070083import static org.slf4j.LoggerFactory.getLogger;
84
tomb5a46e62014-08-26 14:20:00 -070085/**
tomb1260e42014-08-26 18:39:57 -070086 * Provider which uses an OpenFlow controller to detect network
tome06f8552014-08-26 16:58:42 -070087 * infrastructure devices.
tomb5a46e62014-08-26 14:20:00 -070088 */
tomb1260e42014-08-26 18:39:57 -070089@Component(immediate = true)
tomab21e7c2014-08-26 15:23:08 -070090public class OpenFlowDeviceProvider extends AbstractProvider implements DeviceProvider {
tomb5a46e62014-08-26 14:20:00 -070091
alshabiba89cc582014-09-09 16:43:00 -070092 private static final Logger LOG = getLogger(OpenFlowDeviceProvider.class);
Thomas Vachuskad16ce182014-10-29 17:25:29 -070093 private static final long MBPS = 1_000 * 1_000;
tomb5a46e62014-08-26 14:20:00 -070094
95 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
tom96dfcab2014-08-28 09:26:03 -070096 protected DeviceProviderRegistry providerRegistry;
tomab21e7c2014-08-26 15:23:08 -070097
tom5f38b3a2014-08-27 23:50:54 -070098 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
99 protected OpenFlowController controller;
100
tomab21e7c2014-08-26 15:23:08 -0700101 private DeviceProviderService providerService;
tomb5a46e62014-08-26 14:20:00 -0700102
sangho538108b2015-04-08 14:29:20 -0700103 private final InternalDeviceProvider listener = new InternalDeviceProvider();
104
105 // TODO: We need to make the poll interval configurable.
106 static final int POLL_INTERVAL = 10;
107
108 private HashMap<Dpid, PortStatsCollector> collectors = Maps.newHashMap();
tomd40fc7a2014-09-04 16:41:10 -0700109
tomab21e7c2014-08-26 15:23:08 -0700110 /**
111 * Creates an OpenFlow device provider.
112 */
113 public OpenFlowDeviceProvider() {
Brian O'Connorabafb502014-12-02 22:26:20 -0800114 super(new ProviderId("of", "org.onosproject.provider.openflow"));
tomab21e7c2014-08-26 15:23:08 -0700115 }
116
tomb5a46e62014-08-26 14:20:00 -0700117 @Activate
118 public void activate() {
tom96dfcab2014-08-28 09:26:03 -0700119 providerService = providerRegistry.register(this);
tomd40fc7a2014-09-04 16:41:10 -0700120 controller.addListener(listener);
sangho538108b2015-04-08 14:29:20 -0700121 controller.addEventListener(listener);
alshabibc944fd02014-09-10 17:55:17 -0700122 for (OpenFlowSwitch sw : controller.getSwitches()) {
Yuta HIGUCHIac2972a2014-11-18 12:58:52 -0800123 try {
124 listener.switchAdded(new Dpid(sw.getId()));
125 } catch (Exception e) {
126 LOG.warn("Failed initially adding {} : {}", sw.getStringId(), e.getMessage());
127 LOG.debug("Error details:", e);
128 // disconnect to trigger switch-add later
129 sw.disconnectSwitch();
130 }
sangho538108b2015-04-08 14:29:20 -0700131 PortStatsCollector psc = new PortStatsCollector(sw, POLL_INTERVAL);
132 psc.start();
133 collectors.put(new Dpid(sw.getId()), psc);
alshabibc944fd02014-09-10 17:55:17 -0700134 }
alshabiba89cc582014-09-09 16:43:00 -0700135 LOG.info("Started");
tomb5a46e62014-08-26 14:20:00 -0700136 }
137
138 @Deactivate
139 public void deactivate() {
tom96dfcab2014-08-28 09:26:03 -0700140 providerRegistry.unregister(this);
tomd40fc7a2014-09-04 16:41:10 -0700141 controller.removeListener(listener);
tomab21e7c2014-08-26 15:23:08 -0700142 providerService = null;
alshabibc944fd02014-09-10 17:55:17 -0700143
alshabiba89cc582014-09-09 16:43:00 -0700144 LOG.info("Stopped");
tomb5a46e62014-08-26 14:20:00 -0700145 }
146
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700147
148 @Override
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700149 public boolean isReachable(DeviceId deviceId) {
150 OpenFlowSwitch sw = controller.getSwitch(dpid(deviceId.uri()));
Yuta HIGUCHI69a27352014-10-31 15:48:37 -0700151 if (sw == null || !sw.isConnected()) {
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700152 return false;
153 }
154 return true;
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700155 }
156
tomab21e7c2014-08-26 15:23:08 -0700157 @Override
Ayaka Koshibe78bcbc12014-11-19 14:28:58 -0800158 public void triggerProbe(DeviceId deviceId) {
Yuta HIGUCHI802d0e82014-10-31 16:07:37 -0700159 LOG.info("Triggering probe on device {}", deviceId);
Ayaka Koshibee8708e32014-10-22 13:40:18 -0700160
Yuta HIGUCHI802d0e82014-10-31 16:07:37 -0700161 final Dpid dpid = dpid(deviceId.uri());
162 OpenFlowSwitch sw = controller.getSwitch(dpid);
163 if (sw == null || !sw.isConnected()) {
Ayaka Koshibe78bcbc12014-11-19 14:28:58 -0800164 LOG.error("Failed to probe device {} on sw={}", deviceId, sw);
Yuta HIGUCHI802d0e82014-10-31 16:07:37 -0700165 providerService.deviceDisconnected(deviceId);
166 } else {
Ayaka Koshibe78bcbc12014-11-19 14:28:58 -0800167 LOG.trace("Confirmed device {} connection", deviceId);
Yuta HIGUCHI802d0e82014-10-31 16:07:37 -0700168 }
Ayaka Koshibee8708e32014-10-22 13:40:18 -0700169
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700170 // Prompt an update of port information. We can use any XID for this.
Ayaka Koshibee8708e32014-10-22 13:40:18 -0700171 OFFactory fact = sw.factory();
172 switch (fact.getVersion()) {
173 case OF_10:
174 sw.sendMsg(fact.buildFeaturesRequest().setXid(0).build());
175 break;
176 case OF_13:
177 sw.sendMsg(fact.buildPortDescStatsRequest().setXid(0).build());
178 break;
179 default:
180 LOG.warn("Unhandled protocol version");
181 }
tomab21e7c2014-08-26 15:23:08 -0700182 }
183
184 @Override
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700185 public void roleChanged(DeviceId deviceId, MastershipRole newRole) {
alshabibf1216ed2014-09-03 11:53:54 -0700186 switch (newRole) {
tom782a7cf2014-09-11 23:58:38 -0700187 case MASTER:
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700188 controller.setRole(dpid(deviceId.uri()), RoleState.MASTER);
tom782a7cf2014-09-11 23:58:38 -0700189 break;
190 case STANDBY:
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700191 controller.setRole(dpid(deviceId.uri()), RoleState.EQUAL);
tom782a7cf2014-09-11 23:58:38 -0700192 break;
193 case NONE:
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700194 controller.setRole(dpid(deviceId.uri()), RoleState.SLAVE);
tom782a7cf2014-09-11 23:58:38 -0700195 break;
196 default:
197 LOG.error("Unknown Mastership state : {}", newRole);
alshabibf1216ed2014-09-03 11:53:54 -0700198
199 }
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700200 LOG.info("Accepting mastership role change for device {}", deviceId);
tomab21e7c2014-08-26 15:23:08 -0700201 }
202
sangho538108b2015-04-08 14:29:20 -0700203 private void pushPortMetrics(Dpid dpid, OFPortStatsReply msg) {
204 DeviceId deviceId = DeviceId.deviceId(dpid.uri(dpid));
sangho538108b2015-04-08 14:29:20 -0700205 Collection<PortStatistics> stats = buildPortStatistics(deviceId, msg);
sangho538108b2015-04-08 14:29:20 -0700206 providerService.updatePortStatistics(deviceId, stats);
207 }
208
209 private Collection<PortStatistics> buildPortStatistics(DeviceId deviceId, OFPortStatsReply msg) {
sangho538108b2015-04-08 14:29:20 -0700210 HashSet<PortStatistics> stats = Sets.newHashSet();
211
212 for (OFPortStatsEntry entry: msg.getEntries()) {
Thomas Vachuskaf0397b52015-05-29 13:50:17 -0700213 try {
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.getVersion() == OFVersion.OF_10 ? 0 : entry.getDurationSec())
229 .setDurationNano(entry.getVersion() == OFVersion.OF_10 ? 0 : entry.getDurationNsec())
230 .build();
sangho538108b2015-04-08 14:29:20 -0700231
Thomas Vachuskaf0397b52015-05-29 13:50:17 -0700232 stats.add(stat);
233 } catch (Exception e) {
234 LOG.warn("Unable to process port stats", e);
235 }
sangho538108b2015-04-08 14:29:20 -0700236 }
237
238 return Collections.unmodifiableSet(stats);
239
240 }
241
242 private class InternalDeviceProvider implements OpenFlowSwitchListener, OpenFlowEventListener {
alshabibf1216ed2014-09-03 11:53:54 -0700243 @Override
tomd1900f32014-09-03 14:08:16 -0700244 public void switchAdded(Dpid dpid) {
alshabib6f5460b2014-09-03 14:46:17 -0700245 if (providerService == null) {
246 return;
247 }
tom782a7cf2014-09-11 23:58:38 -0700248 DeviceId did = deviceId(uri(dpid));
alshabib6f5460b2014-09-03 14:46:17 -0700249 OpenFlowSwitch sw = controller.getSwitch(dpid);
250
Praseed Balakrishnana22eadf2014-10-20 14:21:45 -0700251 Device.Type deviceType = sw.isOptical() ? Device.Type.ROADM :
252 Device.Type.SWITCH;
alshabib7911a052014-10-16 17:49:37 -0700253 ChassisId cId = new ChassisId(dpid.value());
Ray Milkeye53f1712015-01-16 09:17:16 -0800254
Thomas Vachuska82041f52014-11-30 22:14:02 -0800255 SparseAnnotations annotations = DefaultAnnotations.builder()
Ray Milkeye53f1712015-01-16 09:17:16 -0800256 .set("protocol", sw.factory().getVersion().toString())
257 .set("channelId", sw.channelId())
258 .build();
259
tomd1900f32014-09-03 14:08:16 -0700260 DeviceDescription description =
Praseed Balakrishnana22eadf2014-10-20 14:21:45 -0700261 new DefaultDeviceDescription(did.uri(), deviceType,
Ray Milkeyd3edd032015-01-16 11:38:58 -0800262 sw.manufacturerDescription(),
tom782a7cf2014-09-11 23:58:38 -0700263 sw.hardwareDescription(),
264 sw.softwareDescription(),
alshabib7911a052014-10-16 17:49:37 -0700265 sw.serialNumber(),
Thomas Vachuska82041f52014-11-30 22:14:02 -0800266 cId, annotations);
tom782a7cf2014-09-11 23:58:38 -0700267 providerService.deviceConnected(did, description);
Ayaka Koshibe5460d622015-05-14 12:19:19 -0700268 providerService.updatePorts(did, buildPortDescriptions(sw));
sangho538108b2015-04-08 14:29:20 -0700269
Thomas Vachuskafc52fec2015-05-18 19:13:56 -0700270 PortStatsCollector psc =
271 new PortStatsCollector(controller.getSwitch(dpid), POLL_INTERVAL);
sangho538108b2015-04-08 14:29:20 -0700272 psc.start();
273 collectors.put(dpid, psc);
alshabib25c8eec2014-09-04 16:41:31 -0700274 }
275
alshabibf1216ed2014-09-03 11:53:54 -0700276 @Override
277 public void switchRemoved(Dpid dpid) {
alshabib6f5460b2014-09-03 14:46:17 -0700278 if (providerService == null) {
279 return;
280 }
tom782a7cf2014-09-11 23:58:38 -0700281 providerService.deviceDisconnected(deviceId(uri(dpid)));
alshabibf1216ed2014-09-03 11:53:54 -0700282
sangho538108b2015-04-08 14:29:20 -0700283 PortStatsCollector collector = collectors.remove(dpid);
284 if (collector != null) {
285 collector.stop();
286 }
287 }
Ayaka Koshibe38594c22014-10-22 13:36:12 -0700288
289 @Override
290 public void switchChanged(Dpid dpid) {
291 if (providerService == null) {
292 return;
293 }
294 DeviceId did = deviceId(uri(dpid));
295 OpenFlowSwitch sw = controller.getSwitch(dpid);
Ayaka Koshibe5460d622015-05-14 12:19:19 -0700296 providerService.updatePorts(did, buildPortDescriptions(sw));
Ayaka Koshibe38594c22014-10-22 13:36:12 -0700297 }
298
alshabiba14f3642014-09-05 09:31:31 -0700299 @Override
300 public void portChanged(Dpid dpid, OFPortStatus status) {
alshabibafc514a2014-12-01 14:44:05 -0800301 PortDescription portDescription = buildPortDescription(status);
tom782a7cf2014-09-11 23:58:38 -0700302 providerService.portStatusChanged(deviceId(uri(dpid)), portDescription);
alshabibf1216ed2014-09-03 11:53:54 -0700303 }
304
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700305 @Override
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700306 public void receivedRoleReply(Dpid dpid, RoleState requested, RoleState response) {
307 MastershipRole request = roleOf(requested);
308 MastershipRole reply = roleOf(response);
309
310 providerService.receivedRoleReply(deviceId(uri(dpid)), request, reply);
311 }
312
313 /**
314 * Translates a RoleState to the corresponding MastershipRole.
315 *
Thomas Vachuskafc52fec2015-05-18 19:13:56 -0700316 * @param response role state
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700317 * @return a MastershipRole
318 */
319 private MastershipRole roleOf(RoleState response) {
320 switch (response) {
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700321 case MASTER:
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700322 return MastershipRole.MASTER;
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700323 case EQUAL:
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700324 return MastershipRole.STANDBY;
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700325 case SLAVE:
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700326 return MastershipRole.NONE;
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700327 default:
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700328 LOG.warn("unknown role {}", response);
329 return null;
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700330 }
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700331 }
332
alshabiba14f3642014-09-05 09:31:31 -0700333 /**
334 * Builds a list of port descriptions for a given list of ports.
tomff7eb7c2014-09-08 12:49:03 -0700335 *
alshabiba14f3642014-09-05 09:31:31 -0700336 * @return list of portdescriptions
337 */
Ayaka Koshibe5460d622015-05-14 12:19:19 -0700338 private List<PortDescription> buildPortDescriptions(OpenFlowSwitch sw) {
339 final List<PortDescription> portDescs = new ArrayList<>(sw.getPorts().size());
340 sw.getPorts().forEach(port -> portDescs.add(buildPortDescription(port)));
341 if (sw.isOptical()) {
342 OpenFlowOpticalSwitch opsw = (OpenFlowOpticalSwitch) sw;
343 opsw.getPortTypes().forEach(type -> {
Ayaka Koshibe5460d622015-05-14 12:19:19 -0700344 opsw.getPortsOf(type).forEach(
345 op -> {
346 portDescs.add(buildPortDescription(type, (OFPortOptical) op));
347 }
348 );
349 });
alshabib4680bb62014-09-04 17:15:08 -0700350 }
351 return portDescs;
352 }
353
alshabiba14f3642014-09-05 09:31:31 -0700354 /**
Ray Milkeye0fade72015-01-15 13:29:47 -0800355 * Creates an annotation for the port name if one is available.
356 *
357 * @param port description of the port
358 * @return annotation containing the port name if one is found,
359 * null otherwise
360 */
Ayaka Koshibe5460d622015-05-14 12:19:19 -0700361 private SparseAnnotations makePortNameAnnotation(String port) {
Ray Milkeye0fade72015-01-15 13:29:47 -0800362 SparseAnnotations annotations = null;
Ayaka Koshibe5460d622015-05-14 12:19:19 -0700363 String portName = Strings.emptyToNull(port);
Ray Milkeye0fade72015-01-15 13:29:47 -0800364 if (portName != null) {
365 annotations = DefaultAnnotations.builder()
Thomas Vachuskab52a0142015-04-21 17:48:15 -0700366 .set(AnnotationKeys.PORT_NAME, portName).build();
Ray Milkeye0fade72015-01-15 13:29:47 -0800367 }
368 return annotations;
369 }
370
371 /**
Ayaka Koshibe5460d622015-05-14 12:19:19 -0700372 * Build a portDescription from a given Ethernet port description.
tomff7eb7c2014-09-08 12:49:03 -0700373 *
alshabiba14f3642014-09-05 09:31:31 -0700374 * @param port the port to build from.
375 * @return portDescription for the port.
376 */
377 private PortDescription buildPortDescription(OFPortDesc port) {
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700378 PortNumber portNo = PortNumber.portNumber(port.getPortNo().getPortNumber());
379 boolean enabled =
380 !port.getState().contains(OFPortState.LINK_DOWN) &&
381 !port.getConfig().contains(OFPortConfig.PORT_DOWN);
382 Port.Type type = port.getCurr().contains(OFPortFeatures.PF_FIBER) ? FIBER : COPPER;
Ayaka Koshibe5460d622015-05-14 12:19:19 -0700383 SparseAnnotations annotations = makePortNameAnnotation(port.getName());
Ray Milkeye0fade72015-01-15 13:29:47 -0800384 return new DefaultPortDescription(portNo, enabled, type,
385 portSpeed(port), annotations);
alshabiba14f3642014-09-05 09:31:31 -0700386 }
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700387
Ayaka Koshibe5460d622015-05-14 12:19:19 -0700388 /**
389 * Build a portDescription from a given a port description describing some
390 * Optical port.
391 *
392 * @param port description property type.
393 * @param port the port to build from.
394 * @return portDescription for the port.
395 */
396 private PortDescription buildPortDescription(PortDescPropertyType ptype, OFPortOptical port) {
397 // Minimally functional fixture. This needs to be fixed as we add better support.
398 PortNumber portNo = PortNumber.portNumber(port.getPortNo().getPortNumber());
Ayaka Koshibe74b55272015-05-28 15:16:04 -0700399
Ayaka Koshibe5460d622015-05-14 12:19:19 -0700400 boolean enabled = !port.getState().contains(OFPortState.LINK_DOWN)
401 && !port.getConfig().contains(OFPortConfig.PORT_DOWN);
402 SparseAnnotations annotations = makePortNameAnnotation(port.getName());
Ayaka Koshibe5460d622015-05-14 12:19:19 -0700403
404 if (port.getVersion() == OFVersion.OF_13
405 && ptype == PortDescPropertyType.OPTICAL_TRANSPORT) {
406 // At this point, not much is carried in the optical port message.
Ayaka Koshibeae541732015-05-19 13:37:27 -0700407 LOG.debug("Optical transport port message {}", port.toString());
Ayaka Koshibe5460d622015-05-14 12:19:19 -0700408 } else {
409 // removable once 1.4+ support complete.
Ayaka Koshibeae541732015-05-19 13:37:27 -0700410 LOG.debug("Unsupported optical port properties");
Ayaka Koshibe5460d622015-05-14 12:19:19 -0700411 }
Ayaka Koshibe74b55272015-05-28 15:16:04 -0700412 return new DefaultPortDescription(portNo, enabled, FIBER, 0, annotations);
Ayaka Koshibe5460d622015-05-14 12:19:19 -0700413 }
414
alshabibafc514a2014-12-01 14:44:05 -0800415 private PortDescription buildPortDescription(OFPortStatus status) {
416 OFPortDesc port = status.getDesc();
417 if (status.getReason() != OFPortReason.DELETE) {
418 return buildPortDescription(port);
419 } else {
420 PortNumber portNo = PortNumber.portNumber(port.getPortNo().getPortNumber());
421 Port.Type type = port.getCurr().contains(OFPortFeatures.PF_FIBER) ? FIBER : COPPER;
Ayaka Koshibe5460d622015-05-14 12:19:19 -0700422 SparseAnnotations annotations = makePortNameAnnotation(port.getName());
Ray Milkeye0fade72015-01-15 13:29:47 -0800423 return new DefaultPortDescription(portNo, false, type,
424 portSpeed(port), annotations);
alshabibafc514a2014-12-01 14:44:05 -0800425 }
426 }
427
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700428 private long portSpeed(OFPortDesc port) {
429 if (port.getVersion() == OFVersion.OF_13) {
430 return port.getCurrSpeed() / MBPS;
431 }
432
433 PortSpeed portSpeed = PortSpeed.SPEED_NONE;
434 for (OFPortFeatures feat : port.getCurr()) {
435 portSpeed = PortSpeed.max(portSpeed, feat.getPortSpeed());
436 }
Thomas Vachuska98eda532014-10-29 17:31:02 -0700437 return portSpeed.getSpeedBps() / MBPS;
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700438 }
sangho538108b2015-04-08 14:29:20 -0700439
440 @Override
441 public void handleMessage(Dpid dpid, OFMessage msg) {
442 switch (msg.getType()) {
443 case STATS_REPLY:
444 if (((OFStatsReply) msg).getStatsType() == OFStatsType.PORT) {
445 pushPortMetrics(dpid, (OFPortStatsReply) msg);
446 }
447 break;
448 default:
449 break;
450 }
451 }
alshabibf1216ed2014-09-03 11:53:54 -0700452 }
453
tomb5a46e62014-08-26 14:20:00 -0700454}