blob: 9177e7000084143b46132e44d8df9f13e0b4aee5 [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;
Dusan Pajinbab8a5e2015-07-24 17:37:19 +020022
tomb5a46e62014-08-26 14:20:00 -070023import org.apache.felix.scr.annotations.Activate;
24import org.apache.felix.scr.annotations.Component;
25import org.apache.felix.scr.annotations.Deactivate;
Dusan Pajinbab8a5e2015-07-24 17:37:19 +020026import org.apache.felix.scr.annotations.Modified;
27import org.apache.felix.scr.annotations.Property;
tomb5a46e62014-08-26 14:20:00 -070028import org.apache.felix.scr.annotations.Reference;
29import org.apache.felix.scr.annotations.ReferenceCardinality;
Thomas Vachuskaf0397b52015-05-29 13:50:17 -070030import org.onlab.packet.ChassisId;
Dusan Pajinbab8a5e2015-07-24 17:37:19 +020031import org.onosproject.cfg.ComponentConfigService;
Marc De Leenheerb9311372015-07-09 11:36:49 -070032import org.onlab.util.Frequency;
33import org.onlab.util.Spectrum;
Thomas Vachuskab52a0142015-04-21 17:48:15 -070034import org.onosproject.net.AnnotationKeys;
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;
37import org.onosproject.net.MastershipRole;
38import org.onosproject.net.Port;
39import org.onosproject.net.PortNumber;
40import org.onosproject.net.SparseAnnotations;
41import org.onosproject.net.device.DefaultDeviceDescription;
42import org.onosproject.net.device.DefaultPortDescription;
sangho538108b2015-04-08 14:29:20 -070043import org.onosproject.net.device.DefaultPortStatistics;
Brian O'Connorabafb502014-12-02 22:26:20 -080044import org.onosproject.net.device.DeviceDescription;
45import org.onosproject.net.device.DeviceProvider;
46import org.onosproject.net.device.DeviceProviderRegistry;
47import org.onosproject.net.device.DeviceProviderService;
Marc De Leenheerb9311372015-07-09 11:36:49 -070048import org.onosproject.net.device.OmsPortDescription;
Brian O'Connorabafb502014-12-02 22:26:20 -080049import org.onosproject.net.device.PortDescription;
sangho538108b2015-04-08 14:29:20 -070050import org.onosproject.net.device.PortStatistics;
Brian O'Connorabafb502014-12-02 22:26:20 -080051import org.onosproject.net.provider.AbstractProvider;
52import org.onosproject.net.provider.ProviderId;
53import org.onosproject.openflow.controller.Dpid;
54import org.onosproject.openflow.controller.OpenFlowController;
sangho538108b2015-04-08 14:29:20 -070055import org.onosproject.openflow.controller.OpenFlowEventListener;
Ayaka Koshibe5460d622015-05-14 12:19:19 -070056import org.onosproject.openflow.controller.OpenFlowOpticalSwitch;
Brian O'Connorabafb502014-12-02 22:26:20 -080057import org.onosproject.openflow.controller.OpenFlowSwitch;
58import org.onosproject.openflow.controller.OpenFlowSwitchListener;
Ayaka Koshibe5460d622015-05-14 12:19:19 -070059import org.onosproject.openflow.controller.PortDescPropertyType;
Brian O'Connorabafb502014-12-02 22:26:20 -080060import org.onosproject.openflow.controller.RoleState;
Dusan Pajinbab8a5e2015-07-24 17:37:19 +020061import org.osgi.service.component.ComponentContext;
Marc De Leenheerb9311372015-07-09 11:36:49 -070062import org.projectfloodlight.openflow.protocol.OFCalientPortDescStatsEntry;
Ayaka Koshibee8708e32014-10-22 13:40:18 -070063import org.projectfloodlight.openflow.protocol.OFFactory;
sangho538108b2015-04-08 14:29:20 -070064import org.projectfloodlight.openflow.protocol.OFMessage;
alshabib4680bb62014-09-04 17:15:08 -070065import org.projectfloodlight.openflow.protocol.OFPortConfig;
alshabib25c8eec2014-09-04 16:41:31 -070066import org.projectfloodlight.openflow.protocol.OFPortDesc;
Thomas Vachuskad16ce182014-10-29 17:25:29 -070067import org.projectfloodlight.openflow.protocol.OFPortFeatures;
Ayaka Koshibe5460d622015-05-14 12:19:19 -070068import org.projectfloodlight.openflow.protocol.OFPortOptical;
alshabibafc514a2014-12-01 14:44:05 -080069import org.projectfloodlight.openflow.protocol.OFPortReason;
alshabib4680bb62014-09-04 17:15:08 -070070import org.projectfloodlight.openflow.protocol.OFPortState;
sangho538108b2015-04-08 14:29:20 -070071import org.projectfloodlight.openflow.protocol.OFPortStatsEntry;
72import org.projectfloodlight.openflow.protocol.OFPortStatsReply;
alshabiba14f3642014-09-05 09:31:31 -070073import org.projectfloodlight.openflow.protocol.OFPortStatus;
sangho538108b2015-04-08 14:29:20 -070074import org.projectfloodlight.openflow.protocol.OFStatsReply;
Thomas Vachuska893bf4b2015-05-29 18:13:18 -070075import org.projectfloodlight.openflow.protocol.OFStatsReplyFlags;
sangho538108b2015-04-08 14:29:20 -070076import org.projectfloodlight.openflow.protocol.OFStatsType;
Thomas Vachuskad16ce182014-10-29 17:25:29 -070077import org.projectfloodlight.openflow.protocol.OFVersion;
78import org.projectfloodlight.openflow.types.PortSpeed;
tomb5a46e62014-08-26 14:20:00 -070079import org.slf4j.Logger;
tom5f38b3a2014-08-27 23:50:54 -070080
tom782a7cf2014-09-11 23:58:38 -070081import java.util.ArrayList;
sangho538108b2015-04-08 14:29:20 -070082import java.util.Collection;
83import java.util.Collections;
Dusan Pajinbab8a5e2015-07-24 17:37:19 +020084import java.util.Dictionary;
sangho538108b2015-04-08 14:29:20 -070085import java.util.HashMap;
86import java.util.HashSet;
tom782a7cf2014-09-11 23:58:38 -070087import java.util.List;
88
Dusan Pajinbab8a5e2015-07-24 17:37:19 +020089import static com.google.common.base.Strings.isNullOrEmpty;
90import static org.onlab.util.Tools.get;
Brian O'Connorabafb502014-12-02 22:26:20 -080091import static org.onosproject.net.DeviceId.deviceId;
92import static org.onosproject.net.Port.Type.COPPER;
93import static org.onosproject.net.Port.Type.FIBER;
94import static org.onosproject.openflow.controller.Dpid.dpid;
95import static org.onosproject.openflow.controller.Dpid.uri;
tom782a7cf2014-09-11 23:58:38 -070096import static org.slf4j.LoggerFactory.getLogger;
97
tomb5a46e62014-08-26 14:20:00 -070098/**
tomb1260e42014-08-26 18:39:57 -070099 * Provider which uses an OpenFlow controller to detect network
tome06f8552014-08-26 16:58:42 -0700100 * infrastructure devices.
tomb5a46e62014-08-26 14:20:00 -0700101 */
tomb1260e42014-08-26 18:39:57 -0700102@Component(immediate = true)
tomab21e7c2014-08-26 15:23:08 -0700103public class OpenFlowDeviceProvider extends AbstractProvider implements DeviceProvider {
tomb5a46e62014-08-26 14:20:00 -0700104
alshabiba89cc582014-09-09 16:43:00 -0700105 private static final Logger LOG = getLogger(OpenFlowDeviceProvider.class);
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700106 private static final long MBPS = 1_000 * 1_000;
tomb5a46e62014-08-26 14:20:00 -0700107
108 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
tom96dfcab2014-08-28 09:26:03 -0700109 protected DeviceProviderRegistry providerRegistry;
tomab21e7c2014-08-26 15:23:08 -0700110
tom5f38b3a2014-08-27 23:50:54 -0700111 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
112 protected OpenFlowController controller;
113
Dusan Pajinbab8a5e2015-07-24 17:37:19 +0200114 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
115 protected ComponentConfigService cfgService;
116
tomab21e7c2014-08-26 15:23:08 -0700117 private DeviceProviderService providerService;
tomb5a46e62014-08-26 14:20:00 -0700118
sangho538108b2015-04-08 14:29:20 -0700119 private final InternalDeviceProvider listener = new InternalDeviceProvider();
120
121 // TODO: We need to make the poll interval configurable.
Srikanth Vavilapalli78baf582015-06-05 11:40:14 -0700122 static final int POLL_INTERVAL = 5;
Dusan Pajinbab8a5e2015-07-24 17:37:19 +0200123 @Property(name = "PortStatsPollFrequency", intValue = POLL_INTERVAL,
124 label = "Frequency (in seconds) for polling switch Port statistics")
125 private int portStatsPollFrequency = POLL_INTERVAL;
sangho538108b2015-04-08 14:29:20 -0700126
127 private HashMap<Dpid, PortStatsCollector> collectors = Maps.newHashMap();
tomd40fc7a2014-09-04 16:41:10 -0700128
tomab21e7c2014-08-26 15:23:08 -0700129 /**
130 * Creates an OpenFlow device provider.
131 */
132 public OpenFlowDeviceProvider() {
Brian O'Connorabafb502014-12-02 22:26:20 -0800133 super(new ProviderId("of", "org.onosproject.provider.openflow"));
tomab21e7c2014-08-26 15:23:08 -0700134 }
135
tomb5a46e62014-08-26 14:20:00 -0700136 @Activate
Dusan Pajinbab8a5e2015-07-24 17:37:19 +0200137 public void activate(ComponentContext context) {
138 cfgService.registerProperties(getClass());
tom96dfcab2014-08-28 09:26:03 -0700139 providerService = providerRegistry.register(this);
tomd40fc7a2014-09-04 16:41:10 -0700140 controller.addListener(listener);
sangho538108b2015-04-08 14:29:20 -0700141 controller.addEventListener(listener);
alshabibc944fd02014-09-10 17:55:17 -0700142 for (OpenFlowSwitch sw : controller.getSwitches()) {
Yuta HIGUCHIac2972a2014-11-18 12:58:52 -0800143 try {
144 listener.switchAdded(new Dpid(sw.getId()));
145 } catch (Exception e) {
146 LOG.warn("Failed initially adding {} : {}", sw.getStringId(), e.getMessage());
147 LOG.debug("Error details:", e);
148 // disconnect to trigger switch-add later
149 sw.disconnectSwitch();
150 }
Dusan Pajinbab8a5e2015-07-24 17:37:19 +0200151 PortStatsCollector psc = new PortStatsCollector(sw, portStatsPollFrequency);
sangho538108b2015-04-08 14:29:20 -0700152 psc.start();
153 collectors.put(new Dpid(sw.getId()), psc);
alshabibc944fd02014-09-10 17:55:17 -0700154 }
alshabiba89cc582014-09-09 16:43:00 -0700155 LOG.info("Started");
tomb5a46e62014-08-26 14:20:00 -0700156 }
157
158 @Deactivate
Dusan Pajinbab8a5e2015-07-24 17:37:19 +0200159 public void deactivate(ComponentContext context) {
160 cfgService.unregisterProperties(getClass(), false);
tom96dfcab2014-08-28 09:26:03 -0700161 providerRegistry.unregister(this);
tomd40fc7a2014-09-04 16:41:10 -0700162 controller.removeListener(listener);
Thomas Vachuska74bcd922015-06-02 13:08:57 -0700163 collectors.values().forEach(PortStatsCollector::stop);
tomab21e7c2014-08-26 15:23:08 -0700164 providerService = null;
alshabiba89cc582014-09-09 16:43:00 -0700165 LOG.info("Stopped");
tomb5a46e62014-08-26 14:20:00 -0700166 }
167
Dusan Pajinbab8a5e2015-07-24 17:37:19 +0200168 @Modified
169 public void modified(ComponentContext context) {
170 Dictionary<?, ?> properties = context.getProperties();
171 int newPortStatsPollFrequency;
172 try {
173 String s = get(properties, "PortStatsPollFrequency");
174 newPortStatsPollFrequency = isNullOrEmpty(s) ? portStatsPollFrequency : Integer.parseInt(s.trim());
175
176 } catch (NumberFormatException | ClassCastException e) {
177 newPortStatsPollFrequency = portStatsPollFrequency;
178 }
179
180 if (newPortStatsPollFrequency != portStatsPollFrequency) {
181 portStatsPollFrequency = newPortStatsPollFrequency;
182 collectors.values().forEach(psc -> psc.adjustPollInterval(portStatsPollFrequency));
183 }
184
185 LOG.info("Settings: portStatsPollFrequency={}", portStatsPollFrequency);
186 }
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700187
188 @Override
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700189 public boolean isReachable(DeviceId deviceId) {
190 OpenFlowSwitch sw = controller.getSwitch(dpid(deviceId.uri()));
Yuta HIGUCHI69a27352014-10-31 15:48:37 -0700191 if (sw == null || !sw.isConnected()) {
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700192 return false;
193 }
194 return true;
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700195 }
196
tomab21e7c2014-08-26 15:23:08 -0700197 @Override
Ayaka Koshibe78bcbc12014-11-19 14:28:58 -0800198 public void triggerProbe(DeviceId deviceId) {
Madan Jampanic3328762015-05-31 12:39:27 -0700199 LOG.debug("Triggering probe on device {}", deviceId);
Ayaka Koshibee8708e32014-10-22 13:40:18 -0700200
Yuta HIGUCHI802d0e82014-10-31 16:07:37 -0700201 final Dpid dpid = dpid(deviceId.uri());
202 OpenFlowSwitch sw = controller.getSwitch(dpid);
203 if (sw == null || !sw.isConnected()) {
Ayaka Koshibe78bcbc12014-11-19 14:28:58 -0800204 LOG.error("Failed to probe device {} on sw={}", deviceId, sw);
Yuta HIGUCHI802d0e82014-10-31 16:07:37 -0700205 providerService.deviceDisconnected(deviceId);
Thomas Vachuska74bcd922015-06-02 13:08:57 -0700206 return;
Yuta HIGUCHI802d0e82014-10-31 16:07:37 -0700207 } else {
Ayaka Koshibe78bcbc12014-11-19 14:28:58 -0800208 LOG.trace("Confirmed device {} connection", deviceId);
Yuta HIGUCHI802d0e82014-10-31 16:07:37 -0700209 }
Ayaka Koshibee8708e32014-10-22 13:40:18 -0700210
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700211 // Prompt an update of port information. We can use any XID for this.
Ayaka Koshibee8708e32014-10-22 13:40:18 -0700212 OFFactory fact = sw.factory();
213 switch (fact.getVersion()) {
214 case OF_10:
215 sw.sendMsg(fact.buildFeaturesRequest().setXid(0).build());
216 break;
217 case OF_13:
218 sw.sendMsg(fact.buildPortDescStatsRequest().setXid(0).build());
219 break;
220 default:
221 LOG.warn("Unhandled protocol version");
222 }
tomab21e7c2014-08-26 15:23:08 -0700223 }
224
225 @Override
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700226 public void roleChanged(DeviceId deviceId, MastershipRole newRole) {
alshabibf1216ed2014-09-03 11:53:54 -0700227 switch (newRole) {
tom782a7cf2014-09-11 23:58:38 -0700228 case MASTER:
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700229 controller.setRole(dpid(deviceId.uri()), RoleState.MASTER);
tom782a7cf2014-09-11 23:58:38 -0700230 break;
231 case STANDBY:
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700232 controller.setRole(dpid(deviceId.uri()), RoleState.EQUAL);
tom782a7cf2014-09-11 23:58:38 -0700233 break;
234 case NONE:
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700235 controller.setRole(dpid(deviceId.uri()), RoleState.SLAVE);
tom782a7cf2014-09-11 23:58:38 -0700236 break;
237 default:
238 LOG.error("Unknown Mastership state : {}", newRole);
alshabibf1216ed2014-09-03 11:53:54 -0700239
240 }
Madan Jampanic3328762015-05-31 12:39:27 -0700241 LOG.debug("Accepting mastership role change for device {}", deviceId);
tomab21e7c2014-08-26 15:23:08 -0700242 }
243
Thomas Vachuska893bf4b2015-05-29 18:13:18 -0700244 private void pushPortMetrics(Dpid dpid, List<OFPortStatsEntry> portStatsEntries) {
sangho538108b2015-04-08 14:29:20 -0700245 DeviceId deviceId = DeviceId.deviceId(dpid.uri(dpid));
Thomas Vachuska893bf4b2015-05-29 18:13:18 -0700246 Collection<PortStatistics> stats = buildPortStatistics(deviceId, portStatsEntries);
sangho538108b2015-04-08 14:29:20 -0700247 providerService.updatePortStatistics(deviceId, stats);
248 }
249
Thomas Vachuska893bf4b2015-05-29 18:13:18 -0700250 private Collection<PortStatistics> buildPortStatistics(DeviceId deviceId,
251 List<OFPortStatsEntry> entries) {
sangho538108b2015-04-08 14:29:20 -0700252 HashSet<PortStatistics> stats = Sets.newHashSet();
253
Thomas Vachuska893bf4b2015-05-29 18:13:18 -0700254 for (OFPortStatsEntry entry : entries) {
Thomas Vachuskaf0397b52015-05-29 13:50:17 -0700255 try {
Thomas Vachuska4e6025e2015-06-03 11:25:21 -0700256 if (entry == null || entry.getPortNo() == null || entry.getPortNo().getPortNumber() < 0) {
Thomas Vachuskaf0397b52015-05-29 13:50:17 -0700257 continue;
258 }
259 DefaultPortStatistics.Builder builder = DefaultPortStatistics.builder();
260 DefaultPortStatistics stat = builder.setDeviceId(deviceId)
261 .setPort(entry.getPortNo().getPortNumber())
262 .setPacketsReceived(entry.getRxPackets().getValue())
263 .setPacketsSent(entry.getTxPackets().getValue())
264 .setBytesReceived(entry.getRxBytes().getValue())
265 .setBytesSent(entry.getTxBytes().getValue())
266 .setPacketsRxDropped(entry.getRxDropped().getValue())
267 .setPacketsTxDropped(entry.getTxDropped().getValue())
268 .setPacketsRxErrors(entry.getRxErrors().getValue())
269 .setPacketsTxErrors(entry.getTxErrors().getValue())
270 .setDurationSec(entry.getVersion() == OFVersion.OF_10 ? 0 : entry.getDurationSec())
271 .setDurationNano(entry.getVersion() == OFVersion.OF_10 ? 0 : entry.getDurationNsec())
272 .build();
sangho538108b2015-04-08 14:29:20 -0700273
Thomas Vachuskaf0397b52015-05-29 13:50:17 -0700274 stats.add(stat);
275 } catch (Exception e) {
276 LOG.warn("Unable to process port stats", e);
277 }
sangho538108b2015-04-08 14:29:20 -0700278 }
279
280 return Collections.unmodifiableSet(stats);
281
282 }
283
284 private class InternalDeviceProvider implements OpenFlowSwitchListener, OpenFlowEventListener {
Thomas Vachuska893bf4b2015-05-29 18:13:18 -0700285
Srikanth Vavilapalli78baf582015-06-05 11:40:14 -0700286 private HashMap<Dpid, List<OFPortStatsEntry>> portStatsReplies = new HashMap<>();
Thomas Vachuska893bf4b2015-05-29 18:13:18 -0700287
alshabibf1216ed2014-09-03 11:53:54 -0700288 @Override
tomd1900f32014-09-03 14:08:16 -0700289 public void switchAdded(Dpid dpid) {
alshabib6f5460b2014-09-03 14:46:17 -0700290 if (providerService == null) {
291 return;
292 }
tom782a7cf2014-09-11 23:58:38 -0700293 DeviceId did = deviceId(uri(dpid));
alshabib6f5460b2014-09-03 14:46:17 -0700294 OpenFlowSwitch sw = controller.getSwitch(dpid);
295
alshabib7911a052014-10-16 17:49:37 -0700296 ChassisId cId = new ChassisId(dpid.value());
Ray Milkeye53f1712015-01-16 09:17:16 -0800297
Thomas Vachuska82041f52014-11-30 22:14:02 -0800298 SparseAnnotations annotations = DefaultAnnotations.builder()
Ray Milkeye53f1712015-01-16 09:17:16 -0800299 .set("protocol", sw.factory().getVersion().toString())
300 .set("channelId", sw.channelId())
301 .build();
302
tomd1900f32014-09-03 14:08:16 -0700303 DeviceDescription description =
Marc De Leenheerb9311372015-07-09 11:36:49 -0700304 new DefaultDeviceDescription(did.uri(), sw.deviceType(),
Ray Milkeyd3edd032015-01-16 11:38:58 -0800305 sw.manufacturerDescription(),
tom782a7cf2014-09-11 23:58:38 -0700306 sw.hardwareDescription(),
307 sw.softwareDescription(),
alshabib7911a052014-10-16 17:49:37 -0700308 sw.serialNumber(),
Thomas Vachuska82041f52014-11-30 22:14:02 -0800309 cId, annotations);
tom782a7cf2014-09-11 23:58:38 -0700310 providerService.deviceConnected(did, description);
Ayaka Koshibe5460d622015-05-14 12:19:19 -0700311 providerService.updatePorts(did, buildPortDescriptions(sw));
sangho538108b2015-04-08 14:29:20 -0700312
Thomas Vachuskafc52fec2015-05-18 19:13:56 -0700313 PortStatsCollector psc =
Dusan Pajinbab8a5e2015-07-24 17:37:19 +0200314 new PortStatsCollector(controller.getSwitch(dpid), portStatsPollFrequency);
sangho538108b2015-04-08 14:29:20 -0700315 psc.start();
316 collectors.put(dpid, psc);
alshabib25c8eec2014-09-04 16:41:31 -0700317 }
318
alshabibf1216ed2014-09-03 11:53:54 -0700319 @Override
320 public void switchRemoved(Dpid dpid) {
alshabib6f5460b2014-09-03 14:46:17 -0700321 if (providerService == null) {
322 return;
323 }
tom782a7cf2014-09-11 23:58:38 -0700324 providerService.deviceDisconnected(deviceId(uri(dpid)));
alshabibf1216ed2014-09-03 11:53:54 -0700325
sangho538108b2015-04-08 14:29:20 -0700326 PortStatsCollector collector = collectors.remove(dpid);
327 if (collector != null) {
328 collector.stop();
329 }
330 }
Ayaka Koshibe38594c22014-10-22 13:36:12 -0700331
332 @Override
333 public void switchChanged(Dpid dpid) {
334 if (providerService == null) {
335 return;
336 }
337 DeviceId did = deviceId(uri(dpid));
338 OpenFlowSwitch sw = controller.getSwitch(dpid);
Ayaka Koshibe5460d622015-05-14 12:19:19 -0700339 providerService.updatePorts(did, buildPortDescriptions(sw));
Ayaka Koshibe38594c22014-10-22 13:36:12 -0700340 }
341
alshabiba14f3642014-09-05 09:31:31 -0700342 @Override
343 public void portChanged(Dpid dpid, OFPortStatus status) {
alshabibafc514a2014-12-01 14:44:05 -0800344 PortDescription portDescription = buildPortDescription(status);
tom782a7cf2014-09-11 23:58:38 -0700345 providerService.portStatusChanged(deviceId(uri(dpid)), portDescription);
alshabibf1216ed2014-09-03 11:53:54 -0700346 }
347
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700348 @Override
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700349 public void receivedRoleReply(Dpid dpid, RoleState requested, RoleState response) {
350 MastershipRole request = roleOf(requested);
351 MastershipRole reply = roleOf(response);
352
353 providerService.receivedRoleReply(deviceId(uri(dpid)), request, reply);
354 }
355
356 /**
357 * Translates a RoleState to the corresponding MastershipRole.
358 *
Thomas Vachuskafc52fec2015-05-18 19:13:56 -0700359 * @param response role state
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700360 * @return a MastershipRole
361 */
362 private MastershipRole roleOf(RoleState response) {
363 switch (response) {
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700364 case MASTER:
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700365 return MastershipRole.MASTER;
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700366 case EQUAL:
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700367 return MastershipRole.STANDBY;
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700368 case SLAVE:
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700369 return MastershipRole.NONE;
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700370 default:
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700371 LOG.warn("unknown role {}", response);
372 return null;
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700373 }
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700374 }
375
alshabiba14f3642014-09-05 09:31:31 -0700376 /**
377 * Builds a list of port descriptions for a given list of ports.
tomff7eb7c2014-09-08 12:49:03 -0700378 *
alshabiba14f3642014-09-05 09:31:31 -0700379 * @return list of portdescriptions
380 */
Ayaka Koshibe5460d622015-05-14 12:19:19 -0700381 private List<PortDescription> buildPortDescriptions(OpenFlowSwitch sw) {
382 final List<PortDescription> portDescs = new ArrayList<>(sw.getPorts().size());
383 sw.getPorts().forEach(port -> portDescs.add(buildPortDescription(port)));
Marc De Leenheerb9311372015-07-09 11:36:49 -0700384
385 OpenFlowOpticalSwitch opsw;
386 switch (sw.deviceType()) {
387 case ROADM:
388 opsw = (OpenFlowOpticalSwitch) sw;
389 opsw.getPortTypes().forEach(type -> {
390 opsw.getPortsOf(type).forEach(
391 op -> {
392 portDescs.add(buildPortDescription(type, (OFPortOptical) op));
393 }
394 );
395 });
396 break;
397 case FIBER_SWITCH:
398 opsw = (OpenFlowOpticalSwitch) sw;
399 opsw.getPortTypes().forEach(type -> {
400 opsw.getPortsOf(type).forEach(
401 op -> {
402 portDescs.add(buildPortDescription((OFCalientPortDescStatsEntry) op));
403 }
404 );
405 });
406 break;
407 default:
408 break;
alshabib4680bb62014-09-04 17:15:08 -0700409 }
Marc De Leenheerb9311372015-07-09 11:36:49 -0700410
alshabib4680bb62014-09-04 17:15:08 -0700411 return portDescs;
412 }
413
alshabiba14f3642014-09-05 09:31:31 -0700414 /**
Ray Milkeye0fade72015-01-15 13:29:47 -0800415 * Creates an annotation for the port name if one is available.
416 *
417 * @param port description of the port
418 * @return annotation containing the port name if one is found,
419 * null otherwise
420 */
Ayaka Koshibe5460d622015-05-14 12:19:19 -0700421 private SparseAnnotations makePortNameAnnotation(String port) {
Ray Milkeye0fade72015-01-15 13:29:47 -0800422 SparseAnnotations annotations = null;
Ayaka Koshibe5460d622015-05-14 12:19:19 -0700423 String portName = Strings.emptyToNull(port);
Ray Milkeye0fade72015-01-15 13:29:47 -0800424 if (portName != null) {
425 annotations = DefaultAnnotations.builder()
Thomas Vachuskab52a0142015-04-21 17:48:15 -0700426 .set(AnnotationKeys.PORT_NAME, portName).build();
Ray Milkeye0fade72015-01-15 13:29:47 -0800427 }
428 return annotations;
429 }
430
431 /**
Ayaka Koshibe5460d622015-05-14 12:19:19 -0700432 * Build a portDescription from a given Ethernet port description.
tomff7eb7c2014-09-08 12:49:03 -0700433 *
alshabiba14f3642014-09-05 09:31:31 -0700434 * @param port the port to build from.
435 * @return portDescription for the port.
436 */
437 private PortDescription buildPortDescription(OFPortDesc port) {
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700438 PortNumber portNo = PortNumber.portNumber(port.getPortNo().getPortNumber());
439 boolean enabled =
440 !port.getState().contains(OFPortState.LINK_DOWN) &&
441 !port.getConfig().contains(OFPortConfig.PORT_DOWN);
442 Port.Type type = port.getCurr().contains(OFPortFeatures.PF_FIBER) ? FIBER : COPPER;
Ayaka Koshibe5460d622015-05-14 12:19:19 -0700443 SparseAnnotations annotations = makePortNameAnnotation(port.getName());
Ray Milkeye0fade72015-01-15 13:29:47 -0800444 return new DefaultPortDescription(portNo, enabled, type,
445 portSpeed(port), annotations);
alshabiba14f3642014-09-05 09:31:31 -0700446 }
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700447
Ayaka Koshibe5460d622015-05-14 12:19:19 -0700448 /**
449 * Build a portDescription from a given a port description describing some
450 * Optical port.
451 *
452 * @param port description property type.
453 * @param port the port to build from.
454 * @return portDescription for the port.
455 */
456 private PortDescription buildPortDescription(PortDescPropertyType ptype, OFPortOptical port) {
457 // Minimally functional fixture. This needs to be fixed as we add better support.
458 PortNumber portNo = PortNumber.portNumber(port.getPortNo().getPortNumber());
Ayaka Koshibe74b55272015-05-28 15:16:04 -0700459
Ayaka Koshibe5460d622015-05-14 12:19:19 -0700460 boolean enabled = !port.getState().contains(OFPortState.LINK_DOWN)
461 && !port.getConfig().contains(OFPortConfig.PORT_DOWN);
462 SparseAnnotations annotations = makePortNameAnnotation(port.getName());
Ayaka Koshibe5460d622015-05-14 12:19:19 -0700463
464 if (port.getVersion() == OFVersion.OF_13
465 && ptype == PortDescPropertyType.OPTICAL_TRANSPORT) {
466 // At this point, not much is carried in the optical port message.
Ayaka Koshibeae541732015-05-19 13:37:27 -0700467 LOG.debug("Optical transport port message {}", port.toString());
Ayaka Koshibe5460d622015-05-14 12:19:19 -0700468 } else {
469 // removable once 1.4+ support complete.
Ayaka Koshibeae541732015-05-19 13:37:27 -0700470 LOG.debug("Unsupported optical port properties");
Ayaka Koshibe5460d622015-05-14 12:19:19 -0700471 }
Ayaka Koshibe74b55272015-05-28 15:16:04 -0700472 return new DefaultPortDescription(portNo, enabled, FIBER, 0, annotations);
Ayaka Koshibe5460d622015-05-14 12:19:19 -0700473 }
474
Marc De Leenheerb9311372015-07-09 11:36:49 -0700475 /**
476 * Build a portDescription from a given port description describing a fiber switch optical port.
477 *
478 * @param port description property type.
479 * @param port the port to build from.
480 * @return portDescription for the port.
481 */
482 private PortDescription buildPortDescription(OFCalientPortDescStatsEntry port) {
483 PortNumber portNo = PortNumber.portNumber(port.getPortNo().getPortNumber());
484
485 // FIXME when Calient OF agent reports port status
486 boolean enabled = true;
487 SparseAnnotations annotations = makePortNameAnnotation(port.getName());
488
489 // Wavelength range: 1260 - 1630 nm (S160 data sheet)
490 // Grid is irrelevant for this type of switch
491 Frequency minFreq = Spectrum.O_BAND_MAX;
492 Frequency maxFreq = Spectrum.U_BAND_MIN;
493 Frequency grid = Frequency.ofGHz(100);
494 return new OmsPortDescription(portNo, enabled, minFreq, maxFreq, grid, annotations);
495 }
496
alshabibafc514a2014-12-01 14:44:05 -0800497 private PortDescription buildPortDescription(OFPortStatus status) {
498 OFPortDesc port = status.getDesc();
499 if (status.getReason() != OFPortReason.DELETE) {
500 return buildPortDescription(port);
501 } else {
502 PortNumber portNo = PortNumber.portNumber(port.getPortNo().getPortNumber());
503 Port.Type type = port.getCurr().contains(OFPortFeatures.PF_FIBER) ? FIBER : COPPER;
Ayaka Koshibe5460d622015-05-14 12:19:19 -0700504 SparseAnnotations annotations = makePortNameAnnotation(port.getName());
Ray Milkeye0fade72015-01-15 13:29:47 -0800505 return new DefaultPortDescription(portNo, false, type,
506 portSpeed(port), annotations);
alshabibafc514a2014-12-01 14:44:05 -0800507 }
508 }
509
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700510 private long portSpeed(OFPortDesc port) {
511 if (port.getVersion() == OFVersion.OF_13) {
512 return port.getCurrSpeed() / MBPS;
513 }
514
515 PortSpeed portSpeed = PortSpeed.SPEED_NONE;
516 for (OFPortFeatures feat : port.getCurr()) {
517 portSpeed = PortSpeed.max(portSpeed, feat.getPortSpeed());
518 }
Thomas Vachuska98eda532014-10-29 17:31:02 -0700519 return portSpeed.getSpeedBps() / MBPS;
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700520 }
sangho538108b2015-04-08 14:29:20 -0700521
522 @Override
523 public void handleMessage(Dpid dpid, OFMessage msg) {
524 switch (msg.getType()) {
525 case STATS_REPLY:
526 if (((OFStatsReply) msg).getStatsType() == OFStatsType.PORT) {
Thomas Vachuska893bf4b2015-05-29 18:13:18 -0700527 OFPortStatsReply portStatsReply = (OFPortStatsReply) msg;
Srikanth Vavilapalli78baf582015-06-05 11:40:14 -0700528 List<OFPortStatsEntry> portStatsReplyList = portStatsReplies.get(dpid);
529 if (portStatsReplyList == null) {
530 portStatsReplyList = Lists.newArrayList();
531 }
532 portStatsReplyList.addAll(portStatsReply.getEntries());
533 portStatsReplies.put(dpid, portStatsReplyList);
Thomas Vachuska893bf4b2015-05-29 18:13:18 -0700534 if (!portStatsReply.getFlags().contains(OFStatsReplyFlags.REPLY_MORE)) {
Srikanth Vavilapalli78baf582015-06-05 11:40:14 -0700535 pushPortMetrics(dpid, portStatsReplies.get(dpid));
536 portStatsReplies.get(dpid).clear();
Thomas Vachuska893bf4b2015-05-29 18:13:18 -0700537 }
sangho538108b2015-04-08 14:29:20 -0700538 }
539 break;
540 default:
541 break;
542 }
543 }
alshabibf1216ed2014-09-03 11:53:54 -0700544 }
545
tomb5a46e62014-08-26 14:20:00 -0700546}