blob: 1271bc89ad35291b4064584d3874a5248ed4312c [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;
alshabib4680bb62014-09-04 17:15:08 -070026import org.projectfloodlight.openflow.protocol.OFPortConfig;
alshabib25c8eec2014-09-04 16:41:31 -070027import org.projectfloodlight.openflow.protocol.OFPortDesc;
alshabib4680bb62014-09-04 17:15:08 -070028import org.projectfloodlight.openflow.protocol.OFPortState;
alshabiba14f3642014-09-05 09:31:31 -070029import org.projectfloodlight.openflow.protocol.OFPortStatus;
tomb5a46e62014-08-26 14:20:00 -070030import org.slf4j.Logger;
tom5f38b3a2014-08-27 23:50:54 -070031
tom782a7cf2014-09-11 23:58:38 -070032import java.util.ArrayList;
33import java.util.List;
34
35import static org.onlab.onos.net.DeviceId.deviceId;
tom9c94c5b2014-09-17 13:14:42 -070036import static org.onlab.onos.openflow.controller.Dpid.dpid;
37import static org.onlab.onos.openflow.controller.Dpid.uri;
tom782a7cf2014-09-11 23:58:38 -070038import static org.slf4j.LoggerFactory.getLogger;
39
tomb5a46e62014-08-26 14:20:00 -070040/**
tomb1260e42014-08-26 18:39:57 -070041 * Provider which uses an OpenFlow controller to detect network
tome06f8552014-08-26 16:58:42 -070042 * infrastructure devices.
tomb5a46e62014-08-26 14:20:00 -070043 */
tomb1260e42014-08-26 18:39:57 -070044@Component(immediate = true)
tomab21e7c2014-08-26 15:23:08 -070045public class OpenFlowDeviceProvider extends AbstractProvider implements DeviceProvider {
tomb5a46e62014-08-26 14:20:00 -070046
alshabiba89cc582014-09-09 16:43:00 -070047 private static final Logger LOG = getLogger(OpenFlowDeviceProvider.class);
tomb5a46e62014-08-26 14:20:00 -070048
49 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
tom96dfcab2014-08-28 09:26:03 -070050 protected DeviceProviderRegistry providerRegistry;
tomab21e7c2014-08-26 15:23:08 -070051
tom5f38b3a2014-08-27 23:50:54 -070052 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
53 protected OpenFlowController controller;
54
tomab21e7c2014-08-26 15:23:08 -070055 private DeviceProviderService providerService;
tomb5a46e62014-08-26 14:20:00 -070056
alshabib4680bb62014-09-04 17:15:08 -070057 private final OpenFlowSwitchListener listener = new InternalDeviceProvider();
tomd40fc7a2014-09-04 16:41:10 -070058
tomab21e7c2014-08-26 15:23:08 -070059 /**
60 * Creates an OpenFlow device provider.
61 */
62 public OpenFlowDeviceProvider() {
tom7e02cda2014-09-18 12:05:46 -070063 super(new ProviderId("of", "org.onlab.onos.provider.openflow"));
tomab21e7c2014-08-26 15:23:08 -070064 }
65
tomb5a46e62014-08-26 14:20:00 -070066 @Activate
67 public void activate() {
tom96dfcab2014-08-28 09:26:03 -070068 providerService = providerRegistry.register(this);
tomd40fc7a2014-09-04 16:41:10 -070069 controller.addListener(listener);
alshabibc944fd02014-09-10 17:55:17 -070070 for (OpenFlowSwitch sw : controller.getSwitches()) {
71 listener.switchAdded(new Dpid(sw.getId()));
72 }
alshabiba89cc582014-09-09 16:43:00 -070073 LOG.info("Started");
tomb5a46e62014-08-26 14:20:00 -070074 }
75
76 @Deactivate
77 public void deactivate() {
alshabibc944fd02014-09-10 17:55:17 -070078 for (OpenFlowSwitch sw : controller.getSwitches()) {
tom782a7cf2014-09-11 23:58:38 -070079 providerService.deviceDisconnected(DeviceId.deviceId(uri(sw.getId())));
alshabibc944fd02014-09-10 17:55:17 -070080 }
tom96dfcab2014-08-28 09:26:03 -070081 providerRegistry.unregister(this);
tomd40fc7a2014-09-04 16:41:10 -070082 controller.removeListener(listener);
tomab21e7c2014-08-26 15:23:08 -070083 providerService = null;
alshabibc944fd02014-09-10 17:55:17 -070084
alshabiba89cc582014-09-09 16:43:00 -070085 LOG.info("Stopped");
tomb5a46e62014-08-26 14:20:00 -070086 }
87
tomab21e7c2014-08-26 15:23:08 -070088 @Override
89 public void triggerProbe(Device device) {
alshabiba89cc582014-09-09 16:43:00 -070090 LOG.info("Triggering probe on device {}", device.id());
tomab21e7c2014-08-26 15:23:08 -070091 }
92
93 @Override
94 public void roleChanged(Device device, MastershipRole newRole) {
alshabibf1216ed2014-09-03 11:53:54 -070095 switch (newRole) {
tom782a7cf2014-09-11 23:58:38 -070096 case MASTER:
97 controller.setRole(dpid(device.id().uri()), RoleState.MASTER);
98 break;
99 case STANDBY:
100 controller.setRole(dpid(device.id().uri()), RoleState.EQUAL);
101 break;
102 case NONE:
103 controller.setRole(dpid(device.id().uri()), RoleState.SLAVE);
104 break;
105 default:
106 LOG.error("Unknown Mastership state : {}", newRole);
alshabibf1216ed2014-09-03 11:53:54 -0700107
108 }
alshabiba89cc582014-09-09 16:43:00 -0700109 LOG.info("Accepting mastership role change for device {}", device.id());
tomab21e7c2014-08-26 15:23:08 -0700110 }
111
alshabibf1216ed2014-09-03 11:53:54 -0700112 private class InternalDeviceProvider implements OpenFlowSwitchListener {
alshabibf1216ed2014-09-03 11:53:54 -0700113 @Override
tomd1900f32014-09-03 14:08:16 -0700114 public void switchAdded(Dpid dpid) {
alshabib6f5460b2014-09-03 14:46:17 -0700115 if (providerService == null) {
116 return;
117 }
tom782a7cf2014-09-11 23:58:38 -0700118 DeviceId did = deviceId(uri(dpid));
alshabib6f5460b2014-09-03 14:46:17 -0700119 OpenFlowSwitch sw = controller.getSwitch(dpid);
120
tomd1900f32014-09-03 14:08:16 -0700121 DeviceDescription description =
tom782a7cf2014-09-11 23:58:38 -0700122 new DefaultDeviceDescription(did.uri(), Device.Type.SWITCH,
123 sw.manfacturerDescription(),
124 sw.hardwareDescription(),
125 sw.softwareDescription(),
126 sw.serialNumber());
127 providerService.deviceConnected(did, description);
128 providerService.updatePorts(did, buildPortDescriptions(sw.getPorts()));
alshabib25c8eec2014-09-04 16:41:31 -0700129 }
130
alshabibf1216ed2014-09-03 11:53:54 -0700131 @Override
132 public void switchRemoved(Dpid dpid) {
alshabib6f5460b2014-09-03 14:46:17 -0700133 if (providerService == null) {
134 return;
135 }
tom782a7cf2014-09-11 23:58:38 -0700136 providerService.deviceDisconnected(deviceId(uri(dpid)));
alshabibf1216ed2014-09-03 11:53:54 -0700137 }
138
alshabiba14f3642014-09-05 09:31:31 -0700139 @Override
140 public void portChanged(Dpid dpid, OFPortStatus status) {
tom782a7cf2014-09-11 23:58:38 -0700141 PortDescription portDescription = buildPortDescription(status.getDesc());
142 providerService.portStatusChanged(deviceId(uri(dpid)), portDescription);
alshabibf1216ed2014-09-03 11:53:54 -0700143 }
144
alshabiba14f3642014-09-05 09:31:31 -0700145 /**
146 * Builds a list of port descriptions for a given list of ports.
tomff7eb7c2014-09-08 12:49:03 -0700147 *
alshabiba14f3642014-09-05 09:31:31 -0700148 * @param ports the list of ports
149 * @return list of portdescriptions
150 */
alshabib4680bb62014-09-04 17:15:08 -0700151 private List<PortDescription> buildPortDescriptions(
152 List<OFPortDesc> ports) {
tom782a7cf2014-09-11 23:58:38 -0700153 final List<PortDescription> portDescs = new ArrayList<>();
alshabib4680bb62014-09-04 17:15:08 -0700154 for (OFPortDesc port : ports) {
alshabiba14f3642014-09-05 09:31:31 -0700155 portDescs.add(buildPortDescription(port));
alshabib4680bb62014-09-04 17:15:08 -0700156 }
157 return portDescs;
158 }
159
alshabiba14f3642014-09-05 09:31:31 -0700160 /**
161 * Build a portDescription from a given port.
tomff7eb7c2014-09-08 12:49:03 -0700162 *
alshabiba14f3642014-09-05 09:31:31 -0700163 * @param port the port to build from.
164 * @return portDescription for the port.
165 */
166 private PortDescription buildPortDescription(OFPortDesc port) {
167 final PortNumber portNo = PortNumber.portNumber(port.getPortNo().getPortNumber());
168 final boolean enabled = !port.getState().contains(OFPortState.LINK_DOWN) &&
169 !port.getConfig().contains(OFPortConfig.PORT_DOWN);
170 return new DefaultPortDescription(portNo, enabled);
171 }
alshabibf1216ed2014-09-03 11:53:54 -0700172 }
173
tomb5a46e62014-08-26 14:20:00 -0700174}