blob: ecc50a39a5384c55dce1bdb8453082b5fc9f31b2 [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 Open Networking Laboratory
Thomas Vachuska781d18b2014-10-27 10:31:25 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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 Vachuska781d18b2014-10-27 10:31:25 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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 Vachuska781d18b2014-10-27 10:31:25 -070015 */
tomb5a46e62014-08-26 14:20:00 -070016package org.onlab.onos.provider.of.device.impl;
17
18import org.apache.felix.scr.annotations.Activate;
19import org.apache.felix.scr.annotations.Component;
20import org.apache.felix.scr.annotations.Deactivate;
21import org.apache.felix.scr.annotations.Reference;
22import org.apache.felix.scr.annotations.ReferenceCardinality;
tomab21e7c2014-08-26 15:23:08 -070023import org.onlab.onos.net.Device;
alshabibc944fd02014-09-10 17:55:17 -070024import org.onlab.onos.net.DeviceId;
tomab21e7c2014-08-26 15:23:08 -070025import org.onlab.onos.net.MastershipRole;
alshabib25c8eec2014-09-04 16:41:31 -070026import org.onlab.onos.net.PortNumber;
tomd1900f32014-09-03 14:08:16 -070027import org.onlab.onos.net.device.DefaultDeviceDescription;
alshabib25c8eec2014-09-04 16:41:31 -070028import org.onlab.onos.net.device.DefaultPortDescription;
alshabibf1216ed2014-09-03 11:53:54 -070029import org.onlab.onos.net.device.DeviceDescription;
tomab21e7c2014-08-26 15:23:08 -070030import org.onlab.onos.net.device.DeviceProvider;
tom96dfcab2014-08-28 09:26:03 -070031import org.onlab.onos.net.device.DeviceProviderRegistry;
tomab21e7c2014-08-26 15:23:08 -070032import org.onlab.onos.net.device.DeviceProviderService;
alshabib25c8eec2014-09-04 16:41:31 -070033import org.onlab.onos.net.device.PortDescription;
tomab21e7c2014-08-26 15:23:08 -070034import org.onlab.onos.net.provider.AbstractProvider;
35import org.onlab.onos.net.provider.ProviderId;
tom9c94c5b2014-09-17 13:14:42 -070036import org.onlab.onos.openflow.controller.Dpid;
37import org.onlab.onos.openflow.controller.OpenFlowController;
38import org.onlab.onos.openflow.controller.OpenFlowSwitch;
39import org.onlab.onos.openflow.controller.OpenFlowSwitchListener;
40import org.onlab.onos.openflow.controller.RoleState;
Ayaka Koshibee8708e32014-10-22 13:40:18 -070041import org.onlab.onos.openflow.controller.driver.OpenFlowSwitchDriver;
alshabib7911a052014-10-16 17:49:37 -070042import org.onlab.packet.ChassisId;
Ayaka Koshibee8708e32014-10-22 13:40:18 -070043import org.projectfloodlight.openflow.protocol.OFFactory;
alshabib4680bb62014-09-04 17:15:08 -070044import org.projectfloodlight.openflow.protocol.OFPortConfig;
alshabib25c8eec2014-09-04 16:41:31 -070045import org.projectfloodlight.openflow.protocol.OFPortDesc;
alshabib4680bb62014-09-04 17:15:08 -070046import org.projectfloodlight.openflow.protocol.OFPortState;
alshabiba14f3642014-09-05 09:31:31 -070047import org.projectfloodlight.openflow.protocol.OFPortStatus;
tomb5a46e62014-08-26 14:20:00 -070048import org.slf4j.Logger;
tom5f38b3a2014-08-27 23:50:54 -070049
tom782a7cf2014-09-11 23:58:38 -070050import java.util.ArrayList;
51import java.util.List;
52
53import static org.onlab.onos.net.DeviceId.deviceId;
tom9c94c5b2014-09-17 13:14:42 -070054import static org.onlab.onos.openflow.controller.Dpid.dpid;
55import static org.onlab.onos.openflow.controller.Dpid.uri;
tom782a7cf2014-09-11 23:58:38 -070056import static org.slf4j.LoggerFactory.getLogger;
57
tomb5a46e62014-08-26 14:20:00 -070058/**
tomb1260e42014-08-26 18:39:57 -070059 * Provider which uses an OpenFlow controller to detect network
tome06f8552014-08-26 16:58:42 -070060 * infrastructure devices.
tomb5a46e62014-08-26 14:20:00 -070061 */
tomb1260e42014-08-26 18:39:57 -070062@Component(immediate = true)
tomab21e7c2014-08-26 15:23:08 -070063public class OpenFlowDeviceProvider extends AbstractProvider implements DeviceProvider {
tomb5a46e62014-08-26 14:20:00 -070064
alshabiba89cc582014-09-09 16:43:00 -070065 private static final Logger LOG = getLogger(OpenFlowDeviceProvider.class);
tomb5a46e62014-08-26 14:20:00 -070066
67 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
tom96dfcab2014-08-28 09:26:03 -070068 protected DeviceProviderRegistry providerRegistry;
tomab21e7c2014-08-26 15:23:08 -070069
tom5f38b3a2014-08-27 23:50:54 -070070 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
71 protected OpenFlowController controller;
72
tomab21e7c2014-08-26 15:23:08 -070073 private DeviceProviderService providerService;
tomb5a46e62014-08-26 14:20:00 -070074
alshabib4680bb62014-09-04 17:15:08 -070075 private final OpenFlowSwitchListener listener = new InternalDeviceProvider();
tomd40fc7a2014-09-04 16:41:10 -070076
tomab21e7c2014-08-26 15:23:08 -070077 /**
78 * Creates an OpenFlow device provider.
79 */
80 public OpenFlowDeviceProvider() {
tom7e02cda2014-09-18 12:05:46 -070081 super(new ProviderId("of", "org.onlab.onos.provider.openflow"));
tomab21e7c2014-08-26 15:23:08 -070082 }
83
tomb5a46e62014-08-26 14:20:00 -070084 @Activate
85 public void activate() {
tom96dfcab2014-08-28 09:26:03 -070086 providerService = providerRegistry.register(this);
tomd40fc7a2014-09-04 16:41:10 -070087 controller.addListener(listener);
alshabibc944fd02014-09-10 17:55:17 -070088 for (OpenFlowSwitch sw : controller.getSwitches()) {
89 listener.switchAdded(new Dpid(sw.getId()));
90 }
alshabiba89cc582014-09-09 16:43:00 -070091 LOG.info("Started");
tomb5a46e62014-08-26 14:20:00 -070092 }
93
94 @Deactivate
95 public void deactivate() {
alshabibc944fd02014-09-10 17:55:17 -070096 for (OpenFlowSwitch sw : controller.getSwitches()) {
tom782a7cf2014-09-11 23:58:38 -070097 providerService.deviceDisconnected(DeviceId.deviceId(uri(sw.getId())));
alshabibc944fd02014-09-10 17:55:17 -070098 }
tom96dfcab2014-08-28 09:26:03 -070099 providerRegistry.unregister(this);
tomd40fc7a2014-09-04 16:41:10 -0700100 controller.removeListener(listener);
tomab21e7c2014-08-26 15:23:08 -0700101 providerService = null;
alshabibc944fd02014-09-10 17:55:17 -0700102
alshabiba89cc582014-09-09 16:43:00 -0700103 LOG.info("Stopped");
tomb5a46e62014-08-26 14:20:00 -0700104 }
105
tomab21e7c2014-08-26 15:23:08 -0700106 @Override
107 public void triggerProbe(Device device) {
alshabiba89cc582014-09-09 16:43:00 -0700108 LOG.info("Triggering probe on device {}", device.id());
Ayaka Koshibee8708e32014-10-22 13:40:18 -0700109
110 // 1. check device liveness
111 // FIXME if possible, we might want this to be part of
112 // OpenFlowSwitch interface so the driver interface isn't misused.
113 OpenFlowSwitch sw = controller.getSwitch(dpid(device.id().uri()));
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700114 if (sw == null ||
115 !((OpenFlowSwitchDriver) sw).isConnected()) {
116 LOG.error("Failed to probe device {} on sw={}", device, sw);
Ayaka Koshibee8708e32014-10-22 13:40:18 -0700117 providerService.deviceDisconnected(device.id());
118 return;
119 }
120
121 // 2. Prompt an update of port information. Do we have an XID for this?
122 OFFactory fact = sw.factory();
123 switch (fact.getVersion()) {
124 case OF_10:
125 sw.sendMsg(fact.buildFeaturesRequest().setXid(0).build());
126 break;
127 case OF_13:
128 sw.sendMsg(fact.buildPortDescStatsRequest().setXid(0).build());
129 break;
130 default:
131 LOG.warn("Unhandled protocol version");
132 }
tomab21e7c2014-08-26 15:23:08 -0700133 }
134
135 @Override
136 public void roleChanged(Device device, MastershipRole newRole) {
alshabibf1216ed2014-09-03 11:53:54 -0700137 switch (newRole) {
tom782a7cf2014-09-11 23:58:38 -0700138 case MASTER:
139 controller.setRole(dpid(device.id().uri()), RoleState.MASTER);
140 break;
141 case STANDBY:
142 controller.setRole(dpid(device.id().uri()), RoleState.EQUAL);
143 break;
144 case NONE:
145 controller.setRole(dpid(device.id().uri()), RoleState.SLAVE);
146 break;
147 default:
148 LOG.error("Unknown Mastership state : {}", newRole);
alshabibf1216ed2014-09-03 11:53:54 -0700149
150 }
alshabiba89cc582014-09-09 16:43:00 -0700151 LOG.info("Accepting mastership role change for device {}", device.id());
tomab21e7c2014-08-26 15:23:08 -0700152 }
153
alshabibf1216ed2014-09-03 11:53:54 -0700154 private class InternalDeviceProvider implements OpenFlowSwitchListener {
alshabibf1216ed2014-09-03 11:53:54 -0700155 @Override
tomd1900f32014-09-03 14:08:16 -0700156 public void switchAdded(Dpid dpid) {
alshabib6f5460b2014-09-03 14:46:17 -0700157 if (providerService == null) {
158 return;
159 }
tom782a7cf2014-09-11 23:58:38 -0700160 DeviceId did = deviceId(uri(dpid));
alshabib6f5460b2014-09-03 14:46:17 -0700161 OpenFlowSwitch sw = controller.getSwitch(dpid);
162
Praseed Balakrishnana22eadf2014-10-20 14:21:45 -0700163 Device.Type deviceType = sw.isOptical() ? Device.Type.ROADM :
164 Device.Type.SWITCH;
alshabib7911a052014-10-16 17:49:37 -0700165 ChassisId cId = new ChassisId(dpid.value());
tomd1900f32014-09-03 14:08:16 -0700166 DeviceDescription description =
Praseed Balakrishnana22eadf2014-10-20 14:21:45 -0700167 new DefaultDeviceDescription(did.uri(), deviceType,
tom782a7cf2014-09-11 23:58:38 -0700168 sw.manfacturerDescription(),
169 sw.hardwareDescription(),
170 sw.softwareDescription(),
alshabib7911a052014-10-16 17:49:37 -0700171 sw.serialNumber(),
172 cId);
tom782a7cf2014-09-11 23:58:38 -0700173 providerService.deviceConnected(did, description);
174 providerService.updatePorts(did, buildPortDescriptions(sw.getPorts()));
alshabib25c8eec2014-09-04 16:41:31 -0700175 }
176
alshabibf1216ed2014-09-03 11:53:54 -0700177 @Override
178 public void switchRemoved(Dpid dpid) {
alshabib6f5460b2014-09-03 14:46:17 -0700179 if (providerService == null) {
180 return;
181 }
tom782a7cf2014-09-11 23:58:38 -0700182 providerService.deviceDisconnected(deviceId(uri(dpid)));
alshabibf1216ed2014-09-03 11:53:54 -0700183 }
184
Ayaka Koshibe38594c22014-10-22 13:36:12 -0700185
186 @Override
187 public void switchChanged(Dpid dpid) {
188 if (providerService == null) {
189 return;
190 }
191 DeviceId did = deviceId(uri(dpid));
192 OpenFlowSwitch sw = controller.getSwitch(dpid);
193 providerService.updatePorts(did, buildPortDescriptions(sw.getPorts()));
194 }
195
alshabiba14f3642014-09-05 09:31:31 -0700196 @Override
197 public void portChanged(Dpid dpid, OFPortStatus status) {
tom782a7cf2014-09-11 23:58:38 -0700198 PortDescription portDescription = buildPortDescription(status.getDesc());
199 providerService.portStatusChanged(deviceId(uri(dpid)), portDescription);
alshabibf1216ed2014-09-03 11:53:54 -0700200 }
201
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700202 @Override
203 public void roleAssertFailed(Dpid dpid, RoleState role) {
204 MastershipRole failed;
205 switch (role) {
206 case MASTER:
207 failed = MastershipRole.MASTER;
208 break;
209 case EQUAL:
210 failed = MastershipRole.STANDBY;
211 break;
212 case SLAVE:
213 failed = MastershipRole.NONE;
214 break;
215 default:
216 LOG.warn("unknown role {}", role);
217 return;
218 }
219 providerService.unableToAssertRole(deviceId(uri(dpid)), failed);
220 }
221
alshabiba14f3642014-09-05 09:31:31 -0700222 /**
223 * Builds a list of port descriptions for a given list of ports.
tomff7eb7c2014-09-08 12:49:03 -0700224 *
alshabiba14f3642014-09-05 09:31:31 -0700225 * @param ports the list of ports
226 * @return list of portdescriptions
227 */
alshabib4680bb62014-09-04 17:15:08 -0700228 private List<PortDescription> buildPortDescriptions(
229 List<OFPortDesc> ports) {
Yuta HIGUCHI38294a92014-10-16 18:09:49 -0700230 final List<PortDescription> portDescs = new ArrayList<>(ports.size());
alshabib4680bb62014-09-04 17:15:08 -0700231 for (OFPortDesc port : ports) {
alshabiba14f3642014-09-05 09:31:31 -0700232 portDescs.add(buildPortDescription(port));
alshabib4680bb62014-09-04 17:15:08 -0700233 }
234 return portDescs;
235 }
236
alshabiba14f3642014-09-05 09:31:31 -0700237 /**
238 * Build a portDescription from a given port.
tomff7eb7c2014-09-08 12:49:03 -0700239 *
alshabiba14f3642014-09-05 09:31:31 -0700240 * @param port the port to build from.
241 * @return portDescription for the port.
242 */
243 private PortDescription buildPortDescription(OFPortDesc port) {
244 final PortNumber portNo = PortNumber.portNumber(port.getPortNo().getPortNumber());
245 final boolean enabled = !port.getState().contains(OFPortState.LINK_DOWN) &&
246 !port.getConfig().contains(OFPortConfig.PORT_DOWN);
247 return new DefaultPortDescription(portNo, enabled);
248 }
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700249
alshabibf1216ed2014-09-03 11:53:54 -0700250 }
251
tomb5a46e62014-08-26 14:20:00 -0700252}