blob: c6a44406a949f75160b2dcc94167ad63c0ae7981 [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;
Yafit Hadara9a73de2015-09-06 13:52:52 +0300126 private static final Frequency FREQ100 = Frequency.ofGHz(100);
127 private static final Frequency FREQ193_1 = Frequency.ofTHz(193.1);
128 private static final Frequency FREQ4_4 = Frequency.ofTHz(4.4);
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
143 // TODO: We need to make the poll interval configurable.
Srikanth Vavilapalli78baf582015-06-05 11:40:14 -0700144 static final int POLL_INTERVAL = 5;
Dusan Pajinbab8a5e2015-07-24 17:37:19 +0200145 @Property(name = "PortStatsPollFrequency", intValue = POLL_INTERVAL,
146 label = "Frequency (in seconds) for polling switch Port statistics")
147 private int portStatsPollFrequency = POLL_INTERVAL;
sangho538108b2015-04-08 14:29:20 -0700148
149 private HashMap<Dpid, PortStatsCollector> collectors = Maps.newHashMap();
tomd40fc7a2014-09-04 16:41:10 -0700150
tomab21e7c2014-08-26 15:23:08 -0700151 /**
152 * Creates an OpenFlow device provider.
153 */
154 public OpenFlowDeviceProvider() {
Brian O'Connorabafb502014-12-02 22:26:20 -0800155 super(new ProviderId("of", "org.onosproject.provider.openflow"));
tomab21e7c2014-08-26 15:23:08 -0700156 }
157
tomb5a46e62014-08-26 14:20:00 -0700158 @Activate
Dusan Pajinbab8a5e2015-07-24 17:37:19 +0200159 public void activate(ComponentContext context) {
160 cfgService.registerProperties(getClass());
tom96dfcab2014-08-28 09:26:03 -0700161 providerService = providerRegistry.register(this);
tomd40fc7a2014-09-04 16:41:10 -0700162 controller.addListener(listener);
sangho538108b2015-04-08 14:29:20 -0700163 controller.addEventListener(listener);
Thomas Vachuskad07c0922015-10-06 14:48:06 -0700164 connectInitialDevices();
alshabiba89cc582014-09-09 16:43:00 -0700165 LOG.info("Started");
tomb5a46e62014-08-26 14:20:00 -0700166 }
167
168 @Deactivate
Dusan Pajinbab8a5e2015-07-24 17:37:19 +0200169 public void deactivate(ComponentContext context) {
170 cfgService.unregisterProperties(getClass(), false);
tomd40fc7a2014-09-04 16:41:10 -0700171 controller.removeListener(listener);
Thomas Vachuskad07c0922015-10-06 14:48:06 -0700172 providerRegistry.unregister(this);
Thomas Vachuska74bcd922015-06-02 13:08:57 -0700173 collectors.values().forEach(PortStatsCollector::stop);
tomab21e7c2014-08-26 15:23:08 -0700174 providerService = null;
alshabiba89cc582014-09-09 16:43:00 -0700175 LOG.info("Stopped");
tomb5a46e62014-08-26 14:20:00 -0700176 }
177
Dusan Pajinbab8a5e2015-07-24 17:37:19 +0200178 @Modified
179 public void modified(ComponentContext context) {
180 Dictionary<?, ?> properties = context.getProperties();
181 int newPortStatsPollFrequency;
182 try {
183 String s = get(properties, "PortStatsPollFrequency");
184 newPortStatsPollFrequency = isNullOrEmpty(s) ? portStatsPollFrequency : Integer.parseInt(s.trim());
185
186 } catch (NumberFormatException | ClassCastException e) {
187 newPortStatsPollFrequency = portStatsPollFrequency;
188 }
189
190 if (newPortStatsPollFrequency != portStatsPollFrequency) {
191 portStatsPollFrequency = newPortStatsPollFrequency;
192 collectors.values().forEach(psc -> psc.adjustPollInterval(portStatsPollFrequency));
193 }
194
195 LOG.info("Settings: portStatsPollFrequency={}", portStatsPollFrequency);
196 }
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700197
Thomas Vachuskad07c0922015-10-06 14:48:06 -0700198 private void connectInitialDevices() {
199 for (OpenFlowSwitch sw : controller.getSwitches()) {
200 try {
201 listener.switchAdded(new Dpid(sw.getId()));
202 } catch (Exception e) {
203 LOG.warn("Failed initially adding {} : {}", sw.getStringId(), e.getMessage());
204 LOG.debug("Error details:", e);
205 // disconnect to trigger switch-add later
206 sw.disconnectSwitch();
207 }
208 PortStatsCollector psc = new PortStatsCollector(sw, portStatsPollFrequency);
209 psc.start();
210 collectors.put(new Dpid(sw.getId()), psc);
211 }
212 }
213
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700214 @Override
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700215 public boolean isReachable(DeviceId deviceId) {
216 OpenFlowSwitch sw = controller.getSwitch(dpid(deviceId.uri()));
Thomas Vachuskad07c0922015-10-06 14:48:06 -0700217 return sw != null && sw.isConnected();
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700218 }
219
tomab21e7c2014-08-26 15:23:08 -0700220 @Override
Ayaka Koshibe78bcbc12014-11-19 14:28:58 -0800221 public void triggerProbe(DeviceId deviceId) {
Madan Jampanic3328762015-05-31 12:39:27 -0700222 LOG.debug("Triggering probe on device {}", deviceId);
Ayaka Koshibee8708e32014-10-22 13:40:18 -0700223
Yuta HIGUCHI802d0e82014-10-31 16:07:37 -0700224 final Dpid dpid = dpid(deviceId.uri());
225 OpenFlowSwitch sw = controller.getSwitch(dpid);
226 if (sw == null || !sw.isConnected()) {
Ayaka Koshibe78bcbc12014-11-19 14:28:58 -0800227 LOG.error("Failed to probe device {} on sw={}", deviceId, sw);
Yuta HIGUCHI802d0e82014-10-31 16:07:37 -0700228 providerService.deviceDisconnected(deviceId);
Thomas Vachuska74bcd922015-06-02 13:08:57 -0700229 return;
Yuta HIGUCHI802d0e82014-10-31 16:07:37 -0700230 } else {
Ayaka Koshibe78bcbc12014-11-19 14:28:58 -0800231 LOG.trace("Confirmed device {} connection", deviceId);
Yuta HIGUCHI802d0e82014-10-31 16:07:37 -0700232 }
Ayaka Koshibee8708e32014-10-22 13:40:18 -0700233
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700234 // Prompt an update of port information. We can use any XID for this.
Ayaka Koshibee8708e32014-10-22 13:40:18 -0700235 OFFactory fact = sw.factory();
236 switch (fact.getVersion()) {
237 case OF_10:
238 sw.sendMsg(fact.buildFeaturesRequest().setXid(0).build());
239 break;
240 case OF_13:
241 sw.sendMsg(fact.buildPortDescStatsRequest().setXid(0).build());
242 break;
243 default:
244 LOG.warn("Unhandled protocol version");
245 }
tomab21e7c2014-08-26 15:23:08 -0700246 }
247
248 @Override
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700249 public void roleChanged(DeviceId deviceId, MastershipRole newRole) {
alshabibf1216ed2014-09-03 11:53:54 -0700250 switch (newRole) {
tom782a7cf2014-09-11 23:58:38 -0700251 case MASTER:
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700252 controller.setRole(dpid(deviceId.uri()), RoleState.MASTER);
tom782a7cf2014-09-11 23:58:38 -0700253 break;
254 case STANDBY:
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700255 controller.setRole(dpid(deviceId.uri()), RoleState.EQUAL);
tom782a7cf2014-09-11 23:58:38 -0700256 break;
257 case NONE:
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700258 controller.setRole(dpid(deviceId.uri()), RoleState.SLAVE);
tom782a7cf2014-09-11 23:58:38 -0700259 break;
260 default:
261 LOG.error("Unknown Mastership state : {}", newRole);
alshabibf1216ed2014-09-03 11:53:54 -0700262
263 }
Madan Jampanic3328762015-05-31 12:39:27 -0700264 LOG.debug("Accepting mastership role change for device {}", deviceId);
tomab21e7c2014-08-26 15:23:08 -0700265 }
266
Thomas Vachuska893bf4b2015-05-29 18:13:18 -0700267 private void pushPortMetrics(Dpid dpid, List<OFPortStatsEntry> portStatsEntries) {
sangho538108b2015-04-08 14:29:20 -0700268 DeviceId deviceId = DeviceId.deviceId(dpid.uri(dpid));
Thomas Vachuska893bf4b2015-05-29 18:13:18 -0700269 Collection<PortStatistics> stats = buildPortStatistics(deviceId, portStatsEntries);
sangho538108b2015-04-08 14:29:20 -0700270 providerService.updatePortStatistics(deviceId, stats);
271 }
272
Thomas Vachuska893bf4b2015-05-29 18:13:18 -0700273 private Collection<PortStatistics> buildPortStatistics(DeviceId deviceId,
274 List<OFPortStatsEntry> entries) {
sangho538108b2015-04-08 14:29:20 -0700275 HashSet<PortStatistics> stats = Sets.newHashSet();
276
Thomas Vachuska893bf4b2015-05-29 18:13:18 -0700277 for (OFPortStatsEntry entry : entries) {
Thomas Vachuskaf0397b52015-05-29 13:50:17 -0700278 try {
Thomas Vachuska4e6025e2015-06-03 11:25:21 -0700279 if (entry == null || entry.getPortNo() == null || entry.getPortNo().getPortNumber() < 0) {
Thomas Vachuskaf0397b52015-05-29 13:50:17 -0700280 continue;
281 }
282 DefaultPortStatistics.Builder builder = DefaultPortStatistics.builder();
283 DefaultPortStatistics stat = builder.setDeviceId(deviceId)
284 .setPort(entry.getPortNo().getPortNumber())
285 .setPacketsReceived(entry.getRxPackets().getValue())
286 .setPacketsSent(entry.getTxPackets().getValue())
287 .setBytesReceived(entry.getRxBytes().getValue())
288 .setBytesSent(entry.getTxBytes().getValue())
289 .setPacketsRxDropped(entry.getRxDropped().getValue())
290 .setPacketsTxDropped(entry.getTxDropped().getValue())
291 .setPacketsRxErrors(entry.getRxErrors().getValue())
292 .setPacketsTxErrors(entry.getTxErrors().getValue())
293 .setDurationSec(entry.getVersion() == OFVersion.OF_10 ? 0 : entry.getDurationSec())
294 .setDurationNano(entry.getVersion() == OFVersion.OF_10 ? 0 : entry.getDurationNsec())
295 .build();
sangho538108b2015-04-08 14:29:20 -0700296
Thomas Vachuskaf0397b52015-05-29 13:50:17 -0700297 stats.add(stat);
298 } catch (Exception e) {
299 LOG.warn("Unable to process port stats", e);
300 }
sangho538108b2015-04-08 14:29:20 -0700301 }
302
303 return Collections.unmodifiableSet(stats);
304
305 }
306
307 private class InternalDeviceProvider implements OpenFlowSwitchListener, OpenFlowEventListener {
Thomas Vachuska893bf4b2015-05-29 18:13:18 -0700308
Srikanth Vavilapalli78baf582015-06-05 11:40:14 -0700309 private HashMap<Dpid, List<OFPortStatsEntry>> portStatsReplies = new HashMap<>();
Thomas Vachuska893bf4b2015-05-29 18:13:18 -0700310
alshabibf1216ed2014-09-03 11:53:54 -0700311 @Override
tomd1900f32014-09-03 14:08:16 -0700312 public void switchAdded(Dpid dpid) {
alshabib6f5460b2014-09-03 14:46:17 -0700313 if (providerService == null) {
314 return;
315 }
tom782a7cf2014-09-11 23:58:38 -0700316 DeviceId did = deviceId(uri(dpid));
alshabib6f5460b2014-09-03 14:46:17 -0700317 OpenFlowSwitch sw = controller.getSwitch(dpid);
Lei Xudee1aff2015-10-16 00:45:10 -0500318 if (sw == null) {
319 return;
320 }
alshabib6f5460b2014-09-03 14:46:17 -0700321
alshabib7911a052014-10-16 17:49:37 -0700322 ChassisId cId = new ChassisId(dpid.value());
Ray Milkeye53f1712015-01-16 09:17:16 -0800323
Thomas Vachuska82041f52014-11-30 22:14:02 -0800324 SparseAnnotations annotations = DefaultAnnotations.builder()
andreafe3308f2015-10-06 15:51:25 -0700325 .set(AnnotationKeys.PROTOCOL, sw.factory().getVersion().toString())
326 .set(AnnotationKeys.CHANNEL_ID, sw.channelId())
327 .set(AnnotationKeys.MANAGEMENT_ADDRESS, sw.channelId().split(":")[0])
Ray Milkeye53f1712015-01-16 09:17:16 -0800328 .build();
329
tomd1900f32014-09-03 14:08:16 -0700330 DeviceDescription description =
Marc De Leenheerb9311372015-07-09 11:36:49 -0700331 new DefaultDeviceDescription(did.uri(), sw.deviceType(),
Ray Milkeyd3edd032015-01-16 11:38:58 -0800332 sw.manufacturerDescription(),
tom782a7cf2014-09-11 23:58:38 -0700333 sw.hardwareDescription(),
334 sw.softwareDescription(),
alshabib7911a052014-10-16 17:49:37 -0700335 sw.serialNumber(),
Thomas Vachuska82041f52014-11-30 22:14:02 -0800336 cId, annotations);
tom782a7cf2014-09-11 23:58:38 -0700337 providerService.deviceConnected(did, description);
Ayaka Koshibe5460d622015-05-14 12:19:19 -0700338 providerService.updatePorts(did, buildPortDescriptions(sw));
sangho538108b2015-04-08 14:29:20 -0700339
Thomas Vachuskafc52fec2015-05-18 19:13:56 -0700340 PortStatsCollector psc =
Lei Xudee1aff2015-10-16 00:45:10 -0500341 new PortStatsCollector(sw, portStatsPollFrequency);
sangho538108b2015-04-08 14:29:20 -0700342 psc.start();
343 collectors.put(dpid, psc);
Lei Xudee1aff2015-10-16 00:45:10 -0500344
345 //figure out race condition for collectors.remove() and collectors.put()
346 if (controller.getSwitch(dpid) == null) {
347 switchRemoved(dpid);
348 }
alshabib25c8eec2014-09-04 16:41:31 -0700349 }
350
alshabibf1216ed2014-09-03 11:53:54 -0700351 @Override
352 public void switchRemoved(Dpid dpid) {
alshabib6f5460b2014-09-03 14:46:17 -0700353 if (providerService == null) {
354 return;
355 }
tom782a7cf2014-09-11 23:58:38 -0700356 providerService.deviceDisconnected(deviceId(uri(dpid)));
alshabibf1216ed2014-09-03 11:53:54 -0700357
sangho538108b2015-04-08 14:29:20 -0700358 PortStatsCollector collector = collectors.remove(dpid);
359 if (collector != null) {
360 collector.stop();
361 }
362 }
Ayaka Koshibe38594c22014-10-22 13:36:12 -0700363
364 @Override
365 public void switchChanged(Dpid dpid) {
366 if (providerService == null) {
367 return;
368 }
369 DeviceId did = deviceId(uri(dpid));
370 OpenFlowSwitch sw = controller.getSwitch(dpid);
Lei Xudee1aff2015-10-16 00:45:10 -0500371 if (sw == null) {
372 return;
373 }
Ayaka Koshibe5460d622015-05-14 12:19:19 -0700374 providerService.updatePorts(did, buildPortDescriptions(sw));
Ayaka Koshibe38594c22014-10-22 13:36:12 -0700375 }
376
alshabiba14f3642014-09-05 09:31:31 -0700377 @Override
378 public void portChanged(Dpid dpid, OFPortStatus status) {
alshabibafc514a2014-12-01 14:44:05 -0800379 PortDescription portDescription = buildPortDescription(status);
tom782a7cf2014-09-11 23:58:38 -0700380 providerService.portStatusChanged(deviceId(uri(dpid)), portDescription);
alshabibf1216ed2014-09-03 11:53:54 -0700381 }
382
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700383 @Override
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700384 public void receivedRoleReply(Dpid dpid, RoleState requested, RoleState response) {
385 MastershipRole request = roleOf(requested);
386 MastershipRole reply = roleOf(response);
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700387 providerService.receivedRoleReply(deviceId(uri(dpid)), request, reply);
388 }
389
390 /**
391 * Translates a RoleState to the corresponding MastershipRole.
392 *
Thomas Vachuskafc52fec2015-05-18 19:13:56 -0700393 * @param response role state
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700394 * @return a MastershipRole
395 */
396 private MastershipRole roleOf(RoleState response) {
397 switch (response) {
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700398 case MASTER:
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700399 return MastershipRole.MASTER;
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700400 case EQUAL:
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700401 return MastershipRole.STANDBY;
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700402 case SLAVE:
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700403 return MastershipRole.NONE;
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700404 default:
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700405 LOG.warn("unknown role {}", response);
406 return null;
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700407 }
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700408 }
409
alshabiba14f3642014-09-05 09:31:31 -0700410 /**
411 * Builds a list of port descriptions for a given list of ports.
tomff7eb7c2014-09-08 12:49:03 -0700412 *
alshabiba14f3642014-09-05 09:31:31 -0700413 * @return list of portdescriptions
414 */
Ayaka Koshibe5460d622015-05-14 12:19:19 -0700415 private List<PortDescription> buildPortDescriptions(OpenFlowSwitch sw) {
416 final List<PortDescription> portDescs = new ArrayList<>(sw.getPorts().size());
Yafit Hadara9a73de2015-09-06 13:52:52 +0300417 if (!(Device.Type.ROADM.equals(sw.deviceType()))) {
418 sw.getPorts().forEach(port -> portDescs.add(buildPortDescription(port)));
419 }
Marc De Leenheerb9311372015-07-09 11:36:49 -0700420
421 OpenFlowOpticalSwitch opsw;
422 switch (sw.deviceType()) {
423 case ROADM:
424 opsw = (OpenFlowOpticalSwitch) sw;
Yafit Hadara9a73de2015-09-06 13:52:52 +0300425 List<OFPortDesc> ports = opsw.getPorts();
426 LOG.debug("SW ID {} , ETH- ODU CLT Ports {}", opsw.getId(), ports);
427 // ODU client ports are reported as ETH
428 ports.forEach(port -> portDescs.add(buildOduCltPortDescription(port)));
429
Marc De Leenheerb9311372015-07-09 11:36:49 -0700430 opsw.getPortTypes().forEach(type -> {
Yafit Hadara9a73de2015-09-06 13:52:52 +0300431 List<? extends OFObject> portsOf = opsw.getPortsOf(type);
432 LOG.debug("Ports Of{}", portsOf);
433 portsOf.forEach(
434 op -> {
435 portDescs.add(buildPortDescription(type, (OFObject) op));
436 }
437 );
Marc De Leenheerb9311372015-07-09 11:36:49 -0700438 });
439 break;
440 case FIBER_SWITCH:
441 opsw = (OpenFlowOpticalSwitch) sw;
442 opsw.getPortTypes().forEach(type -> {
443 opsw.getPortsOf(type).forEach(
444 op -> {
445 portDescs.add(buildPortDescription((OFCalientPortDescStatsEntry) op));
446 }
447 );
448 });
449 break;
450 default:
451 break;
alshabib4680bb62014-09-04 17:15:08 -0700452 }
Marc De Leenheerb9311372015-07-09 11:36:49 -0700453
alshabib4680bb62014-09-04 17:15:08 -0700454 return portDescs;
455 }
456
Yafit Hadara9a73de2015-09-06 13:52:52 +0300457 private PortDescription buildOduCltPortDescription(OFPortDesc port) {
458 PortNumber portNo = PortNumber.portNumber(port.getPortNo().getPortNumber());
459 boolean enabled = !port.getState().contains(OFPortState.LINK_DOWN) &&
460 !port.getConfig().contains(OFPortConfig.PORT_DOWN);
461 Long portSpeed = portSpeed(port);
462 OduCltPort.SignalType sigType = null;
463
464 switch (portSpeed.toString()) {
465 case "1":
466 sigType = OduCltPort.SignalType.CLT_1GBE;
467 break;
468 case "10":
469 sigType = OduCltPort.SignalType.CLT_10GBE;
470 break;
471 case "40":
472 sigType = OduCltPort.SignalType.CLT_40GBE;
473 break;
474 case "100":
475 sigType = OduCltPort.SignalType.CLT_100GBE;
476 break;
477 default:
478 throw new RuntimeException("Un recognize OduClt speed: " + portSpeed.toString());
479 }
480
481 SparseAnnotations annotations = buildOduCltAnnotation(port);
482 return new OduCltPortDescription(portNo, enabled, sigType, annotations);
483 }
484
485 private SparseAnnotations buildOduCltAnnotation(OFPortDesc port) {
486 SparseAnnotations annotations = null;
487 String portName = Strings.emptyToNull(port.getName());
488 if (portName != null) {
489 annotations = DefaultAnnotations.builder()
490 .set(AnnotationKeys.PORT_NAME, portName)
491 .set(AnnotationKeys.STATIC_PORT, Boolean.TRUE.toString()).build();
492 }
493 return annotations;
494 }
495
496 private PortDescription buildPortDescription(PortDescPropertyType ptype, OFObject port) {
497 if (port instanceof OFPortOptical) {
498 return buildPortDescription(ptype, (OFPortOptical) port);
499 }
500 return buildPortDescription(ptype, (OFExpPort) port);
501 }
502
503 /**
504 * Build a portDescription from a given a port description describing some
505 * Optical port.
506 *
507 * @param ptype description property type.
508 * @param port the port to build from.
509 * @return portDescription for the port.
510 */
511 private PortDescription buildPortDescription(PortDescPropertyType ptype, OFExpPort port) {
512 PortNumber portNo = PortNumber.portNumber(port.getPortNo().getPortNumber());
513 boolean enabled = !port.getState().contains(OFPortState.LINK_DOWN)
514 && !port.getConfig().contains(OFPortConfig.PORT_DOWN);
lishuai4ee42042015-11-30 17:19:21 +0800515 SparseAnnotations annotations = makePortAnnotation(port.getName(), port.getHwAddr().toString());
Yafit Hadara9a73de2015-09-06 13:52:52 +0300516
517 OFExpPortDescPropOpticalTransport firstProp = port.getProperties().get(0);
518 OFPortOpticalTransportSignalType sigType = firstProp.getPortSignalType();
519
520 DefaultPortDescription portDes = null;
521 switch (sigType) {
522 case OMSN:
523 portDes = new OmsPortDescription(portNo, enabled, FREQ193_1, FREQ193_1.add(FREQ4_4),
524 FREQ100, annotations);
525 break;
526 case OCH:
527 OFExpPortOpticalTransportLayerEntry entry = firstProp.getFeatures().get(0).getValue().get(0);
528 OFPortOpticalTransportLayerClass layerClass = entry.getLayerClass();
529 if (!OFPortOpticalTransportLayerClass.ODU.equals(layerClass)) {
530 LOG.error("Unsupported layer Class {} ", layerClass);
531 return null;
532 }
533
534 // convert to ONOS OduSignalType
535 OduSignalType oduSignalType = OpenFlowDeviceValueMapper.
536 lookupOduSignalType((byte) entry.getSignalType());
537 //OchSignal is needed for OchPortDescription constructor,
538 //yet not relevant for tunable OCH port, creating with default parameters
539 OchSignal signalId = new OchSignal(GridType.DWDM, ChannelSpacing.CHL_50GHZ, 1, 1);
540
541 portDes = new OchPortDescription(portNo, enabled,
542 oduSignalType, true, signalId, annotations);
543
544 break;
545 case OTU2:
546 case OTU4:
547 LOG.error("Signal tpye OTU2/4 not supported yet ", port.toString());
548 break;
549 default:
550 break;
551 }
552
553 return portDes;
554 }
555
alshabiba14f3642014-09-05 09:31:31 -0700556 /**
Ray Milkeye0fade72015-01-15 13:29:47 -0800557 * Creates an annotation for the port name if one is available.
558 *
lishuai4ee42042015-11-30 17:19:21 +0800559 * @param portName the port name
560 * @param portMac the port mac
Ray Milkeye0fade72015-01-15 13:29:47 -0800561 * @return annotation containing the port name if one is found,
562 * null otherwise
563 */
lishuai4ee42042015-11-30 17:19:21 +0800564 private SparseAnnotations makePortAnnotation(String portName, String portMac) {
Ray Milkeye0fade72015-01-15 13:29:47 -0800565 SparseAnnotations annotations = null;
lishuai4ee42042015-11-30 17:19:21 +0800566 String pName = Strings.emptyToNull(portName);
567 String pMac = Strings.emptyToNull(portMac);
Ray Milkeye0fade72015-01-15 13:29:47 -0800568 if (portName != null) {
569 annotations = DefaultAnnotations.builder()
lishuai4ee42042015-11-30 17:19:21 +0800570 .set(AnnotationKeys.PORT_NAME, pName)
571 .set(AnnotationKeys.PORT_MAC, pMac).build();
Ray Milkeye0fade72015-01-15 13:29:47 -0800572 }
573 return annotations;
574 }
575
576 /**
Ayaka Koshibe5460d622015-05-14 12:19:19 -0700577 * Build a portDescription from a given Ethernet port description.
tomff7eb7c2014-09-08 12:49:03 -0700578 *
alshabiba14f3642014-09-05 09:31:31 -0700579 * @param port the port to build from.
580 * @return portDescription for the port.
581 */
582 private PortDescription buildPortDescription(OFPortDesc port) {
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700583 PortNumber portNo = PortNumber.portNumber(port.getPortNo().getPortNumber());
584 boolean enabled =
585 !port.getState().contains(OFPortState.LINK_DOWN) &&
586 !port.getConfig().contains(OFPortConfig.PORT_DOWN);
587 Port.Type type = port.getCurr().contains(OFPortFeatures.PF_FIBER) ? FIBER : COPPER;
lishuai4ee42042015-11-30 17:19:21 +0800588 SparseAnnotations annotations = makePortAnnotation(port.getName(), port.getHwAddr().toString());
Ray Milkeye0fade72015-01-15 13:29:47 -0800589 return new DefaultPortDescription(portNo, enabled, type,
590 portSpeed(port), annotations);
alshabiba14f3642014-09-05 09:31:31 -0700591 }
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700592
Ayaka Koshibe5460d622015-05-14 12:19:19 -0700593 /**
594 * Build a portDescription from a given a port description describing some
595 * Optical port.
596 *
597 * @param port description property type.
598 * @param port the port to build from.
599 * @return portDescription for the port.
600 */
601 private PortDescription buildPortDescription(PortDescPropertyType ptype, OFPortOptical port) {
Marc De Leenheerfc913dd2015-07-30 16:04:55 -0700602 checkArgument(port.getDesc().size() >= 1);
603
Ayaka Koshibe5460d622015-05-14 12:19:19 -0700604 // Minimally functional fixture. This needs to be fixed as we add better support.
605 PortNumber portNo = PortNumber.portNumber(port.getPortNo().getPortNumber());
Ayaka Koshibe74b55272015-05-28 15:16:04 -0700606
Ayaka Koshibe5460d622015-05-14 12:19:19 -0700607 boolean enabled = !port.getState().contains(OFPortState.LINK_DOWN)
608 && !port.getConfig().contains(OFPortConfig.PORT_DOWN);
lishuai4ee42042015-11-30 17:19:21 +0800609 SparseAnnotations annotations = makePortAnnotation(port.getName(), port.getHwAddr().toString());
Ayaka Koshibe5460d622015-05-14 12:19:19 -0700610
611 if (port.getVersion() == OFVersion.OF_13
612 && ptype == PortDescPropertyType.OPTICAL_TRANSPORT) {
613 // At this point, not much is carried in the optical port message.
Ayaka Koshibeae541732015-05-19 13:37:27 -0700614 LOG.debug("Optical transport port message {}", port.toString());
Ayaka Koshibe5460d622015-05-14 12:19:19 -0700615 } else {
616 // removable once 1.4+ support complete.
Ayaka Koshibeae541732015-05-19 13:37:27 -0700617 LOG.debug("Unsupported optical port properties");
Ayaka Koshibe5460d622015-05-14 12:19:19 -0700618 }
Marc De Leenheerfc913dd2015-07-30 16:04:55 -0700619
620 OFPortDescPropOpticalTransport desc = port.getDesc().get(0);
621 switch (desc.getPortSignalType()) {
622 // FIXME: use constants once loxi has full optical extensions
623 case 2: // OMS port
624 // Assume complete optical spectrum and 50 GHz grid
625 // LINC-OE is only supported optical OF device for now
626 return new OmsPortDescription(portNo, enabled,
627 Spectrum.U_BAND_MIN, Spectrum.O_BAND_MAX, Frequency.ofGHz(50), annotations);
628 case 5: // OCH port
629 OchSignal signal = new OchSignal(GridType.DWDM, ChannelSpacing.CHL_50GHZ, 0, 4);
630 return new OchPortDescription(portNo, enabled, OduSignalType.ODU4,
631 true, signal, annotations);
632 default:
633 break;
634 }
635
Ayaka Koshibe74b55272015-05-28 15:16:04 -0700636 return new DefaultPortDescription(portNo, enabled, FIBER, 0, annotations);
Ayaka Koshibe5460d622015-05-14 12:19:19 -0700637 }
638
Marc De Leenheerb9311372015-07-09 11:36:49 -0700639 /**
640 * Build a portDescription from a given port description describing a fiber switch optical port.
641 *
642 * @param port description property type.
643 * @param port the port to build from.
644 * @return portDescription for the port.
645 */
646 private PortDescription buildPortDescription(OFCalientPortDescStatsEntry port) {
647 PortNumber portNo = PortNumber.portNumber(port.getPortNo().getPortNumber());
648
649 // FIXME when Calient OF agent reports port status
650 boolean enabled = true;
lishuai4ee42042015-11-30 17:19:21 +0800651 SparseAnnotations annotations = makePortAnnotation(port.getName(), port.getHwAddr().toString());
Marc De Leenheerb9311372015-07-09 11:36:49 -0700652
Marc De Leenheerfc913dd2015-07-30 16:04:55 -0700653 // S160 data sheet
654 // Wavelength range: 1260 - 1630 nm, grid is irrelevant for this type of switch
655 return new OmsPortDescription(portNo, enabled,
656 Spectrum.U_BAND_MIN, Spectrum.O_BAND_MAX, Frequency.ofGHz(100), annotations);
Marc De Leenheerb9311372015-07-09 11:36:49 -0700657 }
658
alshabibafc514a2014-12-01 14:44:05 -0800659 private PortDescription buildPortDescription(OFPortStatus status) {
660 OFPortDesc port = status.getDesc();
661 if (status.getReason() != OFPortReason.DELETE) {
662 return buildPortDescription(port);
663 } else {
664 PortNumber portNo = PortNumber.portNumber(port.getPortNo().getPortNumber());
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, false, type,
668 portSpeed(port), annotations);
alshabibafc514a2014-12-01 14:44:05 -0800669 }
670 }
671
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700672 private long portSpeed(OFPortDesc port) {
673 if (port.getVersion() == OFVersion.OF_13) {
Charles Chan25b77322015-12-13 01:00:56 -0800674 // Note: getCurrSpeed() returns a value in kbps (this also applies to OF_11 and OF_12)
675 return port.getCurrSpeed() / KBPS;
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700676 }
677
678 PortSpeed portSpeed = PortSpeed.SPEED_NONE;
679 for (OFPortFeatures feat : port.getCurr()) {
680 portSpeed = PortSpeed.max(portSpeed, feat.getPortSpeed());
681 }
Thomas Vachuska98eda532014-10-29 17:31:02 -0700682 return portSpeed.getSpeedBps() / MBPS;
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700683 }
sangho538108b2015-04-08 14:29:20 -0700684
685 @Override
686 public void handleMessage(Dpid dpid, OFMessage msg) {
687 switch (msg.getType()) {
688 case STATS_REPLY:
689 if (((OFStatsReply) msg).getStatsType() == OFStatsType.PORT) {
Thomas Vachuska893bf4b2015-05-29 18:13:18 -0700690 OFPortStatsReply portStatsReply = (OFPortStatsReply) msg;
Srikanth Vavilapalli78baf582015-06-05 11:40:14 -0700691 List<OFPortStatsEntry> portStatsReplyList = portStatsReplies.get(dpid);
692 if (portStatsReplyList == null) {
693 portStatsReplyList = Lists.newArrayList();
694 }
695 portStatsReplyList.addAll(portStatsReply.getEntries());
696 portStatsReplies.put(dpid, portStatsReplyList);
Thomas Vachuska893bf4b2015-05-29 18:13:18 -0700697 if (!portStatsReply.getFlags().contains(OFStatsReplyFlags.REPLY_MORE)) {
Srikanth Vavilapalli78baf582015-06-05 11:40:14 -0700698 pushPortMetrics(dpid, portStatsReplies.get(dpid));
699 portStatsReplies.get(dpid).clear();
Thomas Vachuska893bf4b2015-05-29 18:13:18 -0700700 }
sangho538108b2015-04-08 14:29:20 -0700701 }
702 break;
703 default:
704 break;
705 }
706 }
alshabibf1216ed2014-09-03 11:53:54 -0700707 }
tomb5a46e62014-08-26 14:20:00 -0700708}