Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 1 | /* |
Ray Milkey | 34c9590 | 2015-04-15 09:47:53 -0700 | [diff] [blame] | 2 | * Copyright 2014-2015 Open Networking Laboratory |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 4 | * 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 Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 8 | * 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 Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 15 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.provider.of.device.impl; |
tom | b5a46e6 | 2014-08-26 14:20:00 -0700 | [diff] [blame] | 17 | |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 18 | import com.google.common.collect.Maps; |
| 19 | import com.google.common.collect.Sets; |
Ayaka Koshibe | 5460d62 | 2015-05-14 12:19:19 -0700 | [diff] [blame] | 20 | |
tom | b5a46e6 | 2014-08-26 14:20:00 -0700 | [diff] [blame] | 21 | import org.apache.felix.scr.annotations.Activate; |
| 22 | import org.apache.felix.scr.annotations.Component; |
| 23 | import org.apache.felix.scr.annotations.Deactivate; |
| 24 | import org.apache.felix.scr.annotations.Reference; |
| 25 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
Thomas Vachuska | b52a014 | 2015-04-21 17:48:15 -0700 | [diff] [blame] | 26 | import org.onosproject.net.AnnotationKeys; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 27 | import org.onosproject.net.DefaultAnnotations; |
| 28 | import org.onosproject.net.Device; |
| 29 | import org.onosproject.net.DeviceId; |
| 30 | import org.onosproject.net.MastershipRole; |
| 31 | import org.onosproject.net.Port; |
| 32 | import org.onosproject.net.PortNumber; |
| 33 | import org.onosproject.net.SparseAnnotations; |
| 34 | import org.onosproject.net.device.DefaultDeviceDescription; |
| 35 | import org.onosproject.net.device.DefaultPortDescription; |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 36 | import org.onosproject.net.device.DefaultPortStatistics; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 37 | import org.onosproject.net.device.DeviceDescription; |
| 38 | import org.onosproject.net.device.DeviceProvider; |
| 39 | import org.onosproject.net.device.DeviceProviderRegistry; |
| 40 | import org.onosproject.net.device.DeviceProviderService; |
| 41 | import org.onosproject.net.device.PortDescription; |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 42 | import org.onosproject.net.device.PortStatistics; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 43 | import org.onosproject.net.provider.AbstractProvider; |
| 44 | import org.onosproject.net.provider.ProviderId; |
| 45 | import org.onosproject.openflow.controller.Dpid; |
| 46 | import org.onosproject.openflow.controller.OpenFlowController; |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 47 | import org.onosproject.openflow.controller.OpenFlowEventListener; |
Ayaka Koshibe | 5460d62 | 2015-05-14 12:19:19 -0700 | [diff] [blame] | 48 | import org.onosproject.openflow.controller.OpenFlowOpticalSwitch; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 49 | import org.onosproject.openflow.controller.OpenFlowSwitch; |
| 50 | import org.onosproject.openflow.controller.OpenFlowSwitchListener; |
Ayaka Koshibe | 5460d62 | 2015-05-14 12:19:19 -0700 | [diff] [blame] | 51 | import org.onosproject.openflow.controller.PortDescPropertyType; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 52 | import org.onosproject.openflow.controller.RoleState; |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 53 | import org.onlab.packet.ChassisId; |
Ayaka Koshibe | e8708e3 | 2014-10-22 13:40:18 -0700 | [diff] [blame] | 54 | import org.projectfloodlight.openflow.protocol.OFFactory; |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 55 | import org.projectfloodlight.openflow.protocol.OFMessage; |
alshabib | 4680bb6 | 2014-09-04 17:15:08 -0700 | [diff] [blame] | 56 | import org.projectfloodlight.openflow.protocol.OFPortConfig; |
alshabib | 25c8eec | 2014-09-04 16:41:31 -0700 | [diff] [blame] | 57 | import org.projectfloodlight.openflow.protocol.OFPortDesc; |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 58 | import org.projectfloodlight.openflow.protocol.OFPortFeatures; |
Ayaka Koshibe | 5460d62 | 2015-05-14 12:19:19 -0700 | [diff] [blame] | 59 | import org.projectfloodlight.openflow.protocol.OFPortOptical; |
alshabib | afc514a | 2014-12-01 14:44:05 -0800 | [diff] [blame] | 60 | import org.projectfloodlight.openflow.protocol.OFPortReason; |
alshabib | 4680bb6 | 2014-09-04 17:15:08 -0700 | [diff] [blame] | 61 | import org.projectfloodlight.openflow.protocol.OFPortState; |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 62 | import org.projectfloodlight.openflow.protocol.OFPortStatsEntry; |
| 63 | import org.projectfloodlight.openflow.protocol.OFPortStatsReply; |
alshabib | a14f364 | 2014-09-05 09:31:31 -0700 | [diff] [blame] | 64 | import org.projectfloodlight.openflow.protocol.OFPortStatus; |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 65 | import org.projectfloodlight.openflow.protocol.OFStatsReply; |
| 66 | import org.projectfloodlight.openflow.protocol.OFStatsType; |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 67 | import org.projectfloodlight.openflow.protocol.OFVersion; |
| 68 | import org.projectfloodlight.openflow.types.PortSpeed; |
tom | b5a46e6 | 2014-08-26 14:20:00 -0700 | [diff] [blame] | 69 | import org.slf4j.Logger; |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 70 | |
tom | 782a7cf | 2014-09-11 23:58:38 -0700 | [diff] [blame] | 71 | import java.util.ArrayList; |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 72 | import java.util.Collection; |
| 73 | import java.util.Collections; |
| 74 | import java.util.HashMap; |
| 75 | import java.util.HashSet; |
tom | 782a7cf | 2014-09-11 23:58:38 -0700 | [diff] [blame] | 76 | import java.util.List; |
| 77 | |
Ray Milkey | e0fade7 | 2015-01-15 13:29:47 -0800 | [diff] [blame] | 78 | import com.google.common.base.Strings; |
| 79 | |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 80 | import static org.onosproject.net.DeviceId.deviceId; |
| 81 | import static org.onosproject.net.Port.Type.COPPER; |
| 82 | import static org.onosproject.net.Port.Type.FIBER; |
| 83 | import static org.onosproject.openflow.controller.Dpid.dpid; |
| 84 | import static org.onosproject.openflow.controller.Dpid.uri; |
tom | 782a7cf | 2014-09-11 23:58:38 -0700 | [diff] [blame] | 85 | import static org.slf4j.LoggerFactory.getLogger; |
| 86 | |
tom | b5a46e6 | 2014-08-26 14:20:00 -0700 | [diff] [blame] | 87 | /** |
tom | b1260e4 | 2014-08-26 18:39:57 -0700 | [diff] [blame] | 88 | * Provider which uses an OpenFlow controller to detect network |
tom | e06f855 | 2014-08-26 16:58:42 -0700 | [diff] [blame] | 89 | * infrastructure devices. |
tom | b5a46e6 | 2014-08-26 14:20:00 -0700 | [diff] [blame] | 90 | */ |
tom | b1260e4 | 2014-08-26 18:39:57 -0700 | [diff] [blame] | 91 | @Component(immediate = true) |
tom | ab21e7c | 2014-08-26 15:23:08 -0700 | [diff] [blame] | 92 | public class OpenFlowDeviceProvider extends AbstractProvider implements DeviceProvider { |
tom | b5a46e6 | 2014-08-26 14:20:00 -0700 | [diff] [blame] | 93 | |
alshabib | a89cc58 | 2014-09-09 16:43:00 -0700 | [diff] [blame] | 94 | private static final Logger LOG = getLogger(OpenFlowDeviceProvider.class); |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 95 | private static final long MBPS = 1_000 * 1_000; |
tom | b5a46e6 | 2014-08-26 14:20:00 -0700 | [diff] [blame] | 96 | |
| 97 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
tom | 96dfcab | 2014-08-28 09:26:03 -0700 | [diff] [blame] | 98 | protected DeviceProviderRegistry providerRegistry; |
tom | ab21e7c | 2014-08-26 15:23:08 -0700 | [diff] [blame] | 99 | |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 100 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 101 | protected OpenFlowController controller; |
| 102 | |
tom | ab21e7c | 2014-08-26 15:23:08 -0700 | [diff] [blame] | 103 | private DeviceProviderService providerService; |
tom | b5a46e6 | 2014-08-26 14:20:00 -0700 | [diff] [blame] | 104 | |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 105 | private final InternalDeviceProvider listener = new InternalDeviceProvider(); |
| 106 | |
| 107 | // TODO: We need to make the poll interval configurable. |
| 108 | static final int POLL_INTERVAL = 10; |
| 109 | |
| 110 | private HashMap<Dpid, PortStatsCollector> collectors = Maps.newHashMap(); |
tom | d40fc7a | 2014-09-04 16:41:10 -0700 | [diff] [blame] | 111 | |
tom | ab21e7c | 2014-08-26 15:23:08 -0700 | [diff] [blame] | 112 | /** |
| 113 | * Creates an OpenFlow device provider. |
| 114 | */ |
| 115 | public OpenFlowDeviceProvider() { |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 116 | super(new ProviderId("of", "org.onosproject.provider.openflow")); |
tom | ab21e7c | 2014-08-26 15:23:08 -0700 | [diff] [blame] | 117 | } |
| 118 | |
tom | b5a46e6 | 2014-08-26 14:20:00 -0700 | [diff] [blame] | 119 | @Activate |
| 120 | public void activate() { |
tom | 96dfcab | 2014-08-28 09:26:03 -0700 | [diff] [blame] | 121 | providerService = providerRegistry.register(this); |
tom | d40fc7a | 2014-09-04 16:41:10 -0700 | [diff] [blame] | 122 | controller.addListener(listener); |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 123 | controller.addEventListener(listener); |
alshabib | c944fd0 | 2014-09-10 17:55:17 -0700 | [diff] [blame] | 124 | for (OpenFlowSwitch sw : controller.getSwitches()) { |
Yuta HIGUCHI | ac2972a | 2014-11-18 12:58:52 -0800 | [diff] [blame] | 125 | try { |
| 126 | listener.switchAdded(new Dpid(sw.getId())); |
| 127 | } catch (Exception e) { |
| 128 | LOG.warn("Failed initially adding {} : {}", sw.getStringId(), e.getMessage()); |
| 129 | LOG.debug("Error details:", e); |
| 130 | // disconnect to trigger switch-add later |
| 131 | sw.disconnectSwitch(); |
| 132 | } |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 133 | PortStatsCollector psc = new PortStatsCollector(sw, POLL_INTERVAL); |
| 134 | psc.start(); |
| 135 | collectors.put(new Dpid(sw.getId()), psc); |
alshabib | c944fd0 | 2014-09-10 17:55:17 -0700 | [diff] [blame] | 136 | } |
alshabib | a89cc58 | 2014-09-09 16:43:00 -0700 | [diff] [blame] | 137 | LOG.info("Started"); |
tom | b5a46e6 | 2014-08-26 14:20:00 -0700 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | @Deactivate |
| 141 | public void deactivate() { |
tom | 96dfcab | 2014-08-28 09:26:03 -0700 | [diff] [blame] | 142 | providerRegistry.unregister(this); |
tom | d40fc7a | 2014-09-04 16:41:10 -0700 | [diff] [blame] | 143 | controller.removeListener(listener); |
tom | ab21e7c | 2014-08-26 15:23:08 -0700 | [diff] [blame] | 144 | providerService = null; |
alshabib | c944fd0 | 2014-09-10 17:55:17 -0700 | [diff] [blame] | 145 | |
alshabib | a89cc58 | 2014-09-09 16:43:00 -0700 | [diff] [blame] | 146 | LOG.info("Stopped"); |
tom | b5a46e6 | 2014-08-26 14:20:00 -0700 | [diff] [blame] | 147 | } |
| 148 | |
Ayaka Koshibe | e60d452 | 2014-10-28 15:07:00 -0700 | [diff] [blame] | 149 | |
| 150 | @Override |
Yuta HIGUCHI | 5481532 | 2014-10-31 23:17:08 -0700 | [diff] [blame] | 151 | public boolean isReachable(DeviceId deviceId) { |
| 152 | OpenFlowSwitch sw = controller.getSwitch(dpid(deviceId.uri())); |
Yuta HIGUCHI | 69a2735 | 2014-10-31 15:48:37 -0700 | [diff] [blame] | 153 | if (sw == null || !sw.isConnected()) { |
Ayaka Koshibe | e60d452 | 2014-10-28 15:07:00 -0700 | [diff] [blame] | 154 | return false; |
| 155 | } |
| 156 | return true; |
Ayaka Koshibe | e60d452 | 2014-10-28 15:07:00 -0700 | [diff] [blame] | 157 | } |
| 158 | |
tom | ab21e7c | 2014-08-26 15:23:08 -0700 | [diff] [blame] | 159 | @Override |
Ayaka Koshibe | 78bcbc1 | 2014-11-19 14:28:58 -0800 | [diff] [blame] | 160 | public void triggerProbe(DeviceId deviceId) { |
Yuta HIGUCHI | 802d0e8 | 2014-10-31 16:07:37 -0700 | [diff] [blame] | 161 | LOG.info("Triggering probe on device {}", deviceId); |
Ayaka Koshibe | e8708e3 | 2014-10-22 13:40:18 -0700 | [diff] [blame] | 162 | |
Yuta HIGUCHI | 802d0e8 | 2014-10-31 16:07:37 -0700 | [diff] [blame] | 163 | final Dpid dpid = dpid(deviceId.uri()); |
| 164 | OpenFlowSwitch sw = controller.getSwitch(dpid); |
| 165 | if (sw == null || !sw.isConnected()) { |
Ayaka Koshibe | 78bcbc1 | 2014-11-19 14:28:58 -0800 | [diff] [blame] | 166 | LOG.error("Failed to probe device {} on sw={}", deviceId, sw); |
Yuta HIGUCHI | 802d0e8 | 2014-10-31 16:07:37 -0700 | [diff] [blame] | 167 | providerService.deviceDisconnected(deviceId); |
| 168 | } else { |
Ayaka Koshibe | 78bcbc1 | 2014-11-19 14:28:58 -0800 | [diff] [blame] | 169 | LOG.trace("Confirmed device {} connection", deviceId); |
Yuta HIGUCHI | 802d0e8 | 2014-10-31 16:07:37 -0700 | [diff] [blame] | 170 | } |
Ayaka Koshibe | e8708e3 | 2014-10-22 13:40:18 -0700 | [diff] [blame] | 171 | |
Ayaka Koshibe | e60d452 | 2014-10-28 15:07:00 -0700 | [diff] [blame] | 172 | // Prompt an update of port information. We can use any XID for this. |
Ayaka Koshibe | e8708e3 | 2014-10-22 13:40:18 -0700 | [diff] [blame] | 173 | OFFactory fact = sw.factory(); |
| 174 | switch (fact.getVersion()) { |
| 175 | case OF_10: |
| 176 | sw.sendMsg(fact.buildFeaturesRequest().setXid(0).build()); |
| 177 | break; |
| 178 | case OF_13: |
| 179 | sw.sendMsg(fact.buildPortDescStatsRequest().setXid(0).build()); |
| 180 | break; |
| 181 | default: |
| 182 | LOG.warn("Unhandled protocol version"); |
| 183 | } |
tom | ab21e7c | 2014-08-26 15:23:08 -0700 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | @Override |
Yuta HIGUCHI | 5481532 | 2014-10-31 23:17:08 -0700 | [diff] [blame] | 187 | public void roleChanged(DeviceId deviceId, MastershipRole newRole) { |
alshabib | f1216ed | 2014-09-03 11:53:54 -0700 | [diff] [blame] | 188 | switch (newRole) { |
tom | 782a7cf | 2014-09-11 23:58:38 -0700 | [diff] [blame] | 189 | case MASTER: |
Yuta HIGUCHI | 5481532 | 2014-10-31 23:17:08 -0700 | [diff] [blame] | 190 | controller.setRole(dpid(deviceId.uri()), RoleState.MASTER); |
tom | 782a7cf | 2014-09-11 23:58:38 -0700 | [diff] [blame] | 191 | break; |
| 192 | case STANDBY: |
Yuta HIGUCHI | 5481532 | 2014-10-31 23:17:08 -0700 | [diff] [blame] | 193 | controller.setRole(dpid(deviceId.uri()), RoleState.EQUAL); |
tom | 782a7cf | 2014-09-11 23:58:38 -0700 | [diff] [blame] | 194 | break; |
| 195 | case NONE: |
Yuta HIGUCHI | 5481532 | 2014-10-31 23:17:08 -0700 | [diff] [blame] | 196 | controller.setRole(dpid(deviceId.uri()), RoleState.SLAVE); |
tom | 782a7cf | 2014-09-11 23:58:38 -0700 | [diff] [blame] | 197 | break; |
| 198 | default: |
| 199 | LOG.error("Unknown Mastership state : {}", newRole); |
alshabib | f1216ed | 2014-09-03 11:53:54 -0700 | [diff] [blame] | 200 | |
| 201 | } |
Yuta HIGUCHI | 5481532 | 2014-10-31 23:17:08 -0700 | [diff] [blame] | 202 | LOG.info("Accepting mastership role change for device {}", deviceId); |
tom | ab21e7c | 2014-08-26 15:23:08 -0700 | [diff] [blame] | 203 | } |
| 204 | |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 205 | private void pushPortMetrics(Dpid dpid, OFPortStatsReply msg) { |
| 206 | DeviceId deviceId = DeviceId.deviceId(dpid.uri(dpid)); |
| 207 | |
| 208 | Collection<PortStatistics> stats = buildPortStatistics(deviceId, msg); |
| 209 | |
| 210 | providerService.updatePortStatistics(deviceId, stats); |
| 211 | } |
| 212 | |
| 213 | private Collection<PortStatistics> buildPortStatistics(DeviceId deviceId, OFPortStatsReply msg) { |
| 214 | |
| 215 | HashSet<PortStatistics> stats = Sets.newHashSet(); |
| 216 | |
| 217 | for (OFPortStatsEntry entry: msg.getEntries()) { |
| 218 | if (entry.getPortNo().getPortNumber() < 0) { |
| 219 | continue; |
| 220 | } |
| 221 | DefaultPortStatistics.Builder builder = DefaultPortStatistics.builder(); |
| 222 | DefaultPortStatistics stat = builder.setDeviceId(deviceId) |
| 223 | .setPort(entry.getPortNo().getPortNumber()) |
| 224 | .setPacketsReceived(entry.getRxPackets().getValue()) |
| 225 | .setPacketsSent(entry.getTxPackets().getValue()) |
| 226 | .setBytesReceived(entry.getRxBytes().getValue()) |
| 227 | .setBytesSent(entry.getTxBytes().getValue()) |
| 228 | .setPacketsRxDropped(entry.getRxDropped().getValue()) |
| 229 | .setPacketsTxDropped(entry.getTxDropped().getValue()) |
| 230 | .setPacketsRxErrors(entry.getRxErrors().getValue()) |
| 231 | .setPacketsTxErrors(entry.getTxErrors().getValue()) |
| 232 | .setDurationSec(entry.getDurationSec()) |
| 233 | .setDurationNano(entry.getDurationNsec()) |
| 234 | .build(); |
| 235 | |
| 236 | stats.add(stat); |
| 237 | } |
| 238 | |
| 239 | return Collections.unmodifiableSet(stats); |
| 240 | |
| 241 | } |
| 242 | |
| 243 | private class InternalDeviceProvider implements OpenFlowSwitchListener, OpenFlowEventListener { |
alshabib | f1216ed | 2014-09-03 11:53:54 -0700 | [diff] [blame] | 244 | @Override |
tom | d1900f3 | 2014-09-03 14:08:16 -0700 | [diff] [blame] | 245 | public void switchAdded(Dpid dpid) { |
alshabib | 6f5460b | 2014-09-03 14:46:17 -0700 | [diff] [blame] | 246 | if (providerService == null) { |
| 247 | return; |
| 248 | } |
tom | 782a7cf | 2014-09-11 23:58:38 -0700 | [diff] [blame] | 249 | DeviceId did = deviceId(uri(dpid)); |
alshabib | 6f5460b | 2014-09-03 14:46:17 -0700 | [diff] [blame] | 250 | OpenFlowSwitch sw = controller.getSwitch(dpid); |
| 251 | |
Praseed Balakrishnan | a22eadf | 2014-10-20 14:21:45 -0700 | [diff] [blame] | 252 | Device.Type deviceType = sw.isOptical() ? Device.Type.ROADM : |
| 253 | Device.Type.SWITCH; |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 254 | ChassisId cId = new ChassisId(dpid.value()); |
Ray Milkey | e53f171 | 2015-01-16 09:17:16 -0800 | [diff] [blame] | 255 | |
Thomas Vachuska | 82041f5 | 2014-11-30 22:14:02 -0800 | [diff] [blame] | 256 | SparseAnnotations annotations = DefaultAnnotations.builder() |
Ray Milkey | e53f171 | 2015-01-16 09:17:16 -0800 | [diff] [blame] | 257 | .set("protocol", sw.factory().getVersion().toString()) |
| 258 | .set("channelId", sw.channelId()) |
| 259 | .build(); |
| 260 | |
tom | d1900f3 | 2014-09-03 14:08:16 -0700 | [diff] [blame] | 261 | DeviceDescription description = |
Praseed Balakrishnan | a22eadf | 2014-10-20 14:21:45 -0700 | [diff] [blame] | 262 | new DefaultDeviceDescription(did.uri(), deviceType, |
Ray Milkey | d3edd03 | 2015-01-16 11:38:58 -0800 | [diff] [blame] | 263 | sw.manufacturerDescription(), |
tom | 782a7cf | 2014-09-11 23:58:38 -0700 | [diff] [blame] | 264 | sw.hardwareDescription(), |
| 265 | sw.softwareDescription(), |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 266 | sw.serialNumber(), |
Thomas Vachuska | 82041f5 | 2014-11-30 22:14:02 -0800 | [diff] [blame] | 267 | cId, annotations); |
tom | 782a7cf | 2014-09-11 23:58:38 -0700 | [diff] [blame] | 268 | providerService.deviceConnected(did, description); |
Ayaka Koshibe | 5460d62 | 2015-05-14 12:19:19 -0700 | [diff] [blame] | 269 | providerService.updatePorts(did, buildPortDescriptions(sw)); |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 270 | |
Thomas Vachuska | fc52fec | 2015-05-18 19:13:56 -0700 | [diff] [blame^] | 271 | PortStatsCollector psc = |
| 272 | new PortStatsCollector(controller.getSwitch(dpid), POLL_INTERVAL); |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 273 | psc.start(); |
| 274 | collectors.put(dpid, psc); |
alshabib | 25c8eec | 2014-09-04 16:41:31 -0700 | [diff] [blame] | 275 | } |
| 276 | |
alshabib | f1216ed | 2014-09-03 11:53:54 -0700 | [diff] [blame] | 277 | @Override |
| 278 | public void switchRemoved(Dpid dpid) { |
alshabib | 6f5460b | 2014-09-03 14:46:17 -0700 | [diff] [blame] | 279 | if (providerService == null) { |
| 280 | return; |
| 281 | } |
tom | 782a7cf | 2014-09-11 23:58:38 -0700 | [diff] [blame] | 282 | providerService.deviceDisconnected(deviceId(uri(dpid))); |
alshabib | f1216ed | 2014-09-03 11:53:54 -0700 | [diff] [blame] | 283 | |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 284 | PortStatsCollector collector = collectors.remove(dpid); |
| 285 | if (collector != null) { |
| 286 | collector.stop(); |
| 287 | } |
| 288 | } |
Ayaka Koshibe | 38594c2 | 2014-10-22 13:36:12 -0700 | [diff] [blame] | 289 | |
| 290 | @Override |
| 291 | public void switchChanged(Dpid dpid) { |
| 292 | if (providerService == null) { |
| 293 | return; |
| 294 | } |
| 295 | DeviceId did = deviceId(uri(dpid)); |
| 296 | OpenFlowSwitch sw = controller.getSwitch(dpid); |
Ayaka Koshibe | 5460d62 | 2015-05-14 12:19:19 -0700 | [diff] [blame] | 297 | providerService.updatePorts(did, buildPortDescriptions(sw)); |
Ayaka Koshibe | 38594c2 | 2014-10-22 13:36:12 -0700 | [diff] [blame] | 298 | } |
| 299 | |
alshabib | a14f364 | 2014-09-05 09:31:31 -0700 | [diff] [blame] | 300 | @Override |
| 301 | public void portChanged(Dpid dpid, OFPortStatus status) { |
alshabib | afc514a | 2014-12-01 14:44:05 -0800 | [diff] [blame] | 302 | PortDescription portDescription = buildPortDescription(status); |
tom | 782a7cf | 2014-09-11 23:58:38 -0700 | [diff] [blame] | 303 | providerService.portStatusChanged(deviceId(uri(dpid)), portDescription); |
alshabib | f1216ed | 2014-09-03 11:53:54 -0700 | [diff] [blame] | 304 | } |
| 305 | |
Ayaka Koshibe | ab91cc4 | 2014-09-25 10:20:52 -0700 | [diff] [blame] | 306 | @Override |
Ayaka Koshibe | 3ef2b0d | 2014-10-31 13:58:27 -0700 | [diff] [blame] | 307 | public void receivedRoleReply(Dpid dpid, RoleState requested, RoleState response) { |
| 308 | MastershipRole request = roleOf(requested); |
| 309 | MastershipRole reply = roleOf(response); |
| 310 | |
| 311 | providerService.receivedRoleReply(deviceId(uri(dpid)), request, reply); |
| 312 | } |
| 313 | |
| 314 | /** |
| 315 | * Translates a RoleState to the corresponding MastershipRole. |
| 316 | * |
Thomas Vachuska | fc52fec | 2015-05-18 19:13:56 -0700 | [diff] [blame^] | 317 | * @param response role state |
Ayaka Koshibe | 3ef2b0d | 2014-10-31 13:58:27 -0700 | [diff] [blame] | 318 | * @return a MastershipRole |
| 319 | */ |
| 320 | private MastershipRole roleOf(RoleState response) { |
| 321 | switch (response) { |
Ayaka Koshibe | ab91cc4 | 2014-09-25 10:20:52 -0700 | [diff] [blame] | 322 | case MASTER: |
Ayaka Koshibe | 3ef2b0d | 2014-10-31 13:58:27 -0700 | [diff] [blame] | 323 | return MastershipRole.MASTER; |
Ayaka Koshibe | ab91cc4 | 2014-09-25 10:20:52 -0700 | [diff] [blame] | 324 | case EQUAL: |
Ayaka Koshibe | 3ef2b0d | 2014-10-31 13:58:27 -0700 | [diff] [blame] | 325 | return MastershipRole.STANDBY; |
Ayaka Koshibe | ab91cc4 | 2014-09-25 10:20:52 -0700 | [diff] [blame] | 326 | case SLAVE: |
Ayaka Koshibe | 3ef2b0d | 2014-10-31 13:58:27 -0700 | [diff] [blame] | 327 | return MastershipRole.NONE; |
Ayaka Koshibe | ab91cc4 | 2014-09-25 10:20:52 -0700 | [diff] [blame] | 328 | default: |
Ayaka Koshibe | 3ef2b0d | 2014-10-31 13:58:27 -0700 | [diff] [blame] | 329 | LOG.warn("unknown role {}", response); |
| 330 | return null; |
Ayaka Koshibe | ab91cc4 | 2014-09-25 10:20:52 -0700 | [diff] [blame] | 331 | } |
Ayaka Koshibe | ab91cc4 | 2014-09-25 10:20:52 -0700 | [diff] [blame] | 332 | } |
| 333 | |
alshabib | a14f364 | 2014-09-05 09:31:31 -0700 | [diff] [blame] | 334 | /** |
| 335 | * Builds a list of port descriptions for a given list of ports. |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 336 | * |
alshabib | a14f364 | 2014-09-05 09:31:31 -0700 | [diff] [blame] | 337 | * @return list of portdescriptions |
| 338 | */ |
Ayaka Koshibe | 5460d62 | 2015-05-14 12:19:19 -0700 | [diff] [blame] | 339 | private List<PortDescription> buildPortDescriptions(OpenFlowSwitch sw) { |
| 340 | final List<PortDescription> portDescs = new ArrayList<>(sw.getPorts().size()); |
| 341 | sw.getPorts().forEach(port -> portDescs.add(buildPortDescription(port))); |
| 342 | if (sw.isOptical()) { |
| 343 | OpenFlowOpticalSwitch opsw = (OpenFlowOpticalSwitch) sw; |
| 344 | opsw.getPortTypes().forEach(type -> { |
| 345 | LOG.info("ports: {}", opsw.getPortsOf(type)); |
| 346 | opsw.getPortsOf(type).forEach( |
| 347 | op -> { |
| 348 | portDescs.add(buildPortDescription(type, (OFPortOptical) op)); |
| 349 | } |
| 350 | ); |
| 351 | }); |
alshabib | 4680bb6 | 2014-09-04 17:15:08 -0700 | [diff] [blame] | 352 | } |
| 353 | return portDescs; |
| 354 | } |
| 355 | |
alshabib | a14f364 | 2014-09-05 09:31:31 -0700 | [diff] [blame] | 356 | /** |
Ray Milkey | e0fade7 | 2015-01-15 13:29:47 -0800 | [diff] [blame] | 357 | * Creates an annotation for the port name if one is available. |
| 358 | * |
| 359 | * @param port description of the port |
| 360 | * @return annotation containing the port name if one is found, |
| 361 | * null otherwise |
| 362 | */ |
Ayaka Koshibe | 5460d62 | 2015-05-14 12:19:19 -0700 | [diff] [blame] | 363 | private SparseAnnotations makePortNameAnnotation(String port) { |
Ray Milkey | e0fade7 | 2015-01-15 13:29:47 -0800 | [diff] [blame] | 364 | SparseAnnotations annotations = null; |
Ayaka Koshibe | 5460d62 | 2015-05-14 12:19:19 -0700 | [diff] [blame] | 365 | String portName = Strings.emptyToNull(port); |
Ray Milkey | e0fade7 | 2015-01-15 13:29:47 -0800 | [diff] [blame] | 366 | if (portName != null) { |
| 367 | annotations = DefaultAnnotations.builder() |
Thomas Vachuska | b52a014 | 2015-04-21 17:48:15 -0700 | [diff] [blame] | 368 | .set(AnnotationKeys.PORT_NAME, portName).build(); |
Ray Milkey | e0fade7 | 2015-01-15 13:29:47 -0800 | [diff] [blame] | 369 | } |
| 370 | return annotations; |
| 371 | } |
| 372 | |
| 373 | /** |
Ayaka Koshibe | 5460d62 | 2015-05-14 12:19:19 -0700 | [diff] [blame] | 374 | * Build a portDescription from a given Ethernet port description. |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 375 | * |
alshabib | a14f364 | 2014-09-05 09:31:31 -0700 | [diff] [blame] | 376 | * @param port the port to build from. |
| 377 | * @return portDescription for the port. |
| 378 | */ |
| 379 | private PortDescription buildPortDescription(OFPortDesc port) { |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 380 | PortNumber portNo = PortNumber.portNumber(port.getPortNo().getPortNumber()); |
| 381 | boolean enabled = |
| 382 | !port.getState().contains(OFPortState.LINK_DOWN) && |
| 383 | !port.getConfig().contains(OFPortConfig.PORT_DOWN); |
| 384 | Port.Type type = port.getCurr().contains(OFPortFeatures.PF_FIBER) ? FIBER : COPPER; |
Ayaka Koshibe | 5460d62 | 2015-05-14 12:19:19 -0700 | [diff] [blame] | 385 | SparseAnnotations annotations = makePortNameAnnotation(port.getName()); |
Ray Milkey | e0fade7 | 2015-01-15 13:29:47 -0800 | [diff] [blame] | 386 | return new DefaultPortDescription(portNo, enabled, type, |
| 387 | portSpeed(port), annotations); |
alshabib | a14f364 | 2014-09-05 09:31:31 -0700 | [diff] [blame] | 388 | } |
Ayaka Koshibe | ab91cc4 | 2014-09-25 10:20:52 -0700 | [diff] [blame] | 389 | |
Ayaka Koshibe | 5460d62 | 2015-05-14 12:19:19 -0700 | [diff] [blame] | 390 | /** |
| 391 | * Build a portDescription from a given a port description describing some |
| 392 | * Optical port. |
| 393 | * |
| 394 | * @param port description property type. |
| 395 | * @param port the port to build from. |
| 396 | * @return portDescription for the port. |
| 397 | */ |
| 398 | private PortDescription buildPortDescription(PortDescPropertyType ptype, OFPortOptical port) { |
| 399 | // Minimally functional fixture. This needs to be fixed as we add better support. |
| 400 | PortNumber portNo = PortNumber.portNumber(port.getPortNo().getPortNumber()); |
| 401 | boolean enabled = !port.getState().contains(OFPortState.LINK_DOWN) |
| 402 | && !port.getConfig().contains(OFPortConfig.PORT_DOWN); |
| 403 | SparseAnnotations annotations = makePortNameAnnotation(port.getName()); |
| 404 | Port.Type type = FIBER; |
| 405 | |
| 406 | if (port.getVersion() == OFVersion.OF_13 |
| 407 | && ptype == PortDescPropertyType.OPTICAL_TRANSPORT) { |
| 408 | // At this point, not much is carried in the optical port message. |
| 409 | LOG.info("Optical transport port message {}", port.toString()); |
| 410 | } else { |
| 411 | // removable once 1.4+ support complete. |
| 412 | LOG.warn("Unsupported optical port properties"); |
| 413 | } |
| 414 | return new DefaultPortDescription(portNo, enabled, type, 0, annotations); |
| 415 | } |
| 416 | |
alshabib | afc514a | 2014-12-01 14:44:05 -0800 | [diff] [blame] | 417 | private PortDescription buildPortDescription(OFPortStatus status) { |
| 418 | OFPortDesc port = status.getDesc(); |
| 419 | if (status.getReason() != OFPortReason.DELETE) { |
| 420 | return buildPortDescription(port); |
| 421 | } else { |
| 422 | PortNumber portNo = PortNumber.portNumber(port.getPortNo().getPortNumber()); |
| 423 | Port.Type type = port.getCurr().contains(OFPortFeatures.PF_FIBER) ? FIBER : COPPER; |
Ayaka Koshibe | 5460d62 | 2015-05-14 12:19:19 -0700 | [diff] [blame] | 424 | SparseAnnotations annotations = makePortNameAnnotation(port.getName()); |
Ray Milkey | e0fade7 | 2015-01-15 13:29:47 -0800 | [diff] [blame] | 425 | return new DefaultPortDescription(portNo, false, type, |
| 426 | portSpeed(port), annotations); |
alshabib | afc514a | 2014-12-01 14:44:05 -0800 | [diff] [blame] | 427 | } |
| 428 | } |
| 429 | |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 430 | private long portSpeed(OFPortDesc port) { |
| 431 | if (port.getVersion() == OFVersion.OF_13) { |
| 432 | return port.getCurrSpeed() / MBPS; |
| 433 | } |
| 434 | |
| 435 | PortSpeed portSpeed = PortSpeed.SPEED_NONE; |
| 436 | for (OFPortFeatures feat : port.getCurr()) { |
| 437 | portSpeed = PortSpeed.max(portSpeed, feat.getPortSpeed()); |
| 438 | } |
Thomas Vachuska | 98eda53 | 2014-10-29 17:31:02 -0700 | [diff] [blame] | 439 | return portSpeed.getSpeedBps() / MBPS; |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 440 | } |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 441 | |
| 442 | @Override |
| 443 | public void handleMessage(Dpid dpid, OFMessage msg) { |
| 444 | switch (msg.getType()) { |
| 445 | case STATS_REPLY: |
| 446 | if (((OFStatsReply) msg).getStatsType() == OFStatsType.PORT) { |
| 447 | pushPortMetrics(dpid, (OFPortStatsReply) msg); |
| 448 | } |
| 449 | break; |
| 450 | default: |
| 451 | break; |
| 452 | } |
| 453 | } |
alshabib | f1216ed | 2014-09-03 11:53:54 -0700 | [diff] [blame] | 454 | } |
| 455 | |
tom | b5a46e6 | 2014-08-26 14:20:00 -0700 | [diff] [blame] | 456 | } |