Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 1 | /* |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 2 | * Copyright 2014 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 | */ |
tom | b5a46e6 | 2014-08-26 14:20:00 -0700 | [diff] [blame] | 16 | package org.onlab.onos.provider.of.device.impl; |
| 17 | |
| 18 | import org.apache.felix.scr.annotations.Activate; |
| 19 | import org.apache.felix.scr.annotations.Component; |
| 20 | import org.apache.felix.scr.annotations.Deactivate; |
| 21 | import org.apache.felix.scr.annotations.Reference; |
| 22 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
Thomas Vachuska | 82041f5 | 2014-11-30 22:14:02 -0800 | [diff] [blame^] | 23 | import org.onlab.onos.net.DefaultAnnotations; |
tom | ab21e7c | 2014-08-26 15:23:08 -0700 | [diff] [blame] | 24 | import org.onlab.onos.net.Device; |
alshabib | c944fd0 | 2014-09-10 17:55:17 -0700 | [diff] [blame] | 25 | import org.onlab.onos.net.DeviceId; |
tom | ab21e7c | 2014-08-26 15:23:08 -0700 | [diff] [blame] | 26 | import org.onlab.onos.net.MastershipRole; |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 27 | import org.onlab.onos.net.Port; |
alshabib | 25c8eec | 2014-09-04 16:41:31 -0700 | [diff] [blame] | 28 | import org.onlab.onos.net.PortNumber; |
Thomas Vachuska | 82041f5 | 2014-11-30 22:14:02 -0800 | [diff] [blame^] | 29 | import org.onlab.onos.net.SparseAnnotations; |
tom | d1900f3 | 2014-09-03 14:08:16 -0700 | [diff] [blame] | 30 | import org.onlab.onos.net.device.DefaultDeviceDescription; |
alshabib | 25c8eec | 2014-09-04 16:41:31 -0700 | [diff] [blame] | 31 | import org.onlab.onos.net.device.DefaultPortDescription; |
alshabib | f1216ed | 2014-09-03 11:53:54 -0700 | [diff] [blame] | 32 | import org.onlab.onos.net.device.DeviceDescription; |
tom | ab21e7c | 2014-08-26 15:23:08 -0700 | [diff] [blame] | 33 | import org.onlab.onos.net.device.DeviceProvider; |
tom | 96dfcab | 2014-08-28 09:26:03 -0700 | [diff] [blame] | 34 | import org.onlab.onos.net.device.DeviceProviderRegistry; |
tom | ab21e7c | 2014-08-26 15:23:08 -0700 | [diff] [blame] | 35 | import org.onlab.onos.net.device.DeviceProviderService; |
alshabib | 25c8eec | 2014-09-04 16:41:31 -0700 | [diff] [blame] | 36 | import org.onlab.onos.net.device.PortDescription; |
tom | ab21e7c | 2014-08-26 15:23:08 -0700 | [diff] [blame] | 37 | import org.onlab.onos.net.provider.AbstractProvider; |
| 38 | import org.onlab.onos.net.provider.ProviderId; |
tom | 9c94c5b | 2014-09-17 13:14:42 -0700 | [diff] [blame] | 39 | import org.onlab.onos.openflow.controller.Dpid; |
| 40 | import org.onlab.onos.openflow.controller.OpenFlowController; |
| 41 | import org.onlab.onos.openflow.controller.OpenFlowSwitch; |
| 42 | import org.onlab.onos.openflow.controller.OpenFlowSwitchListener; |
| 43 | import org.onlab.onos.openflow.controller.RoleState; |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 44 | import org.onlab.packet.ChassisId; |
Ayaka Koshibe | e8708e3 | 2014-10-22 13:40:18 -0700 | [diff] [blame] | 45 | import org.projectfloodlight.openflow.protocol.OFFactory; |
alshabib | 4680bb6 | 2014-09-04 17:15:08 -0700 | [diff] [blame] | 46 | import org.projectfloodlight.openflow.protocol.OFPortConfig; |
alshabib | 25c8eec | 2014-09-04 16:41:31 -0700 | [diff] [blame] | 47 | import org.projectfloodlight.openflow.protocol.OFPortDesc; |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 48 | import org.projectfloodlight.openflow.protocol.OFPortFeatures; |
alshabib | 4680bb6 | 2014-09-04 17:15:08 -0700 | [diff] [blame] | 49 | import org.projectfloodlight.openflow.protocol.OFPortState; |
alshabib | a14f364 | 2014-09-05 09:31:31 -0700 | [diff] [blame] | 50 | import org.projectfloodlight.openflow.protocol.OFPortStatus; |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 51 | import org.projectfloodlight.openflow.protocol.OFVersion; |
| 52 | import org.projectfloodlight.openflow.types.PortSpeed; |
tom | b5a46e6 | 2014-08-26 14:20:00 -0700 | [diff] [blame] | 53 | import org.slf4j.Logger; |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 54 | |
tom | 782a7cf | 2014-09-11 23:58:38 -0700 | [diff] [blame] | 55 | import java.util.ArrayList; |
| 56 | import java.util.List; |
| 57 | |
| 58 | import static org.onlab.onos.net.DeviceId.deviceId; |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 59 | import static org.onlab.onos.net.Port.Type.COPPER; |
| 60 | import static org.onlab.onos.net.Port.Type.FIBER; |
tom | 9c94c5b | 2014-09-17 13:14:42 -0700 | [diff] [blame] | 61 | import static org.onlab.onos.openflow.controller.Dpid.dpid; |
| 62 | import static org.onlab.onos.openflow.controller.Dpid.uri; |
tom | 782a7cf | 2014-09-11 23:58:38 -0700 | [diff] [blame] | 63 | import static org.slf4j.LoggerFactory.getLogger; |
| 64 | |
tom | b5a46e6 | 2014-08-26 14:20:00 -0700 | [diff] [blame] | 65 | /** |
tom | b1260e4 | 2014-08-26 18:39:57 -0700 | [diff] [blame] | 66 | * Provider which uses an OpenFlow controller to detect network |
tom | e06f855 | 2014-08-26 16:58:42 -0700 | [diff] [blame] | 67 | * infrastructure devices. |
tom | b5a46e6 | 2014-08-26 14:20:00 -0700 | [diff] [blame] | 68 | */ |
tom | b1260e4 | 2014-08-26 18:39:57 -0700 | [diff] [blame] | 69 | @Component(immediate = true) |
tom | ab21e7c | 2014-08-26 15:23:08 -0700 | [diff] [blame] | 70 | public class OpenFlowDeviceProvider extends AbstractProvider implements DeviceProvider { |
tom | b5a46e6 | 2014-08-26 14:20:00 -0700 | [diff] [blame] | 71 | |
alshabib | a89cc58 | 2014-09-09 16:43:00 -0700 | [diff] [blame] | 72 | private static final Logger LOG = getLogger(OpenFlowDeviceProvider.class); |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 73 | private static final long MBPS = 1_000 * 1_000; |
tom | b5a46e6 | 2014-08-26 14:20:00 -0700 | [diff] [blame] | 74 | |
| 75 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
tom | 96dfcab | 2014-08-28 09:26:03 -0700 | [diff] [blame] | 76 | protected DeviceProviderRegistry providerRegistry; |
tom | ab21e7c | 2014-08-26 15:23:08 -0700 | [diff] [blame] | 77 | |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 78 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 79 | protected OpenFlowController controller; |
| 80 | |
tom | ab21e7c | 2014-08-26 15:23:08 -0700 | [diff] [blame] | 81 | private DeviceProviderService providerService; |
tom | b5a46e6 | 2014-08-26 14:20:00 -0700 | [diff] [blame] | 82 | |
alshabib | 4680bb6 | 2014-09-04 17:15:08 -0700 | [diff] [blame] | 83 | private final OpenFlowSwitchListener listener = new InternalDeviceProvider(); |
tom | d40fc7a | 2014-09-04 16:41:10 -0700 | [diff] [blame] | 84 | |
tom | ab21e7c | 2014-08-26 15:23:08 -0700 | [diff] [blame] | 85 | /** |
| 86 | * Creates an OpenFlow device provider. |
| 87 | */ |
| 88 | public OpenFlowDeviceProvider() { |
tom | 7e02cda | 2014-09-18 12:05:46 -0700 | [diff] [blame] | 89 | super(new ProviderId("of", "org.onlab.onos.provider.openflow")); |
tom | ab21e7c | 2014-08-26 15:23:08 -0700 | [diff] [blame] | 90 | } |
| 91 | |
tom | b5a46e6 | 2014-08-26 14:20:00 -0700 | [diff] [blame] | 92 | @Activate |
| 93 | public void activate() { |
tom | 96dfcab | 2014-08-28 09:26:03 -0700 | [diff] [blame] | 94 | providerService = providerRegistry.register(this); |
tom | d40fc7a | 2014-09-04 16:41:10 -0700 | [diff] [blame] | 95 | controller.addListener(listener); |
alshabib | c944fd0 | 2014-09-10 17:55:17 -0700 | [diff] [blame] | 96 | for (OpenFlowSwitch sw : controller.getSwitches()) { |
Yuta HIGUCHI | ac2972a | 2014-11-18 12:58:52 -0800 | [diff] [blame] | 97 | try { |
| 98 | listener.switchAdded(new Dpid(sw.getId())); |
| 99 | } catch (Exception e) { |
| 100 | LOG.warn("Failed initially adding {} : {}", sw.getStringId(), e.getMessage()); |
| 101 | LOG.debug("Error details:", e); |
| 102 | // disconnect to trigger switch-add later |
| 103 | sw.disconnectSwitch(); |
| 104 | } |
alshabib | c944fd0 | 2014-09-10 17:55:17 -0700 | [diff] [blame] | 105 | } |
alshabib | a89cc58 | 2014-09-09 16:43:00 -0700 | [diff] [blame] | 106 | LOG.info("Started"); |
tom | b5a46e6 | 2014-08-26 14:20:00 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | @Deactivate |
| 110 | public void deactivate() { |
alshabib | c944fd0 | 2014-09-10 17:55:17 -0700 | [diff] [blame] | 111 | for (OpenFlowSwitch sw : controller.getSwitches()) { |
tom | 782a7cf | 2014-09-11 23:58:38 -0700 | [diff] [blame] | 112 | providerService.deviceDisconnected(DeviceId.deviceId(uri(sw.getId()))); |
alshabib | c944fd0 | 2014-09-10 17:55:17 -0700 | [diff] [blame] | 113 | } |
tom | 96dfcab | 2014-08-28 09:26:03 -0700 | [diff] [blame] | 114 | providerRegistry.unregister(this); |
tom | d40fc7a | 2014-09-04 16:41:10 -0700 | [diff] [blame] | 115 | controller.removeListener(listener); |
tom | ab21e7c | 2014-08-26 15:23:08 -0700 | [diff] [blame] | 116 | providerService = null; |
alshabib | c944fd0 | 2014-09-10 17:55:17 -0700 | [diff] [blame] | 117 | |
alshabib | a89cc58 | 2014-09-09 16:43:00 -0700 | [diff] [blame] | 118 | LOG.info("Stopped"); |
tom | b5a46e6 | 2014-08-26 14:20:00 -0700 | [diff] [blame] | 119 | } |
| 120 | |
Ayaka Koshibe | e60d452 | 2014-10-28 15:07:00 -0700 | [diff] [blame] | 121 | |
| 122 | @Override |
Yuta HIGUCHI | 5481532 | 2014-10-31 23:17:08 -0700 | [diff] [blame] | 123 | public boolean isReachable(DeviceId deviceId) { |
| 124 | OpenFlowSwitch sw = controller.getSwitch(dpid(deviceId.uri())); |
Yuta HIGUCHI | 69a2735 | 2014-10-31 15:48:37 -0700 | [diff] [blame] | 125 | if (sw == null || !sw.isConnected()) { |
Ayaka Koshibe | e60d452 | 2014-10-28 15:07:00 -0700 | [diff] [blame] | 126 | return false; |
| 127 | } |
| 128 | return true; |
Ayaka Koshibe | e60d452 | 2014-10-28 15:07:00 -0700 | [diff] [blame] | 129 | } |
| 130 | |
tom | ab21e7c | 2014-08-26 15:23:08 -0700 | [diff] [blame] | 131 | @Override |
Ayaka Koshibe | 78bcbc1 | 2014-11-19 14:28:58 -0800 | [diff] [blame] | 132 | public void triggerProbe(DeviceId deviceId) { |
Yuta HIGUCHI | 802d0e8 | 2014-10-31 16:07:37 -0700 | [diff] [blame] | 133 | LOG.info("Triggering probe on device {}", deviceId); |
Ayaka Koshibe | e8708e3 | 2014-10-22 13:40:18 -0700 | [diff] [blame] | 134 | |
Yuta HIGUCHI | 802d0e8 | 2014-10-31 16:07:37 -0700 | [diff] [blame] | 135 | final Dpid dpid = dpid(deviceId.uri()); |
| 136 | OpenFlowSwitch sw = controller.getSwitch(dpid); |
| 137 | if (sw == null || !sw.isConnected()) { |
Ayaka Koshibe | 78bcbc1 | 2014-11-19 14:28:58 -0800 | [diff] [blame] | 138 | LOG.error("Failed to probe device {} on sw={}", deviceId, sw); |
Yuta HIGUCHI | 802d0e8 | 2014-10-31 16:07:37 -0700 | [diff] [blame] | 139 | providerService.deviceDisconnected(deviceId); |
| 140 | } else { |
Ayaka Koshibe | 78bcbc1 | 2014-11-19 14:28:58 -0800 | [diff] [blame] | 141 | LOG.trace("Confirmed device {} connection", deviceId); |
Yuta HIGUCHI | 802d0e8 | 2014-10-31 16:07:37 -0700 | [diff] [blame] | 142 | // FIXME require something like below to match javadoc description |
| 143 | // but this starts infinite loop with current DeviceManager |
| 144 | // final ChassisId cId = new ChassisId(dpid.value()); |
| 145 | // final Type deviceType = device.type(); |
| 146 | // DeviceDescription description = |
| 147 | // new DefaultDeviceDescription(deviceId.uri(), deviceType, |
| 148 | // sw.manfacturerDescription(), |
| 149 | // sw.hardwareDescription(), |
| 150 | // sw.softwareDescription(), |
| 151 | // sw.serialNumber(), |
| 152 | // cId); |
| 153 | // providerService.deviceConnected(deviceId, description); |
| 154 | } |
Ayaka Koshibe | e8708e3 | 2014-10-22 13:40:18 -0700 | [diff] [blame] | 155 | |
Ayaka Koshibe | e60d452 | 2014-10-28 15:07:00 -0700 | [diff] [blame] | 156 | // 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] | 157 | OFFactory fact = sw.factory(); |
| 158 | switch (fact.getVersion()) { |
| 159 | case OF_10: |
| 160 | sw.sendMsg(fact.buildFeaturesRequest().setXid(0).build()); |
| 161 | break; |
| 162 | case OF_13: |
| 163 | sw.sendMsg(fact.buildPortDescStatsRequest().setXid(0).build()); |
| 164 | break; |
| 165 | default: |
| 166 | LOG.warn("Unhandled protocol version"); |
| 167 | } |
tom | ab21e7c | 2014-08-26 15:23:08 -0700 | [diff] [blame] | 168 | } |
| 169 | |
Ayaka Koshibe | e60d452 | 2014-10-28 15:07:00 -0700 | [diff] [blame] | 170 | // Checks if the OF channel is connected. |
| 171 | //private boolean checkChannel(Device device, OpenFlowSwitch sw) { |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 172 | // FIXME if possible, we might want this to be part of |
| 173 | // OpenFlowSwitch interface so the driver interface isn't misused. |
Ayaka Koshibe | e60d452 | 2014-10-28 15:07:00 -0700 | [diff] [blame] | 174 | // if (sw == null || !((OpenFlowSwitchDriver) sw).isConnected()) { |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 175 | // return false; |
| 176 | // } |
Ayaka Koshibe | e60d452 | 2014-10-28 15:07:00 -0700 | [diff] [blame] | 177 | // return true; |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 178 | // } |
Ayaka Koshibe | e60d452 | 2014-10-28 15:07:00 -0700 | [diff] [blame] | 179 | |
tom | ab21e7c | 2014-08-26 15:23:08 -0700 | [diff] [blame] | 180 | @Override |
Yuta HIGUCHI | 5481532 | 2014-10-31 23:17:08 -0700 | [diff] [blame] | 181 | public void roleChanged(DeviceId deviceId, MastershipRole newRole) { |
alshabib | f1216ed | 2014-09-03 11:53:54 -0700 | [diff] [blame] | 182 | switch (newRole) { |
tom | 782a7cf | 2014-09-11 23:58:38 -0700 | [diff] [blame] | 183 | case MASTER: |
Yuta HIGUCHI | 5481532 | 2014-10-31 23:17:08 -0700 | [diff] [blame] | 184 | controller.setRole(dpid(deviceId.uri()), RoleState.MASTER); |
tom | 782a7cf | 2014-09-11 23:58:38 -0700 | [diff] [blame] | 185 | break; |
| 186 | case STANDBY: |
Yuta HIGUCHI | 5481532 | 2014-10-31 23:17:08 -0700 | [diff] [blame] | 187 | controller.setRole(dpid(deviceId.uri()), RoleState.EQUAL); |
tom | 782a7cf | 2014-09-11 23:58:38 -0700 | [diff] [blame] | 188 | break; |
| 189 | case NONE: |
Yuta HIGUCHI | 5481532 | 2014-10-31 23:17:08 -0700 | [diff] [blame] | 190 | controller.setRole(dpid(deviceId.uri()), RoleState.SLAVE); |
tom | 782a7cf | 2014-09-11 23:58:38 -0700 | [diff] [blame] | 191 | break; |
| 192 | default: |
| 193 | LOG.error("Unknown Mastership state : {}", newRole); |
alshabib | f1216ed | 2014-09-03 11:53:54 -0700 | [diff] [blame] | 194 | |
| 195 | } |
Yuta HIGUCHI | 5481532 | 2014-10-31 23:17:08 -0700 | [diff] [blame] | 196 | LOG.info("Accepting mastership role change for device {}", deviceId); |
tom | ab21e7c | 2014-08-26 15:23:08 -0700 | [diff] [blame] | 197 | } |
| 198 | |
alshabib | f1216ed | 2014-09-03 11:53:54 -0700 | [diff] [blame] | 199 | private class InternalDeviceProvider implements OpenFlowSwitchListener { |
alshabib | f1216ed | 2014-09-03 11:53:54 -0700 | [diff] [blame] | 200 | @Override |
tom | d1900f3 | 2014-09-03 14:08:16 -0700 | [diff] [blame] | 201 | public void switchAdded(Dpid dpid) { |
alshabib | 6f5460b | 2014-09-03 14:46:17 -0700 | [diff] [blame] | 202 | if (providerService == null) { |
| 203 | return; |
| 204 | } |
tom | 782a7cf | 2014-09-11 23:58:38 -0700 | [diff] [blame] | 205 | DeviceId did = deviceId(uri(dpid)); |
alshabib | 6f5460b | 2014-09-03 14:46:17 -0700 | [diff] [blame] | 206 | OpenFlowSwitch sw = controller.getSwitch(dpid); |
| 207 | |
Praseed Balakrishnan | a22eadf | 2014-10-20 14:21:45 -0700 | [diff] [blame] | 208 | Device.Type deviceType = sw.isOptical() ? Device.Type.ROADM : |
| 209 | Device.Type.SWITCH; |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 210 | ChassisId cId = new ChassisId(dpid.value()); |
Thomas Vachuska | 82041f5 | 2014-11-30 22:14:02 -0800 | [diff] [blame^] | 211 | SparseAnnotations annotations = DefaultAnnotations.builder() |
| 212 | .set("protocol", sw.factory().getVersion().toString()).build(); |
tom | d1900f3 | 2014-09-03 14:08:16 -0700 | [diff] [blame] | 213 | DeviceDescription description = |
Praseed Balakrishnan | a22eadf | 2014-10-20 14:21:45 -0700 | [diff] [blame] | 214 | new DefaultDeviceDescription(did.uri(), deviceType, |
tom | 782a7cf | 2014-09-11 23:58:38 -0700 | [diff] [blame] | 215 | sw.manfacturerDescription(), |
| 216 | sw.hardwareDescription(), |
| 217 | sw.softwareDescription(), |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 218 | sw.serialNumber(), |
Thomas Vachuska | 82041f5 | 2014-11-30 22:14:02 -0800 | [diff] [blame^] | 219 | cId, annotations); |
tom | 782a7cf | 2014-09-11 23:58:38 -0700 | [diff] [blame] | 220 | providerService.deviceConnected(did, description); |
| 221 | providerService.updatePorts(did, buildPortDescriptions(sw.getPorts())); |
alshabib | 25c8eec | 2014-09-04 16:41:31 -0700 | [diff] [blame] | 222 | } |
| 223 | |
alshabib | f1216ed | 2014-09-03 11:53:54 -0700 | [diff] [blame] | 224 | @Override |
| 225 | public void switchRemoved(Dpid dpid) { |
alshabib | 6f5460b | 2014-09-03 14:46:17 -0700 | [diff] [blame] | 226 | if (providerService == null) { |
| 227 | return; |
| 228 | } |
tom | 782a7cf | 2014-09-11 23:58:38 -0700 | [diff] [blame] | 229 | providerService.deviceDisconnected(deviceId(uri(dpid))); |
alshabib | f1216ed | 2014-09-03 11:53:54 -0700 | [diff] [blame] | 230 | } |
| 231 | |
Ayaka Koshibe | 38594c2 | 2014-10-22 13:36:12 -0700 | [diff] [blame] | 232 | |
| 233 | @Override |
| 234 | public void switchChanged(Dpid dpid) { |
| 235 | if (providerService == null) { |
| 236 | return; |
| 237 | } |
| 238 | DeviceId did = deviceId(uri(dpid)); |
| 239 | OpenFlowSwitch sw = controller.getSwitch(dpid); |
| 240 | providerService.updatePorts(did, buildPortDescriptions(sw.getPorts())); |
| 241 | } |
| 242 | |
alshabib | a14f364 | 2014-09-05 09:31:31 -0700 | [diff] [blame] | 243 | @Override |
| 244 | public void portChanged(Dpid dpid, OFPortStatus status) { |
tom | 782a7cf | 2014-09-11 23:58:38 -0700 | [diff] [blame] | 245 | PortDescription portDescription = buildPortDescription(status.getDesc()); |
| 246 | providerService.portStatusChanged(deviceId(uri(dpid)), portDescription); |
alshabib | f1216ed | 2014-09-03 11:53:54 -0700 | [diff] [blame] | 247 | } |
| 248 | |
Ayaka Koshibe | ab91cc4 | 2014-09-25 10:20:52 -0700 | [diff] [blame] | 249 | @Override |
Ayaka Koshibe | 3ef2b0d | 2014-10-31 13:58:27 -0700 | [diff] [blame] | 250 | public void receivedRoleReply(Dpid dpid, RoleState requested, RoleState response) { |
| 251 | MastershipRole request = roleOf(requested); |
| 252 | MastershipRole reply = roleOf(response); |
| 253 | |
| 254 | providerService.receivedRoleReply(deviceId(uri(dpid)), request, reply); |
| 255 | } |
| 256 | |
| 257 | /** |
| 258 | * Translates a RoleState to the corresponding MastershipRole. |
| 259 | * |
| 260 | * @param response |
| 261 | * @return a MastershipRole |
| 262 | */ |
| 263 | private MastershipRole roleOf(RoleState response) { |
| 264 | switch (response) { |
Ayaka Koshibe | ab91cc4 | 2014-09-25 10:20:52 -0700 | [diff] [blame] | 265 | case MASTER: |
Ayaka Koshibe | 3ef2b0d | 2014-10-31 13:58:27 -0700 | [diff] [blame] | 266 | return MastershipRole.MASTER; |
Ayaka Koshibe | ab91cc4 | 2014-09-25 10:20:52 -0700 | [diff] [blame] | 267 | case EQUAL: |
Ayaka Koshibe | 3ef2b0d | 2014-10-31 13:58:27 -0700 | [diff] [blame] | 268 | return MastershipRole.STANDBY; |
Ayaka Koshibe | ab91cc4 | 2014-09-25 10:20:52 -0700 | [diff] [blame] | 269 | case SLAVE: |
Ayaka Koshibe | 3ef2b0d | 2014-10-31 13:58:27 -0700 | [diff] [blame] | 270 | return MastershipRole.NONE; |
Ayaka Koshibe | ab91cc4 | 2014-09-25 10:20:52 -0700 | [diff] [blame] | 271 | default: |
Ayaka Koshibe | 3ef2b0d | 2014-10-31 13:58:27 -0700 | [diff] [blame] | 272 | LOG.warn("unknown role {}", response); |
| 273 | return null; |
Ayaka Koshibe | ab91cc4 | 2014-09-25 10:20:52 -0700 | [diff] [blame] | 274 | } |
Ayaka Koshibe | ab91cc4 | 2014-09-25 10:20:52 -0700 | [diff] [blame] | 275 | } |
| 276 | |
alshabib | a14f364 | 2014-09-05 09:31:31 -0700 | [diff] [blame] | 277 | /** |
| 278 | * Builds a list of port descriptions for a given list of ports. |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 279 | * |
alshabib | a14f364 | 2014-09-05 09:31:31 -0700 | [diff] [blame] | 280 | * @param ports the list of ports |
| 281 | * @return list of portdescriptions |
| 282 | */ |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 283 | private List<PortDescription> buildPortDescriptions(List<OFPortDesc> ports) { |
Yuta HIGUCHI | 38294a9 | 2014-10-16 18:09:49 -0700 | [diff] [blame] | 284 | final List<PortDescription> portDescs = new ArrayList<>(ports.size()); |
alshabib | 4680bb6 | 2014-09-04 17:15:08 -0700 | [diff] [blame] | 285 | for (OFPortDesc port : ports) { |
alshabib | a14f364 | 2014-09-05 09:31:31 -0700 | [diff] [blame] | 286 | portDescs.add(buildPortDescription(port)); |
alshabib | 4680bb6 | 2014-09-04 17:15:08 -0700 | [diff] [blame] | 287 | } |
| 288 | return portDescs; |
| 289 | } |
| 290 | |
alshabib | a14f364 | 2014-09-05 09:31:31 -0700 | [diff] [blame] | 291 | /** |
| 292 | * Build a portDescription from a given port. |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 293 | * |
alshabib | a14f364 | 2014-09-05 09:31:31 -0700 | [diff] [blame] | 294 | * @param port the port to build from. |
| 295 | * @return portDescription for the port. |
| 296 | */ |
| 297 | private PortDescription buildPortDescription(OFPortDesc port) { |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 298 | PortNumber portNo = PortNumber.portNumber(port.getPortNo().getPortNumber()); |
| 299 | boolean enabled = |
| 300 | !port.getState().contains(OFPortState.LINK_DOWN) && |
| 301 | !port.getConfig().contains(OFPortConfig.PORT_DOWN); |
| 302 | Port.Type type = port.getCurr().contains(OFPortFeatures.PF_FIBER) ? FIBER : COPPER; |
| 303 | return new DefaultPortDescription(portNo, enabled, type, portSpeed(port)); |
alshabib | a14f364 | 2014-09-05 09:31:31 -0700 | [diff] [blame] | 304 | } |
Ayaka Koshibe | ab91cc4 | 2014-09-25 10:20:52 -0700 | [diff] [blame] | 305 | |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 306 | private long portSpeed(OFPortDesc port) { |
| 307 | if (port.getVersion() == OFVersion.OF_13) { |
| 308 | return port.getCurrSpeed() / MBPS; |
| 309 | } |
| 310 | |
| 311 | PortSpeed portSpeed = PortSpeed.SPEED_NONE; |
| 312 | for (OFPortFeatures feat : port.getCurr()) { |
| 313 | portSpeed = PortSpeed.max(portSpeed, feat.getPortSpeed()); |
| 314 | } |
Thomas Vachuska | 98eda53 | 2014-10-29 17:31:02 -0700 | [diff] [blame] | 315 | return portSpeed.getSpeedBps() / MBPS; |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 316 | } |
alshabib | f1216ed | 2014-09-03 11:53:54 -0700 | [diff] [blame] | 317 | } |
| 318 | |
tom | b5a46e6 | 2014-08-26 14:20:00 -0700 | [diff] [blame] | 319 | } |