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