Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
tom | b5a46e6 | 2014-08-26 14:20:00 -0700 | [diff] [blame] | 19 | package org.onlab.onos.provider.of.device.impl; |
| 20 | |
| 21 | import org.apache.felix.scr.annotations.Activate; |
| 22 | import org.apache.felix.scr.annotations.Component; |
| 23 | import org.apache.felix.scr.annotations.Deactivate; |
| 24 | import org.apache.felix.scr.annotations.Reference; |
| 25 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
tom | ab21e7c | 2014-08-26 15:23:08 -0700 | [diff] [blame] | 26 | import org.onlab.onos.net.Device; |
alshabib | c944fd0 | 2014-09-10 17:55:17 -0700 | [diff] [blame] | 27 | import org.onlab.onos.net.DeviceId; |
tom | ab21e7c | 2014-08-26 15:23:08 -0700 | [diff] [blame] | 28 | import org.onlab.onos.net.MastershipRole; |
alshabib | 25c8eec | 2014-09-04 16:41:31 -0700 | [diff] [blame] | 29 | import org.onlab.onos.net.PortNumber; |
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; |
Ayaka Koshibe | e8708e3 | 2014-10-22 13:40:18 -0700 | [diff] [blame] | 44 | import org.onlab.onos.openflow.controller.driver.OpenFlowSwitchDriver; |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 45 | import org.onlab.packet.ChassisId; |
Ayaka Koshibe | e8708e3 | 2014-10-22 13:40:18 -0700 | [diff] [blame] | 46 | import org.projectfloodlight.openflow.protocol.OFFactory; |
alshabib | 4680bb6 | 2014-09-04 17:15:08 -0700 | [diff] [blame] | 47 | import org.projectfloodlight.openflow.protocol.OFPortConfig; |
alshabib | 25c8eec | 2014-09-04 16:41:31 -0700 | [diff] [blame] | 48 | import org.projectfloodlight.openflow.protocol.OFPortDesc; |
alshabib | 4680bb6 | 2014-09-04 17:15:08 -0700 | [diff] [blame] | 49 | import org.projectfloodlight.openflow.protocol.OFPortState; |
alshabib | a14f364 | 2014-09-05 09:31:31 -0700 | [diff] [blame] | 50 | import org.projectfloodlight.openflow.protocol.OFPortStatus; |
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; |
tom | 9c94c5b | 2014-09-17 13:14:42 -0700 | [diff] [blame] | 57 | import static org.onlab.onos.openflow.controller.Dpid.dpid; |
| 58 | import static org.onlab.onos.openflow.controller.Dpid.uri; |
tom | 782a7cf | 2014-09-11 23:58:38 -0700 | [diff] [blame] | 59 | import static org.slf4j.LoggerFactory.getLogger; |
| 60 | |
tom | b5a46e6 | 2014-08-26 14:20:00 -0700 | [diff] [blame] | 61 | /** |
tom | b1260e4 | 2014-08-26 18:39:57 -0700 | [diff] [blame] | 62 | * Provider which uses an OpenFlow controller to detect network |
tom | e06f855 | 2014-08-26 16:58:42 -0700 | [diff] [blame] | 63 | * infrastructure devices. |
tom | b5a46e6 | 2014-08-26 14:20:00 -0700 | [diff] [blame] | 64 | */ |
tom | b1260e4 | 2014-08-26 18:39:57 -0700 | [diff] [blame] | 65 | @Component(immediate = true) |
tom | ab21e7c | 2014-08-26 15:23:08 -0700 | [diff] [blame] | 66 | public class OpenFlowDeviceProvider extends AbstractProvider implements DeviceProvider { |
tom | b5a46e6 | 2014-08-26 14:20:00 -0700 | [diff] [blame] | 67 | |
alshabib | a89cc58 | 2014-09-09 16:43:00 -0700 | [diff] [blame] | 68 | private static final Logger LOG = getLogger(OpenFlowDeviceProvider.class); |
tom | b5a46e6 | 2014-08-26 14:20:00 -0700 | [diff] [blame] | 69 | |
| 70 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
tom | 96dfcab | 2014-08-28 09:26:03 -0700 | [diff] [blame] | 71 | protected DeviceProviderRegistry providerRegistry; |
tom | ab21e7c | 2014-08-26 15:23:08 -0700 | [diff] [blame] | 72 | |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 73 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 74 | protected OpenFlowController controller; |
| 75 | |
tom | ab21e7c | 2014-08-26 15:23:08 -0700 | [diff] [blame] | 76 | private DeviceProviderService providerService; |
tom | b5a46e6 | 2014-08-26 14:20:00 -0700 | [diff] [blame] | 77 | |
alshabib | 4680bb6 | 2014-09-04 17:15:08 -0700 | [diff] [blame] | 78 | private final OpenFlowSwitchListener listener = new InternalDeviceProvider(); |
tom | d40fc7a | 2014-09-04 16:41:10 -0700 | [diff] [blame] | 79 | |
tom | ab21e7c | 2014-08-26 15:23:08 -0700 | [diff] [blame] | 80 | /** |
| 81 | * Creates an OpenFlow device provider. |
| 82 | */ |
| 83 | public OpenFlowDeviceProvider() { |
tom | 7e02cda | 2014-09-18 12:05:46 -0700 | [diff] [blame] | 84 | super(new ProviderId("of", "org.onlab.onos.provider.openflow")); |
tom | ab21e7c | 2014-08-26 15:23:08 -0700 | [diff] [blame] | 85 | } |
| 86 | |
tom | b5a46e6 | 2014-08-26 14:20:00 -0700 | [diff] [blame] | 87 | @Activate |
| 88 | public void activate() { |
tom | 96dfcab | 2014-08-28 09:26:03 -0700 | [diff] [blame] | 89 | providerService = providerRegistry.register(this); |
tom | d40fc7a | 2014-09-04 16:41:10 -0700 | [diff] [blame] | 90 | controller.addListener(listener); |
alshabib | c944fd0 | 2014-09-10 17:55:17 -0700 | [diff] [blame] | 91 | for (OpenFlowSwitch sw : controller.getSwitches()) { |
| 92 | listener.switchAdded(new Dpid(sw.getId())); |
| 93 | } |
alshabib | a89cc58 | 2014-09-09 16:43:00 -0700 | [diff] [blame] | 94 | LOG.info("Started"); |
tom | b5a46e6 | 2014-08-26 14:20:00 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | @Deactivate |
| 98 | public void deactivate() { |
alshabib | c944fd0 | 2014-09-10 17:55:17 -0700 | [diff] [blame] | 99 | for (OpenFlowSwitch sw : controller.getSwitches()) { |
tom | 782a7cf | 2014-09-11 23:58:38 -0700 | [diff] [blame] | 100 | providerService.deviceDisconnected(DeviceId.deviceId(uri(sw.getId()))); |
alshabib | c944fd0 | 2014-09-10 17:55:17 -0700 | [diff] [blame] | 101 | } |
tom | 96dfcab | 2014-08-28 09:26:03 -0700 | [diff] [blame] | 102 | providerRegistry.unregister(this); |
tom | d40fc7a | 2014-09-04 16:41:10 -0700 | [diff] [blame] | 103 | controller.removeListener(listener); |
tom | ab21e7c | 2014-08-26 15:23:08 -0700 | [diff] [blame] | 104 | providerService = null; |
alshabib | c944fd0 | 2014-09-10 17:55:17 -0700 | [diff] [blame] | 105 | |
alshabib | a89cc58 | 2014-09-09 16:43:00 -0700 | [diff] [blame] | 106 | LOG.info("Stopped"); |
tom | b5a46e6 | 2014-08-26 14:20:00 -0700 | [diff] [blame] | 107 | } |
| 108 | |
tom | ab21e7c | 2014-08-26 15:23:08 -0700 | [diff] [blame] | 109 | @Override |
| 110 | public void triggerProbe(Device device) { |
alshabib | a89cc58 | 2014-09-09 16:43:00 -0700 | [diff] [blame] | 111 | LOG.info("Triggering probe on device {}", device.id()); |
Ayaka Koshibe | e8708e3 | 2014-10-22 13:40:18 -0700 | [diff] [blame] | 112 | |
| 113 | // 1. check device liveness |
| 114 | // FIXME if possible, we might want this to be part of |
| 115 | // OpenFlowSwitch interface so the driver interface isn't misused. |
| 116 | OpenFlowSwitch sw = controller.getSwitch(dpid(device.id().uri())); |
Yuta HIGUCHI | eb24e9d | 2014-10-26 19:34:20 -0700 | [diff] [blame] | 117 | if (sw == null || |
| 118 | !((OpenFlowSwitchDriver) sw).isConnected()) { |
| 119 | LOG.error("Failed to probe device {} on sw={}", device, sw); |
Ayaka Koshibe | e8708e3 | 2014-10-22 13:40:18 -0700 | [diff] [blame] | 120 | providerService.deviceDisconnected(device.id()); |
| 121 | return; |
| 122 | } |
| 123 | |
| 124 | // 2. Prompt an update of port information. Do we have an XID for this? |
| 125 | OFFactory fact = sw.factory(); |
| 126 | switch (fact.getVersion()) { |
| 127 | case OF_10: |
| 128 | sw.sendMsg(fact.buildFeaturesRequest().setXid(0).build()); |
| 129 | break; |
| 130 | case OF_13: |
| 131 | sw.sendMsg(fact.buildPortDescStatsRequest().setXid(0).build()); |
| 132 | break; |
| 133 | default: |
| 134 | LOG.warn("Unhandled protocol version"); |
| 135 | } |
tom | ab21e7c | 2014-08-26 15:23:08 -0700 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | @Override |
| 139 | public void roleChanged(Device device, MastershipRole newRole) { |
alshabib | f1216ed | 2014-09-03 11:53:54 -0700 | [diff] [blame] | 140 | switch (newRole) { |
tom | 782a7cf | 2014-09-11 23:58:38 -0700 | [diff] [blame] | 141 | case MASTER: |
| 142 | controller.setRole(dpid(device.id().uri()), RoleState.MASTER); |
| 143 | break; |
| 144 | case STANDBY: |
| 145 | controller.setRole(dpid(device.id().uri()), RoleState.EQUAL); |
| 146 | break; |
| 147 | case NONE: |
| 148 | controller.setRole(dpid(device.id().uri()), RoleState.SLAVE); |
| 149 | break; |
| 150 | default: |
| 151 | LOG.error("Unknown Mastership state : {}", newRole); |
alshabib | f1216ed | 2014-09-03 11:53:54 -0700 | [diff] [blame] | 152 | |
| 153 | } |
alshabib | a89cc58 | 2014-09-09 16:43:00 -0700 | [diff] [blame] | 154 | LOG.info("Accepting mastership role change for device {}", device.id()); |
tom | ab21e7c | 2014-08-26 15:23:08 -0700 | [diff] [blame] | 155 | } |
| 156 | |
alshabib | f1216ed | 2014-09-03 11:53:54 -0700 | [diff] [blame] | 157 | private class InternalDeviceProvider implements OpenFlowSwitchListener { |
alshabib | f1216ed | 2014-09-03 11:53:54 -0700 | [diff] [blame] | 158 | @Override |
tom | d1900f3 | 2014-09-03 14:08:16 -0700 | [diff] [blame] | 159 | public void switchAdded(Dpid dpid) { |
alshabib | 6f5460b | 2014-09-03 14:46:17 -0700 | [diff] [blame] | 160 | if (providerService == null) { |
| 161 | return; |
| 162 | } |
tom | 782a7cf | 2014-09-11 23:58:38 -0700 | [diff] [blame] | 163 | DeviceId did = deviceId(uri(dpid)); |
alshabib | 6f5460b | 2014-09-03 14:46:17 -0700 | [diff] [blame] | 164 | OpenFlowSwitch sw = controller.getSwitch(dpid); |
| 165 | |
Praseed Balakrishnan | a22eadf | 2014-10-20 14:21:45 -0700 | [diff] [blame] | 166 | Device.Type deviceType = sw.isOptical() ? Device.Type.ROADM : |
| 167 | Device.Type.SWITCH; |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 168 | ChassisId cId = new ChassisId(dpid.value()); |
tom | d1900f3 | 2014-09-03 14:08:16 -0700 | [diff] [blame] | 169 | DeviceDescription description = |
Praseed Balakrishnan | a22eadf | 2014-10-20 14:21:45 -0700 | [diff] [blame] | 170 | new DefaultDeviceDescription(did.uri(), deviceType, |
tom | 782a7cf | 2014-09-11 23:58:38 -0700 | [diff] [blame] | 171 | sw.manfacturerDescription(), |
| 172 | sw.hardwareDescription(), |
| 173 | sw.softwareDescription(), |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 174 | sw.serialNumber(), |
| 175 | cId); |
tom | 782a7cf | 2014-09-11 23:58:38 -0700 | [diff] [blame] | 176 | providerService.deviceConnected(did, description); |
| 177 | providerService.updatePorts(did, buildPortDescriptions(sw.getPorts())); |
alshabib | 25c8eec | 2014-09-04 16:41:31 -0700 | [diff] [blame] | 178 | } |
| 179 | |
alshabib | f1216ed | 2014-09-03 11:53:54 -0700 | [diff] [blame] | 180 | @Override |
| 181 | public void switchRemoved(Dpid dpid) { |
alshabib | 6f5460b | 2014-09-03 14:46:17 -0700 | [diff] [blame] | 182 | if (providerService == null) { |
| 183 | return; |
| 184 | } |
tom | 782a7cf | 2014-09-11 23:58:38 -0700 | [diff] [blame] | 185 | providerService.deviceDisconnected(deviceId(uri(dpid))); |
alshabib | f1216ed | 2014-09-03 11:53:54 -0700 | [diff] [blame] | 186 | } |
| 187 | |
Ayaka Koshibe | 38594c2 | 2014-10-22 13:36:12 -0700 | [diff] [blame] | 188 | |
| 189 | @Override |
| 190 | public void switchChanged(Dpid dpid) { |
| 191 | if (providerService == null) { |
| 192 | return; |
| 193 | } |
| 194 | DeviceId did = deviceId(uri(dpid)); |
| 195 | OpenFlowSwitch sw = controller.getSwitch(dpid); |
| 196 | providerService.updatePorts(did, buildPortDescriptions(sw.getPorts())); |
| 197 | } |
| 198 | |
alshabib | a14f364 | 2014-09-05 09:31:31 -0700 | [diff] [blame] | 199 | @Override |
| 200 | public void portChanged(Dpid dpid, OFPortStatus status) { |
tom | 782a7cf | 2014-09-11 23:58:38 -0700 | [diff] [blame] | 201 | PortDescription portDescription = buildPortDescription(status.getDesc()); |
| 202 | providerService.portStatusChanged(deviceId(uri(dpid)), portDescription); |
alshabib | f1216ed | 2014-09-03 11:53:54 -0700 | [diff] [blame] | 203 | } |
| 204 | |
Ayaka Koshibe | ab91cc4 | 2014-09-25 10:20:52 -0700 | [diff] [blame] | 205 | @Override |
| 206 | public void roleAssertFailed(Dpid dpid, RoleState role) { |
| 207 | MastershipRole failed; |
| 208 | switch (role) { |
| 209 | case MASTER: |
| 210 | failed = MastershipRole.MASTER; |
| 211 | break; |
| 212 | case EQUAL: |
| 213 | failed = MastershipRole.STANDBY; |
| 214 | break; |
| 215 | case SLAVE: |
| 216 | failed = MastershipRole.NONE; |
| 217 | break; |
| 218 | default: |
| 219 | LOG.warn("unknown role {}", role); |
| 220 | return; |
| 221 | } |
| 222 | providerService.unableToAssertRole(deviceId(uri(dpid)), failed); |
| 223 | } |
| 224 | |
alshabib | a14f364 | 2014-09-05 09:31:31 -0700 | [diff] [blame] | 225 | /** |
| 226 | * Builds a list of port descriptions for a given list of ports. |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 227 | * |
alshabib | a14f364 | 2014-09-05 09:31:31 -0700 | [diff] [blame] | 228 | * @param ports the list of ports |
| 229 | * @return list of portdescriptions |
| 230 | */ |
alshabib | 4680bb6 | 2014-09-04 17:15:08 -0700 | [diff] [blame] | 231 | private List<PortDescription> buildPortDescriptions( |
| 232 | List<OFPortDesc> ports) { |
Yuta HIGUCHI | 38294a9 | 2014-10-16 18:09:49 -0700 | [diff] [blame] | 233 | final List<PortDescription> portDescs = new ArrayList<>(ports.size()); |
alshabib | 4680bb6 | 2014-09-04 17:15:08 -0700 | [diff] [blame] | 234 | for (OFPortDesc port : ports) { |
alshabib | a14f364 | 2014-09-05 09:31:31 -0700 | [diff] [blame] | 235 | portDescs.add(buildPortDescription(port)); |
alshabib | 4680bb6 | 2014-09-04 17:15:08 -0700 | [diff] [blame] | 236 | } |
| 237 | return portDescs; |
| 238 | } |
| 239 | |
alshabib | a14f364 | 2014-09-05 09:31:31 -0700 | [diff] [blame] | 240 | /** |
| 241 | * Build a portDescription from a given port. |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 242 | * |
alshabib | a14f364 | 2014-09-05 09:31:31 -0700 | [diff] [blame] | 243 | * @param port the port to build from. |
| 244 | * @return portDescription for the port. |
| 245 | */ |
| 246 | private PortDescription buildPortDescription(OFPortDesc port) { |
| 247 | final PortNumber portNo = PortNumber.portNumber(port.getPortNo().getPortNumber()); |
| 248 | final boolean enabled = !port.getState().contains(OFPortState.LINK_DOWN) && |
| 249 | !port.getConfig().contains(OFPortConfig.PORT_DOWN); |
| 250 | return new DefaultPortDescription(portNo, enabled); |
| 251 | } |
Ayaka Koshibe | ab91cc4 | 2014-09-25 10:20:52 -0700 | [diff] [blame] | 252 | |
alshabib | f1216ed | 2014-09-03 11:53:54 -0700 | [diff] [blame] | 253 | } |
| 254 | |
tom | b5a46e6 | 2014-08-26 14:20:00 -0700 | [diff] [blame] | 255 | } |