blob: fe7f7024f318fa351722ab567ca47d5ca338b758 [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
Yafit Hadara9a73de2015-09-06 13:52:52 +030018import static com.google.common.base.Preconditions.checkArgument;
19import static com.google.common.base.Strings.isNullOrEmpty;
20import static org.onlab.util.Tools.get;
21import static org.onosproject.net.DeviceId.deviceId;
22import static org.onosproject.net.Port.Type.COPPER;
23import static org.onosproject.net.Port.Type.FIBER;
24import static org.onosproject.openflow.controller.Dpid.dpid;
25import static org.onosproject.openflow.controller.Dpid.uri;
26import static org.slf4j.LoggerFactory.getLogger;
27
28import java.util.ArrayList;
29import java.util.Collection;
30import java.util.Collections;
31import java.util.Dictionary;
32import java.util.HashMap;
33import java.util.HashSet;
34import java.util.List;
35
tomb5a46e62014-08-26 14:20:00 -070036import org.apache.felix.scr.annotations.Activate;
37import org.apache.felix.scr.annotations.Component;
38import org.apache.felix.scr.annotations.Deactivate;
Dusan Pajinbab8a5e2015-07-24 17:37:19 +020039import org.apache.felix.scr.annotations.Modified;
40import org.apache.felix.scr.annotations.Property;
tomb5a46e62014-08-26 14:20:00 -070041import org.apache.felix.scr.annotations.Reference;
42import org.apache.felix.scr.annotations.ReferenceCardinality;
Thomas Vachuskaf0397b52015-05-29 13:50:17 -070043import org.onlab.packet.ChassisId;
Marc De Leenheerb9311372015-07-09 11:36:49 -070044import org.onlab.util.Frequency;
45import org.onlab.util.Spectrum;
Yafit Hadara9a73de2015-09-06 13:52:52 +030046import org.onosproject.cfg.ComponentConfigService;
Thomas Vachuskab52a0142015-04-21 17:48:15 -070047import org.onosproject.net.AnnotationKeys;
Marc De Leenheerfc913dd2015-07-30 16:04:55 -070048import org.onosproject.net.ChannelSpacing;
Brian O'Connorabafb502014-12-02 22:26:20 -080049import org.onosproject.net.DefaultAnnotations;
Yafit Hadara9a73de2015-09-06 13:52:52 +030050import org.onosproject.net.Device;
Brian O'Connorabafb502014-12-02 22:26:20 -080051import org.onosproject.net.DeviceId;
Marc De Leenheerfc913dd2015-07-30 16:04:55 -070052import org.onosproject.net.GridType;
Brian O'Connorabafb502014-12-02 22:26:20 -080053import org.onosproject.net.MastershipRole;
Marc De Leenheerfc913dd2015-07-30 16:04:55 -070054import org.onosproject.net.OchSignal;
Yafit Hadara9a73de2015-09-06 13:52:52 +030055import org.onosproject.net.OduCltPort;
Marc De Leenheerfc913dd2015-07-30 16:04:55 -070056import org.onosproject.net.OduSignalType;
Brian O'Connorabafb502014-12-02 22:26:20 -080057import org.onosproject.net.Port;
58import org.onosproject.net.PortNumber;
59import org.onosproject.net.SparseAnnotations;
60import org.onosproject.net.device.DefaultDeviceDescription;
61import org.onosproject.net.device.DefaultPortDescription;
sangho538108b2015-04-08 14:29:20 -070062import org.onosproject.net.device.DefaultPortStatistics;
Brian O'Connorabafb502014-12-02 22:26:20 -080063import org.onosproject.net.device.DeviceDescription;
64import org.onosproject.net.device.DeviceProvider;
65import org.onosproject.net.device.DeviceProviderRegistry;
66import org.onosproject.net.device.DeviceProviderService;
Marc De Leenheerfc913dd2015-07-30 16:04:55 -070067import org.onosproject.net.device.OchPortDescription;
Yafit Hadara9a73de2015-09-06 13:52:52 +030068import org.onosproject.net.device.OduCltPortDescription;
Marc De Leenheerb9311372015-07-09 11:36:49 -070069import org.onosproject.net.device.OmsPortDescription;
Brian O'Connorabafb502014-12-02 22:26:20 -080070import org.onosproject.net.device.PortDescription;
sangho538108b2015-04-08 14:29:20 -070071import org.onosproject.net.device.PortStatistics;
Brian O'Connorabafb502014-12-02 22:26:20 -080072import org.onosproject.net.provider.AbstractProvider;
73import org.onosproject.net.provider.ProviderId;
74import org.onosproject.openflow.controller.Dpid;
75import org.onosproject.openflow.controller.OpenFlowController;
sangho538108b2015-04-08 14:29:20 -070076import org.onosproject.openflow.controller.OpenFlowEventListener;
Ayaka Koshibe5460d622015-05-14 12:19:19 -070077import org.onosproject.openflow.controller.OpenFlowOpticalSwitch;
Brian O'Connorabafb502014-12-02 22:26:20 -080078import org.onosproject.openflow.controller.OpenFlowSwitch;
79import org.onosproject.openflow.controller.OpenFlowSwitchListener;
Ayaka Koshibe5460d622015-05-14 12:19:19 -070080import org.onosproject.openflow.controller.PortDescPropertyType;
Brian O'Connorabafb502014-12-02 22:26:20 -080081import org.onosproject.openflow.controller.RoleState;
Dusan Pajinbab8a5e2015-07-24 17:37:19 +020082import org.osgi.service.component.ComponentContext;
Marc De Leenheerb9311372015-07-09 11:36:49 -070083import org.projectfloodlight.openflow.protocol.OFCalientPortDescStatsEntry;
Yafit Hadara9a73de2015-09-06 13:52:52 +030084import org.projectfloodlight.openflow.protocol.OFExpPort;
85import org.projectfloodlight.openflow.protocol.OFExpPortDescPropOpticalTransport;
86import org.projectfloodlight.openflow.protocol.OFExpPortOpticalTransportLayerEntry;
Ayaka Koshibee8708e32014-10-22 13:40:18 -070087import org.projectfloodlight.openflow.protocol.OFFactory;
sangho538108b2015-04-08 14:29:20 -070088import org.projectfloodlight.openflow.protocol.OFMessage;
Yafit Hadara9a73de2015-09-06 13:52:52 +030089import org.projectfloodlight.openflow.protocol.OFObject;
alshabib4680bb62014-09-04 17:15:08 -070090import org.projectfloodlight.openflow.protocol.OFPortConfig;
alshabib25c8eec2014-09-04 16:41:31 -070091import org.projectfloodlight.openflow.protocol.OFPortDesc;
Marc De Leenheerfc913dd2015-07-30 16:04:55 -070092import org.projectfloodlight.openflow.protocol.OFPortDescPropOpticalTransport;
Thomas Vachuskad16ce182014-10-29 17:25:29 -070093import org.projectfloodlight.openflow.protocol.OFPortFeatures;
Ayaka Koshibe5460d622015-05-14 12:19:19 -070094import org.projectfloodlight.openflow.protocol.OFPortOptical;
Yafit Hadara9a73de2015-09-06 13:52:52 +030095import org.projectfloodlight.openflow.protocol.OFPortOpticalTransportLayerClass;
96import org.projectfloodlight.openflow.protocol.OFPortOpticalTransportSignalType;
alshabibafc514a2014-12-01 14:44:05 -080097import org.projectfloodlight.openflow.protocol.OFPortReason;
alshabib4680bb62014-09-04 17:15:08 -070098import org.projectfloodlight.openflow.protocol.OFPortState;
sangho538108b2015-04-08 14:29:20 -070099import org.projectfloodlight.openflow.protocol.OFPortStatsEntry;
100import org.projectfloodlight.openflow.protocol.OFPortStatsReply;
alshabiba14f3642014-09-05 09:31:31 -0700101import org.projectfloodlight.openflow.protocol.OFPortStatus;
sangho538108b2015-04-08 14:29:20 -0700102import org.projectfloodlight.openflow.protocol.OFStatsReply;
Thomas Vachuska893bf4b2015-05-29 18:13:18 -0700103import org.projectfloodlight.openflow.protocol.OFStatsReplyFlags;
sangho538108b2015-04-08 14:29:20 -0700104import org.projectfloodlight.openflow.protocol.OFStatsType;
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700105import org.projectfloodlight.openflow.protocol.OFVersion;
106import org.projectfloodlight.openflow.types.PortSpeed;
tomb5a46e62014-08-26 14:20:00 -0700107import org.slf4j.Logger;
tom5f38b3a2014-08-27 23:50:54 -0700108
Yafit Hadara9a73de2015-09-06 13:52:52 +0300109import com.google.common.base.Strings;
110import com.google.common.collect.Lists;
111import com.google.common.collect.Maps;
112import com.google.common.collect.Sets;
tom782a7cf2014-09-11 23:58:38 -0700113
tomb5a46e62014-08-26 14:20:00 -0700114/**
tomb1260e42014-08-26 18:39:57 -0700115 * Provider which uses an OpenFlow controller to detect network
tome06f8552014-08-26 16:58:42 -0700116 * infrastructure devices.
tomb5a46e62014-08-26 14:20:00 -0700117 */
tomb1260e42014-08-26 18:39:57 -0700118@Component(immediate = true)
tomab21e7c2014-08-26 15:23:08 -0700119public class OpenFlowDeviceProvider extends AbstractProvider implements DeviceProvider {
tomb5a46e62014-08-26 14:20:00 -0700120
alshabiba89cc582014-09-09 16:43:00 -0700121 private static final Logger LOG = getLogger(OpenFlowDeviceProvider.class);
Thomas Vachuskad07c0922015-10-06 14:48:06 -0700122
Charles Chan25b77322015-12-13 01:00:56 -0800123 //TODO consider renaming KBPS and MBPS (as they are used to convert by division)
124 private static final long KBPS = 1_000;
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700125 private static final long MBPS = 1_000 * 1_000;
Rimon Ashkenazye3201032016-01-11 14:27:30 +0200126 private static final Frequency FREQ50 = Frequency.ofGHz(50);
127 private static final Frequency FREQ191_7 = Frequency.ofGHz(191_700);
Sho SHIMIZU00762ee2016-01-05 16:32:24 -0800128 private static final Frequency FREQ4_4 = Frequency.ofGHz(4_400);
tomb5a46e62014-08-26 14:20:00 -0700129
130 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
tom96dfcab2014-08-28 09:26:03 -0700131 protected DeviceProviderRegistry providerRegistry;
tomab21e7c2014-08-26 15:23:08 -0700132
tom5f38b3a2014-08-27 23:50:54 -0700133 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
134 protected OpenFlowController controller;
135
Dusan Pajinbab8a5e2015-07-24 17:37:19 +0200136 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
137 protected ComponentConfigService cfgService;
138
tomab21e7c2014-08-26 15:23:08 -0700139 private DeviceProviderService providerService;
tomb5a46e62014-08-26 14:20:00 -0700140
sangho538108b2015-04-08 14:29:20 -0700141 private final InternalDeviceProvider listener = new InternalDeviceProvider();
142
Jian Li152b8852015-12-07 14:47:25 -0800143 private final IncomingMessageProvider inMsgListener = new IncomingMessageProvider();
144
145 private final OutgoingMessageProvider outMsgListener = new OutgoingMessageProvider();
146
147 private boolean isCtrlMsgMonitor;
148
sangho538108b2015-04-08 14:29:20 -0700149 // TODO: We need to make the poll interval configurable.
Srikanth Vavilapalli78baf582015-06-05 11:40:14 -0700150 static final int POLL_INTERVAL = 5;
Dusan Pajinbab8a5e2015-07-24 17:37:19 +0200151 @Property(name = "PortStatsPollFrequency", intValue = POLL_INTERVAL,
152 label = "Frequency (in seconds) for polling switch Port statistics")
153 private int portStatsPollFrequency = POLL_INTERVAL;
sangho538108b2015-04-08 14:29:20 -0700154
155 private HashMap<Dpid, PortStatsCollector> collectors = Maps.newHashMap();
tomd40fc7a2014-09-04 16:41:10 -0700156
tomab21e7c2014-08-26 15:23:08 -0700157 /**
158 * Creates an OpenFlow device provider.
159 */
160 public OpenFlowDeviceProvider() {
Brian O'Connorabafb502014-12-02 22:26:20 -0800161 super(new ProviderId("of", "org.onosproject.provider.openflow"));
tomab21e7c2014-08-26 15:23:08 -0700162 }
163
tomb5a46e62014-08-26 14:20:00 -0700164 @Activate
Dusan Pajinbab8a5e2015-07-24 17:37:19 +0200165 public void activate(ComponentContext context) {
166 cfgService.registerProperties(getClass());
tom96dfcab2014-08-28 09:26:03 -0700167 providerService = providerRegistry.register(this);
tomd40fc7a2014-09-04 16:41:10 -0700168 controller.addListener(listener);
sangho538108b2015-04-08 14:29:20 -0700169 controller.addEventListener(listener);
Jian Li152b8852015-12-07 14:47:25 -0800170
Thomas Vachuskad07c0922015-10-06 14:48:06 -0700171 connectInitialDevices();
alshabiba89cc582014-09-09 16:43:00 -0700172 LOG.info("Started");
tomb5a46e62014-08-26 14:20:00 -0700173 }
174
175 @Deactivate
Dusan Pajinbab8a5e2015-07-24 17:37:19 +0200176 public void deactivate(ComponentContext context) {
177 cfgService.unregisterProperties(getClass(), false);
tomd40fc7a2014-09-04 16:41:10 -0700178 controller.removeListener(listener);
Thomas Vachuskad07c0922015-10-06 14:48:06 -0700179 providerRegistry.unregister(this);
Thomas Vachuska74bcd922015-06-02 13:08:57 -0700180 collectors.values().forEach(PortStatsCollector::stop);
tomab21e7c2014-08-26 15:23:08 -0700181 providerService = null;
alshabiba89cc582014-09-09 16:43:00 -0700182 LOG.info("Stopped");
tomb5a46e62014-08-26 14:20:00 -0700183 }
184
Dusan Pajinbab8a5e2015-07-24 17:37:19 +0200185 @Modified
186 public void modified(ComponentContext context) {
187 Dictionary<?, ?> properties = context.getProperties();
188 int newPortStatsPollFrequency;
189 try {
190 String s = get(properties, "PortStatsPollFrequency");
191 newPortStatsPollFrequency = isNullOrEmpty(s) ? portStatsPollFrequency : Integer.parseInt(s.trim());
192
193 } catch (NumberFormatException | ClassCastException e) {
194 newPortStatsPollFrequency = portStatsPollFrequency;
195 }
196
197 if (newPortStatsPollFrequency != portStatsPollFrequency) {
198 portStatsPollFrequency = newPortStatsPollFrequency;
199 collectors.values().forEach(psc -> psc.adjustPollInterval(portStatsPollFrequency));
200 }
201
202 LOG.info("Settings: portStatsPollFrequency={}", portStatsPollFrequency);
203 }
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700204
Thomas Vachuskad07c0922015-10-06 14:48:06 -0700205 private void connectInitialDevices() {
206 for (OpenFlowSwitch sw : controller.getSwitches()) {
207 try {
208 listener.switchAdded(new Dpid(sw.getId()));
209 } catch (Exception e) {
210 LOG.warn("Failed initially adding {} : {}", sw.getStringId(), e.getMessage());
211 LOG.debug("Error details:", e);
212 // disconnect to trigger switch-add later
213 sw.disconnectSwitch();
214 }
215 PortStatsCollector psc = new PortStatsCollector(sw, portStatsPollFrequency);
216 psc.start();
217 collectors.put(new Dpid(sw.getId()), psc);
218 }
219 }
220
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700221 @Override
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700222 public boolean isReachable(DeviceId deviceId) {
223 OpenFlowSwitch sw = controller.getSwitch(dpid(deviceId.uri()));
Thomas Vachuskad07c0922015-10-06 14:48:06 -0700224 return sw != null && sw.isConnected();
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700225 }
226
tomab21e7c2014-08-26 15:23:08 -0700227 @Override
Ayaka Koshibe78bcbc12014-11-19 14:28:58 -0800228 public void triggerProbe(DeviceId deviceId) {
Madan Jampanic3328762015-05-31 12:39:27 -0700229 LOG.debug("Triggering probe on device {}", deviceId);
Ayaka Koshibee8708e32014-10-22 13:40:18 -0700230
Yuta HIGUCHI802d0e82014-10-31 16:07:37 -0700231 final Dpid dpid = dpid(deviceId.uri());
232 OpenFlowSwitch sw = controller.getSwitch(dpid);
233 if (sw == null || !sw.isConnected()) {
Ayaka Koshibe78bcbc12014-11-19 14:28:58 -0800234 LOG.error("Failed to probe device {} on sw={}", deviceId, sw);
Yuta HIGUCHI802d0e82014-10-31 16:07:37 -0700235 providerService.deviceDisconnected(deviceId);
Thomas Vachuska74bcd922015-06-02 13:08:57 -0700236 return;
Yuta HIGUCHI802d0e82014-10-31 16:07:37 -0700237 } else {
Ayaka Koshibe78bcbc12014-11-19 14:28:58 -0800238 LOG.trace("Confirmed device {} connection", deviceId);
Yuta HIGUCHI802d0e82014-10-31 16:07:37 -0700239 }
Ayaka Koshibee8708e32014-10-22 13:40:18 -0700240
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700241 // Prompt an update of port information. We can use any XID for this.
Ayaka Koshibee8708e32014-10-22 13:40:18 -0700242 OFFactory fact = sw.factory();
243 switch (fact.getVersion()) {
244 case OF_10:
245 sw.sendMsg(fact.buildFeaturesRequest().setXid(0).build());
246 break;
247 case OF_13:
248 sw.sendMsg(fact.buildPortDescStatsRequest().setXid(0).build());
249 break;
250 default:
251 LOG.warn("Unhandled protocol version");
252 }
tomab21e7c2014-08-26 15:23:08 -0700253 }
254
255 @Override
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700256 public void roleChanged(DeviceId deviceId, MastershipRole newRole) {
alshabibf1216ed2014-09-03 11:53:54 -0700257 switch (newRole) {
tom782a7cf2014-09-11 23:58:38 -0700258 case MASTER:
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700259 controller.setRole(dpid(deviceId.uri()), RoleState.MASTER);
tom782a7cf2014-09-11 23:58:38 -0700260 break;
261 case STANDBY:
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700262 controller.setRole(dpid(deviceId.uri()), RoleState.EQUAL);
tom782a7cf2014-09-11 23:58:38 -0700263 break;
264 case NONE:
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700265 controller.setRole(dpid(deviceId.uri()), RoleState.SLAVE);
tom782a7cf2014-09-11 23:58:38 -0700266 break;
267 default:
268 LOG.error("Unknown Mastership state : {}", newRole);
alshabibf1216ed2014-09-03 11:53:54 -0700269
270 }
HIGUCHI Yuta1979f552015-12-28 21:24:26 -0800271 LOG.debug("Accepting mastership role change to {} for device {}", newRole, deviceId);
tomab21e7c2014-08-26 15:23:08 -0700272 }
273
Jian Li152b8852015-12-07 14:47:25 -0800274
275 /**
276 * Enable OpenFlow control message monitoring.
277 */
278 public void enableCtrlMsgMonitor() {
279 isCtrlMsgMonitor = true;
280 controller.addEventListener(inMsgListener);
Jian Li28247b52016-01-07 17:24:15 -0800281 controller.monitorAllEvents(isCtrlMsgMonitor);
Jian Li152b8852015-12-07 14:47:25 -0800282 for (OpenFlowSwitch sw : controller.getSwitches()) {
283 sw.addEventListener(outMsgListener);
284 }
285 LOG.info("Enable control message monitoring.");
286 }
287
288 /**
289 * Disable OpenFlow control message monitoring.
290 */
291 public void disableCtrlMsgMonitor() {
292 isCtrlMsgMonitor = false;
Jian Li28247b52016-01-07 17:24:15 -0800293 controller.monitorAllEvents(isCtrlMsgMonitor);
Jian Li152b8852015-12-07 14:47:25 -0800294 controller.removeEventListener(inMsgListener);
295 for (OpenFlowSwitch sw: controller.getSwitches()) {
296 sw.removeEventListener(outMsgListener);
297 }
298 LOG.info("Disable control message monitoring");
299 }
300
Thomas Vachuska893bf4b2015-05-29 18:13:18 -0700301 private void pushPortMetrics(Dpid dpid, List<OFPortStatsEntry> portStatsEntries) {
HIGUCHI Yuta1979f552015-12-28 21:24:26 -0800302 DeviceId deviceId = DeviceId.deviceId(Dpid.uri(dpid));
Thomas Vachuska893bf4b2015-05-29 18:13:18 -0700303 Collection<PortStatistics> stats = buildPortStatistics(deviceId, portStatsEntries);
sangho538108b2015-04-08 14:29:20 -0700304 providerService.updatePortStatistics(deviceId, stats);
305 }
306
Thomas Vachuska893bf4b2015-05-29 18:13:18 -0700307 private Collection<PortStatistics> buildPortStatistics(DeviceId deviceId,
308 List<OFPortStatsEntry> entries) {
sangho538108b2015-04-08 14:29:20 -0700309 HashSet<PortStatistics> stats = Sets.newHashSet();
310
Thomas Vachuska893bf4b2015-05-29 18:13:18 -0700311 for (OFPortStatsEntry entry : entries) {
Thomas Vachuskaf0397b52015-05-29 13:50:17 -0700312 try {
Thomas Vachuska4e6025e2015-06-03 11:25:21 -0700313 if (entry == null || entry.getPortNo() == null || entry.getPortNo().getPortNumber() < 0) {
Thomas Vachuskaf0397b52015-05-29 13:50:17 -0700314 continue;
315 }
316 DefaultPortStatistics.Builder builder = DefaultPortStatistics.builder();
317 DefaultPortStatistics stat = builder.setDeviceId(deviceId)
318 .setPort(entry.getPortNo().getPortNumber())
319 .setPacketsReceived(entry.getRxPackets().getValue())
320 .setPacketsSent(entry.getTxPackets().getValue())
321 .setBytesReceived(entry.getRxBytes().getValue())
322 .setBytesSent(entry.getTxBytes().getValue())
323 .setPacketsRxDropped(entry.getRxDropped().getValue())
324 .setPacketsTxDropped(entry.getTxDropped().getValue())
325 .setPacketsRxErrors(entry.getRxErrors().getValue())
326 .setPacketsTxErrors(entry.getTxErrors().getValue())
327 .setDurationSec(entry.getVersion() == OFVersion.OF_10 ? 0 : entry.getDurationSec())
328 .setDurationNano(entry.getVersion() == OFVersion.OF_10 ? 0 : entry.getDurationNsec())
329 .build();
sangho538108b2015-04-08 14:29:20 -0700330
Thomas Vachuskaf0397b52015-05-29 13:50:17 -0700331 stats.add(stat);
332 } catch (Exception e) {
333 LOG.warn("Unable to process port stats", e);
334 }
sangho538108b2015-04-08 14:29:20 -0700335 }
336
337 return Collections.unmodifiableSet(stats);
338
339 }
340
Jian Li152b8852015-12-07 14:47:25 -0800341 /**
342 * A listener for incoming OpenFlow messages.
343 */
344 private class IncomingMessageProvider implements OpenFlowEventListener {
345
346 @Override
347 public void handleMessage(Dpid dpid, OFMessage msg) {
348 if (isCtrlMsgMonitor) {
Jian Li28247b52016-01-07 17:24:15 -0800349 // TODO: handle all incoming OF messages
Jian Li152b8852015-12-07 14:47:25 -0800350 }
351 }
352 }
353
354 /**
355 * A listener for outgoing OpenFlow messages.
356 */
357 private class OutgoingMessageProvider implements OpenFlowEventListener {
358
359 @Override
360 public void handleMessage(Dpid dpid, OFMessage msg) {
361 if (isCtrlMsgMonitor) {
Jian Li28247b52016-01-07 17:24:15 -0800362 // TODO: handle all outgoing OF messages
Jian Li152b8852015-12-07 14:47:25 -0800363 }
364 }
365 }
366
sangho538108b2015-04-08 14:29:20 -0700367 private class InternalDeviceProvider implements OpenFlowSwitchListener, OpenFlowEventListener {
Thomas Vachuska893bf4b2015-05-29 18:13:18 -0700368
Srikanth Vavilapalli78baf582015-06-05 11:40:14 -0700369 private HashMap<Dpid, List<OFPortStatsEntry>> portStatsReplies = new HashMap<>();
Thomas Vachuska893bf4b2015-05-29 18:13:18 -0700370
alshabibf1216ed2014-09-03 11:53:54 -0700371 @Override
tomd1900f32014-09-03 14:08:16 -0700372 public void switchAdded(Dpid dpid) {
alshabib6f5460b2014-09-03 14:46:17 -0700373 if (providerService == null) {
374 return;
375 }
tom782a7cf2014-09-11 23:58:38 -0700376 DeviceId did = deviceId(uri(dpid));
alshabib6f5460b2014-09-03 14:46:17 -0700377 OpenFlowSwitch sw = controller.getSwitch(dpid);
Lei Xudee1aff2015-10-16 00:45:10 -0500378 if (sw == null) {
379 return;
380 }
alshabib6f5460b2014-09-03 14:46:17 -0700381
Jian Li152b8852015-12-07 14:47:25 -0800382 if (isCtrlMsgMonitor) {
383 // start to monitor the outgoing control messages
384 sw.addEventListener(outMsgListener);
385 }
386
alshabib7911a052014-10-16 17:49:37 -0700387 ChassisId cId = new ChassisId(dpid.value());
Ray Milkeye53f1712015-01-16 09:17:16 -0800388
Thomas Vachuska82041f52014-11-30 22:14:02 -0800389 SparseAnnotations annotations = DefaultAnnotations.builder()
andreafe3308f2015-10-06 15:51:25 -0700390 .set(AnnotationKeys.PROTOCOL, sw.factory().getVersion().toString())
391 .set(AnnotationKeys.CHANNEL_ID, sw.channelId())
392 .set(AnnotationKeys.MANAGEMENT_ADDRESS, sw.channelId().split(":")[0])
Ray Milkeye53f1712015-01-16 09:17:16 -0800393 .build();
394
tomd1900f32014-09-03 14:08:16 -0700395 DeviceDescription description =
Marc De Leenheerb9311372015-07-09 11:36:49 -0700396 new DefaultDeviceDescription(did.uri(), sw.deviceType(),
Ray Milkeyd3edd032015-01-16 11:38:58 -0800397 sw.manufacturerDescription(),
tom782a7cf2014-09-11 23:58:38 -0700398 sw.hardwareDescription(),
399 sw.softwareDescription(),
alshabib7911a052014-10-16 17:49:37 -0700400 sw.serialNumber(),
Thomas Vachuska82041f52014-11-30 22:14:02 -0800401 cId, annotations);
tom782a7cf2014-09-11 23:58:38 -0700402 providerService.deviceConnected(did, description);
Ayaka Koshibe5460d622015-05-14 12:19:19 -0700403 providerService.updatePorts(did, buildPortDescriptions(sw));
sangho538108b2015-04-08 14:29:20 -0700404
Thomas Vachuskafc52fec2015-05-18 19:13:56 -0700405 PortStatsCollector psc =
Lei Xudee1aff2015-10-16 00:45:10 -0500406 new PortStatsCollector(sw, portStatsPollFrequency);
sangho538108b2015-04-08 14:29:20 -0700407 psc.start();
408 collectors.put(dpid, psc);
Lei Xudee1aff2015-10-16 00:45:10 -0500409
410 //figure out race condition for collectors.remove() and collectors.put()
411 if (controller.getSwitch(dpid) == null) {
412 switchRemoved(dpid);
413 }
alshabib25c8eec2014-09-04 16:41:31 -0700414 }
415
alshabibf1216ed2014-09-03 11:53:54 -0700416 @Override
417 public void switchRemoved(Dpid dpid) {
alshabib6f5460b2014-09-03 14:46:17 -0700418 if (providerService == null) {
419 return;
420 }
tom782a7cf2014-09-11 23:58:38 -0700421 providerService.deviceDisconnected(deviceId(uri(dpid)));
alshabibf1216ed2014-09-03 11:53:54 -0700422
sangho538108b2015-04-08 14:29:20 -0700423 PortStatsCollector collector = collectors.remove(dpid);
424 if (collector != null) {
425 collector.stop();
426 }
Jian Li152b8852015-12-07 14:47:25 -0800427
428 OpenFlowSwitch sw = controller.getSwitch(dpid);
429 if (sw != null) {
430 if (isCtrlMsgMonitor) {
431 // stop monitoring the outgoing control messages
432 sw.removeEventListener(outMsgListener);
433 }
434 }
sangho538108b2015-04-08 14:29:20 -0700435 }
Ayaka Koshibe38594c22014-10-22 13:36:12 -0700436
437 @Override
438 public void switchChanged(Dpid dpid) {
HIGUCHI Yuta1979f552015-12-28 21:24:26 -0800439 LOG.debug("switchChanged({})", dpid);
Ayaka Koshibe38594c22014-10-22 13:36:12 -0700440 if (providerService == null) {
441 return;
442 }
443 DeviceId did = deviceId(uri(dpid));
444 OpenFlowSwitch sw = controller.getSwitch(dpid);
Lei Xudee1aff2015-10-16 00:45:10 -0500445 if (sw == null) {
446 return;
447 }
HIGUCHI Yuta1979f552015-12-28 21:24:26 -0800448 final List<PortDescription> ports = buildPortDescriptions(sw);
449 LOG.debug("switchChanged({}) {}", did, ports);
450 providerService.updatePorts(did, ports);
Ayaka Koshibe38594c22014-10-22 13:36:12 -0700451 }
452
alshabiba14f3642014-09-05 09:31:31 -0700453 @Override
454 public void portChanged(Dpid dpid, OFPortStatus status) {
HIGUCHI Yuta1979f552015-12-28 21:24:26 -0800455 LOG.debug("portChanged({},{})", dpid, status);
alshabibafc514a2014-12-01 14:44:05 -0800456 PortDescription portDescription = buildPortDescription(status);
tom782a7cf2014-09-11 23:58:38 -0700457 providerService.portStatusChanged(deviceId(uri(dpid)), portDescription);
alshabibf1216ed2014-09-03 11:53:54 -0700458 }
459
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700460 @Override
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700461 public void receivedRoleReply(Dpid dpid, RoleState requested, RoleState response) {
HIGUCHI Yuta1979f552015-12-28 21:24:26 -0800462 LOG.debug("receivedRoleReply({},{},{})", dpid, requested, response);
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700463 MastershipRole request = roleOf(requested);
464 MastershipRole reply = roleOf(response);
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700465 providerService.receivedRoleReply(deviceId(uri(dpid)), request, reply);
466 }
467
468 /**
469 * Translates a RoleState to the corresponding MastershipRole.
470 *
Thomas Vachuskafc52fec2015-05-18 19:13:56 -0700471 * @param response role state
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700472 * @return a MastershipRole
473 */
474 private MastershipRole roleOf(RoleState response) {
475 switch (response) {
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700476 case MASTER:
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700477 return MastershipRole.MASTER;
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700478 case EQUAL:
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700479 return MastershipRole.STANDBY;
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700480 case SLAVE:
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700481 return MastershipRole.NONE;
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700482 default:
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700483 LOG.warn("unknown role {}", response);
484 return null;
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700485 }
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700486 }
487
alshabiba14f3642014-09-05 09:31:31 -0700488 /**
489 * Builds a list of port descriptions for a given list of ports.
tomff7eb7c2014-09-08 12:49:03 -0700490 *
alshabiba14f3642014-09-05 09:31:31 -0700491 * @return list of portdescriptions
492 */
Ayaka Koshibe5460d622015-05-14 12:19:19 -0700493 private List<PortDescription> buildPortDescriptions(OpenFlowSwitch sw) {
494 final List<PortDescription> portDescs = new ArrayList<>(sw.getPorts().size());
Yafit Hadara9a73de2015-09-06 13:52:52 +0300495 if (!(Device.Type.ROADM.equals(sw.deviceType()))) {
496 sw.getPorts().forEach(port -> portDescs.add(buildPortDescription(port)));
497 }
Marc De Leenheerb9311372015-07-09 11:36:49 -0700498
499 OpenFlowOpticalSwitch opsw;
500 switch (sw.deviceType()) {
501 case ROADM:
502 opsw = (OpenFlowOpticalSwitch) sw;
Yafit Hadara9a73de2015-09-06 13:52:52 +0300503 List<OFPortDesc> ports = opsw.getPorts();
504 LOG.debug("SW ID {} , ETH- ODU CLT Ports {}", opsw.getId(), ports);
505 // ODU client ports are reported as ETH
506 ports.forEach(port -> portDescs.add(buildOduCltPortDescription(port)));
507
Marc De Leenheerb9311372015-07-09 11:36:49 -0700508 opsw.getPortTypes().forEach(type -> {
Yafit Hadara9a73de2015-09-06 13:52:52 +0300509 List<? extends OFObject> portsOf = opsw.getPortsOf(type);
510 LOG.debug("Ports Of{}", portsOf);
511 portsOf.forEach(
512 op -> {
HIGUCHI Yuta1979f552015-12-28 21:24:26 -0800513 portDescs.add(buildPortDescription(type, op));
Yafit Hadara9a73de2015-09-06 13:52:52 +0300514 }
515 );
Marc De Leenheerb9311372015-07-09 11:36:49 -0700516 });
517 break;
518 case FIBER_SWITCH:
519 opsw = (OpenFlowOpticalSwitch) sw;
520 opsw.getPortTypes().forEach(type -> {
521 opsw.getPortsOf(type).forEach(
522 op -> {
523 portDescs.add(buildPortDescription((OFCalientPortDescStatsEntry) op));
524 }
525 );
526 });
527 break;
528 default:
529 break;
alshabib4680bb62014-09-04 17:15:08 -0700530 }
Marc De Leenheerb9311372015-07-09 11:36:49 -0700531
alshabib4680bb62014-09-04 17:15:08 -0700532 return portDescs;
533 }
534
Yafit Hadara9a73de2015-09-06 13:52:52 +0300535 private PortDescription buildOduCltPortDescription(OFPortDesc port) {
536 PortNumber portNo = PortNumber.portNumber(port.getPortNo().getPortNumber());
537 boolean enabled = !port.getState().contains(OFPortState.LINK_DOWN) &&
538 !port.getConfig().contains(OFPortConfig.PORT_DOWN);
Rimon Ashkenazyd15ebf52016-01-06 14:36:13 +0200539 Long portSpeedInMbps = portSpeed(port);
Yafit Hadara9a73de2015-09-06 13:52:52 +0300540 OduCltPort.SignalType sigType = null;
541
Rimon Ashkenazyd15ebf52016-01-06 14:36:13 +0200542 switch (portSpeedInMbps.toString()) {
543 case "1000":
Yafit Hadara9a73de2015-09-06 13:52:52 +0300544 sigType = OduCltPort.SignalType.CLT_1GBE;
545 break;
Rimon Ashkenazyd15ebf52016-01-06 14:36:13 +0200546 case "10000":
Yafit Hadara9a73de2015-09-06 13:52:52 +0300547 sigType = OduCltPort.SignalType.CLT_10GBE;
548 break;
Rimon Ashkenazyd15ebf52016-01-06 14:36:13 +0200549 case "40000":
Yafit Hadara9a73de2015-09-06 13:52:52 +0300550 sigType = OduCltPort.SignalType.CLT_40GBE;
551 break;
Rimon Ashkenazyd15ebf52016-01-06 14:36:13 +0200552 case "100000":
Yafit Hadara9a73de2015-09-06 13:52:52 +0300553 sigType = OduCltPort.SignalType.CLT_100GBE;
554 break;
555 default:
Rimon Ashkenazyd15ebf52016-01-06 14:36:13 +0200556 throw new RuntimeException("Un recognize OduClt speed: " + portSpeedInMbps.toString());
Yafit Hadara9a73de2015-09-06 13:52:52 +0300557 }
558
559 SparseAnnotations annotations = buildOduCltAnnotation(port);
560 return new OduCltPortDescription(portNo, enabled, sigType, annotations);
561 }
562
563 private SparseAnnotations buildOduCltAnnotation(OFPortDesc port) {
564 SparseAnnotations annotations = null;
565 String portName = Strings.emptyToNull(port.getName());
566 if (portName != null) {
567 annotations = DefaultAnnotations.builder()
568 .set(AnnotationKeys.PORT_NAME, portName)
569 .set(AnnotationKeys.STATIC_PORT, Boolean.TRUE.toString()).build();
570 }
571 return annotations;
572 }
573
574 private PortDescription buildPortDescription(PortDescPropertyType ptype, OFObject port) {
575 if (port instanceof OFPortOptical) {
576 return buildPortDescription(ptype, (OFPortOptical) port);
577 }
578 return buildPortDescription(ptype, (OFExpPort) port);
579 }
580
581 /**
582 * Build a portDescription from a given a port description describing some
583 * Optical port.
584 *
585 * @param ptype description property type.
586 * @param port the port to build from.
587 * @return portDescription for the port.
588 */
589 private PortDescription buildPortDescription(PortDescPropertyType ptype, OFExpPort port) {
590 PortNumber portNo = PortNumber.portNumber(port.getPortNo().getPortNumber());
591 boolean enabled = !port.getState().contains(OFPortState.LINK_DOWN)
592 && !port.getConfig().contains(OFPortConfig.PORT_DOWN);
lishuai4ee42042015-11-30 17:19:21 +0800593 SparseAnnotations annotations = makePortAnnotation(port.getName(), port.getHwAddr().toString());
Yafit Hadara9a73de2015-09-06 13:52:52 +0300594
595 OFExpPortDescPropOpticalTransport firstProp = port.getProperties().get(0);
596 OFPortOpticalTransportSignalType sigType = firstProp.getPortSignalType();
597
598 DefaultPortDescription portDes = null;
599 switch (sigType) {
600 case OMSN:
Sho SHIMIZU70e76d82016-01-05 16:26:26 -0800601 portDes = new OmsPortDescription(portNo, enabled,
Rimon Ashkenazye3201032016-01-11 14:27:30 +0200602 FREQ191_7, FREQ191_7.add(FREQ4_4), FREQ50, annotations);
Yafit Hadara9a73de2015-09-06 13:52:52 +0300603 break;
604 case OCH:
605 OFExpPortOpticalTransportLayerEntry entry = firstProp.getFeatures().get(0).getValue().get(0);
606 OFPortOpticalTransportLayerClass layerClass = entry.getLayerClass();
607 if (!OFPortOpticalTransportLayerClass.ODU.equals(layerClass)) {
608 LOG.error("Unsupported layer Class {} ", layerClass);
609 return null;
610 }
611
612 // convert to ONOS OduSignalType
613 OduSignalType oduSignalType = OpenFlowDeviceValueMapper.
614 lookupOduSignalType((byte) entry.getSignalType());
615 //OchSignal is needed for OchPortDescription constructor,
616 //yet not relevant for tunable OCH port, creating with default parameters
617 OchSignal signalId = new OchSignal(GridType.DWDM, ChannelSpacing.CHL_50GHZ, 1, 1);
618
619 portDes = new OchPortDescription(portNo, enabled,
620 oduSignalType, true, signalId, annotations);
621
622 break;
623 case OTU2:
624 case OTU4:
625 LOG.error("Signal tpye OTU2/4 not supported yet ", port.toString());
626 break;
627 default:
628 break;
629 }
630
631 return portDes;
632 }
633
alshabiba14f3642014-09-05 09:31:31 -0700634 /**
Ray Milkeye0fade72015-01-15 13:29:47 -0800635 * Creates an annotation for the port name if one is available.
636 *
lishuai4ee42042015-11-30 17:19:21 +0800637 * @param portName the port name
638 * @param portMac the port mac
Ray Milkeye0fade72015-01-15 13:29:47 -0800639 * @return annotation containing the port name if one is found,
640 * null otherwise
641 */
lishuai4ee42042015-11-30 17:19:21 +0800642 private SparseAnnotations makePortAnnotation(String portName, String portMac) {
Ray Milkeye0fade72015-01-15 13:29:47 -0800643 SparseAnnotations annotations = null;
lishuai4ee42042015-11-30 17:19:21 +0800644 String pName = Strings.emptyToNull(portName);
645 String pMac = Strings.emptyToNull(portMac);
Ray Milkeye0fade72015-01-15 13:29:47 -0800646 if (portName != null) {
647 annotations = DefaultAnnotations.builder()
lishuai4ee42042015-11-30 17:19:21 +0800648 .set(AnnotationKeys.PORT_NAME, pName)
649 .set(AnnotationKeys.PORT_MAC, pMac).build();
Ray Milkeye0fade72015-01-15 13:29:47 -0800650 }
651 return annotations;
652 }
653
654 /**
Ayaka Koshibe5460d622015-05-14 12:19:19 -0700655 * Build a portDescription from a given Ethernet port description.
tomff7eb7c2014-09-08 12:49:03 -0700656 *
alshabiba14f3642014-09-05 09:31:31 -0700657 * @param port the port to build from.
658 * @return portDescription for the port.
659 */
660 private PortDescription buildPortDescription(OFPortDesc port) {
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700661 PortNumber portNo = PortNumber.portNumber(port.getPortNo().getPortNumber());
662 boolean enabled =
663 !port.getState().contains(OFPortState.LINK_DOWN) &&
664 !port.getConfig().contains(OFPortConfig.PORT_DOWN);
665 Port.Type type = port.getCurr().contains(OFPortFeatures.PF_FIBER) ? FIBER : COPPER;
lishuai4ee42042015-11-30 17:19:21 +0800666 SparseAnnotations annotations = makePortAnnotation(port.getName(), port.getHwAddr().toString());
Ray Milkeye0fade72015-01-15 13:29:47 -0800667 return new DefaultPortDescription(portNo, enabled, type,
668 portSpeed(port), annotations);
alshabiba14f3642014-09-05 09:31:31 -0700669 }
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700670
Ayaka Koshibe5460d622015-05-14 12:19:19 -0700671 /**
672 * Build a portDescription from a given a port description describing some
673 * Optical port.
674 *
675 * @param port description property type.
676 * @param port the port to build from.
677 * @return portDescription for the port.
678 */
679 private PortDescription buildPortDescription(PortDescPropertyType ptype, OFPortOptical port) {
Marc De Leenheerfc913dd2015-07-30 16:04:55 -0700680 checkArgument(port.getDesc().size() >= 1);
681
Ayaka Koshibe5460d622015-05-14 12:19:19 -0700682 // Minimally functional fixture. This needs to be fixed as we add better support.
683 PortNumber portNo = PortNumber.portNumber(port.getPortNo().getPortNumber());
Ayaka Koshibe74b55272015-05-28 15:16:04 -0700684
Ayaka Koshibe5460d622015-05-14 12:19:19 -0700685 boolean enabled = !port.getState().contains(OFPortState.LINK_DOWN)
686 && !port.getConfig().contains(OFPortConfig.PORT_DOWN);
lishuai4ee42042015-11-30 17:19:21 +0800687 SparseAnnotations annotations = makePortAnnotation(port.getName(), port.getHwAddr().toString());
Ayaka Koshibe5460d622015-05-14 12:19:19 -0700688
689 if (port.getVersion() == OFVersion.OF_13
690 && ptype == PortDescPropertyType.OPTICAL_TRANSPORT) {
691 // At this point, not much is carried in the optical port message.
Ayaka Koshibeae541732015-05-19 13:37:27 -0700692 LOG.debug("Optical transport port message {}", port.toString());
Ayaka Koshibe5460d622015-05-14 12:19:19 -0700693 } else {
694 // removable once 1.4+ support complete.
Ayaka Koshibeae541732015-05-19 13:37:27 -0700695 LOG.debug("Unsupported optical port properties");
Ayaka Koshibe5460d622015-05-14 12:19:19 -0700696 }
Marc De Leenheerfc913dd2015-07-30 16:04:55 -0700697
698 OFPortDescPropOpticalTransport desc = port.getDesc().get(0);
699 switch (desc.getPortSignalType()) {
700 // FIXME: use constants once loxi has full optical extensions
701 case 2: // OMS port
702 // Assume complete optical spectrum and 50 GHz grid
703 // LINC-OE is only supported optical OF device for now
704 return new OmsPortDescription(portNo, enabled,
705 Spectrum.U_BAND_MIN, Spectrum.O_BAND_MAX, Frequency.ofGHz(50), annotations);
706 case 5: // OCH port
707 OchSignal signal = new OchSignal(GridType.DWDM, ChannelSpacing.CHL_50GHZ, 0, 4);
708 return new OchPortDescription(portNo, enabled, OduSignalType.ODU4,
709 true, signal, annotations);
710 default:
711 break;
712 }
713
Ayaka Koshibe74b55272015-05-28 15:16:04 -0700714 return new DefaultPortDescription(portNo, enabled, FIBER, 0, annotations);
Ayaka Koshibe5460d622015-05-14 12:19:19 -0700715 }
716
Marc De Leenheerb9311372015-07-09 11:36:49 -0700717 /**
718 * Build a portDescription from a given port description describing a fiber switch optical port.
719 *
720 * @param port description property type.
721 * @param port the port to build from.
722 * @return portDescription for the port.
723 */
724 private PortDescription buildPortDescription(OFCalientPortDescStatsEntry port) {
725 PortNumber portNo = PortNumber.portNumber(port.getPortNo().getPortNumber());
726
727 // FIXME when Calient OF agent reports port status
728 boolean enabled = true;
lishuai4ee42042015-11-30 17:19:21 +0800729 SparseAnnotations annotations = makePortAnnotation(port.getName(), port.getHwAddr().toString());
Marc De Leenheerb9311372015-07-09 11:36:49 -0700730
Marc De Leenheerfc913dd2015-07-30 16:04:55 -0700731 // S160 data sheet
732 // Wavelength range: 1260 - 1630 nm, grid is irrelevant for this type of switch
733 return new OmsPortDescription(portNo, enabled,
734 Spectrum.U_BAND_MIN, Spectrum.O_BAND_MAX, Frequency.ofGHz(100), annotations);
Marc De Leenheerb9311372015-07-09 11:36:49 -0700735 }
736
alshabibafc514a2014-12-01 14:44:05 -0800737 private PortDescription buildPortDescription(OFPortStatus status) {
738 OFPortDesc port = status.getDesc();
739 if (status.getReason() != OFPortReason.DELETE) {
740 return buildPortDescription(port);
741 } else {
742 PortNumber portNo = PortNumber.portNumber(port.getPortNo().getPortNumber());
743 Port.Type type = port.getCurr().contains(OFPortFeatures.PF_FIBER) ? FIBER : COPPER;
lishuai4ee42042015-11-30 17:19:21 +0800744 SparseAnnotations annotations = makePortAnnotation(port.getName(), port.getHwAddr().toString());
Ray Milkeye0fade72015-01-15 13:29:47 -0800745 return new DefaultPortDescription(portNo, false, type,
746 portSpeed(port), annotations);
alshabibafc514a2014-12-01 14:44:05 -0800747 }
748 }
749
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700750 private long portSpeed(OFPortDesc port) {
751 if (port.getVersion() == OFVersion.OF_13) {
Charles Chan25b77322015-12-13 01:00:56 -0800752 // Note: getCurrSpeed() returns a value in kbps (this also applies to OF_11 and OF_12)
753 return port.getCurrSpeed() / KBPS;
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700754 }
755
756 PortSpeed portSpeed = PortSpeed.SPEED_NONE;
757 for (OFPortFeatures feat : port.getCurr()) {
758 portSpeed = PortSpeed.max(portSpeed, feat.getPortSpeed());
759 }
Thomas Vachuska98eda532014-10-29 17:31:02 -0700760 return portSpeed.getSpeedBps() / MBPS;
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700761 }
sangho538108b2015-04-08 14:29:20 -0700762
763 @Override
764 public void handleMessage(Dpid dpid, OFMessage msg) {
765 switch (msg.getType()) {
766 case STATS_REPLY:
767 if (((OFStatsReply) msg).getStatsType() == OFStatsType.PORT) {
Thomas Vachuska893bf4b2015-05-29 18:13:18 -0700768 OFPortStatsReply portStatsReply = (OFPortStatsReply) msg;
Srikanth Vavilapalli78baf582015-06-05 11:40:14 -0700769 List<OFPortStatsEntry> portStatsReplyList = portStatsReplies.get(dpid);
770 if (portStatsReplyList == null) {
771 portStatsReplyList = Lists.newArrayList();
772 }
773 portStatsReplyList.addAll(portStatsReply.getEntries());
774 portStatsReplies.put(dpid, portStatsReplyList);
Thomas Vachuska893bf4b2015-05-29 18:13:18 -0700775 if (!portStatsReply.getFlags().contains(OFStatsReplyFlags.REPLY_MORE)) {
Srikanth Vavilapalli78baf582015-06-05 11:40:14 -0700776 pushPortMetrics(dpid, portStatsReplies.get(dpid));
777 portStatsReplies.get(dpid).clear();
Thomas Vachuska893bf4b2015-05-29 18:13:18 -0700778 }
sangho538108b2015-04-08 14:29:20 -0700779 }
780 break;
781 default:
782 break;
783 }
784 }
alshabibf1216ed2014-09-03 11:53:54 -0700785 }
tomb5a46e62014-08-26 14:20:00 -0700786}