blob: f32ca556f5d02936c42d960d71e4110e8af82087 [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 */
tome06f8552014-08-26 16:58:42 -070016package org.onlab.onos.provider.of.link.impl;
17
alshabib63d5afe2014-09-15 09:40:24 -070018import static org.slf4j.LoggerFactory.getLogger;
19
20import java.util.Map;
21import java.util.concurrent.ConcurrentHashMap;
22
tome06f8552014-08-26 16:58:42 -070023import org.apache.felix.scr.annotations.Activate;
24import org.apache.felix.scr.annotations.Component;
25import org.apache.felix.scr.annotations.Deactivate;
26import org.apache.felix.scr.annotations.Reference;
27import org.apache.felix.scr.annotations.ReferenceCardinality;
alshabibdf652ad2014-09-09 11:53:19 -070028import org.onlab.onos.net.DeviceId;
tome06f8552014-08-26 16:58:42 -070029import org.onlab.onos.net.link.LinkProvider;
tom96dfcab2014-08-28 09:26:03 -070030import org.onlab.onos.net.link.LinkProviderRegistry;
tome06f8552014-08-26 16:58:42 -070031import org.onlab.onos.net.link.LinkProviderService;
32import org.onlab.onos.net.provider.AbstractProvider;
33import org.onlab.onos.net.provider.ProviderId;
tom9c94c5b2014-09-17 13:14:42 -070034import org.onlab.onos.openflow.controller.Dpid;
35import org.onlab.onos.openflow.controller.OpenFlowController;
36import org.onlab.onos.openflow.controller.OpenFlowPacketContext;
37import org.onlab.onos.openflow.controller.OpenFlowSwitch;
38import org.onlab.onos.openflow.controller.OpenFlowSwitchListener;
39import org.onlab.onos.openflow.controller.PacketListener;
Ayaka Koshibeab91cc42014-09-25 10:20:52 -070040import org.onlab.onos.openflow.controller.RoleState;
alshabibdf652ad2014-09-09 11:53:19 -070041import org.projectfloodlight.openflow.protocol.OFPortConfig;
42import org.projectfloodlight.openflow.protocol.OFPortDesc;
43import org.projectfloodlight.openflow.protocol.OFPortState;
alshabib289652c2014-09-07 19:09:28 -070044import org.projectfloodlight.openflow.protocol.OFPortStatus;
tome06f8552014-08-26 16:58:42 -070045import org.slf4j.Logger;
tom5f38b3a2014-08-27 23:50:54 -070046
alshabib64231d82014-09-25 18:25:31 -070047
tome06f8552014-08-26 16:58:42 -070048/**
tomb1260e42014-08-26 18:39:57 -070049 * Provider which uses an OpenFlow controller to detect network
tome06f8552014-08-26 16:58:42 -070050 * infrastructure links.
51 */
tomb1260e42014-08-26 18:39:57 -070052@Component(immediate = true)
alshabib75e77422014-10-16 18:03:40 -070053@Deprecated
tome06f8552014-08-26 16:58:42 -070054public class OpenFlowLinkProvider extends AbstractProvider implements LinkProvider {
55
tom5f38b3a2014-08-27 23:50:54 -070056 private final Logger log = getLogger(getClass());
tome06f8552014-08-26 16:58:42 -070057
58 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
tom96dfcab2014-08-28 09:26:03 -070059 protected LinkProviderRegistry providerRegistry;
tome06f8552014-08-26 16:58:42 -070060
tom5f38b3a2014-08-27 23:50:54 -070061 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
62 protected OpenFlowController controller;
tome06f8552014-08-26 16:58:42 -070063
tom5f38b3a2014-08-27 23:50:54 -070064 private LinkProviderService providerService;
tome06f8552014-08-26 16:58:42 -070065
alshabibdf652ad2014-09-09 11:53:19 -070066 private final boolean useBDDP = true;
67
alshabib289652c2014-09-07 19:09:28 -070068 private final InternalLinkProvider listener = new InternalLinkProvider();
alshabibc4901cd2014-09-05 16:50:40 -070069
alshabib64231d82014-09-25 18:25:31 -070070 protected final Map<Dpid, LinkDiscovery> discoverers = new ConcurrentHashMap<>();
alshabibc944fd02014-09-10 17:55:17 -070071
tome06f8552014-08-26 16:58:42 -070072 /**
73 * Creates an OpenFlow link provider.
74 */
75 public OpenFlowLinkProvider() {
tom7e02cda2014-09-18 12:05:46 -070076 super(new ProviderId("of", "org.onlab.onos.provider.openflow"));
tome06f8552014-08-26 16:58:42 -070077 }
78
79 @Activate
80 public void activate() {
tom96dfcab2014-08-28 09:26:03 -070081 providerService = providerRegistry.register(this);
alshabib289652c2014-09-07 19:09:28 -070082 controller.addListener(listener);
alshabibc4901cd2014-09-05 16:50:40 -070083 controller.addPacketListener(0, listener);
alshabibc944fd02014-09-10 17:55:17 -070084 for (OpenFlowSwitch sw : controller.getSwitches()) {
85 listener.switchAdded(new Dpid(sw.getId()));
86 }
tome06f8552014-08-26 16:58:42 -070087 log.info("Started");
88 }
89
90 @Deactivate
91 public void deactivate() {
alshabibc944fd02014-09-10 17:55:17 -070092 for (LinkDiscovery ld : discoverers.values()) {
93 ld.stop();
94 }
tom96dfcab2014-08-28 09:26:03 -070095 providerRegistry.unregister(this);
alshabib289652c2014-09-07 19:09:28 -070096 controller.removeListener(listener);
alshabibc4901cd2014-09-05 16:50:40 -070097 controller.removePacketListener(listener);
tome06f8552014-08-26 16:58:42 -070098 providerService = null;
alshabibc944fd02014-09-10 17:55:17 -070099
tome06f8552014-08-26 16:58:42 -0700100 log.info("Stopped");
101 }
102
alshabibc4901cd2014-09-05 16:50:40 -0700103
alshabib289652c2014-09-07 19:09:28 -0700104 private class InternalLinkProvider implements PacketListener, OpenFlowSwitchListener {
alshabibc4901cd2014-09-05 16:50:40 -0700105
alshabibdf652ad2014-09-09 11:53:19 -0700106
alshabibc4901cd2014-09-05 16:50:40 -0700107 @Override
alshabibe7031562014-09-12 18:17:37 -0700108 public void handlePacket(OpenFlowPacketContext pktCtx) {
alshabibdf652ad2014-09-09 11:53:19 -0700109 LinkDiscovery ld = discoverers.get(pktCtx.dpid());
110 if (ld == null) {
111 return;
112 }
alshabib505bc6b2014-09-09 15:04:13 -0700113 if (ld.handleLLDP(pktCtx.unparsed(), pktCtx.inPort())) {
alshabib7b2748f2014-09-16 20:21:11 -0700114 pktCtx.block();
alshabib505bc6b2014-09-09 15:04:13 -0700115 }
alshabibc4901cd2014-09-05 16:50:40 -0700116
117 }
118
alshabib289652c2014-09-07 19:09:28 -0700119 @Override
120 public void switchAdded(Dpid dpid) {
alshabibdf652ad2014-09-09 11:53:19 -0700121 discoverers.put(dpid, new LinkDiscovery(controller.getSwitch(dpid),
alshabib63d5afe2014-09-15 09:40:24 -0700122 controller, providerService, useBDDP));
alshabib289652c2014-09-07 19:09:28 -0700123
124 }
125
126 @Override
127 public void switchRemoved(Dpid dpid) {
alshabibc944fd02014-09-10 17:55:17 -0700128 LinkDiscovery ld = discoverers.remove(dpid);
alshabibdf652ad2014-09-09 11:53:19 -0700129 if (ld != null) {
130 ld.removeAllPorts();
131 }
132 providerService.linksVanished(
133 DeviceId.deviceId("of:" + Long.toHexString(dpid.value())));
alshabib289652c2014-09-07 19:09:28 -0700134 }
135
Ayaka Koshibe38594c22014-10-22 13:36:12 -0700136
137 @Override
138 public void switchChanged(Dpid dpid) {
139 //might not need to do anything since DeviceManager is notified
140 }
141
alshabib289652c2014-09-07 19:09:28 -0700142 @Override
143 public void portChanged(Dpid dpid, OFPortStatus status) {
alshabibdf652ad2014-09-09 11:53:19 -0700144 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 {
alshabibc944fd02014-09-10 17:55:17 -0700154 /*
155 * remove port calls linkVanished
156 */
alshabiba159a322014-09-09 14:50:51 -0700157 ld.removePort(port);
alshabibdf652ad2014-09-09 11:53:19 -0700158 }
alshabib289652c2014-09-07 19:09:28 -0700159
160 }
161
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700162 @Override
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700163 public void receivedRoleReply(Dpid dpid, RoleState requested,
164 RoleState response) {
alshabib64231d82014-09-25 18:25:31 -0700165 // do nothing for this.
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700166 }
167
alshabibc4901cd2014-09-05 16:50:40 -0700168 }
169
tome06f8552014-08-26 16:58:42 -0700170}