blob: 2309de40ad64b8fdd7fba6609d3bb52a1837095a [file] [log] [blame]
tomb5a46e62014-08-26 14:20:00 -07001package org.onlab.onos.provider.of.device.impl;
2
alshabib25c8eec2014-09-04 16:41:31 -07003import static org.onlab.onos.net.DeviceId.deviceId;
4import static org.slf4j.LoggerFactory.getLogger;
5
6import java.net.URI;
7import java.net.URISyntaxException;
8import java.util.ArrayList;
9import java.util.List;
10
tomb5a46e62014-08-26 14:20:00 -070011import org.apache.felix.scr.annotations.Activate;
12import org.apache.felix.scr.annotations.Component;
13import org.apache.felix.scr.annotations.Deactivate;
14import org.apache.felix.scr.annotations.Reference;
15import org.apache.felix.scr.annotations.ReferenceCardinality;
tomab21e7c2014-08-26 15:23:08 -070016import org.onlab.onos.net.Device;
17import org.onlab.onos.net.MastershipRole;
alshabib25c8eec2014-09-04 16:41:31 -070018import org.onlab.onos.net.PortNumber;
tomd1900f32014-09-03 14:08:16 -070019import org.onlab.onos.net.device.DefaultDeviceDescription;
alshabib25c8eec2014-09-04 16:41:31 -070020import org.onlab.onos.net.device.DefaultPortDescription;
alshabibf1216ed2014-09-03 11:53:54 -070021import org.onlab.onos.net.device.DeviceDescription;
tomab21e7c2014-08-26 15:23:08 -070022import org.onlab.onos.net.device.DeviceProvider;
tom96dfcab2014-08-28 09:26:03 -070023import org.onlab.onos.net.device.DeviceProviderRegistry;
tomab21e7c2014-08-26 15:23:08 -070024import org.onlab.onos.net.device.DeviceProviderService;
alshabib25c8eec2014-09-04 16:41:31 -070025import org.onlab.onos.net.device.PortDescription;
tomab21e7c2014-08-26 15:23:08 -070026import org.onlab.onos.net.provider.AbstractProvider;
27import org.onlab.onos.net.provider.ProviderId;
alshabibf1216ed2014-09-03 11:53:54 -070028import org.onlab.onos.of.controller.Dpid;
tom5f38b3a2014-08-27 23:50:54 -070029import org.onlab.onos.of.controller.OpenFlowController;
alshabib6f5460b2014-09-03 14:46:17 -070030import org.onlab.onos.of.controller.OpenFlowSwitch;
alshabibf1216ed2014-09-03 11:53:54 -070031import org.onlab.onos.of.controller.OpenFlowSwitchListener;
32import org.onlab.onos.of.controller.RoleState;
alshabib4680bb62014-09-04 17:15:08 -070033import org.projectfloodlight.openflow.protocol.OFPortConfig;
alshabib25c8eec2014-09-04 16:41:31 -070034import org.projectfloodlight.openflow.protocol.OFPortDesc;
alshabib4680bb62014-09-04 17:15:08 -070035import org.projectfloodlight.openflow.protocol.OFPortState;
alshabiba14f3642014-09-05 09:31:31 -070036import org.projectfloodlight.openflow.protocol.OFPortStatus;
tomb5a46e62014-08-26 14:20:00 -070037import org.slf4j.Logger;
tom5f38b3a2014-08-27 23:50:54 -070038
tomb5a46e62014-08-26 14:20:00 -070039/**
tomb1260e42014-08-26 18:39:57 -070040 * Provider which uses an OpenFlow controller to detect network
tome06f8552014-08-26 16:58:42 -070041 * infrastructure devices.
tomb5a46e62014-08-26 14:20:00 -070042 */
tomb1260e42014-08-26 18:39:57 -070043@Component(immediate = true)
tomab21e7c2014-08-26 15:23:08 -070044public class OpenFlowDeviceProvider extends AbstractProvider implements DeviceProvider {
tomb5a46e62014-08-26 14:20:00 -070045
tom5f38b3a2014-08-27 23:50:54 -070046 private final Logger log = getLogger(getClass());
tomb5a46e62014-08-26 14:20:00 -070047
48 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
tom96dfcab2014-08-28 09:26:03 -070049 protected DeviceProviderRegistry providerRegistry;
tomab21e7c2014-08-26 15:23:08 -070050
tom5f38b3a2014-08-27 23:50:54 -070051 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
52 protected OpenFlowController controller;
53
tomab21e7c2014-08-26 15:23:08 -070054 private DeviceProviderService providerService;
tomb5a46e62014-08-26 14:20:00 -070055
alshabib4680bb62014-09-04 17:15:08 -070056 private final OpenFlowSwitchListener listener = new InternalDeviceProvider();
tomd40fc7a2014-09-04 16:41:10 -070057
tomab21e7c2014-08-26 15:23:08 -070058 /**
59 * Creates an OpenFlow device provider.
60 */
61 public OpenFlowDeviceProvider() {
62 super(new ProviderId("org.onlab.onos.provider.of.device"));
63 }
64
tomb5a46e62014-08-26 14:20:00 -070065 @Activate
66 public void activate() {
tom96dfcab2014-08-28 09:26:03 -070067 providerService = providerRegistry.register(this);
tomd40fc7a2014-09-04 16:41:10 -070068 controller.addListener(listener);
tomb5a46e62014-08-26 14:20:00 -070069 log.info("Started");
70 }
71
72 @Deactivate
73 public void deactivate() {
tom96dfcab2014-08-28 09:26:03 -070074 providerRegistry.unregister(this);
tomd40fc7a2014-09-04 16:41:10 -070075 controller.removeListener(listener);
tomab21e7c2014-08-26 15:23:08 -070076 providerService = null;
tomb5a46e62014-08-26 14:20:00 -070077 log.info("Stopped");
78 }
79
tomab21e7c2014-08-26 15:23:08 -070080 @Override
81 public void triggerProbe(Device device) {
tome06f8552014-08-26 16:58:42 -070082 log.info("Triggering probe on device {}", device.id());
tomab21e7c2014-08-26 15:23:08 -070083 }
84
85 @Override
86 public void roleChanged(Device device, MastershipRole newRole) {
alshabibf1216ed2014-09-03 11:53:54 -070087 switch (newRole) {
alshabib25c8eec2014-09-04 16:41:31 -070088 case MASTER:
89 controller.setRole(new Dpid(device.id().uri().getSchemeSpecificPart()),
90 RoleState.MASTER);
91 break;
92 case STANDBY:
93 controller.setRole(new Dpid(device.id().uri().getSchemeSpecificPart()),
94 RoleState.EQUAL);
95 case NONE:
96 controller.setRole(new Dpid(device.id().uri().getSchemeSpecificPart()),
97 RoleState.SLAVE);
98 break;
99 default:
100 log.error("Unknown Mastership state : {}", newRole);
alshabibf1216ed2014-09-03 11:53:54 -0700101
102 }
tomb1260e42014-08-26 18:39:57 -0700103 log.info("Accepting mastership role change for device {}", device.id());
tomab21e7c2014-08-26 15:23:08 -0700104 }
105
alshabibf1216ed2014-09-03 11:53:54 -0700106 private class InternalDeviceProvider implements OpenFlowSwitchListener {
alshabibf1216ed2014-09-03 11:53:54 -0700107 @Override
tomd1900f32014-09-03 14:08:16 -0700108 public void switchAdded(Dpid dpid) {
alshabib6f5460b2014-09-03 14:46:17 -0700109 if (providerService == null) {
110 return;
111 }
alshabibf1216ed2014-09-03 11:53:54 -0700112 URI uri = buildURI(dpid);
alshabib6f5460b2014-09-03 14:46:17 -0700113 OpenFlowSwitch sw = controller.getSwitch(dpid);
114
tomd1900f32014-09-03 14:08:16 -0700115 DeviceDescription description =
116 new DefaultDeviceDescription(buildURI(dpid), Device.Type.SWITCH,
alshabib25c8eec2014-09-04 16:41:31 -0700117 sw.manfacturerDescription(),
118 sw.hardwareDescription(),
119 sw.softwareDescription(),
120 sw.softwareDescription());
tomca20e0c2014-09-03 23:22:24 -0700121 providerService.deviceConnected(deviceId(uri), description);
alshabib25c8eec2014-09-04 16:41:31 -0700122 providerService.updatePorts(deviceId(uri), buildPortDescriptions(sw.getPorts()));
123 }
124
alshabibf1216ed2014-09-03 11:53:54 -0700125 @Override
126 public void switchRemoved(Dpid dpid) {
alshabib6f5460b2014-09-03 14:46:17 -0700127 if (providerService == null) {
128 return;
129 }
alshabibf1216ed2014-09-03 11:53:54 -0700130 URI uri = buildURI(dpid);
tomca20e0c2014-09-03 23:22:24 -0700131 providerService.deviceDisconnected(deviceId(uri));
alshabibf1216ed2014-09-03 11:53:54 -0700132 }
133
alshabiba14f3642014-09-05 09:31:31 -0700134 @Override
135 public void portChanged(Dpid dpid, OFPortStatus status) {
136 final PortDescription portDescription = buildPortDescription(status.getDesc());
137 final URI uri = buildURI(dpid);
138 providerService.portStatusChanged(deviceId(uri), portDescription);
139 }
140
141 /**
142 * Given a dpid builds a URI for the device.
143 * @param dpid the dpid to build the uri from
144 * @return returns a uri of the form of:<dpidHexForm>
145 */
alshabibf1216ed2014-09-03 11:53:54 -0700146 private URI buildURI(Dpid dpid) {
147 URI uri = null;
148 try {
149 uri = new URI("of", Long.toHexString(dpid.value()), null);
150 } catch (URISyntaxException e) {
151 log.warn("URI construction for device {} failed.", dpid);
152 }
153 return uri;
154 }
155
alshabiba14f3642014-09-05 09:31:31 -0700156 /**
157 * Builds a list of port descriptions for a given list of ports.
158 * @param ports the list of ports
159 * @return list of portdescriptions
160 */
alshabib4680bb62014-09-04 17:15:08 -0700161 private List<PortDescription> buildPortDescriptions(
162 List<OFPortDesc> ports) {
163 final List<PortDescription> portDescs = new ArrayList<PortDescription>();
alshabib4680bb62014-09-04 17:15:08 -0700164 for (OFPortDesc port : ports) {
alshabiba14f3642014-09-05 09:31:31 -0700165 portDescs.add(buildPortDescription(port));
alshabib4680bb62014-09-04 17:15:08 -0700166 }
167 return portDescs;
168 }
169
alshabiba14f3642014-09-05 09:31:31 -0700170 /**
171 * Build a portDescription from a given port.
172 * @param port the port to build from.
173 * @return portDescription for the port.
174 */
175 private PortDescription buildPortDescription(OFPortDesc port) {
176 final PortNumber portNo = PortNumber.portNumber(port.getPortNo().getPortNumber());
177 final boolean enabled = !port.getState().contains(OFPortState.LINK_DOWN) &&
178 !port.getConfig().contains(OFPortConfig.PORT_DOWN);
179 return new DefaultPortDescription(portNo, enabled);
180 }
alshabibf1216ed2014-09-03 11:53:54 -0700181 }
182
tomb5a46e62014-08-26 14:20:00 -0700183}