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 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.provider.of.device.impl; |
tom | b5a46e6 | 2014-08-26 14:20:00 -0700 | [diff] [blame] | 17 | |
| 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; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 23 | import org.onosproject.net.DefaultAnnotations; |
| 24 | import org.onosproject.net.Device; |
| 25 | import org.onosproject.net.DeviceId; |
| 26 | import org.onosproject.net.MastershipRole; |
| 27 | import org.onosproject.net.Port; |
| 28 | import org.onosproject.net.PortNumber; |
| 29 | import org.onosproject.net.SparseAnnotations; |
| 30 | import org.onosproject.net.device.DefaultDeviceDescription; |
| 31 | import org.onosproject.net.device.DefaultPortDescription; |
| 32 | import org.onosproject.net.device.DeviceDescription; |
| 33 | import org.onosproject.net.device.DeviceProvider; |
| 34 | import org.onosproject.net.device.DeviceProviderRegistry; |
| 35 | import org.onosproject.net.device.DeviceProviderService; |
| 36 | import org.onosproject.net.device.PortDescription; |
| 37 | import org.onosproject.net.provider.AbstractProvider; |
| 38 | import org.onosproject.net.provider.ProviderId; |
| 39 | import org.onosproject.openflow.controller.Dpid; |
| 40 | import org.onosproject.openflow.controller.OpenFlowController; |
| 41 | import org.onosproject.openflow.controller.OpenFlowSwitch; |
| 42 | import org.onosproject.openflow.controller.OpenFlowSwitchListener; |
| 43 | import org.onosproject.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 | |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 59 | import static org.onosproject.net.DeviceId.deviceId; |
| 60 | import static org.onosproject.net.Port.Type.COPPER; |
| 61 | import static org.onosproject.net.Port.Type.FIBER; |
| 62 | import static org.onosproject.openflow.controller.Dpid.dpid; |
| 63 | import static org.onosproject.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() { |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 90 | super(new ProviderId("of", "org.onosproject.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() { |
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 | } |
Ayaka Koshibe | e8708e3 | 2014-10-22 13:40:18 -0700 | [diff] [blame] | 141 | |
Ayaka Koshibe | e60d452 | 2014-10-28 15:07:00 -0700 | [diff] [blame] | 142 | // 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] | 143 | OFFactory fact = sw.factory(); |
| 144 | switch (fact.getVersion()) { |
| 145 | case OF_10: |
| 146 | sw.sendMsg(fact.buildFeaturesRequest().setXid(0).build()); |
| 147 | break; |
| 148 | case OF_13: |
| 149 | sw.sendMsg(fact.buildPortDescStatsRequest().setXid(0).build()); |
| 150 | break; |
| 151 | default: |
| 152 | LOG.warn("Unhandled protocol version"); |
| 153 | } |
tom | ab21e7c | 2014-08-26 15:23:08 -0700 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | @Override |
Yuta HIGUCHI | 5481532 | 2014-10-31 23:17:08 -0700 | [diff] [blame] | 157 | public void roleChanged(DeviceId deviceId, MastershipRole newRole) { |
alshabib | f1216ed | 2014-09-03 11:53:54 -0700 | [diff] [blame] | 158 | switch (newRole) { |
tom | 782a7cf | 2014-09-11 23:58:38 -0700 | [diff] [blame] | 159 | case MASTER: |
Yuta HIGUCHI | 5481532 | 2014-10-31 23:17:08 -0700 | [diff] [blame] | 160 | controller.setRole(dpid(deviceId.uri()), RoleState.MASTER); |
tom | 782a7cf | 2014-09-11 23:58:38 -0700 | [diff] [blame] | 161 | break; |
| 162 | case STANDBY: |
Yuta HIGUCHI | 5481532 | 2014-10-31 23:17:08 -0700 | [diff] [blame] | 163 | controller.setRole(dpid(deviceId.uri()), RoleState.EQUAL); |
tom | 782a7cf | 2014-09-11 23:58:38 -0700 | [diff] [blame] | 164 | break; |
| 165 | case NONE: |
Yuta HIGUCHI | 5481532 | 2014-10-31 23:17:08 -0700 | [diff] [blame] | 166 | controller.setRole(dpid(deviceId.uri()), RoleState.SLAVE); |
tom | 782a7cf | 2014-09-11 23:58:38 -0700 | [diff] [blame] | 167 | break; |
| 168 | default: |
| 169 | LOG.error("Unknown Mastership state : {}", newRole); |
alshabib | f1216ed | 2014-09-03 11:53:54 -0700 | [diff] [blame] | 170 | |
| 171 | } |
Yuta HIGUCHI | 5481532 | 2014-10-31 23:17:08 -0700 | [diff] [blame] | 172 | LOG.info("Accepting mastership role change for device {}", deviceId); |
tom | ab21e7c | 2014-08-26 15:23:08 -0700 | [diff] [blame] | 173 | } |
| 174 | |
alshabib | f1216ed | 2014-09-03 11:53:54 -0700 | [diff] [blame] | 175 | private class InternalDeviceProvider implements OpenFlowSwitchListener { |
alshabib | f1216ed | 2014-09-03 11:53:54 -0700 | [diff] [blame] | 176 | @Override |
tom | d1900f3 | 2014-09-03 14:08:16 -0700 | [diff] [blame] | 177 | public void switchAdded(Dpid dpid) { |
alshabib | 6f5460b | 2014-09-03 14:46:17 -0700 | [diff] [blame] | 178 | if (providerService == null) { |
| 179 | return; |
| 180 | } |
tom | 782a7cf | 2014-09-11 23:58:38 -0700 | [diff] [blame] | 181 | DeviceId did = deviceId(uri(dpid)); |
alshabib | 6f5460b | 2014-09-03 14:46:17 -0700 | [diff] [blame] | 182 | OpenFlowSwitch sw = controller.getSwitch(dpid); |
| 183 | |
Praseed Balakrishnan | a22eadf | 2014-10-20 14:21:45 -0700 | [diff] [blame] | 184 | Device.Type deviceType = sw.isOptical() ? Device.Type.ROADM : |
| 185 | Device.Type.SWITCH; |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 186 | ChassisId cId = new ChassisId(dpid.value()); |
Thomas Vachuska | 82041f5 | 2014-11-30 22:14:02 -0800 | [diff] [blame] | 187 | SparseAnnotations annotations = DefaultAnnotations.builder() |
| 188 | .set("protocol", sw.factory().getVersion().toString()).build(); |
tom | d1900f3 | 2014-09-03 14:08:16 -0700 | [diff] [blame] | 189 | DeviceDescription description = |
Praseed Balakrishnan | a22eadf | 2014-10-20 14:21:45 -0700 | [diff] [blame] | 190 | new DefaultDeviceDescription(did.uri(), deviceType, |
Ray Milkey | d3edd03 | 2015-01-16 11:38:58 -0800 | [diff] [blame^] | 191 | sw.manufacturerDescription(), |
tom | 782a7cf | 2014-09-11 23:58:38 -0700 | [diff] [blame] | 192 | sw.hardwareDescription(), |
| 193 | sw.softwareDescription(), |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 194 | sw.serialNumber(), |
Thomas Vachuska | 82041f5 | 2014-11-30 22:14:02 -0800 | [diff] [blame] | 195 | cId, annotations); |
tom | 782a7cf | 2014-09-11 23:58:38 -0700 | [diff] [blame] | 196 | providerService.deviceConnected(did, description); |
| 197 | providerService.updatePorts(did, buildPortDescriptions(sw.getPorts())); |
alshabib | 25c8eec | 2014-09-04 16:41:31 -0700 | [diff] [blame] | 198 | } |
| 199 | |
alshabib | f1216ed | 2014-09-03 11:53:54 -0700 | [diff] [blame] | 200 | @Override |
| 201 | public void switchRemoved(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 | providerService.deviceDisconnected(deviceId(uri(dpid))); |
alshabib | f1216ed | 2014-09-03 11:53:54 -0700 | [diff] [blame] | 206 | } |
| 207 | |
Ayaka Koshibe | 38594c2 | 2014-10-22 13:36:12 -0700 | [diff] [blame] | 208 | |
| 209 | @Override |
| 210 | public void switchChanged(Dpid dpid) { |
| 211 | if (providerService == null) { |
| 212 | return; |
| 213 | } |
| 214 | DeviceId did = deviceId(uri(dpid)); |
| 215 | OpenFlowSwitch sw = controller.getSwitch(dpid); |
| 216 | providerService.updatePorts(did, buildPortDescriptions(sw.getPorts())); |
| 217 | } |
| 218 | |
alshabib | a14f364 | 2014-09-05 09:31:31 -0700 | [diff] [blame] | 219 | @Override |
| 220 | public void portChanged(Dpid dpid, OFPortStatus status) { |
alshabib | afc514a | 2014-12-01 14:44:05 -0800 | [diff] [blame] | 221 | PortDescription portDescription = buildPortDescription(status); |
tom | 782a7cf | 2014-09-11 23:58:38 -0700 | [diff] [blame] | 222 | providerService.portStatusChanged(deviceId(uri(dpid)), portDescription); |
alshabib | f1216ed | 2014-09-03 11:53:54 -0700 | [diff] [blame] | 223 | } |
| 224 | |
Ayaka Koshibe | ab91cc4 | 2014-09-25 10:20:52 -0700 | [diff] [blame] | 225 | @Override |
Ayaka Koshibe | 3ef2b0d | 2014-10-31 13:58:27 -0700 | [diff] [blame] | 226 | public void receivedRoleReply(Dpid dpid, RoleState requested, RoleState response) { |
| 227 | MastershipRole request = roleOf(requested); |
| 228 | MastershipRole reply = roleOf(response); |
| 229 | |
| 230 | providerService.receivedRoleReply(deviceId(uri(dpid)), request, reply); |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * Translates a RoleState to the corresponding MastershipRole. |
| 235 | * |
| 236 | * @param response |
| 237 | * @return a MastershipRole |
| 238 | */ |
| 239 | private MastershipRole roleOf(RoleState response) { |
| 240 | switch (response) { |
Ayaka Koshibe | ab91cc4 | 2014-09-25 10:20:52 -0700 | [diff] [blame] | 241 | case MASTER: |
Ayaka Koshibe | 3ef2b0d | 2014-10-31 13:58:27 -0700 | [diff] [blame] | 242 | return MastershipRole.MASTER; |
Ayaka Koshibe | ab91cc4 | 2014-09-25 10:20:52 -0700 | [diff] [blame] | 243 | case EQUAL: |
Ayaka Koshibe | 3ef2b0d | 2014-10-31 13:58:27 -0700 | [diff] [blame] | 244 | return MastershipRole.STANDBY; |
Ayaka Koshibe | ab91cc4 | 2014-09-25 10:20:52 -0700 | [diff] [blame] | 245 | case SLAVE: |
Ayaka Koshibe | 3ef2b0d | 2014-10-31 13:58:27 -0700 | [diff] [blame] | 246 | return MastershipRole.NONE; |
Ayaka Koshibe | ab91cc4 | 2014-09-25 10:20:52 -0700 | [diff] [blame] | 247 | default: |
Ayaka Koshibe | 3ef2b0d | 2014-10-31 13:58:27 -0700 | [diff] [blame] | 248 | LOG.warn("unknown role {}", response); |
| 249 | return null; |
Ayaka Koshibe | ab91cc4 | 2014-09-25 10:20:52 -0700 | [diff] [blame] | 250 | } |
Ayaka Koshibe | ab91cc4 | 2014-09-25 10:20:52 -0700 | [diff] [blame] | 251 | } |
| 252 | |
alshabib | a14f364 | 2014-09-05 09:31:31 -0700 | [diff] [blame] | 253 | /** |
| 254 | * Builds a list of port descriptions for a given list of ports. |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 255 | * |
alshabib | a14f364 | 2014-09-05 09:31:31 -0700 | [diff] [blame] | 256 | * @param ports the list of ports |
| 257 | * @return list of portdescriptions |
| 258 | */ |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 259 | private List<PortDescription> buildPortDescriptions(List<OFPortDesc> ports) { |
Yuta HIGUCHI | 38294a9 | 2014-10-16 18:09:49 -0700 | [diff] [blame] | 260 | final List<PortDescription> portDescs = new ArrayList<>(ports.size()); |
alshabib | 4680bb6 | 2014-09-04 17:15:08 -0700 | [diff] [blame] | 261 | for (OFPortDesc port : ports) { |
alshabib | a14f364 | 2014-09-05 09:31:31 -0700 | [diff] [blame] | 262 | portDescs.add(buildPortDescription(port)); |
alshabib | 4680bb6 | 2014-09-04 17:15:08 -0700 | [diff] [blame] | 263 | } |
| 264 | return portDescs; |
| 265 | } |
| 266 | |
alshabib | a14f364 | 2014-09-05 09:31:31 -0700 | [diff] [blame] | 267 | /** |
| 268 | * Build a portDescription from a given port. |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 269 | * |
alshabib | a14f364 | 2014-09-05 09:31:31 -0700 | [diff] [blame] | 270 | * @param port the port to build from. |
| 271 | * @return portDescription for the port. |
| 272 | */ |
| 273 | private PortDescription buildPortDescription(OFPortDesc port) { |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 274 | PortNumber portNo = PortNumber.portNumber(port.getPortNo().getPortNumber()); |
| 275 | boolean enabled = |
| 276 | !port.getState().contains(OFPortState.LINK_DOWN) && |
| 277 | !port.getConfig().contains(OFPortConfig.PORT_DOWN); |
| 278 | Port.Type type = port.getCurr().contains(OFPortFeatures.PF_FIBER) ? FIBER : COPPER; |
| 279 | return new DefaultPortDescription(portNo, enabled, type, portSpeed(port)); |
alshabib | a14f364 | 2014-09-05 09:31:31 -0700 | [diff] [blame] | 280 | } |
Ayaka Koshibe | ab91cc4 | 2014-09-25 10:20:52 -0700 | [diff] [blame] | 281 | |
alshabib | afc514a | 2014-12-01 14:44:05 -0800 | [diff] [blame] | 282 | private PortDescription buildPortDescription(OFPortStatus status) { |
| 283 | OFPortDesc port = status.getDesc(); |
| 284 | if (status.getReason() != OFPortReason.DELETE) { |
| 285 | return buildPortDescription(port); |
| 286 | } else { |
| 287 | PortNumber portNo = PortNumber.portNumber(port.getPortNo().getPortNumber()); |
| 288 | Port.Type type = port.getCurr().contains(OFPortFeatures.PF_FIBER) ? FIBER : COPPER; |
| 289 | return new DefaultPortDescription(portNo, false, type, portSpeed(port)); |
| 290 | } |
| 291 | } |
| 292 | |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 293 | private long portSpeed(OFPortDesc port) { |
| 294 | if (port.getVersion() == OFVersion.OF_13) { |
| 295 | return port.getCurrSpeed() / MBPS; |
| 296 | } |
| 297 | |
| 298 | PortSpeed portSpeed = PortSpeed.SPEED_NONE; |
| 299 | for (OFPortFeatures feat : port.getCurr()) { |
| 300 | portSpeed = PortSpeed.max(portSpeed, feat.getPortSpeed()); |
| 301 | } |
Thomas Vachuska | 98eda53 | 2014-10-29 17:31:02 -0700 | [diff] [blame] | 302 | return portSpeed.getSpeedBps() / MBPS; |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 303 | } |
alshabib | f1216ed | 2014-09-03 11:53:54 -0700 | [diff] [blame] | 304 | } |
| 305 | |
tom | b5a46e6 | 2014-08-26 14:20:00 -0700 | [diff] [blame] | 306 | } |