blob: 7ae6db1651a1951c2a8212bb29fe90761f78dc35 [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;
Thomas Vachuska893bf4b2015-05-29 18:13:18 -070019import com.google.common.collect.Lists;
sangho538108b2015-04-08 14:29:20 -070020import com.google.common.collect.Maps;
21import com.google.common.collect.Sets;
tomb5a46e62014-08-26 14:20:00 -070022import org.apache.felix.scr.annotations.Activate;
23import org.apache.felix.scr.annotations.Component;
24import org.apache.felix.scr.annotations.Deactivate;
25import org.apache.felix.scr.annotations.Reference;
26import org.apache.felix.scr.annotations.ReferenceCardinality;
Thomas Vachuskaf0397b52015-05-29 13:50:17 -070027import org.onlab.packet.ChassisId;
Thomas Vachuskab52a0142015-04-21 17:48:15 -070028import org.onosproject.net.AnnotationKeys;
Brian O'Connorabafb502014-12-02 22:26:20 -080029import org.onosproject.net.DefaultAnnotations;
30import org.onosproject.net.Device;
31import org.onosproject.net.DeviceId;
32import org.onosproject.net.MastershipRole;
33import org.onosproject.net.Port;
34import org.onosproject.net.PortNumber;
35import org.onosproject.net.SparseAnnotations;
36import org.onosproject.net.device.DefaultDeviceDescription;
37import org.onosproject.net.device.DefaultPortDescription;
sangho538108b2015-04-08 14:29:20 -070038import org.onosproject.net.device.DefaultPortStatistics;
Brian O'Connorabafb502014-12-02 22:26:20 -080039import org.onosproject.net.device.DeviceDescription;
40import org.onosproject.net.device.DeviceProvider;
41import org.onosproject.net.device.DeviceProviderRegistry;
42import org.onosproject.net.device.DeviceProviderService;
43import org.onosproject.net.device.PortDescription;
sangho538108b2015-04-08 14:29:20 -070044import org.onosproject.net.device.PortStatistics;
Brian O'Connorabafb502014-12-02 22:26:20 -080045import org.onosproject.net.provider.AbstractProvider;
46import org.onosproject.net.provider.ProviderId;
47import org.onosproject.openflow.controller.Dpid;
48import org.onosproject.openflow.controller.OpenFlowController;
sangho538108b2015-04-08 14:29:20 -070049import org.onosproject.openflow.controller.OpenFlowEventListener;
Ayaka Koshibe5460d622015-05-14 12:19:19 -070050import org.onosproject.openflow.controller.OpenFlowOpticalSwitch;
Brian O'Connorabafb502014-12-02 22:26:20 -080051import org.onosproject.openflow.controller.OpenFlowSwitch;
52import org.onosproject.openflow.controller.OpenFlowSwitchListener;
Ayaka Koshibe5460d622015-05-14 12:19:19 -070053import org.onosproject.openflow.controller.PortDescPropertyType;
Brian O'Connorabafb502014-12-02 22:26:20 -080054import org.onosproject.openflow.controller.RoleState;
Ayaka Koshibee8708e32014-10-22 13:40:18 -070055import org.projectfloodlight.openflow.protocol.OFFactory;
sangho538108b2015-04-08 14:29:20 -070056import org.projectfloodlight.openflow.protocol.OFMessage;
alshabib4680bb62014-09-04 17:15:08 -070057import org.projectfloodlight.openflow.protocol.OFPortConfig;
alshabib25c8eec2014-09-04 16:41:31 -070058import org.projectfloodlight.openflow.protocol.OFPortDesc;
Thomas Vachuskad16ce182014-10-29 17:25:29 -070059import org.projectfloodlight.openflow.protocol.OFPortFeatures;
Ayaka Koshibe5460d622015-05-14 12:19:19 -070060import org.projectfloodlight.openflow.protocol.OFPortOptical;
alshabibafc514a2014-12-01 14:44:05 -080061import org.projectfloodlight.openflow.protocol.OFPortReason;
alshabib4680bb62014-09-04 17:15:08 -070062import org.projectfloodlight.openflow.protocol.OFPortState;
sangho538108b2015-04-08 14:29:20 -070063import org.projectfloodlight.openflow.protocol.OFPortStatsEntry;
64import org.projectfloodlight.openflow.protocol.OFPortStatsReply;
alshabiba14f3642014-09-05 09:31:31 -070065import org.projectfloodlight.openflow.protocol.OFPortStatus;
sangho538108b2015-04-08 14:29:20 -070066import org.projectfloodlight.openflow.protocol.OFStatsReply;
Thomas Vachuska893bf4b2015-05-29 18:13:18 -070067import org.projectfloodlight.openflow.protocol.OFStatsReplyFlags;
sangho538108b2015-04-08 14:29:20 -070068import org.projectfloodlight.openflow.protocol.OFStatsType;
Thomas Vachuskad16ce182014-10-29 17:25:29 -070069import org.projectfloodlight.openflow.protocol.OFVersion;
70import org.projectfloodlight.openflow.types.PortSpeed;
tomb5a46e62014-08-26 14:20:00 -070071import org.slf4j.Logger;
tom5f38b3a2014-08-27 23:50:54 -070072
tom782a7cf2014-09-11 23:58:38 -070073import java.util.ArrayList;
sangho538108b2015-04-08 14:29:20 -070074import java.util.Collection;
75import java.util.Collections;
76import java.util.HashMap;
77import java.util.HashSet;
tom782a7cf2014-09-11 23:58:38 -070078import java.util.List;
79
Brian O'Connorabafb502014-12-02 22:26:20 -080080import static org.onosproject.net.DeviceId.deviceId;
81import static org.onosproject.net.Port.Type.COPPER;
82import static org.onosproject.net.Port.Type.FIBER;
83import static org.onosproject.openflow.controller.Dpid.dpid;
84import static org.onosproject.openflow.controller.Dpid.uri;
tom782a7cf2014-09-11 23:58:38 -070085import static org.slf4j.LoggerFactory.getLogger;
86
tomb5a46e62014-08-26 14:20:00 -070087/**
tomb1260e42014-08-26 18:39:57 -070088 * Provider which uses an OpenFlow controller to detect network
tome06f8552014-08-26 16:58:42 -070089 * infrastructure devices.
tomb5a46e62014-08-26 14:20:00 -070090 */
tomb1260e42014-08-26 18:39:57 -070091@Component(immediate = true)
tomab21e7c2014-08-26 15:23:08 -070092public class OpenFlowDeviceProvider extends AbstractProvider implements DeviceProvider {
tomb5a46e62014-08-26 14:20:00 -070093
alshabiba89cc582014-09-09 16:43:00 -070094 private static final Logger LOG = getLogger(OpenFlowDeviceProvider.class);
Thomas Vachuskad16ce182014-10-29 17:25:29 -070095 private static final long MBPS = 1_000 * 1_000;
tomb5a46e62014-08-26 14:20:00 -070096
97 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
tom96dfcab2014-08-28 09:26:03 -070098 protected DeviceProviderRegistry providerRegistry;
tomab21e7c2014-08-26 15:23:08 -070099
tom5f38b3a2014-08-27 23:50:54 -0700100 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
101 protected OpenFlowController controller;
102
tomab21e7c2014-08-26 15:23:08 -0700103 private DeviceProviderService providerService;
tomb5a46e62014-08-26 14:20:00 -0700104
sangho538108b2015-04-08 14:29:20 -0700105 private final InternalDeviceProvider listener = new InternalDeviceProvider();
106
107 // TODO: We need to make the poll interval configurable.
108 static final int POLL_INTERVAL = 10;
109
110 private HashMap<Dpid, PortStatsCollector> collectors = Maps.newHashMap();
tomd40fc7a2014-09-04 16:41:10 -0700111
tomab21e7c2014-08-26 15:23:08 -0700112 /**
113 * Creates an OpenFlow device provider.
114 */
115 public OpenFlowDeviceProvider() {
Brian O'Connorabafb502014-12-02 22:26:20 -0800116 super(new ProviderId("of", "org.onosproject.provider.openflow"));
tomab21e7c2014-08-26 15:23:08 -0700117 }
118
tomb5a46e62014-08-26 14:20:00 -0700119 @Activate
120 public void activate() {
tom96dfcab2014-08-28 09:26:03 -0700121 providerService = providerRegistry.register(this);
tomd40fc7a2014-09-04 16:41:10 -0700122 controller.addListener(listener);
sangho538108b2015-04-08 14:29:20 -0700123 controller.addEventListener(listener);
alshabibc944fd02014-09-10 17:55:17 -0700124 for (OpenFlowSwitch sw : controller.getSwitches()) {
Yuta HIGUCHIac2972a2014-11-18 12:58:52 -0800125 try {
126 listener.switchAdded(new Dpid(sw.getId()));
127 } catch (Exception e) {
128 LOG.warn("Failed initially adding {} : {}", sw.getStringId(), e.getMessage());
129 LOG.debug("Error details:", e);
130 // disconnect to trigger switch-add later
131 sw.disconnectSwitch();
132 }
sangho538108b2015-04-08 14:29:20 -0700133 PortStatsCollector psc = new PortStatsCollector(sw, POLL_INTERVAL);
134 psc.start();
135 collectors.put(new Dpid(sw.getId()), psc);
alshabibc944fd02014-09-10 17:55:17 -0700136 }
alshabiba89cc582014-09-09 16:43:00 -0700137 LOG.info("Started");
tomb5a46e62014-08-26 14:20:00 -0700138 }
139
140 @Deactivate
141 public void deactivate() {
tom96dfcab2014-08-28 09:26:03 -0700142 providerRegistry.unregister(this);
tomd40fc7a2014-09-04 16:41:10 -0700143 controller.removeListener(listener);
Thomas Vachuska74bcd922015-06-02 13:08:57 -0700144 collectors.values().forEach(PortStatsCollector::stop);
tomab21e7c2014-08-26 15:23:08 -0700145 providerService = null;
alshabiba89cc582014-09-09 16:43:00 -0700146 LOG.info("Stopped");
tomb5a46e62014-08-26 14:20:00 -0700147 }
148
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700149
150 @Override
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700151 public boolean isReachable(DeviceId deviceId) {
152 OpenFlowSwitch sw = controller.getSwitch(dpid(deviceId.uri()));
Yuta HIGUCHI69a27352014-10-31 15:48:37 -0700153 if (sw == null || !sw.isConnected()) {
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700154 return false;
155 }
156 return true;
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700157 }
158
tomab21e7c2014-08-26 15:23:08 -0700159 @Override
Ayaka Koshibe78bcbc12014-11-19 14:28:58 -0800160 public void triggerProbe(DeviceId deviceId) {
Madan Jampanic3328762015-05-31 12:39:27 -0700161 LOG.debug("Triggering probe on device {}", deviceId);
Ayaka Koshibee8708e32014-10-22 13:40:18 -0700162
Yuta HIGUCHI802d0e82014-10-31 16:07:37 -0700163 final Dpid dpid = dpid(deviceId.uri());
164 OpenFlowSwitch sw = controller.getSwitch(dpid);
165 if (sw == null || !sw.isConnected()) {
Ayaka Koshibe78bcbc12014-11-19 14:28:58 -0800166 LOG.error("Failed to probe device {} on sw={}", deviceId, sw);
Yuta HIGUCHI802d0e82014-10-31 16:07:37 -0700167 providerService.deviceDisconnected(deviceId);
Thomas Vachuska74bcd922015-06-02 13:08:57 -0700168 return;
Yuta HIGUCHI802d0e82014-10-31 16:07:37 -0700169 } else {
Ayaka Koshibe78bcbc12014-11-19 14:28:58 -0800170 LOG.trace("Confirmed device {} connection", deviceId);
Yuta HIGUCHI802d0e82014-10-31 16:07:37 -0700171 }
Ayaka Koshibee8708e32014-10-22 13:40:18 -0700172
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700173 // Prompt an update of port information. We can use any XID for this.
Ayaka Koshibee8708e32014-10-22 13:40:18 -0700174 OFFactory fact = sw.factory();
175 switch (fact.getVersion()) {
176 case OF_10:
177 sw.sendMsg(fact.buildFeaturesRequest().setXid(0).build());
178 break;
179 case OF_13:
180 sw.sendMsg(fact.buildPortDescStatsRequest().setXid(0).build());
181 break;
182 default:
183 LOG.warn("Unhandled protocol version");
184 }
tomab21e7c2014-08-26 15:23:08 -0700185 }
186
187 @Override
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700188 public void roleChanged(DeviceId deviceId, MastershipRole newRole) {
alshabibf1216ed2014-09-03 11:53:54 -0700189 switch (newRole) {
tom782a7cf2014-09-11 23:58:38 -0700190 case MASTER:
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700191 controller.setRole(dpid(deviceId.uri()), RoleState.MASTER);
tom782a7cf2014-09-11 23:58:38 -0700192 break;
193 case STANDBY:
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700194 controller.setRole(dpid(deviceId.uri()), RoleState.EQUAL);
tom782a7cf2014-09-11 23:58:38 -0700195 break;
196 case NONE:
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700197 controller.setRole(dpid(deviceId.uri()), RoleState.SLAVE);
tom782a7cf2014-09-11 23:58:38 -0700198 break;
199 default:
200 LOG.error("Unknown Mastership state : {}", newRole);
alshabibf1216ed2014-09-03 11:53:54 -0700201
202 }
Madan Jampanic3328762015-05-31 12:39:27 -0700203 LOG.debug("Accepting mastership role change for device {}", deviceId);
tomab21e7c2014-08-26 15:23:08 -0700204 }
205
Thomas Vachuska893bf4b2015-05-29 18:13:18 -0700206 private void pushPortMetrics(Dpid dpid, List<OFPortStatsEntry> portStatsEntries) {
sangho538108b2015-04-08 14:29:20 -0700207 DeviceId deviceId = DeviceId.deviceId(dpid.uri(dpid));
Thomas Vachuska893bf4b2015-05-29 18:13:18 -0700208 Collection<PortStatistics> stats = buildPortStatistics(deviceId, portStatsEntries);
sangho538108b2015-04-08 14:29:20 -0700209 providerService.updatePortStatistics(deviceId, stats);
210 }
211
Thomas Vachuska893bf4b2015-05-29 18:13:18 -0700212 private Collection<PortStatistics> buildPortStatistics(DeviceId deviceId,
213 List<OFPortStatsEntry> entries) {
sangho538108b2015-04-08 14:29:20 -0700214 HashSet<PortStatistics> stats = Sets.newHashSet();
215
Thomas Vachuska893bf4b2015-05-29 18:13:18 -0700216 for (OFPortStatsEntry entry : entries) {
Thomas Vachuskaf0397b52015-05-29 13:50:17 -0700217 try {
Thomas Vachuska4e6025e2015-06-03 11:25:21 -0700218 if (entry == null || entry.getPortNo() == null || entry.getPortNo().getPortNumber() < 0) {
Thomas Vachuskaf0397b52015-05-29 13:50:17 -0700219 continue;
220 }
221 DefaultPortStatistics.Builder builder = DefaultPortStatistics.builder();
222 DefaultPortStatistics stat = builder.setDeviceId(deviceId)
223 .setPort(entry.getPortNo().getPortNumber())
224 .setPacketsReceived(entry.getRxPackets().getValue())
225 .setPacketsSent(entry.getTxPackets().getValue())
226 .setBytesReceived(entry.getRxBytes().getValue())
227 .setBytesSent(entry.getTxBytes().getValue())
228 .setPacketsRxDropped(entry.getRxDropped().getValue())
229 .setPacketsTxDropped(entry.getTxDropped().getValue())
230 .setPacketsRxErrors(entry.getRxErrors().getValue())
231 .setPacketsTxErrors(entry.getTxErrors().getValue())
232 .setDurationSec(entry.getVersion() == OFVersion.OF_10 ? 0 : entry.getDurationSec())
233 .setDurationNano(entry.getVersion() == OFVersion.OF_10 ? 0 : entry.getDurationNsec())
234 .build();
sangho538108b2015-04-08 14:29:20 -0700235
Thomas Vachuskaf0397b52015-05-29 13:50:17 -0700236 stats.add(stat);
237 } catch (Exception e) {
238 LOG.warn("Unable to process port stats", e);
239 }
sangho538108b2015-04-08 14:29:20 -0700240 }
241
242 return Collections.unmodifiableSet(stats);
243
244 }
245
246 private class InternalDeviceProvider implements OpenFlowSwitchListener, OpenFlowEventListener {
Thomas Vachuska893bf4b2015-05-29 18:13:18 -0700247
248 private List<OFPortStatsEntry> portStatsReplies = Lists.newArrayList();
249
alshabibf1216ed2014-09-03 11:53:54 -0700250 @Override
tomd1900f32014-09-03 14:08:16 -0700251 public void switchAdded(Dpid dpid) {
alshabib6f5460b2014-09-03 14:46:17 -0700252 if (providerService == null) {
253 return;
254 }
tom782a7cf2014-09-11 23:58:38 -0700255 DeviceId did = deviceId(uri(dpid));
alshabib6f5460b2014-09-03 14:46:17 -0700256 OpenFlowSwitch sw = controller.getSwitch(dpid);
257
Praseed Balakrishnana22eadf2014-10-20 14:21:45 -0700258 Device.Type deviceType = sw.isOptical() ? Device.Type.ROADM :
259 Device.Type.SWITCH;
alshabib7911a052014-10-16 17:49:37 -0700260 ChassisId cId = new ChassisId(dpid.value());
Ray Milkeye53f1712015-01-16 09:17:16 -0800261
Thomas Vachuska82041f52014-11-30 22:14:02 -0800262 SparseAnnotations annotations = DefaultAnnotations.builder()
Ray Milkeye53f1712015-01-16 09:17:16 -0800263 .set("protocol", sw.factory().getVersion().toString())
264 .set("channelId", sw.channelId())
265 .build();
266
tomd1900f32014-09-03 14:08:16 -0700267 DeviceDescription description =
Praseed Balakrishnana22eadf2014-10-20 14:21:45 -0700268 new DefaultDeviceDescription(did.uri(), deviceType,
Ray Milkeyd3edd032015-01-16 11:38:58 -0800269 sw.manufacturerDescription(),
tom782a7cf2014-09-11 23:58:38 -0700270 sw.hardwareDescription(),
271 sw.softwareDescription(),
alshabib7911a052014-10-16 17:49:37 -0700272 sw.serialNumber(),
Thomas Vachuska82041f52014-11-30 22:14:02 -0800273 cId, annotations);
tom782a7cf2014-09-11 23:58:38 -0700274 providerService.deviceConnected(did, description);
Ayaka Koshibe5460d622015-05-14 12:19:19 -0700275 providerService.updatePorts(did, buildPortDescriptions(sw));
sangho538108b2015-04-08 14:29:20 -0700276
Thomas Vachuskafc52fec2015-05-18 19:13:56 -0700277 PortStatsCollector psc =
278 new PortStatsCollector(controller.getSwitch(dpid), POLL_INTERVAL);
sangho538108b2015-04-08 14:29:20 -0700279 psc.start();
280 collectors.put(dpid, psc);
alshabib25c8eec2014-09-04 16:41:31 -0700281 }
282
alshabibf1216ed2014-09-03 11:53:54 -0700283 @Override
284 public void switchRemoved(Dpid dpid) {
alshabib6f5460b2014-09-03 14:46:17 -0700285 if (providerService == null) {
286 return;
287 }
tom782a7cf2014-09-11 23:58:38 -0700288 providerService.deviceDisconnected(deviceId(uri(dpid)));
alshabibf1216ed2014-09-03 11:53:54 -0700289
sangho538108b2015-04-08 14:29:20 -0700290 PortStatsCollector collector = collectors.remove(dpid);
291 if (collector != null) {
292 collector.stop();
293 }
294 }
Ayaka Koshibe38594c22014-10-22 13:36:12 -0700295
296 @Override
297 public void switchChanged(Dpid dpid) {
298 if (providerService == null) {
299 return;
300 }
301 DeviceId did = deviceId(uri(dpid));
302 OpenFlowSwitch sw = controller.getSwitch(dpid);
Ayaka Koshibe5460d622015-05-14 12:19:19 -0700303 providerService.updatePorts(did, buildPortDescriptions(sw));
Ayaka Koshibe38594c22014-10-22 13:36:12 -0700304 }
305
alshabiba14f3642014-09-05 09:31:31 -0700306 @Override
307 public void portChanged(Dpid dpid, OFPortStatus status) {
alshabibafc514a2014-12-01 14:44:05 -0800308 PortDescription portDescription = buildPortDescription(status);
tom782a7cf2014-09-11 23:58:38 -0700309 providerService.portStatusChanged(deviceId(uri(dpid)), portDescription);
alshabibf1216ed2014-09-03 11:53:54 -0700310 }
311
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700312 @Override
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700313 public void receivedRoleReply(Dpid dpid, RoleState requested, RoleState response) {
314 MastershipRole request = roleOf(requested);
315 MastershipRole reply = roleOf(response);
316
317 providerService.receivedRoleReply(deviceId(uri(dpid)), request, reply);
318 }
319
320 /**
321 * Translates a RoleState to the corresponding MastershipRole.
322 *
Thomas Vachuskafc52fec2015-05-18 19:13:56 -0700323 * @param response role state
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700324 * @return a MastershipRole
325 */
326 private MastershipRole roleOf(RoleState response) {
327 switch (response) {
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700328 case MASTER:
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700329 return MastershipRole.MASTER;
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700330 case EQUAL:
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700331 return MastershipRole.STANDBY;
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700332 case SLAVE:
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700333 return MastershipRole.NONE;
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700334 default:
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700335 LOG.warn("unknown role {}", response);
336 return null;
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700337 }
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700338 }
339
alshabiba14f3642014-09-05 09:31:31 -0700340 /**
341 * Builds a list of port descriptions for a given list of ports.
tomff7eb7c2014-09-08 12:49:03 -0700342 *
alshabiba14f3642014-09-05 09:31:31 -0700343 * @return list of portdescriptions
344 */
Ayaka Koshibe5460d622015-05-14 12:19:19 -0700345 private List<PortDescription> buildPortDescriptions(OpenFlowSwitch sw) {
346 final List<PortDescription> portDescs = new ArrayList<>(sw.getPorts().size());
347 sw.getPorts().forEach(port -> portDescs.add(buildPortDescription(port)));
348 if (sw.isOptical()) {
349 OpenFlowOpticalSwitch opsw = (OpenFlowOpticalSwitch) sw;
350 opsw.getPortTypes().forEach(type -> {
Ayaka Koshibe5460d622015-05-14 12:19:19 -0700351 opsw.getPortsOf(type).forEach(
352 op -> {
353 portDescs.add(buildPortDescription(type, (OFPortOptical) op));
354 }
355 );
356 });
alshabib4680bb62014-09-04 17:15:08 -0700357 }
358 return portDescs;
359 }
360
alshabiba14f3642014-09-05 09:31:31 -0700361 /**
Ray Milkeye0fade72015-01-15 13:29:47 -0800362 * Creates an annotation for the port name if one is available.
363 *
364 * @param port description of the port
365 * @return annotation containing the port name if one is found,
366 * null otherwise
367 */
Ayaka Koshibe5460d622015-05-14 12:19:19 -0700368 private SparseAnnotations makePortNameAnnotation(String port) {
Ray Milkeye0fade72015-01-15 13:29:47 -0800369 SparseAnnotations annotations = null;
Ayaka Koshibe5460d622015-05-14 12:19:19 -0700370 String portName = Strings.emptyToNull(port);
Ray Milkeye0fade72015-01-15 13:29:47 -0800371 if (portName != null) {
372 annotations = DefaultAnnotations.builder()
Thomas Vachuskab52a0142015-04-21 17:48:15 -0700373 .set(AnnotationKeys.PORT_NAME, portName).build();
Ray Milkeye0fade72015-01-15 13:29:47 -0800374 }
375 return annotations;
376 }
377
378 /**
Ayaka Koshibe5460d622015-05-14 12:19:19 -0700379 * Build a portDescription from a given Ethernet port description.
tomff7eb7c2014-09-08 12:49:03 -0700380 *
alshabiba14f3642014-09-05 09:31:31 -0700381 * @param port the port to build from.
382 * @return portDescription for the port.
383 */
384 private PortDescription buildPortDescription(OFPortDesc port) {
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700385 PortNumber portNo = PortNumber.portNumber(port.getPortNo().getPortNumber());
386 boolean enabled =
387 !port.getState().contains(OFPortState.LINK_DOWN) &&
388 !port.getConfig().contains(OFPortConfig.PORT_DOWN);
389 Port.Type type = port.getCurr().contains(OFPortFeatures.PF_FIBER) ? FIBER : COPPER;
Ayaka Koshibe5460d622015-05-14 12:19:19 -0700390 SparseAnnotations annotations = makePortNameAnnotation(port.getName());
Ray Milkeye0fade72015-01-15 13:29:47 -0800391 return new DefaultPortDescription(portNo, enabled, type,
392 portSpeed(port), annotations);
alshabiba14f3642014-09-05 09:31:31 -0700393 }
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700394
Ayaka Koshibe5460d622015-05-14 12:19:19 -0700395 /**
396 * Build a portDescription from a given a port description describing some
397 * Optical port.
398 *
399 * @param port description property type.
400 * @param port the port to build from.
401 * @return portDescription for the port.
402 */
403 private PortDescription buildPortDescription(PortDescPropertyType ptype, OFPortOptical port) {
404 // Minimally functional fixture. This needs to be fixed as we add better support.
405 PortNumber portNo = PortNumber.portNumber(port.getPortNo().getPortNumber());
Ayaka Koshibe74b55272015-05-28 15:16:04 -0700406
Ayaka Koshibe5460d622015-05-14 12:19:19 -0700407 boolean enabled = !port.getState().contains(OFPortState.LINK_DOWN)
408 && !port.getConfig().contains(OFPortConfig.PORT_DOWN);
409 SparseAnnotations annotations = makePortNameAnnotation(port.getName());
Ayaka Koshibe5460d622015-05-14 12:19:19 -0700410
411 if (port.getVersion() == OFVersion.OF_13
412 && ptype == PortDescPropertyType.OPTICAL_TRANSPORT) {
413 // At this point, not much is carried in the optical port message.
Ayaka Koshibeae541732015-05-19 13:37:27 -0700414 LOG.debug("Optical transport port message {}", port.toString());
Ayaka Koshibe5460d622015-05-14 12:19:19 -0700415 } else {
416 // removable once 1.4+ support complete.
Ayaka Koshibeae541732015-05-19 13:37:27 -0700417 LOG.debug("Unsupported optical port properties");
Ayaka Koshibe5460d622015-05-14 12:19:19 -0700418 }
Ayaka Koshibe74b55272015-05-28 15:16:04 -0700419 return new DefaultPortDescription(portNo, enabled, FIBER, 0, annotations);
Ayaka Koshibe5460d622015-05-14 12:19:19 -0700420 }
421
alshabibafc514a2014-12-01 14:44:05 -0800422 private PortDescription buildPortDescription(OFPortStatus status) {
423 OFPortDesc port = status.getDesc();
424 if (status.getReason() != OFPortReason.DELETE) {
425 return buildPortDescription(port);
426 } else {
427 PortNumber portNo = PortNumber.portNumber(port.getPortNo().getPortNumber());
428 Port.Type type = port.getCurr().contains(OFPortFeatures.PF_FIBER) ? FIBER : COPPER;
Ayaka Koshibe5460d622015-05-14 12:19:19 -0700429 SparseAnnotations annotations = makePortNameAnnotation(port.getName());
Ray Milkeye0fade72015-01-15 13:29:47 -0800430 return new DefaultPortDescription(portNo, false, type,
431 portSpeed(port), annotations);
alshabibafc514a2014-12-01 14:44:05 -0800432 }
433 }
434
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700435 private long portSpeed(OFPortDesc port) {
436 if (port.getVersion() == OFVersion.OF_13) {
437 return port.getCurrSpeed() / MBPS;
438 }
439
440 PortSpeed portSpeed = PortSpeed.SPEED_NONE;
441 for (OFPortFeatures feat : port.getCurr()) {
442 portSpeed = PortSpeed.max(portSpeed, feat.getPortSpeed());
443 }
Thomas Vachuska98eda532014-10-29 17:31:02 -0700444 return portSpeed.getSpeedBps() / MBPS;
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700445 }
sangho538108b2015-04-08 14:29:20 -0700446
447 @Override
448 public void handleMessage(Dpid dpid, OFMessage msg) {
449 switch (msg.getType()) {
450 case STATS_REPLY:
451 if (((OFStatsReply) msg).getStatsType() == OFStatsType.PORT) {
Thomas Vachuska893bf4b2015-05-29 18:13:18 -0700452 OFPortStatsReply portStatsReply = (OFPortStatsReply) msg;
453 portStatsReplies.addAll(portStatsReply.getEntries());
454 if (!portStatsReply.getFlags().contains(OFStatsReplyFlags.REPLY_MORE)) {
455 pushPortMetrics(dpid, portStatsReplies);
456 portStatsReplies.clear();
457 }
sangho538108b2015-04-08 14:29:20 -0700458 }
459 break;
460 default:
461 break;
462 }
463 }
alshabibf1216ed2014-09-03 11:53:54 -0700464 }
465
tomb5a46e62014-08-26 14:20:00 -0700466}