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