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