blob: 2f6ad16459a89c3ef10918e970b76e961b00684b [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
tomb5a46e62014-08-26 14:20:00 -070019package org.onlab.onos.provider.of.device.impl;
20
21import org.apache.felix.scr.annotations.Activate;
22import org.apache.felix.scr.annotations.Component;
23import org.apache.felix.scr.annotations.Deactivate;
24import org.apache.felix.scr.annotations.Reference;
25import org.apache.felix.scr.annotations.ReferenceCardinality;
tomab21e7c2014-08-26 15:23:08 -070026import org.onlab.onos.net.Device;
alshabibc944fd02014-09-10 17:55:17 -070027import org.onlab.onos.net.DeviceId;
tomab21e7c2014-08-26 15:23:08 -070028import org.onlab.onos.net.MastershipRole;
alshabib25c8eec2014-09-04 16:41:31 -070029import org.onlab.onos.net.PortNumber;
tomd1900f32014-09-03 14:08:16 -070030import org.onlab.onos.net.device.DefaultDeviceDescription;
alshabib25c8eec2014-09-04 16:41:31 -070031import org.onlab.onos.net.device.DefaultPortDescription;
alshabibf1216ed2014-09-03 11:53:54 -070032import org.onlab.onos.net.device.DeviceDescription;
tomab21e7c2014-08-26 15:23:08 -070033import org.onlab.onos.net.device.DeviceProvider;
tom96dfcab2014-08-28 09:26:03 -070034import org.onlab.onos.net.device.DeviceProviderRegistry;
tomab21e7c2014-08-26 15:23:08 -070035import org.onlab.onos.net.device.DeviceProviderService;
alshabib25c8eec2014-09-04 16:41:31 -070036import org.onlab.onos.net.device.PortDescription;
tomab21e7c2014-08-26 15:23:08 -070037import org.onlab.onos.net.provider.AbstractProvider;
38import org.onlab.onos.net.provider.ProviderId;
tom9c94c5b2014-09-17 13:14:42 -070039import org.onlab.onos.openflow.controller.Dpid;
40import org.onlab.onos.openflow.controller.OpenFlowController;
41import org.onlab.onos.openflow.controller.OpenFlowSwitch;
42import org.onlab.onos.openflow.controller.OpenFlowSwitchListener;
43import org.onlab.onos.openflow.controller.RoleState;
Ayaka Koshibee8708e32014-10-22 13:40:18 -070044import org.onlab.onos.openflow.controller.driver.OpenFlowSwitchDriver;
alshabib7911a052014-10-16 17:49:37 -070045import org.onlab.packet.ChassisId;
Ayaka Koshibee8708e32014-10-22 13:40:18 -070046import org.projectfloodlight.openflow.protocol.OFFactory;
alshabib4680bb62014-09-04 17:15:08 -070047import org.projectfloodlight.openflow.protocol.OFPortConfig;
alshabib25c8eec2014-09-04 16:41:31 -070048import org.projectfloodlight.openflow.protocol.OFPortDesc;
alshabib4680bb62014-09-04 17:15:08 -070049import org.projectfloodlight.openflow.protocol.OFPortState;
alshabiba14f3642014-09-05 09:31:31 -070050import org.projectfloodlight.openflow.protocol.OFPortStatus;
tomb5a46e62014-08-26 14:20:00 -070051import org.slf4j.Logger;
tom5f38b3a2014-08-27 23:50:54 -070052
tom782a7cf2014-09-11 23:58:38 -070053import java.util.ArrayList;
54import java.util.List;
55
56import static org.onlab.onos.net.DeviceId.deviceId;
tom9c94c5b2014-09-17 13:14:42 -070057import static org.onlab.onos.openflow.controller.Dpid.dpid;
58import static org.onlab.onos.openflow.controller.Dpid.uri;
tom782a7cf2014-09-11 23:58:38 -070059import static org.slf4j.LoggerFactory.getLogger;
60
tomb5a46e62014-08-26 14:20:00 -070061/**
tomb1260e42014-08-26 18:39:57 -070062 * Provider which uses an OpenFlow controller to detect network
tome06f8552014-08-26 16:58:42 -070063 * infrastructure devices.
tomb5a46e62014-08-26 14:20:00 -070064 */
tomb1260e42014-08-26 18:39:57 -070065@Component(immediate = true)
tomab21e7c2014-08-26 15:23:08 -070066public class OpenFlowDeviceProvider extends AbstractProvider implements DeviceProvider {
tomb5a46e62014-08-26 14:20:00 -070067
alshabiba89cc582014-09-09 16:43:00 -070068 private static final Logger LOG = getLogger(OpenFlowDeviceProvider.class);
tomb5a46e62014-08-26 14:20:00 -070069
70 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
tom96dfcab2014-08-28 09:26:03 -070071 protected DeviceProviderRegistry providerRegistry;
tomab21e7c2014-08-26 15:23:08 -070072
tom5f38b3a2014-08-27 23:50:54 -070073 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
74 protected OpenFlowController controller;
75
tomab21e7c2014-08-26 15:23:08 -070076 private DeviceProviderService providerService;
tomb5a46e62014-08-26 14:20:00 -070077
alshabib4680bb62014-09-04 17:15:08 -070078 private final OpenFlowSwitchListener listener = new InternalDeviceProvider();
tomd40fc7a2014-09-04 16:41:10 -070079
tomab21e7c2014-08-26 15:23:08 -070080 /**
81 * Creates an OpenFlow device provider.
82 */
83 public OpenFlowDeviceProvider() {
tom7e02cda2014-09-18 12:05:46 -070084 super(new ProviderId("of", "org.onlab.onos.provider.openflow"));
tomab21e7c2014-08-26 15:23:08 -070085 }
86
tomb5a46e62014-08-26 14:20:00 -070087 @Activate
88 public void activate() {
tom96dfcab2014-08-28 09:26:03 -070089 providerService = providerRegistry.register(this);
tomd40fc7a2014-09-04 16:41:10 -070090 controller.addListener(listener);
alshabibc944fd02014-09-10 17:55:17 -070091 for (OpenFlowSwitch sw : controller.getSwitches()) {
92 listener.switchAdded(new Dpid(sw.getId()));
93 }
alshabiba89cc582014-09-09 16:43:00 -070094 LOG.info("Started");
tomb5a46e62014-08-26 14:20:00 -070095 }
96
97 @Deactivate
98 public void deactivate() {
alshabibc944fd02014-09-10 17:55:17 -070099 for (OpenFlowSwitch sw : controller.getSwitches()) {
tom782a7cf2014-09-11 23:58:38 -0700100 providerService.deviceDisconnected(DeviceId.deviceId(uri(sw.getId())));
alshabibc944fd02014-09-10 17:55:17 -0700101 }
tom96dfcab2014-08-28 09:26:03 -0700102 providerRegistry.unregister(this);
tomd40fc7a2014-09-04 16:41:10 -0700103 controller.removeListener(listener);
tomab21e7c2014-08-26 15:23:08 -0700104 providerService = null;
alshabibc944fd02014-09-10 17:55:17 -0700105
alshabiba89cc582014-09-09 16:43:00 -0700106 LOG.info("Stopped");
tomb5a46e62014-08-26 14:20:00 -0700107 }
108
tomab21e7c2014-08-26 15:23:08 -0700109 @Override
110 public void triggerProbe(Device device) {
alshabiba89cc582014-09-09 16:43:00 -0700111 LOG.info("Triggering probe on device {}", device.id());
Ayaka Koshibee8708e32014-10-22 13:40:18 -0700112
113 // 1. check device liveness
114 // FIXME if possible, we might want this to be part of
115 // OpenFlowSwitch interface so the driver interface isn't misused.
116 OpenFlowSwitch sw = controller.getSwitch(dpid(device.id().uri()));
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700117 if (sw == null ||
118 !((OpenFlowSwitchDriver) sw).isConnected()) {
119 LOG.error("Failed to probe device {} on sw={}", device, sw);
Ayaka Koshibee8708e32014-10-22 13:40:18 -0700120 providerService.deviceDisconnected(device.id());
121 return;
122 }
123
124 // 2. Prompt an update of port information. Do we have an XID for this?
125 OFFactory fact = sw.factory();
126 switch (fact.getVersion()) {
127 case OF_10:
128 sw.sendMsg(fact.buildFeaturesRequest().setXid(0).build());
129 break;
130 case OF_13:
131 sw.sendMsg(fact.buildPortDescStatsRequest().setXid(0).build());
132 break;
133 default:
134 LOG.warn("Unhandled protocol version");
135 }
tomab21e7c2014-08-26 15:23:08 -0700136 }
137
138 @Override
139 public void roleChanged(Device device, MastershipRole newRole) {
alshabibf1216ed2014-09-03 11:53:54 -0700140 switch (newRole) {
tom782a7cf2014-09-11 23:58:38 -0700141 case MASTER:
142 controller.setRole(dpid(device.id().uri()), RoleState.MASTER);
143 break;
144 case STANDBY:
145 controller.setRole(dpid(device.id().uri()), RoleState.EQUAL);
146 break;
147 case NONE:
148 controller.setRole(dpid(device.id().uri()), RoleState.SLAVE);
149 break;
150 default:
151 LOG.error("Unknown Mastership state : {}", newRole);
alshabibf1216ed2014-09-03 11:53:54 -0700152
153 }
alshabiba89cc582014-09-09 16:43:00 -0700154 LOG.info("Accepting mastership role change for device {}", device.id());
tomab21e7c2014-08-26 15:23:08 -0700155 }
156
alshabibf1216ed2014-09-03 11:53:54 -0700157 private class InternalDeviceProvider implements OpenFlowSwitchListener {
alshabibf1216ed2014-09-03 11:53:54 -0700158 @Override
tomd1900f32014-09-03 14:08:16 -0700159 public void switchAdded(Dpid dpid) {
alshabib6f5460b2014-09-03 14:46:17 -0700160 if (providerService == null) {
161 return;
162 }
tom782a7cf2014-09-11 23:58:38 -0700163 DeviceId did = deviceId(uri(dpid));
alshabib6f5460b2014-09-03 14:46:17 -0700164 OpenFlowSwitch sw = controller.getSwitch(dpid);
165
Praseed Balakrishnana22eadf2014-10-20 14:21:45 -0700166 Device.Type deviceType = sw.isOptical() ? Device.Type.ROADM :
167 Device.Type.SWITCH;
alshabib7911a052014-10-16 17:49:37 -0700168 ChassisId cId = new ChassisId(dpid.value());
tomd1900f32014-09-03 14:08:16 -0700169 DeviceDescription description =
Praseed Balakrishnana22eadf2014-10-20 14:21:45 -0700170 new DefaultDeviceDescription(did.uri(), deviceType,
tom782a7cf2014-09-11 23:58:38 -0700171 sw.manfacturerDescription(),
172 sw.hardwareDescription(),
173 sw.softwareDescription(),
alshabib7911a052014-10-16 17:49:37 -0700174 sw.serialNumber(),
175 cId);
tom782a7cf2014-09-11 23:58:38 -0700176 providerService.deviceConnected(did, description);
177 providerService.updatePorts(did, buildPortDescriptions(sw.getPorts()));
alshabib25c8eec2014-09-04 16:41:31 -0700178 }
179
alshabibf1216ed2014-09-03 11:53:54 -0700180 @Override
181 public void switchRemoved(Dpid dpid) {
alshabib6f5460b2014-09-03 14:46:17 -0700182 if (providerService == null) {
183 return;
184 }
tom782a7cf2014-09-11 23:58:38 -0700185 providerService.deviceDisconnected(deviceId(uri(dpid)));
alshabibf1216ed2014-09-03 11:53:54 -0700186 }
187
Ayaka Koshibe38594c22014-10-22 13:36:12 -0700188
189 @Override
190 public void switchChanged(Dpid dpid) {
191 if (providerService == null) {
192 return;
193 }
194 DeviceId did = deviceId(uri(dpid));
195 OpenFlowSwitch sw = controller.getSwitch(dpid);
196 providerService.updatePorts(did, buildPortDescriptions(sw.getPorts()));
197 }
198
alshabiba14f3642014-09-05 09:31:31 -0700199 @Override
200 public void portChanged(Dpid dpid, OFPortStatus status) {
tom782a7cf2014-09-11 23:58:38 -0700201 PortDescription portDescription = buildPortDescription(status.getDesc());
202 providerService.portStatusChanged(deviceId(uri(dpid)), portDescription);
alshabibf1216ed2014-09-03 11:53:54 -0700203 }
204
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700205 @Override
206 public void roleAssertFailed(Dpid dpid, RoleState role) {
207 MastershipRole failed;
208 switch (role) {
209 case MASTER:
210 failed = MastershipRole.MASTER;
211 break;
212 case EQUAL:
213 failed = MastershipRole.STANDBY;
214 break;
215 case SLAVE:
216 failed = MastershipRole.NONE;
217 break;
218 default:
219 LOG.warn("unknown role {}", role);
220 return;
221 }
222 providerService.unableToAssertRole(deviceId(uri(dpid)), failed);
223 }
224
alshabiba14f3642014-09-05 09:31:31 -0700225 /**
226 * Builds a list of port descriptions for a given list of ports.
tomff7eb7c2014-09-08 12:49:03 -0700227 *
alshabiba14f3642014-09-05 09:31:31 -0700228 * @param ports the list of ports
229 * @return list of portdescriptions
230 */
alshabib4680bb62014-09-04 17:15:08 -0700231 private List<PortDescription> buildPortDescriptions(
232 List<OFPortDesc> ports) {
Yuta HIGUCHI38294a92014-10-16 18:09:49 -0700233 final List<PortDescription> portDescs = new ArrayList<>(ports.size());
alshabib4680bb62014-09-04 17:15:08 -0700234 for (OFPortDesc port : ports) {
alshabiba14f3642014-09-05 09:31:31 -0700235 portDescs.add(buildPortDescription(port));
alshabib4680bb62014-09-04 17:15:08 -0700236 }
237 return portDescs;
238 }
239
alshabiba14f3642014-09-05 09:31:31 -0700240 /**
241 * Build a portDescription from a given port.
tomff7eb7c2014-09-08 12:49:03 -0700242 *
alshabiba14f3642014-09-05 09:31:31 -0700243 * @param port the port to build from.
244 * @return portDescription for the port.
245 */
246 private PortDescription buildPortDescription(OFPortDesc port) {
247 final PortNumber portNo = PortNumber.portNumber(port.getPortNo().getPortNumber());
248 final boolean enabled = !port.getState().contains(OFPortState.LINK_DOWN) &&
249 !port.getConfig().contains(OFPortConfig.PORT_DOWN);
250 return new DefaultPortDescription(portNo, enabled);
251 }
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700252
alshabibf1216ed2014-09-03 11:53:54 -0700253 }
254
tomb5a46e62014-08-26 14:20:00 -0700255}