Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 1 | /* |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 2 | * Copyright 2014 Open Networking Laboratory |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 4 | * 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 Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 8 | * 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 Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 15 | */ |
tom | e06f855 | 2014-08-26 16:58:42 -0700 | [diff] [blame] | 16 | package org.onlab.onos.provider.of.link.impl; |
| 17 | |
alshabib | 63d5afe | 2014-09-15 09:40:24 -0700 | [diff] [blame] | 18 | import static org.slf4j.LoggerFactory.getLogger; |
| 19 | |
| 20 | import java.util.Map; |
| 21 | import java.util.concurrent.ConcurrentHashMap; |
| 22 | |
tom | e06f855 | 2014-08-26 16:58:42 -0700 | [diff] [blame] | 23 | import org.apache.felix.scr.annotations.Activate; |
| 24 | import org.apache.felix.scr.annotations.Component; |
| 25 | import org.apache.felix.scr.annotations.Deactivate; |
| 26 | import org.apache.felix.scr.annotations.Reference; |
| 27 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
alshabib | df652ad | 2014-09-09 11:53:19 -0700 | [diff] [blame] | 28 | import org.onlab.onos.net.DeviceId; |
tom | e06f855 | 2014-08-26 16:58:42 -0700 | [diff] [blame] | 29 | import org.onlab.onos.net.link.LinkProvider; |
tom | 96dfcab | 2014-08-28 09:26:03 -0700 | [diff] [blame] | 30 | import org.onlab.onos.net.link.LinkProviderRegistry; |
tom | e06f855 | 2014-08-26 16:58:42 -0700 | [diff] [blame] | 31 | import org.onlab.onos.net.link.LinkProviderService; |
| 32 | import org.onlab.onos.net.provider.AbstractProvider; |
| 33 | import org.onlab.onos.net.provider.ProviderId; |
tom | 9c94c5b | 2014-09-17 13:14:42 -0700 | [diff] [blame] | 34 | import org.onlab.onos.openflow.controller.Dpid; |
| 35 | import org.onlab.onos.openflow.controller.OpenFlowController; |
| 36 | import org.onlab.onos.openflow.controller.OpenFlowPacketContext; |
| 37 | import org.onlab.onos.openflow.controller.OpenFlowSwitch; |
| 38 | import org.onlab.onos.openflow.controller.OpenFlowSwitchListener; |
| 39 | import org.onlab.onos.openflow.controller.PacketListener; |
Ayaka Koshibe | ab91cc4 | 2014-09-25 10:20:52 -0700 | [diff] [blame] | 40 | import org.onlab.onos.openflow.controller.RoleState; |
alshabib | df652ad | 2014-09-09 11:53:19 -0700 | [diff] [blame] | 41 | import org.projectfloodlight.openflow.protocol.OFPortConfig; |
| 42 | import org.projectfloodlight.openflow.protocol.OFPortDesc; |
| 43 | import org.projectfloodlight.openflow.protocol.OFPortState; |
alshabib | 289652c | 2014-09-07 19:09:28 -0700 | [diff] [blame] | 44 | import org.projectfloodlight.openflow.protocol.OFPortStatus; |
tom | e06f855 | 2014-08-26 16:58:42 -0700 | [diff] [blame] | 45 | import org.slf4j.Logger; |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 46 | |
alshabib | 64231d8 | 2014-09-25 18:25:31 -0700 | [diff] [blame] | 47 | |
tom | e06f855 | 2014-08-26 16:58:42 -0700 | [diff] [blame] | 48 | /** |
tom | b1260e4 | 2014-08-26 18:39:57 -0700 | [diff] [blame] | 49 | * Provider which uses an OpenFlow controller to detect network |
tom | e06f855 | 2014-08-26 16:58:42 -0700 | [diff] [blame] | 50 | * infrastructure links. |
| 51 | */ |
tom | b1260e4 | 2014-08-26 18:39:57 -0700 | [diff] [blame] | 52 | @Component(immediate = true) |
alshabib | 75e7742 | 2014-10-16 18:03:40 -0700 | [diff] [blame] | 53 | @Deprecated |
tom | e06f855 | 2014-08-26 16:58:42 -0700 | [diff] [blame] | 54 | public class OpenFlowLinkProvider extends AbstractProvider implements LinkProvider { |
| 55 | |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 56 | private final Logger log = getLogger(getClass()); |
tom | e06f855 | 2014-08-26 16:58:42 -0700 | [diff] [blame] | 57 | |
| 58 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
tom | 96dfcab | 2014-08-28 09:26:03 -0700 | [diff] [blame] | 59 | protected LinkProviderRegistry providerRegistry; |
tom | e06f855 | 2014-08-26 16:58:42 -0700 | [diff] [blame] | 60 | |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 61 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 62 | protected OpenFlowController controller; |
tom | e06f855 | 2014-08-26 16:58:42 -0700 | [diff] [blame] | 63 | |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 64 | private LinkProviderService providerService; |
tom | e06f855 | 2014-08-26 16:58:42 -0700 | [diff] [blame] | 65 | |
alshabib | df652ad | 2014-09-09 11:53:19 -0700 | [diff] [blame] | 66 | private final boolean useBDDP = true; |
| 67 | |
alshabib | 289652c | 2014-09-07 19:09:28 -0700 | [diff] [blame] | 68 | private final InternalLinkProvider listener = new InternalLinkProvider(); |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 69 | |
alshabib | 64231d8 | 2014-09-25 18:25:31 -0700 | [diff] [blame] | 70 | protected final Map<Dpid, LinkDiscovery> discoverers = new ConcurrentHashMap<>(); |
alshabib | c944fd0 | 2014-09-10 17:55:17 -0700 | [diff] [blame] | 71 | |
tom | e06f855 | 2014-08-26 16:58:42 -0700 | [diff] [blame] | 72 | /** |
| 73 | * Creates an OpenFlow link provider. |
| 74 | */ |
| 75 | public OpenFlowLinkProvider() { |
tom | 7e02cda | 2014-09-18 12:05:46 -0700 | [diff] [blame] | 76 | super(new ProviderId("of", "org.onlab.onos.provider.openflow")); |
tom | e06f855 | 2014-08-26 16:58:42 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | @Activate |
| 80 | public void activate() { |
tom | 96dfcab | 2014-08-28 09:26:03 -0700 | [diff] [blame] | 81 | providerService = providerRegistry.register(this); |
alshabib | 289652c | 2014-09-07 19:09:28 -0700 | [diff] [blame] | 82 | controller.addListener(listener); |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 83 | controller.addPacketListener(0, listener); |
alshabib | c944fd0 | 2014-09-10 17:55:17 -0700 | [diff] [blame] | 84 | for (OpenFlowSwitch sw : controller.getSwitches()) { |
| 85 | listener.switchAdded(new Dpid(sw.getId())); |
| 86 | } |
tom | e06f855 | 2014-08-26 16:58:42 -0700 | [diff] [blame] | 87 | log.info("Started"); |
| 88 | } |
| 89 | |
| 90 | @Deactivate |
| 91 | public void deactivate() { |
alshabib | c944fd0 | 2014-09-10 17:55:17 -0700 | [diff] [blame] | 92 | for (LinkDiscovery ld : discoverers.values()) { |
| 93 | ld.stop(); |
| 94 | } |
tom | 96dfcab | 2014-08-28 09:26:03 -0700 | [diff] [blame] | 95 | providerRegistry.unregister(this); |
alshabib | 289652c | 2014-09-07 19:09:28 -0700 | [diff] [blame] | 96 | controller.removeListener(listener); |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 97 | controller.removePacketListener(listener); |
tom | e06f855 | 2014-08-26 16:58:42 -0700 | [diff] [blame] | 98 | providerService = null; |
alshabib | c944fd0 | 2014-09-10 17:55:17 -0700 | [diff] [blame] | 99 | |
tom | e06f855 | 2014-08-26 16:58:42 -0700 | [diff] [blame] | 100 | log.info("Stopped"); |
| 101 | } |
| 102 | |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 103 | |
alshabib | 289652c | 2014-09-07 19:09:28 -0700 | [diff] [blame] | 104 | private class InternalLinkProvider implements PacketListener, OpenFlowSwitchListener { |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 105 | |
alshabib | df652ad | 2014-09-09 11:53:19 -0700 | [diff] [blame] | 106 | |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 107 | @Override |
alshabib | e703156 | 2014-09-12 18:17:37 -0700 | [diff] [blame] | 108 | public void handlePacket(OpenFlowPacketContext pktCtx) { |
alshabib | df652ad | 2014-09-09 11:53:19 -0700 | [diff] [blame] | 109 | LinkDiscovery ld = discoverers.get(pktCtx.dpid()); |
| 110 | if (ld == null) { |
| 111 | return; |
| 112 | } |
alshabib | 505bc6b | 2014-09-09 15:04:13 -0700 | [diff] [blame] | 113 | if (ld.handleLLDP(pktCtx.unparsed(), pktCtx.inPort())) { |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame] | 114 | pktCtx.block(); |
alshabib | 505bc6b | 2014-09-09 15:04:13 -0700 | [diff] [blame] | 115 | } |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 116 | |
| 117 | } |
| 118 | |
alshabib | 289652c | 2014-09-07 19:09:28 -0700 | [diff] [blame] | 119 | @Override |
| 120 | public void switchAdded(Dpid dpid) { |
alshabib | df652ad | 2014-09-09 11:53:19 -0700 | [diff] [blame] | 121 | discoverers.put(dpid, new LinkDiscovery(controller.getSwitch(dpid), |
alshabib | 63d5afe | 2014-09-15 09:40:24 -0700 | [diff] [blame] | 122 | controller, providerService, useBDDP)); |
alshabib | 289652c | 2014-09-07 19:09:28 -0700 | [diff] [blame] | 123 | |
| 124 | } |
| 125 | |
| 126 | @Override |
| 127 | public void switchRemoved(Dpid dpid) { |
alshabib | c944fd0 | 2014-09-10 17:55:17 -0700 | [diff] [blame] | 128 | LinkDiscovery ld = discoverers.remove(dpid); |
alshabib | df652ad | 2014-09-09 11:53:19 -0700 | [diff] [blame] | 129 | if (ld != null) { |
| 130 | ld.removeAllPorts(); |
| 131 | } |
| 132 | providerService.linksVanished( |
| 133 | DeviceId.deviceId("of:" + Long.toHexString(dpid.value()))); |
alshabib | 289652c | 2014-09-07 19:09:28 -0700 | [diff] [blame] | 134 | } |
| 135 | |
Ayaka Koshibe | 38594c2 | 2014-10-22 13:36:12 -0700 | [diff] [blame] | 136 | |
| 137 | @Override |
| 138 | public void switchChanged(Dpid dpid) { |
| 139 | //might not need to do anything since DeviceManager is notified |
| 140 | } |
| 141 | |
alshabib | 289652c | 2014-09-07 19:09:28 -0700 | [diff] [blame] | 142 | @Override |
| 143 | public void portChanged(Dpid dpid, OFPortStatus status) { |
alshabib | df652ad | 2014-09-09 11:53:19 -0700 | [diff] [blame] | 144 | LinkDiscovery ld = discoverers.get(dpid); |
| 145 | if (ld == null) { |
| 146 | return; |
| 147 | } |
| 148 | final OFPortDesc port = status.getDesc(); |
| 149 | final boolean enabled = !port.getState().contains(OFPortState.LINK_DOWN) && |
| 150 | !port.getConfig().contains(OFPortConfig.PORT_DOWN); |
| 151 | if (enabled) { |
| 152 | ld.addPort(port); |
| 153 | } else { |
alshabib | c944fd0 | 2014-09-10 17:55:17 -0700 | [diff] [blame] | 154 | /* |
| 155 | * remove port calls linkVanished |
| 156 | */ |
alshabib | a159a32 | 2014-09-09 14:50:51 -0700 | [diff] [blame] | 157 | ld.removePort(port); |
alshabib | df652ad | 2014-09-09 11:53:19 -0700 | [diff] [blame] | 158 | } |
alshabib | 289652c | 2014-09-07 19:09:28 -0700 | [diff] [blame] | 159 | |
| 160 | } |
| 161 | |
Ayaka Koshibe | ab91cc4 | 2014-09-25 10:20:52 -0700 | [diff] [blame] | 162 | @Override |
| 163 | public void roleAssertFailed(Dpid dpid, RoleState role) { |
alshabib | 64231d8 | 2014-09-25 18:25:31 -0700 | [diff] [blame] | 164 | // do nothing for this. |
Ayaka Koshibe | ab91cc4 | 2014-09-25 10:20:52 -0700 | [diff] [blame] | 165 | } |
| 166 | |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 167 | } |
| 168 | |
tom | e06f855 | 2014-08-26 16:58:42 -0700 | [diff] [blame] | 169 | } |