blob: 6ba0fe3dacdde611b9b9356b76d1627bde472ebd [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
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700106
107 @Override
108 public boolean isReachable(Device device) {
109 // FIXME if possible, we might want this to be part of
110 // OpenFlowSwitch interface so the driver interface isn't misused.
111 OpenFlowSwitch sw = controller.getSwitch(dpid(device.id().uri()));
112 if (sw == null || !((OpenFlowSwitchDriver) sw).isConnected()) {
113 return false;
114 }
115 return true;
116 //return checkChannel(device, sw);
117 }
118
tomab21e7c2014-08-26 15:23:08 -0700119 @Override
120 public void triggerProbe(Device device) {
alshabiba89cc582014-09-09 16:43:00 -0700121 LOG.info("Triggering probe on device {}", device.id());
Ayaka Koshibee8708e32014-10-22 13:40:18 -0700122
Ayaka Koshibee8708e32014-10-22 13:40:18 -0700123 OpenFlowSwitch sw = controller.getSwitch(dpid(device.id().uri()));
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700124 //if (!checkChannel(device, sw)) {
125 // LOG.error("Failed to probe device {} on sw={}", device, sw);
126 // providerService.deviceDisconnected(device.id());
127 //return;
128 //}
Ayaka Koshibee8708e32014-10-22 13:40:18 -0700129
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700130 // Prompt an update of port information. We can use any XID for this.
Ayaka Koshibee8708e32014-10-22 13:40:18 -0700131 OFFactory fact = sw.factory();
132 switch (fact.getVersion()) {
133 case OF_10:
134 sw.sendMsg(fact.buildFeaturesRequest().setXid(0).build());
135 break;
136 case OF_13:
137 sw.sendMsg(fact.buildPortDescStatsRequest().setXid(0).build());
138 break;
139 default:
140 LOG.warn("Unhandled protocol version");
141 }
tomab21e7c2014-08-26 15:23:08 -0700142 }
143
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700144 // Checks if the OF channel is connected.
145 //private boolean checkChannel(Device device, OpenFlowSwitch sw) {
146 // FIXME if possible, we might want this to be part of
147 // OpenFlowSwitch interface so the driver interface isn't misused.
148 // if (sw == null || !((OpenFlowSwitchDriver) sw).isConnected()) {
149 // return false;
150 // }
151 // return true;
152 // }
153
tomab21e7c2014-08-26 15:23:08 -0700154 @Override
155 public void roleChanged(Device device, MastershipRole newRole) {
alshabibf1216ed2014-09-03 11:53:54 -0700156 switch (newRole) {
tom782a7cf2014-09-11 23:58:38 -0700157 case MASTER:
158 controller.setRole(dpid(device.id().uri()), RoleState.MASTER);
159 break;
160 case STANDBY:
161 controller.setRole(dpid(device.id().uri()), RoleState.EQUAL);
162 break;
163 case NONE:
164 controller.setRole(dpid(device.id().uri()), RoleState.SLAVE);
165 break;
166 default:
167 LOG.error("Unknown Mastership state : {}", newRole);
alshabibf1216ed2014-09-03 11:53:54 -0700168
169 }
alshabiba89cc582014-09-09 16:43:00 -0700170 LOG.info("Accepting mastership role change for device {}", device.id());
tomab21e7c2014-08-26 15:23:08 -0700171 }
172
alshabibf1216ed2014-09-03 11:53:54 -0700173 private class InternalDeviceProvider implements OpenFlowSwitchListener {
alshabibf1216ed2014-09-03 11:53:54 -0700174 @Override
tomd1900f32014-09-03 14:08:16 -0700175 public void switchAdded(Dpid dpid) {
alshabib6f5460b2014-09-03 14:46:17 -0700176 if (providerService == null) {
177 return;
178 }
tom782a7cf2014-09-11 23:58:38 -0700179 DeviceId did = deviceId(uri(dpid));
alshabib6f5460b2014-09-03 14:46:17 -0700180 OpenFlowSwitch sw = controller.getSwitch(dpid);
181
Praseed Balakrishnana22eadf2014-10-20 14:21:45 -0700182 Device.Type deviceType = sw.isOptical() ? Device.Type.ROADM :
183 Device.Type.SWITCH;
alshabib7911a052014-10-16 17:49:37 -0700184 ChassisId cId = new ChassisId(dpid.value());
tomd1900f32014-09-03 14:08:16 -0700185 DeviceDescription description =
Praseed Balakrishnana22eadf2014-10-20 14:21:45 -0700186 new DefaultDeviceDescription(did.uri(), deviceType,
tom782a7cf2014-09-11 23:58:38 -0700187 sw.manfacturerDescription(),
188 sw.hardwareDescription(),
189 sw.softwareDescription(),
alshabib7911a052014-10-16 17:49:37 -0700190 sw.serialNumber(),
191 cId);
tom782a7cf2014-09-11 23:58:38 -0700192 providerService.deviceConnected(did, description);
193 providerService.updatePorts(did, buildPortDescriptions(sw.getPorts()));
alshabib25c8eec2014-09-04 16:41:31 -0700194 }
195
alshabibf1216ed2014-09-03 11:53:54 -0700196 @Override
197 public void switchRemoved(Dpid dpid) {
alshabib6f5460b2014-09-03 14:46:17 -0700198 if (providerService == null) {
199 return;
200 }
tom782a7cf2014-09-11 23:58:38 -0700201 providerService.deviceDisconnected(deviceId(uri(dpid)));
alshabibf1216ed2014-09-03 11:53:54 -0700202 }
203
Ayaka Koshibe38594c22014-10-22 13:36:12 -0700204
205 @Override
206 public void switchChanged(Dpid dpid) {
207 if (providerService == null) {
208 return;
209 }
210 DeviceId did = deviceId(uri(dpid));
211 OpenFlowSwitch sw = controller.getSwitch(dpid);
212 providerService.updatePorts(did, buildPortDescriptions(sw.getPorts()));
213 }
214
alshabiba14f3642014-09-05 09:31:31 -0700215 @Override
216 public void portChanged(Dpid dpid, OFPortStatus status) {
tom782a7cf2014-09-11 23:58:38 -0700217 PortDescription portDescription = buildPortDescription(status.getDesc());
218 providerService.portStatusChanged(deviceId(uri(dpid)), portDescription);
alshabibf1216ed2014-09-03 11:53:54 -0700219 }
220
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700221 @Override
222 public void roleAssertFailed(Dpid dpid, RoleState role) {
223 MastershipRole failed;
224 switch (role) {
225 case MASTER:
226 failed = MastershipRole.MASTER;
227 break;
228 case EQUAL:
229 failed = MastershipRole.STANDBY;
230 break;
231 case SLAVE:
232 failed = MastershipRole.NONE;
233 break;
234 default:
235 LOG.warn("unknown role {}", role);
236 return;
237 }
238 providerService.unableToAssertRole(deviceId(uri(dpid)), failed);
239 }
240
alshabiba14f3642014-09-05 09:31:31 -0700241 /**
242 * Builds a list of port descriptions for a given list of ports.
tomff7eb7c2014-09-08 12:49:03 -0700243 *
alshabiba14f3642014-09-05 09:31:31 -0700244 * @param ports the list of ports
245 * @return list of portdescriptions
246 */
alshabib4680bb62014-09-04 17:15:08 -0700247 private List<PortDescription> buildPortDescriptions(
248 List<OFPortDesc> ports) {
Yuta HIGUCHI38294a92014-10-16 18:09:49 -0700249 final List<PortDescription> portDescs = new ArrayList<>(ports.size());
alshabib4680bb62014-09-04 17:15:08 -0700250 for (OFPortDesc port : ports) {
alshabiba14f3642014-09-05 09:31:31 -0700251 portDescs.add(buildPortDescription(port));
alshabib4680bb62014-09-04 17:15:08 -0700252 }
253 return portDescs;
254 }
255
alshabiba14f3642014-09-05 09:31:31 -0700256 /**
257 * Build a portDescription from a given port.
tomff7eb7c2014-09-08 12:49:03 -0700258 *
alshabiba14f3642014-09-05 09:31:31 -0700259 * @param port the port to build from.
260 * @return portDescription for the port.
261 */
262 private PortDescription buildPortDescription(OFPortDesc port) {
263 final PortNumber portNo = PortNumber.portNumber(port.getPortNo().getPortNumber());
264 final boolean enabled = !port.getState().contains(OFPortState.LINK_DOWN) &&
265 !port.getConfig().contains(OFPortConfig.PORT_DOWN);
266 return new DefaultPortDescription(portNo, enabled);
267 }
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700268
alshabibf1216ed2014-09-03 11:53:54 -0700269 }
270
tomb5a46e62014-08-26 14:20:00 -0700271}