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