blob: 88cb1acb6d380bba5dabd28cb3f72af7e194fb88 [file] [log] [blame]
tomb5a46e62014-08-26 14:20:00 -07001package org.onlab.onos.provider.of.device.impl;
2
3import org.apache.felix.scr.annotations.Activate;
4import org.apache.felix.scr.annotations.Component;
5import org.apache.felix.scr.annotations.Deactivate;
6import org.apache.felix.scr.annotations.Reference;
7import org.apache.felix.scr.annotations.ReferenceCardinality;
tomab21e7c2014-08-26 15:23:08 -07008import org.onlab.onos.net.Device;
alshabibc944fd02014-09-10 17:55:17 -07009import org.onlab.onos.net.DeviceId;
tomab21e7c2014-08-26 15:23:08 -070010import org.onlab.onos.net.MastershipRole;
alshabib25c8eec2014-09-04 16:41:31 -070011import org.onlab.onos.net.PortNumber;
tomd1900f32014-09-03 14:08:16 -070012import org.onlab.onos.net.device.DefaultDeviceDescription;
alshabib25c8eec2014-09-04 16:41:31 -070013import org.onlab.onos.net.device.DefaultPortDescription;
alshabibf1216ed2014-09-03 11:53:54 -070014import org.onlab.onos.net.device.DeviceDescription;
tomab21e7c2014-08-26 15:23:08 -070015import org.onlab.onos.net.device.DeviceProvider;
tom96dfcab2014-08-28 09:26:03 -070016import org.onlab.onos.net.device.DeviceProviderRegistry;
tomab21e7c2014-08-26 15:23:08 -070017import org.onlab.onos.net.device.DeviceProviderService;
alshabib25c8eec2014-09-04 16:41:31 -070018import org.onlab.onos.net.device.PortDescription;
tomab21e7c2014-08-26 15:23:08 -070019import org.onlab.onos.net.provider.AbstractProvider;
20import org.onlab.onos.net.provider.ProviderId;
tom9c94c5b2014-09-17 13:14:42 -070021import org.onlab.onos.openflow.controller.Dpid;
22import org.onlab.onos.openflow.controller.OpenFlowController;
23import org.onlab.onos.openflow.controller.OpenFlowSwitch;
24import org.onlab.onos.openflow.controller.OpenFlowSwitchListener;
25import org.onlab.onos.openflow.controller.RoleState;
alshabib7911a052014-10-16 17:49:37 -070026import org.onlab.packet.ChassisId;
alshabib4680bb62014-09-04 17:15:08 -070027import org.projectfloodlight.openflow.protocol.OFPortConfig;
alshabib25c8eec2014-09-04 16:41:31 -070028import org.projectfloodlight.openflow.protocol.OFPortDesc;
alshabib4680bb62014-09-04 17:15:08 -070029import org.projectfloodlight.openflow.protocol.OFPortState;
alshabiba14f3642014-09-05 09:31:31 -070030import org.projectfloodlight.openflow.protocol.OFPortStatus;
tomb5a46e62014-08-26 14:20:00 -070031import org.slf4j.Logger;
tom5f38b3a2014-08-27 23:50:54 -070032
tom782a7cf2014-09-11 23:58:38 -070033import java.util.ArrayList;
34import java.util.List;
35
36import static org.onlab.onos.net.DeviceId.deviceId;
tom9c94c5b2014-09-17 13:14:42 -070037import static org.onlab.onos.openflow.controller.Dpid.dpid;
38import static org.onlab.onos.openflow.controller.Dpid.uri;
tom782a7cf2014-09-11 23:58:38 -070039import static org.slf4j.LoggerFactory.getLogger;
40
tomb5a46e62014-08-26 14:20:00 -070041/**
tomb1260e42014-08-26 18:39:57 -070042 * Provider which uses an OpenFlow controller to detect network
tome06f8552014-08-26 16:58:42 -070043 * infrastructure devices.
tomb5a46e62014-08-26 14:20:00 -070044 */
tomb1260e42014-08-26 18:39:57 -070045@Component(immediate = true)
tomab21e7c2014-08-26 15:23:08 -070046public class OpenFlowDeviceProvider extends AbstractProvider implements DeviceProvider {
tomb5a46e62014-08-26 14:20:00 -070047
alshabiba89cc582014-09-09 16:43:00 -070048 private static final Logger LOG = getLogger(OpenFlowDeviceProvider.class);
tomb5a46e62014-08-26 14:20:00 -070049
50 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
tom96dfcab2014-08-28 09:26:03 -070051 protected DeviceProviderRegistry providerRegistry;
tomab21e7c2014-08-26 15:23:08 -070052
tom5f38b3a2014-08-27 23:50:54 -070053 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
54 protected OpenFlowController controller;
55
tomab21e7c2014-08-26 15:23:08 -070056 private DeviceProviderService providerService;
tomb5a46e62014-08-26 14:20:00 -070057
alshabib4680bb62014-09-04 17:15:08 -070058 private final OpenFlowSwitchListener listener = new InternalDeviceProvider();
tomd40fc7a2014-09-04 16:41:10 -070059
tomab21e7c2014-08-26 15:23:08 -070060 /**
61 * Creates an OpenFlow device provider.
62 */
63 public OpenFlowDeviceProvider() {
tom7e02cda2014-09-18 12:05:46 -070064 super(new ProviderId("of", "org.onlab.onos.provider.openflow"));
tomab21e7c2014-08-26 15:23:08 -070065 }
66
tomb5a46e62014-08-26 14:20:00 -070067 @Activate
68 public void activate() {
tom96dfcab2014-08-28 09:26:03 -070069 providerService = providerRegistry.register(this);
tomd40fc7a2014-09-04 16:41:10 -070070 controller.addListener(listener);
alshabibc944fd02014-09-10 17:55:17 -070071 for (OpenFlowSwitch sw : controller.getSwitches()) {
72 listener.switchAdded(new Dpid(sw.getId()));
73 }
alshabiba89cc582014-09-09 16:43:00 -070074 LOG.info("Started");
tomb5a46e62014-08-26 14:20:00 -070075 }
76
77 @Deactivate
78 public void deactivate() {
alshabibc944fd02014-09-10 17:55:17 -070079 for (OpenFlowSwitch sw : controller.getSwitches()) {
tom782a7cf2014-09-11 23:58:38 -070080 providerService.deviceDisconnected(DeviceId.deviceId(uri(sw.getId())));
alshabibc944fd02014-09-10 17:55:17 -070081 }
tom96dfcab2014-08-28 09:26:03 -070082 providerRegistry.unregister(this);
tomd40fc7a2014-09-04 16:41:10 -070083 controller.removeListener(listener);
tomab21e7c2014-08-26 15:23:08 -070084 providerService = null;
alshabibc944fd02014-09-10 17:55:17 -070085
alshabiba89cc582014-09-09 16:43:00 -070086 LOG.info("Stopped");
tomb5a46e62014-08-26 14:20:00 -070087 }
88
tomab21e7c2014-08-26 15:23:08 -070089 @Override
90 public void triggerProbe(Device device) {
alshabiba89cc582014-09-09 16:43:00 -070091 LOG.info("Triggering probe on device {}", device.id());
tomab21e7c2014-08-26 15:23:08 -070092 }
93
94 @Override
95 public void roleChanged(Device device, MastershipRole newRole) {
alshabibf1216ed2014-09-03 11:53:54 -070096 switch (newRole) {
tom782a7cf2014-09-11 23:58:38 -070097 case MASTER:
98 controller.setRole(dpid(device.id().uri()), RoleState.MASTER);
99 break;
100 case STANDBY:
101 controller.setRole(dpid(device.id().uri()), RoleState.EQUAL);
102 break;
103 case NONE:
104 controller.setRole(dpid(device.id().uri()), RoleState.SLAVE);
105 break;
106 default:
107 LOG.error("Unknown Mastership state : {}", newRole);
alshabibf1216ed2014-09-03 11:53:54 -0700108
109 }
alshabiba89cc582014-09-09 16:43:00 -0700110 LOG.info("Accepting mastership role change for device {}", device.id());
tomab21e7c2014-08-26 15:23:08 -0700111 }
112
alshabibf1216ed2014-09-03 11:53:54 -0700113 private class InternalDeviceProvider implements OpenFlowSwitchListener {
alshabibf1216ed2014-09-03 11:53:54 -0700114 @Override
tomd1900f32014-09-03 14:08:16 -0700115 public void switchAdded(Dpid dpid) {
alshabib6f5460b2014-09-03 14:46:17 -0700116 if (providerService == null) {
117 return;
118 }
tom782a7cf2014-09-11 23:58:38 -0700119 DeviceId did = deviceId(uri(dpid));
alshabib6f5460b2014-09-03 14:46:17 -0700120 OpenFlowSwitch sw = controller.getSwitch(dpid);
alshabib7911a052014-10-16 17:49:37 -0700121 ChassisId cId = new ChassisId(dpid.value());
tomd1900f32014-09-03 14:08:16 -0700122 DeviceDescription description =
tom782a7cf2014-09-11 23:58:38 -0700123 new DefaultDeviceDescription(did.uri(), Device.Type.SWITCH,
124 sw.manfacturerDescription(),
125 sw.hardwareDescription(),
126 sw.softwareDescription(),
alshabib7911a052014-10-16 17:49:37 -0700127 sw.serialNumber(),
128 cId);
tom782a7cf2014-09-11 23:58:38 -0700129 providerService.deviceConnected(did, description);
130 providerService.updatePorts(did, buildPortDescriptions(sw.getPorts()));
alshabib25c8eec2014-09-04 16:41:31 -0700131 }
132
alshabibf1216ed2014-09-03 11:53:54 -0700133 @Override
134 public void switchRemoved(Dpid dpid) {
alshabib6f5460b2014-09-03 14:46:17 -0700135 if (providerService == null) {
136 return;
137 }
tom782a7cf2014-09-11 23:58:38 -0700138 providerService.deviceDisconnected(deviceId(uri(dpid)));
alshabibf1216ed2014-09-03 11:53:54 -0700139 }
140
alshabiba14f3642014-09-05 09:31:31 -0700141 @Override
142 public void portChanged(Dpid dpid, OFPortStatus status) {
tom782a7cf2014-09-11 23:58:38 -0700143 PortDescription portDescription = buildPortDescription(status.getDesc());
144 providerService.portStatusChanged(deviceId(uri(dpid)), portDescription);
alshabibf1216ed2014-09-03 11:53:54 -0700145 }
146
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700147 @Override
148 public void roleAssertFailed(Dpid dpid, RoleState role) {
149 MastershipRole failed;
150 switch (role) {
151 case MASTER:
152 failed = MastershipRole.MASTER;
153 break;
154 case EQUAL:
155 failed = MastershipRole.STANDBY;
156 break;
157 case SLAVE:
158 failed = MastershipRole.NONE;
159 break;
160 default:
161 LOG.warn("unknown role {}", role);
162 return;
163 }
164 providerService.unableToAssertRole(deviceId(uri(dpid)), failed);
165 }
166
alshabiba14f3642014-09-05 09:31:31 -0700167 /**
168 * Builds a list of port descriptions for a given list of ports.
tomff7eb7c2014-09-08 12:49:03 -0700169 *
alshabiba14f3642014-09-05 09:31:31 -0700170 * @param ports the list of ports
171 * @return list of portdescriptions
172 */
alshabib4680bb62014-09-04 17:15:08 -0700173 private List<PortDescription> buildPortDescriptions(
174 List<OFPortDesc> ports) {
tom782a7cf2014-09-11 23:58:38 -0700175 final List<PortDescription> portDescs = new ArrayList<>();
alshabib4680bb62014-09-04 17:15:08 -0700176 for (OFPortDesc port : ports) {
alshabiba14f3642014-09-05 09:31:31 -0700177 portDescs.add(buildPortDescription(port));
alshabib4680bb62014-09-04 17:15:08 -0700178 }
179 return portDescs;
180 }
181
alshabiba14f3642014-09-05 09:31:31 -0700182 /**
183 * Build a portDescription from a given port.
tomff7eb7c2014-09-08 12:49:03 -0700184 *
alshabiba14f3642014-09-05 09:31:31 -0700185 * @param port the port to build from.
186 * @return portDescription for the port.
187 */
188 private PortDescription buildPortDescription(OFPortDesc port) {
189 final PortNumber portNo = PortNumber.portNumber(port.getPortNo().getPortNumber());
190 final boolean enabled = !port.getState().contains(OFPortState.LINK_DOWN) &&
191 !port.getConfig().contains(OFPortConfig.PORT_DOWN);
192 return new DefaultPortDescription(portNo, enabled);
193 }
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700194
alshabibf1216ed2014-09-03 11:53:54 -0700195 }
196
tomb5a46e62014-08-26 14:20:00 -0700197}