blob: 0ae6fa8c4915c97582dd53b349d9595170ddc702 [file] [log] [blame]
Jonathan Hartf5829202015-02-12 09:37:02 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Jonathan Hartf5829202015-02-12 09:37:02 -08003 *
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
7 *
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.
15 */
16package org.onosproject.bgprouter;
17
Jonathan Hartf5829202015-02-12 09:37:02 -080018import 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;
Jonathan Hartf5829202015-02-12 09:37:02 -080023import org.onosproject.core.ApplicationId;
24import org.onosproject.core.CoreService;
Jonathan Hartc22e8472015-11-17 18:25:45 -080025import org.onosproject.incubator.component.ComponentService;
Jonathan Hart4cb39882015-08-12 23:50:55 -040026import org.onosproject.incubator.net.intf.InterfaceService;
Jonathan Hartf5829202015-02-12 09:37:02 -080027import org.onosproject.net.DeviceId;
Jonathan Hartdf207092015-12-10 11:19:25 -080028import org.onosproject.net.config.NetworkConfigService;
Saurav Dasbd7f7422015-04-23 16:31:47 -070029import org.onosproject.net.device.DeviceEvent;
30import org.onosproject.net.device.DeviceListener;
31import org.onosproject.net.device.DeviceService;
alshabib910aff12015-04-09 16:55:57 -070032import org.onosproject.net.flowobjective.FlowObjectiveService;
Jonathan Hartf5829202015-02-12 09:37:02 -080033import org.onosproject.net.packet.PacketService;
Jonathan Hart2da1e602015-02-18 19:09:24 -080034import org.onosproject.routing.RoutingService;
Jonathan Hart4cb39882015-08-12 23:50:55 -040035import org.onosproject.routing.config.BgpConfig;
Jonathan Hartdf207092015-12-10 11:19:25 -080036import org.onosproject.routing.config.RoutingConfigurationService;
Jonathan Hartf5829202015-02-12 09:37:02 -080037import org.slf4j.Logger;
38import org.slf4j.LoggerFactory;
39
Jonathan Hartc22e8472015-11-17 18:25:45 -080040import java.util.ArrayList;
41import java.util.List;
Jonathan Hart4cb39882015-08-12 23:50:55 -040042import java.util.Optional;
Saurav Dasdfc639e2015-04-30 11:48:16 -070043
Jonathan Hartf5829202015-02-12 09:37:02 -080044/**
45 * BgpRouter component.
46 */
47@Component(immediate = true)
48public class BgpRouter {
49
50 private static final Logger log = LoggerFactory.getLogger(BgpRouter.class);
51
Jonathan Hartdf207092015-12-10 11:19:25 -080052 public static final String BGP_ROUTER_APP = "org.onosproject.bgprouter";
Jonathan Hartf5829202015-02-12 09:37:02 -080053
54 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
55 protected CoreService coreService;
56
Jonathan Hartdf207092015-12-10 11:19:25 -080057 // We depend on the routing configuration being available before starting
58 // up. When we have dynamic configuration support this will no longer be
59 // necessary.
Jonathan Hartf5829202015-02-12 09:37:02 -080060 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Jonathan Hartdf207092015-12-10 11:19:25 -080061 protected RoutingConfigurationService routingConfigurationService;
Jonathan Hartf5829202015-02-12 09:37:02 -080062
63 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Jonathan Hart4cb39882015-08-12 23:50:55 -040064 protected InterfaceService interfaceService;
65
66 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
67 protected NetworkConfigService networkConfigService;
Jonathan Hartf5829202015-02-12 09:37:02 -080068
69 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
70 protected PacketService packetService;
71
Saurav Dasbd7f7422015-04-23 16:31:47 -070072 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
73 protected FlowObjectiveService flowObjectiveService;
74
75 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
76 protected DeviceService deviceService;
77
Jonathan Hartc22e8472015-11-17 18:25:45 -080078 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
79 protected ComponentService componentService;
80
Jonathan Hartf5829202015-02-12 09:37:02 -080081 private ApplicationId appId;
82
Saurav Dasfbe25c52015-03-04 11:12:00 -080083 // Device id of control-plane switch (OVS) connected to BGP Speaker - should be
84 // learned from config
85 private DeviceId ctrlDeviceId;
Jonathan Hartf5829202015-02-12 09:37:02 -080086
Saurav Dasbd7f7422015-04-23 16:31:47 -070087 // Responsible for handling BGP traffic (encapsulated within OF messages)
88 // between the data-plane switch and the Quagga VM using a control plane OVS.
Jonathan Hartf5829202015-02-12 09:37:02 -080089 private TunnellingConnectivityManager connectivityManager;
90
Saurav Dasbd7f7422015-04-23 16:31:47 -070091 private DeviceListener deviceListener;
sangho5eaf0332015-03-09 15:08:12 -070092 private IcmpHandler icmpHandler;
93
Jonathan Hartc22e8472015-11-17 18:25:45 -080094 private static List<String> components = new ArrayList<>();
95 static {
96 components.add("org.onosproject.routing.bgp.BgpSessionManager");
97 components.add("org.onosproject.routing.impl.Router");
98 components.add("org.onosproject.routing.impl.SingleSwitchFibInstaller");
99 }
100
Jonathan Hartf5829202015-02-12 09:37:02 -0800101 @Activate
102 protected void activate() {
Jonathan Hartf5829202015-02-12 09:37:02 -0800103 appId = coreService.registerApplication(BGP_ROUTER_APP);
Jonathan Hart4cb39882015-08-12 23:50:55 -0400104
Jonathan Hartc22e8472015-11-17 18:25:45 -0800105 components.forEach(name -> componentService.activate(appId, name));
106
Jonathan Hart4cb39882015-08-12 23:50:55 -0400107 ApplicationId routerAppId = coreService.getAppId(RoutingService.ROUTER_APP_ID);
108 BgpConfig bgpConfig =
109 networkConfigService.getConfig(routerAppId, RoutingService.CONFIG_CLASS);
110
111 if (bgpConfig == null) {
112 log.error("No BgpConfig found");
113 return;
114 }
115
116 getDeviceConfiguration(bgpConfig);
Jonathan Hartf5829202015-02-12 09:37:02 -0800117
118 connectivityManager = new TunnellingConnectivityManager(appId,
Jonathan Hart4cb39882015-08-12 23:50:55 -0400119 bgpConfig,
120 interfaceService,
Jonathan Hart936a7292015-03-06 18:02:57 -0800121 packetService,
Saurav Das3d038262015-04-23 12:36:58 -0700122 flowObjectiveService);
Jonathan Hartf5829202015-02-12 09:37:02 -0800123
Jonathan Hart4cb39882015-08-12 23:50:55 -0400124 icmpHandler = new IcmpHandler(interfaceService, packetService);
Jonathan Hartdf207092015-12-10 11:19:25 -0800125
Saurav Dasbd7f7422015-04-23 16:31:47 -0700126 deviceListener = new InnerDeviceListener();
Saurav Dasbd7f7422015-04-23 16:31:47 -0700127 deviceService.addListener(deviceListener);
Jonathan Hartdf207092015-12-10 11:19:25 -0800128
Jonathan Hartf5829202015-02-12 09:37:02 -0800129 connectivityManager.start();
sangho5eaf0332015-03-09 15:08:12 -0700130 icmpHandler.start();
131
Jonathan Hart49bcae92015-06-04 15:33:15 -0700132 if (deviceService.isAvailable(ctrlDeviceId)) {
133 connectivityManager.notifySwitchAvailable();
134 }
135
Jonathan Hartf5829202015-02-12 09:37:02 -0800136 log.info("BgpRouter started");
137 }
138
139 @Deactivate
140 protected void deactivate() {
Jonathan Hartc22e8472015-11-17 18:25:45 -0800141 components.forEach(name -> componentService.deactivate(appId, name));
142
Jonathan Hartf5829202015-02-12 09:37:02 -0800143 connectivityManager.stop();
sangho5eaf0332015-03-09 15:08:12 -0700144 icmpHandler.stop();
Saurav Dasbd7f7422015-04-23 16:31:47 -0700145 deviceService.removeListener(deviceListener);
Jonathan Hartdf207092015-12-10 11:19:25 -0800146
Jonathan Hartf5829202015-02-12 09:37:02 -0800147 log.info("BgpRouter stopped");
148 }
149
Jonathan Hart4cb39882015-08-12 23:50:55 -0400150 private void getDeviceConfiguration(BgpConfig bgpConfig) {
151 Optional<BgpConfig.BgpSpeakerConfig> bgpSpeaker =
152 bgpConfig.bgpSpeakers().stream().findAny();
153
154 if (!bgpSpeaker.isPresent()) {
155 log.error("BGP speaker configuration not found");
Saurav Dasfbe25c52015-03-04 11:12:00 -0800156 return;
157 }
Jonathan Hart4cb39882015-08-12 23:50:55 -0400158
159 ctrlDeviceId = bgpSpeaker.get().connectPoint().deviceId();
160
Saurav Dasfbe25c52015-03-04 11:12:00 -0800161 log.info("Control Plane OVS dpid: {}", ctrlDeviceId);
162 }
163
Saurav Dasbd7f7422015-04-23 16:31:47 -0700164 // Triggers driver setup when a device is (re)detected.
165 private class InnerDeviceListener implements DeviceListener {
Jonathan Hart7baba072015-02-23 14:27:59 -0800166 @Override
Saurav Dasbd7f7422015-04-23 16:31:47 -0700167 public void event(DeviceEvent event) {
168 switch (event.type()) {
Jonathan Hartdf207092015-12-10 11:19:25 -0800169 case DEVICE_ADDED:
170 case DEVICE_AVAILABILITY_CHANGED:
171 if (deviceService.isAvailable(event.subject().id())) {
172 log.info("Device connected {}", event.subject().id());
Jonathan Hart7baba072015-02-23 14:27:59 -0800173
Jonathan Hartdf207092015-12-10 11:19:25 -0800174 if (event.subject().id().equals(ctrlDeviceId)) {
175 connectivityManager.notifySwitchAvailable();
Saurav Dasbd7f7422015-04-23 16:31:47 -0700176 }
Jonathan Hartdf207092015-12-10 11:19:25 -0800177 }
178 break;
179 // TODO other cases
180 case DEVICE_UPDATED:
181 case DEVICE_REMOVED:
182 case DEVICE_SUSPENDED:
183 case PORT_ADDED:
184 case PORT_UPDATED:
185 case PORT_REMOVED:
186 default:
187 break;
Jonathan Hart7baba072015-02-23 14:27:59 -0800188 }
189 }
Saurav Dasbd7f7422015-04-23 16:31:47 -0700190 }
Jonathan Hartdf207092015-12-10 11:19:25 -0800191
Jonathan Hartf5829202015-02-12 09:37:02 -0800192}