blob: cb19dc52b3b4e72702d6f0a21dda4b05865246ef [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;
Dusan Pajinbab8a5e2015-07-24 17:37:19 +020025import org.apache.felix.scr.annotations.Modified;
26import org.apache.felix.scr.annotations.Property;
tomb5a46e62014-08-26 14:20:00 -070027import org.apache.felix.scr.annotations.Reference;
28import org.apache.felix.scr.annotations.ReferenceCardinality;
Thomas Vachuskaf0397b52015-05-29 13:50:17 -070029import org.onlab.packet.ChassisId;
Marc De Leenheerb9311372015-07-09 11:36:49 -070030import org.onlab.util.Frequency;
Marc De Leenheerfc913dd2015-07-30 16:04:55 -070031import org.onosproject.cfg.ComponentConfigService;
Marc De Leenheerb9311372015-07-09 11:36:49 -070032import org.onlab.util.Spectrum;
Thomas Vachuskab52a0142015-04-21 17:48:15 -070033import org.onosproject.net.AnnotationKeys;
Marc De Leenheerfc913dd2015-07-30 16:04:55 -070034import org.onosproject.net.ChannelSpacing;
Brian O'Connorabafb502014-12-02 22:26:20 -080035import org.onosproject.net.DefaultAnnotations;
Brian O'Connorabafb502014-12-02 22:26:20 -080036import org.onosproject.net.DeviceId;
Marc De Leenheerfc913dd2015-07-30 16:04:55 -070037import org.onosproject.net.GridType;
Brian O'Connorabafb502014-12-02 22:26:20 -080038import org.onosproject.net.MastershipRole;
Marc De Leenheerfc913dd2015-07-30 16:04:55 -070039import org.onosproject.net.OchSignal;
40import org.onosproject.net.OduSignalType;
Brian O'Connorabafb502014-12-02 22:26:20 -080041import org.onosproject.net.Port;
42import org.onosproject.net.PortNumber;
43import org.onosproject.net.SparseAnnotations;
44import org.onosproject.net.device.DefaultDeviceDescription;
45import org.onosproject.net.device.DefaultPortDescription;
sangho538108b2015-04-08 14:29:20 -070046import org.onosproject.net.device.DefaultPortStatistics;
Brian O'Connorabafb502014-12-02 22:26:20 -080047import org.onosproject.net.device.DeviceDescription;
48import org.onosproject.net.device.DeviceProvider;
49import org.onosproject.net.device.DeviceProviderRegistry;
50import org.onosproject.net.device.DeviceProviderService;
Marc De Leenheerfc913dd2015-07-30 16:04:55 -070051import org.onosproject.net.device.OchPortDescription;
Marc De Leenheerb9311372015-07-09 11:36:49 -070052import org.onosproject.net.device.OmsPortDescription;
Brian O'Connorabafb502014-12-02 22:26:20 -080053import org.onosproject.net.device.PortDescription;
sangho538108b2015-04-08 14:29:20 -070054import org.onosproject.net.device.PortStatistics;
Brian O'Connorabafb502014-12-02 22:26:20 -080055import org.onosproject.net.provider.AbstractProvider;
56import org.onosproject.net.provider.ProviderId;
57import org.onosproject.openflow.controller.Dpid;
58import org.onosproject.openflow.controller.OpenFlowController;
sangho538108b2015-04-08 14:29:20 -070059import org.onosproject.openflow.controller.OpenFlowEventListener;
Ayaka Koshibe5460d622015-05-14 12:19:19 -070060import org.onosproject.openflow.controller.OpenFlowOpticalSwitch;
Brian O'Connorabafb502014-12-02 22:26:20 -080061import org.onosproject.openflow.controller.OpenFlowSwitch;
62import org.onosproject.openflow.controller.OpenFlowSwitchListener;
Ayaka Koshibe5460d622015-05-14 12:19:19 -070063import org.onosproject.openflow.controller.PortDescPropertyType;
Brian O'Connorabafb502014-12-02 22:26:20 -080064import org.onosproject.openflow.controller.RoleState;
Dusan Pajinbab8a5e2015-07-24 17:37:19 +020065import org.osgi.service.component.ComponentContext;
Marc De Leenheerb9311372015-07-09 11:36:49 -070066import org.projectfloodlight.openflow.protocol.OFCalientPortDescStatsEntry;
Ayaka Koshibee8708e32014-10-22 13:40:18 -070067import org.projectfloodlight.openflow.protocol.OFFactory;
sangho538108b2015-04-08 14:29:20 -070068import org.projectfloodlight.openflow.protocol.OFMessage;
alshabib4680bb62014-09-04 17:15:08 -070069import org.projectfloodlight.openflow.protocol.OFPortConfig;
alshabib25c8eec2014-09-04 16:41:31 -070070import org.projectfloodlight.openflow.protocol.OFPortDesc;
Marc De Leenheerfc913dd2015-07-30 16:04:55 -070071import org.projectfloodlight.openflow.protocol.OFPortDescPropOpticalTransport;
Thomas Vachuskad16ce182014-10-29 17:25:29 -070072import org.projectfloodlight.openflow.protocol.OFPortFeatures;
Ayaka Koshibe5460d622015-05-14 12:19:19 -070073import org.projectfloodlight.openflow.protocol.OFPortOptical;
alshabibafc514a2014-12-01 14:44:05 -080074import org.projectfloodlight.openflow.protocol.OFPortReason;
alshabib4680bb62014-09-04 17:15:08 -070075import org.projectfloodlight.openflow.protocol.OFPortState;
sangho538108b2015-04-08 14:29:20 -070076import org.projectfloodlight.openflow.protocol.OFPortStatsEntry;
77import org.projectfloodlight.openflow.protocol.OFPortStatsReply;
alshabiba14f3642014-09-05 09:31:31 -070078import org.projectfloodlight.openflow.protocol.OFPortStatus;
sangho538108b2015-04-08 14:29:20 -070079import org.projectfloodlight.openflow.protocol.OFStatsReply;
Thomas Vachuska893bf4b2015-05-29 18:13:18 -070080import org.projectfloodlight.openflow.protocol.OFStatsReplyFlags;
sangho538108b2015-04-08 14:29:20 -070081import org.projectfloodlight.openflow.protocol.OFStatsType;
Thomas Vachuskad16ce182014-10-29 17:25:29 -070082import org.projectfloodlight.openflow.protocol.OFVersion;
83import org.projectfloodlight.openflow.types.PortSpeed;
tomb5a46e62014-08-26 14:20:00 -070084import org.slf4j.Logger;
tom5f38b3a2014-08-27 23:50:54 -070085
tom782a7cf2014-09-11 23:58:38 -070086import java.util.ArrayList;
sangho538108b2015-04-08 14:29:20 -070087import java.util.Collection;
88import java.util.Collections;
Dusan Pajinbab8a5e2015-07-24 17:37:19 +020089import java.util.Dictionary;
sangho538108b2015-04-08 14:29:20 -070090import java.util.HashMap;
91import java.util.HashSet;
tom782a7cf2014-09-11 23:58:38 -070092import java.util.List;
93
Marc De Leenheerfc913dd2015-07-30 16:04:55 -070094import static com.google.common.base.Preconditions.checkArgument;
Dusan Pajinbab8a5e2015-07-24 17:37:19 +020095import static com.google.common.base.Strings.isNullOrEmpty;
96import static org.onlab.util.Tools.get;
Brian O'Connorabafb502014-12-02 22:26:20 -080097import static org.onosproject.net.DeviceId.deviceId;
98import static org.onosproject.net.Port.Type.COPPER;
99import static org.onosproject.net.Port.Type.FIBER;
100import static org.onosproject.openflow.controller.Dpid.dpid;
101import static org.onosproject.openflow.controller.Dpid.uri;
tom782a7cf2014-09-11 23:58:38 -0700102import static org.slf4j.LoggerFactory.getLogger;
103
tomb5a46e62014-08-26 14:20:00 -0700104/**
tomb1260e42014-08-26 18:39:57 -0700105 * Provider which uses an OpenFlow controller to detect network
tome06f8552014-08-26 16:58:42 -0700106 * infrastructure devices.
tomb5a46e62014-08-26 14:20:00 -0700107 */
tomb1260e42014-08-26 18:39:57 -0700108@Component(immediate = true)
tomab21e7c2014-08-26 15:23:08 -0700109public class OpenFlowDeviceProvider extends AbstractProvider implements DeviceProvider {
tomb5a46e62014-08-26 14:20:00 -0700110
alshabiba89cc582014-09-09 16:43:00 -0700111 private static final Logger LOG = getLogger(OpenFlowDeviceProvider.class);
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700112 private static final long MBPS = 1_000 * 1_000;
tomb5a46e62014-08-26 14:20:00 -0700113
114 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
tom96dfcab2014-08-28 09:26:03 -0700115 protected DeviceProviderRegistry providerRegistry;
tomab21e7c2014-08-26 15:23:08 -0700116
tom5f38b3a2014-08-27 23:50:54 -0700117 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
118 protected OpenFlowController controller;
119
Dusan Pajinbab8a5e2015-07-24 17:37:19 +0200120 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
121 protected ComponentConfigService cfgService;
122
tomab21e7c2014-08-26 15:23:08 -0700123 private DeviceProviderService providerService;
tomb5a46e62014-08-26 14:20:00 -0700124
sangho538108b2015-04-08 14:29:20 -0700125 private final InternalDeviceProvider listener = new InternalDeviceProvider();
126
127 // TODO: We need to make the poll interval configurable.
Srikanth Vavilapalli78baf582015-06-05 11:40:14 -0700128 static final int POLL_INTERVAL = 5;
Dusan Pajinbab8a5e2015-07-24 17:37:19 +0200129 @Property(name = "PortStatsPollFrequency", intValue = POLL_INTERVAL,
130 label = "Frequency (in seconds) for polling switch Port statistics")
131 private int portStatsPollFrequency = POLL_INTERVAL;
sangho538108b2015-04-08 14:29:20 -0700132
133 private HashMap<Dpid, PortStatsCollector> collectors = Maps.newHashMap();
tomd40fc7a2014-09-04 16:41:10 -0700134
tomab21e7c2014-08-26 15:23:08 -0700135 /**
136 * Creates an OpenFlow device provider.
137 */
138 public OpenFlowDeviceProvider() {
Brian O'Connorabafb502014-12-02 22:26:20 -0800139 super(new ProviderId("of", "org.onosproject.provider.openflow"));
tomab21e7c2014-08-26 15:23:08 -0700140 }
141
tomb5a46e62014-08-26 14:20:00 -0700142 @Activate
Dusan Pajinbab8a5e2015-07-24 17:37:19 +0200143 public void activate(ComponentContext context) {
144 cfgService.registerProperties(getClass());
tom96dfcab2014-08-28 09:26:03 -0700145 providerService = providerRegistry.register(this);
tomd40fc7a2014-09-04 16:41:10 -0700146 controller.addListener(listener);
sangho538108b2015-04-08 14:29:20 -0700147 controller.addEventListener(listener);
alshabibc944fd02014-09-10 17:55:17 -0700148 for (OpenFlowSwitch sw : controller.getSwitches()) {
Yuta HIGUCHIac2972a2014-11-18 12:58:52 -0800149 try {
150 listener.switchAdded(new Dpid(sw.getId()));
151 } catch (Exception e) {
152 LOG.warn("Failed initially adding {} : {}", sw.getStringId(), e.getMessage());
153 LOG.debug("Error details:", e);
154 // disconnect to trigger switch-add later
155 sw.disconnectSwitch();
156 }
Dusan Pajinbab8a5e2015-07-24 17:37:19 +0200157 PortStatsCollector psc = new PortStatsCollector(sw, portStatsPollFrequency);
sangho538108b2015-04-08 14:29:20 -0700158 psc.start();
159 collectors.put(new Dpid(sw.getId()), psc);
alshabibc944fd02014-09-10 17:55:17 -0700160 }
alshabiba89cc582014-09-09 16:43:00 -0700161 LOG.info("Started");
tomb5a46e62014-08-26 14:20:00 -0700162 }
163
164 @Deactivate
Dusan Pajinbab8a5e2015-07-24 17:37:19 +0200165 public void deactivate(ComponentContext context) {
166 cfgService.unregisterProperties(getClass(), false);
tom96dfcab2014-08-28 09:26:03 -0700167 providerRegistry.unregister(this);
tomd40fc7a2014-09-04 16:41:10 -0700168 controller.removeListener(listener);
Thomas Vachuska74bcd922015-06-02 13:08:57 -0700169 collectors.values().forEach(PortStatsCollector::stop);
tomab21e7c2014-08-26 15:23:08 -0700170 providerService = null;
alshabiba89cc582014-09-09 16:43:00 -0700171 LOG.info("Stopped");
tomb5a46e62014-08-26 14:20:00 -0700172 }
173
Dusan Pajinbab8a5e2015-07-24 17:37:19 +0200174 @Modified
175 public void modified(ComponentContext context) {
176 Dictionary<?, ?> properties = context.getProperties();
177 int newPortStatsPollFrequency;
178 try {
179 String s = get(properties, "PortStatsPollFrequency");
180 newPortStatsPollFrequency = isNullOrEmpty(s) ? portStatsPollFrequency : Integer.parseInt(s.trim());
181
182 } catch (NumberFormatException | ClassCastException e) {
183 newPortStatsPollFrequency = portStatsPollFrequency;
184 }
185
186 if (newPortStatsPollFrequency != portStatsPollFrequency) {
187 portStatsPollFrequency = newPortStatsPollFrequency;
188 collectors.values().forEach(psc -> psc.adjustPollInterval(portStatsPollFrequency));
189 }
190
191 LOG.info("Settings: portStatsPollFrequency={}", portStatsPollFrequency);
192 }
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700193
194 @Override
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700195 public boolean isReachable(DeviceId deviceId) {
196 OpenFlowSwitch sw = controller.getSwitch(dpid(deviceId.uri()));
Yuta HIGUCHI69a27352014-10-31 15:48:37 -0700197 if (sw == null || !sw.isConnected()) {
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700198 return false;
199 }
200 return true;
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700201 }
202
tomab21e7c2014-08-26 15:23:08 -0700203 @Override
Ayaka Koshibe78bcbc12014-11-19 14:28:58 -0800204 public void triggerProbe(DeviceId deviceId) {
Madan Jampanic3328762015-05-31 12:39:27 -0700205 LOG.debug("Triggering probe on device {}", deviceId);
Ayaka Koshibee8708e32014-10-22 13:40:18 -0700206
Yuta HIGUCHI802d0e82014-10-31 16:07:37 -0700207 final Dpid dpid = dpid(deviceId.uri());
208 OpenFlowSwitch sw = controller.getSwitch(dpid);
209 if (sw == null || !sw.isConnected()) {
Ayaka Koshibe78bcbc12014-11-19 14:28:58 -0800210 LOG.error("Failed to probe device {} on sw={}", deviceId, sw);
Yuta HIGUCHI802d0e82014-10-31 16:07:37 -0700211 providerService.deviceDisconnected(deviceId);
Thomas Vachuska74bcd922015-06-02 13:08:57 -0700212 return;
Yuta HIGUCHI802d0e82014-10-31 16:07:37 -0700213 } else {
Ayaka Koshibe78bcbc12014-11-19 14:28:58 -0800214 LOG.trace("Confirmed device {} connection", deviceId);
Yuta HIGUCHI802d0e82014-10-31 16:07:37 -0700215 }
Ayaka Koshibee8708e32014-10-22 13:40:18 -0700216
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700217 // Prompt an update of port information. We can use any XID for this.
Ayaka Koshibee8708e32014-10-22 13:40:18 -0700218 OFFactory fact = sw.factory();
219 switch (fact.getVersion()) {
220 case OF_10:
221 sw.sendMsg(fact.buildFeaturesRequest().setXid(0).build());
222 break;
223 case OF_13:
224 sw.sendMsg(fact.buildPortDescStatsRequest().setXid(0).build());
225 break;
226 default:
227 LOG.warn("Unhandled protocol version");
228 }
tomab21e7c2014-08-26 15:23:08 -0700229 }
230
231 @Override
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700232 public void roleChanged(DeviceId deviceId, MastershipRole newRole) {
alshabibf1216ed2014-09-03 11:53:54 -0700233 switch (newRole) {
tom782a7cf2014-09-11 23:58:38 -0700234 case MASTER:
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700235 controller.setRole(dpid(deviceId.uri()), RoleState.MASTER);
tom782a7cf2014-09-11 23:58:38 -0700236 break;
237 case STANDBY:
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700238 controller.setRole(dpid(deviceId.uri()), RoleState.EQUAL);
tom782a7cf2014-09-11 23:58:38 -0700239 break;
240 case NONE:
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700241 controller.setRole(dpid(deviceId.uri()), RoleState.SLAVE);
tom782a7cf2014-09-11 23:58:38 -0700242 break;
243 default:
244 LOG.error("Unknown Mastership state : {}", newRole);
alshabibf1216ed2014-09-03 11:53:54 -0700245
246 }
Madan Jampanic3328762015-05-31 12:39:27 -0700247 LOG.debug("Accepting mastership role change for device {}", deviceId);
tomab21e7c2014-08-26 15:23:08 -0700248 }
249
Thomas Vachuska893bf4b2015-05-29 18:13:18 -0700250 private void pushPortMetrics(Dpid dpid, List<OFPortStatsEntry> portStatsEntries) {
sangho538108b2015-04-08 14:29:20 -0700251 DeviceId deviceId = DeviceId.deviceId(dpid.uri(dpid));
Thomas Vachuska893bf4b2015-05-29 18:13:18 -0700252 Collection<PortStatistics> stats = buildPortStatistics(deviceId, portStatsEntries);
sangho538108b2015-04-08 14:29:20 -0700253 providerService.updatePortStatistics(deviceId, stats);
254 }
255
Thomas Vachuska893bf4b2015-05-29 18:13:18 -0700256 private Collection<PortStatistics> buildPortStatistics(DeviceId deviceId,
257 List<OFPortStatsEntry> entries) {
sangho538108b2015-04-08 14:29:20 -0700258 HashSet<PortStatistics> stats = Sets.newHashSet();
259
Thomas Vachuska893bf4b2015-05-29 18:13:18 -0700260 for (OFPortStatsEntry entry : entries) {
Thomas Vachuskaf0397b52015-05-29 13:50:17 -0700261 try {
Thomas Vachuska4e6025e2015-06-03 11:25:21 -0700262 if (entry == null || entry.getPortNo() == null || entry.getPortNo().getPortNumber() < 0) {
Thomas Vachuskaf0397b52015-05-29 13:50:17 -0700263 continue;
264 }
265 DefaultPortStatistics.Builder builder = DefaultPortStatistics.builder();
266 DefaultPortStatistics stat = builder.setDeviceId(deviceId)
267 .setPort(entry.getPortNo().getPortNumber())
268 .setPacketsReceived(entry.getRxPackets().getValue())
269 .setPacketsSent(entry.getTxPackets().getValue())
270 .setBytesReceived(entry.getRxBytes().getValue())
271 .setBytesSent(entry.getTxBytes().getValue())
272 .setPacketsRxDropped(entry.getRxDropped().getValue())
273 .setPacketsTxDropped(entry.getTxDropped().getValue())
274 .setPacketsRxErrors(entry.getRxErrors().getValue())
275 .setPacketsTxErrors(entry.getTxErrors().getValue())
276 .setDurationSec(entry.getVersion() == OFVersion.OF_10 ? 0 : entry.getDurationSec())
277 .setDurationNano(entry.getVersion() == OFVersion.OF_10 ? 0 : entry.getDurationNsec())
278 .build();
sangho538108b2015-04-08 14:29:20 -0700279
Thomas Vachuskaf0397b52015-05-29 13:50:17 -0700280 stats.add(stat);
281 } catch (Exception e) {
282 LOG.warn("Unable to process port stats", e);
283 }
sangho538108b2015-04-08 14:29:20 -0700284 }
285
286 return Collections.unmodifiableSet(stats);
287
288 }
289
290 private class InternalDeviceProvider implements OpenFlowSwitchListener, OpenFlowEventListener {
Thomas Vachuska893bf4b2015-05-29 18:13:18 -0700291
Srikanth Vavilapalli78baf582015-06-05 11:40:14 -0700292 private HashMap<Dpid, List<OFPortStatsEntry>> portStatsReplies = new HashMap<>();
Thomas Vachuska893bf4b2015-05-29 18:13:18 -0700293
alshabibf1216ed2014-09-03 11:53:54 -0700294 @Override
tomd1900f32014-09-03 14:08:16 -0700295 public void switchAdded(Dpid dpid) {
alshabib6f5460b2014-09-03 14:46:17 -0700296 if (providerService == null) {
297 return;
298 }
tom782a7cf2014-09-11 23:58:38 -0700299 DeviceId did = deviceId(uri(dpid));
alshabib6f5460b2014-09-03 14:46:17 -0700300 OpenFlowSwitch sw = controller.getSwitch(dpid);
301
alshabib7911a052014-10-16 17:49:37 -0700302 ChassisId cId = new ChassisId(dpid.value());
Ray Milkeye53f1712015-01-16 09:17:16 -0800303
Thomas Vachuska82041f52014-11-30 22:14:02 -0800304 SparseAnnotations annotations = DefaultAnnotations.builder()
Ray Milkeye53f1712015-01-16 09:17:16 -0800305 .set("protocol", sw.factory().getVersion().toString())
306 .set("channelId", sw.channelId())
307 .build();
308
tomd1900f32014-09-03 14:08:16 -0700309 DeviceDescription description =
Marc De Leenheerb9311372015-07-09 11:36:49 -0700310 new DefaultDeviceDescription(did.uri(), sw.deviceType(),
Ray Milkeyd3edd032015-01-16 11:38:58 -0800311 sw.manufacturerDescription(),
tom782a7cf2014-09-11 23:58:38 -0700312 sw.hardwareDescription(),
313 sw.softwareDescription(),
alshabib7911a052014-10-16 17:49:37 -0700314 sw.serialNumber(),
Thomas Vachuska82041f52014-11-30 22:14:02 -0800315 cId, annotations);
tom782a7cf2014-09-11 23:58:38 -0700316 providerService.deviceConnected(did, description);
Ayaka Koshibe5460d622015-05-14 12:19:19 -0700317 providerService.updatePorts(did, buildPortDescriptions(sw));
sangho538108b2015-04-08 14:29:20 -0700318
Thomas Vachuskafc52fec2015-05-18 19:13:56 -0700319 PortStatsCollector psc =
Dusan Pajinbab8a5e2015-07-24 17:37:19 +0200320 new PortStatsCollector(controller.getSwitch(dpid), portStatsPollFrequency);
sangho538108b2015-04-08 14:29:20 -0700321 psc.start();
322 collectors.put(dpid, psc);
alshabib25c8eec2014-09-04 16:41:31 -0700323 }
324
alshabibf1216ed2014-09-03 11:53:54 -0700325 @Override
326 public void switchRemoved(Dpid dpid) {
alshabib6f5460b2014-09-03 14:46:17 -0700327 if (providerService == null) {
328 return;
329 }
tom782a7cf2014-09-11 23:58:38 -0700330 providerService.deviceDisconnected(deviceId(uri(dpid)));
alshabibf1216ed2014-09-03 11:53:54 -0700331
sangho538108b2015-04-08 14:29:20 -0700332 PortStatsCollector collector = collectors.remove(dpid);
333 if (collector != null) {
334 collector.stop();
335 }
336 }
Ayaka Koshibe38594c22014-10-22 13:36:12 -0700337
338 @Override
339 public void switchChanged(Dpid dpid) {
340 if (providerService == null) {
341 return;
342 }
343 DeviceId did = deviceId(uri(dpid));
344 OpenFlowSwitch sw = controller.getSwitch(dpid);
Ayaka Koshibe5460d622015-05-14 12:19:19 -0700345 providerService.updatePorts(did, buildPortDescriptions(sw));
Ayaka Koshibe38594c22014-10-22 13:36:12 -0700346 }
347
alshabiba14f3642014-09-05 09:31:31 -0700348 @Override
349 public void portChanged(Dpid dpid, OFPortStatus status) {
alshabibafc514a2014-12-01 14:44:05 -0800350 PortDescription portDescription = buildPortDescription(status);
tom782a7cf2014-09-11 23:58:38 -0700351 providerService.portStatusChanged(deviceId(uri(dpid)), portDescription);
alshabibf1216ed2014-09-03 11:53:54 -0700352 }
353
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700354 @Override
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700355 public void receivedRoleReply(Dpid dpid, RoleState requested, RoleState response) {
356 MastershipRole request = roleOf(requested);
357 MastershipRole reply = roleOf(response);
358
359 providerService.receivedRoleReply(deviceId(uri(dpid)), request, reply);
360 }
361
362 /**
363 * Translates a RoleState to the corresponding MastershipRole.
364 *
Thomas Vachuskafc52fec2015-05-18 19:13:56 -0700365 * @param response role state
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700366 * @return a MastershipRole
367 */
368 private MastershipRole roleOf(RoleState response) {
369 switch (response) {
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700370 case MASTER:
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700371 return MastershipRole.MASTER;
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700372 case EQUAL:
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700373 return MastershipRole.STANDBY;
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700374 case SLAVE:
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700375 return MastershipRole.NONE;
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700376 default:
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700377 LOG.warn("unknown role {}", response);
378 return null;
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700379 }
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700380 }
381
alshabiba14f3642014-09-05 09:31:31 -0700382 /**
383 * Builds a list of port descriptions for a given list of ports.
tomff7eb7c2014-09-08 12:49:03 -0700384 *
alshabiba14f3642014-09-05 09:31:31 -0700385 * @return list of portdescriptions
386 */
Ayaka Koshibe5460d622015-05-14 12:19:19 -0700387 private List<PortDescription> buildPortDescriptions(OpenFlowSwitch sw) {
388 final List<PortDescription> portDescs = new ArrayList<>(sw.getPorts().size());
389 sw.getPorts().forEach(port -> portDescs.add(buildPortDescription(port)));
Marc De Leenheerb9311372015-07-09 11:36:49 -0700390
391 OpenFlowOpticalSwitch opsw;
392 switch (sw.deviceType()) {
393 case ROADM:
394 opsw = (OpenFlowOpticalSwitch) sw;
395 opsw.getPortTypes().forEach(type -> {
396 opsw.getPortsOf(type).forEach(
397 op -> {
398 portDescs.add(buildPortDescription(type, (OFPortOptical) op));
399 }
400 );
401 });
402 break;
403 case FIBER_SWITCH:
404 opsw = (OpenFlowOpticalSwitch) sw;
405 opsw.getPortTypes().forEach(type -> {
406 opsw.getPortsOf(type).forEach(
407 op -> {
408 portDescs.add(buildPortDescription((OFCalientPortDescStatsEntry) op));
409 }
410 );
411 });
412 break;
413 default:
414 break;
alshabib4680bb62014-09-04 17:15:08 -0700415 }
Marc De Leenheerb9311372015-07-09 11:36:49 -0700416
alshabib4680bb62014-09-04 17:15:08 -0700417 return portDescs;
418 }
419
alshabiba14f3642014-09-05 09:31:31 -0700420 /**
Ray Milkeye0fade72015-01-15 13:29:47 -0800421 * Creates an annotation for the port name if one is available.
422 *
423 * @param port description of the port
424 * @return annotation containing the port name if one is found,
425 * null otherwise
426 */
Ayaka Koshibe5460d622015-05-14 12:19:19 -0700427 private SparseAnnotations makePortNameAnnotation(String port) {
Ray Milkeye0fade72015-01-15 13:29:47 -0800428 SparseAnnotations annotations = null;
Ayaka Koshibe5460d622015-05-14 12:19:19 -0700429 String portName = Strings.emptyToNull(port);
Ray Milkeye0fade72015-01-15 13:29:47 -0800430 if (portName != null) {
431 annotations = DefaultAnnotations.builder()
Thomas Vachuskab52a0142015-04-21 17:48:15 -0700432 .set(AnnotationKeys.PORT_NAME, portName).build();
Ray Milkeye0fade72015-01-15 13:29:47 -0800433 }
434 return annotations;
435 }
436
437 /**
Ayaka Koshibe5460d622015-05-14 12:19:19 -0700438 * Build a portDescription from a given Ethernet port description.
tomff7eb7c2014-09-08 12:49:03 -0700439 *
alshabiba14f3642014-09-05 09:31:31 -0700440 * @param port the port to build from.
441 * @return portDescription for the port.
442 */
443 private PortDescription buildPortDescription(OFPortDesc port) {
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700444 PortNumber portNo = PortNumber.portNumber(port.getPortNo().getPortNumber());
445 boolean enabled =
446 !port.getState().contains(OFPortState.LINK_DOWN) &&
447 !port.getConfig().contains(OFPortConfig.PORT_DOWN);
448 Port.Type type = port.getCurr().contains(OFPortFeatures.PF_FIBER) ? FIBER : COPPER;
Ayaka Koshibe5460d622015-05-14 12:19:19 -0700449 SparseAnnotations annotations = makePortNameAnnotation(port.getName());
Ray Milkeye0fade72015-01-15 13:29:47 -0800450 return new DefaultPortDescription(portNo, enabled, type,
451 portSpeed(port), annotations);
alshabiba14f3642014-09-05 09:31:31 -0700452 }
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700453
Ayaka Koshibe5460d622015-05-14 12:19:19 -0700454 /**
455 * Build a portDescription from a given a port description describing some
456 * Optical port.
457 *
458 * @param port description property type.
459 * @param port the port to build from.
460 * @return portDescription for the port.
461 */
462 private PortDescription buildPortDescription(PortDescPropertyType ptype, OFPortOptical port) {
Marc De Leenheerfc913dd2015-07-30 16:04:55 -0700463 checkArgument(port.getDesc().size() >= 1);
464
Ayaka Koshibe5460d622015-05-14 12:19:19 -0700465 // Minimally functional fixture. This needs to be fixed as we add better support.
466 PortNumber portNo = PortNumber.portNumber(port.getPortNo().getPortNumber());
Ayaka Koshibe74b55272015-05-28 15:16:04 -0700467
Ayaka Koshibe5460d622015-05-14 12:19:19 -0700468 boolean enabled = !port.getState().contains(OFPortState.LINK_DOWN)
469 && !port.getConfig().contains(OFPortConfig.PORT_DOWN);
470 SparseAnnotations annotations = makePortNameAnnotation(port.getName());
Ayaka Koshibe5460d622015-05-14 12:19:19 -0700471
472 if (port.getVersion() == OFVersion.OF_13
473 && ptype == PortDescPropertyType.OPTICAL_TRANSPORT) {
474 // At this point, not much is carried in the optical port message.
Ayaka Koshibeae541732015-05-19 13:37:27 -0700475 LOG.debug("Optical transport port message {}", port.toString());
Ayaka Koshibe5460d622015-05-14 12:19:19 -0700476 } else {
477 // removable once 1.4+ support complete.
Ayaka Koshibeae541732015-05-19 13:37:27 -0700478 LOG.debug("Unsupported optical port properties");
Ayaka Koshibe5460d622015-05-14 12:19:19 -0700479 }
Marc De Leenheerfc913dd2015-07-30 16:04:55 -0700480
481 OFPortDescPropOpticalTransport desc = port.getDesc().get(0);
482 switch (desc.getPortSignalType()) {
483 // FIXME: use constants once loxi has full optical extensions
484 case 2: // OMS port
485 // Assume complete optical spectrum and 50 GHz grid
486 // LINC-OE is only supported optical OF device for now
487 return new OmsPortDescription(portNo, enabled,
488 Spectrum.U_BAND_MIN, Spectrum.O_BAND_MAX, Frequency.ofGHz(50), annotations);
489 case 5: // OCH port
490 OchSignal signal = new OchSignal(GridType.DWDM, ChannelSpacing.CHL_50GHZ, 0, 4);
491 return new OchPortDescription(portNo, enabled, OduSignalType.ODU4,
492 true, signal, annotations);
493 default:
494 break;
495 }
496
Ayaka Koshibe74b55272015-05-28 15:16:04 -0700497 return new DefaultPortDescription(portNo, enabled, FIBER, 0, annotations);
Ayaka Koshibe5460d622015-05-14 12:19:19 -0700498 }
499
Marc De Leenheerb9311372015-07-09 11:36:49 -0700500 /**
501 * Build a portDescription from a given port description describing a fiber switch optical port.
502 *
503 * @param port description property type.
504 * @param port the port to build from.
505 * @return portDescription for the port.
506 */
507 private PortDescription buildPortDescription(OFCalientPortDescStatsEntry port) {
508 PortNumber portNo = PortNumber.portNumber(port.getPortNo().getPortNumber());
509
510 // FIXME when Calient OF agent reports port status
511 boolean enabled = true;
512 SparseAnnotations annotations = makePortNameAnnotation(port.getName());
513
Marc De Leenheerfc913dd2015-07-30 16:04:55 -0700514 // S160 data sheet
515 // Wavelength range: 1260 - 1630 nm, grid is irrelevant for this type of switch
516 return new OmsPortDescription(portNo, enabled,
517 Spectrum.U_BAND_MIN, Spectrum.O_BAND_MAX, Frequency.ofGHz(100), annotations);
Marc De Leenheerb9311372015-07-09 11:36:49 -0700518 }
519
alshabibafc514a2014-12-01 14:44:05 -0800520 private PortDescription buildPortDescription(OFPortStatus status) {
521 OFPortDesc port = status.getDesc();
522 if (status.getReason() != OFPortReason.DELETE) {
523 return buildPortDescription(port);
524 } else {
525 PortNumber portNo = PortNumber.portNumber(port.getPortNo().getPortNumber());
526 Port.Type type = port.getCurr().contains(OFPortFeatures.PF_FIBER) ? FIBER : COPPER;
Ayaka Koshibe5460d622015-05-14 12:19:19 -0700527 SparseAnnotations annotations = makePortNameAnnotation(port.getName());
Ray Milkeye0fade72015-01-15 13:29:47 -0800528 return new DefaultPortDescription(portNo, false, type,
529 portSpeed(port), annotations);
alshabibafc514a2014-12-01 14:44:05 -0800530 }
531 }
532
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700533 private long portSpeed(OFPortDesc port) {
534 if (port.getVersion() == OFVersion.OF_13) {
535 return port.getCurrSpeed() / MBPS;
536 }
537
538 PortSpeed portSpeed = PortSpeed.SPEED_NONE;
539 for (OFPortFeatures feat : port.getCurr()) {
540 portSpeed = PortSpeed.max(portSpeed, feat.getPortSpeed());
541 }
Thomas Vachuska98eda532014-10-29 17:31:02 -0700542 return portSpeed.getSpeedBps() / MBPS;
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700543 }
sangho538108b2015-04-08 14:29:20 -0700544
545 @Override
546 public void handleMessage(Dpid dpid, OFMessage msg) {
547 switch (msg.getType()) {
548 case STATS_REPLY:
549 if (((OFStatsReply) msg).getStatsType() == OFStatsType.PORT) {
Thomas Vachuska893bf4b2015-05-29 18:13:18 -0700550 OFPortStatsReply portStatsReply = (OFPortStatsReply) msg;
Srikanth Vavilapalli78baf582015-06-05 11:40:14 -0700551 List<OFPortStatsEntry> portStatsReplyList = portStatsReplies.get(dpid);
552 if (portStatsReplyList == null) {
553 portStatsReplyList = Lists.newArrayList();
554 }
555 portStatsReplyList.addAll(portStatsReply.getEntries());
556 portStatsReplies.put(dpid, portStatsReplyList);
Thomas Vachuska893bf4b2015-05-29 18:13:18 -0700557 if (!portStatsReply.getFlags().contains(OFStatsReplyFlags.REPLY_MORE)) {
Srikanth Vavilapalli78baf582015-06-05 11:40:14 -0700558 pushPortMetrics(dpid, portStatsReplies.get(dpid));
559 portStatsReplies.get(dpid).clear();
Thomas Vachuska893bf4b2015-05-29 18:13:18 -0700560 }
sangho538108b2015-04-08 14:29:20 -0700561 }
562 break;
563 default:
564 break;
565 }
566 }
alshabibf1216ed2014-09-03 11:53:54 -0700567 }
568
tomb5a46e62014-08-26 14:20:00 -0700569}