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