blob: 43416455ce87b76b9f07a7776afc71f1b5841d1b [file] [log] [blame]
Jonathan Hartf5829202015-02-12 09:37:02 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
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;
Ray Milkeye56c34d2017-08-03 13:39:30 -070025import org.onosproject.component.ComponentService;
Ray Milkeyfacf2862017-08-03 11:58:29 -070026import org.onosproject.net.intf.InterfaceService;
Jonathan Hartf5829202015-02-12 09:37:02 -080027import org.onosproject.net.DeviceId;
Jonathan Hart60e7f512017-02-10 10:24:24 -080028import org.onosproject.net.config.NetworkConfigRegistry;
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 Hart60e7f512017-02-10 10:24:24 -080036import org.onosproject.routing.config.RoutingConfiguration;
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 Hartf5829202015-02-12 09:37:02 -080057 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Jonathan Hart4cb39882015-08-12 23:50:55 -040058 protected InterfaceService interfaceService;
59
60 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Jonathan Hart60e7f512017-02-10 10:24:24 -080061 protected NetworkConfigRegistry networkConfigService;
Jonathan Hartf5829202015-02-12 09:37:02 -080062
63 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
64 protected PacketService packetService;
65
Saurav Dasbd7f7422015-04-23 16:31:47 -070066 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
67 protected FlowObjectiveService flowObjectiveService;
68
69 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
70 protected DeviceService deviceService;
71
Jonathan Hartc22e8472015-11-17 18:25:45 -080072 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
73 protected ComponentService componentService;
74
Jonathan Hartf5829202015-02-12 09:37:02 -080075 private ApplicationId appId;
76
Saurav Dasfbe25c52015-03-04 11:12:00 -080077 // Device id of control-plane switch (OVS) connected to BGP Speaker - should be
78 // learned from config
79 private DeviceId ctrlDeviceId;
Jonathan Hartf5829202015-02-12 09:37:02 -080080
Saurav Dasbd7f7422015-04-23 16:31:47 -070081 // Responsible for handling BGP traffic (encapsulated within OF messages)
82 // between the data-plane switch and the Quagga VM using a control plane OVS.
Jonathan Hartf5829202015-02-12 09:37:02 -080083 private TunnellingConnectivityManager connectivityManager;
84
Saurav Dasbd7f7422015-04-23 16:31:47 -070085 private DeviceListener deviceListener;
sangho5eaf0332015-03-09 15:08:12 -070086 private IcmpHandler icmpHandler;
87
Jonathan Hartc22e8472015-11-17 18:25:45 -080088 private static List<String> components = new ArrayList<>();
89 static {
90 components.add("org.onosproject.routing.bgp.BgpSessionManager");
Jonathan Harta9e29552016-09-09 07:17:41 -070091 components.add("org.onosproject.routing.impl.BgpSpeakerNeighbourHandler");
Jonathan Hartc22e8472015-11-17 18:25:45 -080092 }
93
Jonathan Hartf5829202015-02-12 09:37:02 -080094 @Activate
95 protected void activate() {
Jonathan Hartf5829202015-02-12 09:37:02 -080096 appId = coreService.registerApplication(BGP_ROUTER_APP);
Jonathan Hart4cb39882015-08-12 23:50:55 -040097
Jonathan Hart60e7f512017-02-10 10:24:24 -080098 RoutingConfiguration.register(networkConfigService);
99
Jonathan Hartc22e8472015-11-17 18:25:45 -0800100 components.forEach(name -> componentService.activate(appId, name));
101
Jonathan Hart4cb39882015-08-12 23:50:55 -0400102 ApplicationId routerAppId = coreService.getAppId(RoutingService.ROUTER_APP_ID);
103 BgpConfig bgpConfig =
104 networkConfigService.getConfig(routerAppId, RoutingService.CONFIG_CLASS);
105
106 if (bgpConfig == null) {
107 log.error("No BgpConfig found");
108 return;
109 }
110
111 getDeviceConfiguration(bgpConfig);
Jonathan Hartf5829202015-02-12 09:37:02 -0800112
113 connectivityManager = new TunnellingConnectivityManager(appId,
Jonathan Hart4cb39882015-08-12 23:50:55 -0400114 bgpConfig,
115 interfaceService,
Jonathan Hart936a7292015-03-06 18:02:57 -0800116 packetService,
Saurav Das3d038262015-04-23 12:36:58 -0700117 flowObjectiveService);
Jonathan Hartf5829202015-02-12 09:37:02 -0800118
Jonathan Hart4cb39882015-08-12 23:50:55 -0400119 icmpHandler = new IcmpHandler(interfaceService, packetService);
Jonathan Hartdf207092015-12-10 11:19:25 -0800120
Saurav Dasbd7f7422015-04-23 16:31:47 -0700121 deviceListener = new InnerDeviceListener();
Saurav Dasbd7f7422015-04-23 16:31:47 -0700122 deviceService.addListener(deviceListener);
Jonathan Hartdf207092015-12-10 11:19:25 -0800123
Jonathan Hartf5829202015-02-12 09:37:02 -0800124 connectivityManager.start();
sangho5eaf0332015-03-09 15:08:12 -0700125 icmpHandler.start();
126
Jonathan Hart49bcae92015-06-04 15:33:15 -0700127 if (deviceService.isAvailable(ctrlDeviceId)) {
128 connectivityManager.notifySwitchAvailable();
129 }
130
Jonathan Hartf5829202015-02-12 09:37:02 -0800131 log.info("BgpRouter started");
132 }
133
134 @Deactivate
135 protected void deactivate() {
Jonathan Hartc22e8472015-11-17 18:25:45 -0800136 components.forEach(name -> componentService.deactivate(appId, name));
137
Jonathan Hart60e7f512017-02-10 10:24:24 -0800138 RoutingConfiguration.unregister(networkConfigService);
139
Jonathan Hartf5829202015-02-12 09:37:02 -0800140 connectivityManager.stop();
sangho5eaf0332015-03-09 15:08:12 -0700141 icmpHandler.stop();
Saurav Dasbd7f7422015-04-23 16:31:47 -0700142 deviceService.removeListener(deviceListener);
Jonathan Hartdf207092015-12-10 11:19:25 -0800143
Jonathan Hartf5829202015-02-12 09:37:02 -0800144 log.info("BgpRouter stopped");
145 }
146
Jonathan Hart4cb39882015-08-12 23:50:55 -0400147 private void getDeviceConfiguration(BgpConfig bgpConfig) {
148 Optional<BgpConfig.BgpSpeakerConfig> bgpSpeaker =
149 bgpConfig.bgpSpeakers().stream().findAny();
150
151 if (!bgpSpeaker.isPresent()) {
152 log.error("BGP speaker configuration not found");
Saurav Dasfbe25c52015-03-04 11:12:00 -0800153 return;
154 }
Jonathan Hart4cb39882015-08-12 23:50:55 -0400155
156 ctrlDeviceId = bgpSpeaker.get().connectPoint().deviceId();
157
Saurav Dasfbe25c52015-03-04 11:12:00 -0800158 log.info("Control Plane OVS dpid: {}", ctrlDeviceId);
159 }
160
Saurav Dasbd7f7422015-04-23 16:31:47 -0700161 // Triggers driver setup when a device is (re)detected.
162 private class InnerDeviceListener implements DeviceListener {
Jonathan Hart7baba072015-02-23 14:27:59 -0800163 @Override
Saurav Dasbd7f7422015-04-23 16:31:47 -0700164 public void event(DeviceEvent event) {
165 switch (event.type()) {
Jonathan Hartdf207092015-12-10 11:19:25 -0800166 case DEVICE_ADDED:
167 case DEVICE_AVAILABILITY_CHANGED:
168 if (deviceService.isAvailable(event.subject().id())) {
169 log.info("Device connected {}", event.subject().id());
Jonathan Hart7baba072015-02-23 14:27:59 -0800170
Jonathan Hartdf207092015-12-10 11:19:25 -0800171 if (event.subject().id().equals(ctrlDeviceId)) {
172 connectivityManager.notifySwitchAvailable();
Saurav Dasbd7f7422015-04-23 16:31:47 -0700173 }
Jonathan Hartdf207092015-12-10 11:19:25 -0800174 }
175 break;
176 // TODO other cases
177 case DEVICE_UPDATED:
178 case DEVICE_REMOVED:
179 case DEVICE_SUSPENDED:
180 case PORT_ADDED:
181 case PORT_UPDATED:
182 case PORT_REMOVED:
183 default:
184 break;
Jonathan Hart7baba072015-02-23 14:27:59 -0800185 }
186 }
Saurav Dasbd7f7422015-04-23 16:31:47 -0700187 }
Jonathan Hartdf207092015-12-10 11:19:25 -0800188
Jonathan Hartf5829202015-02-12 09:37:02 -0800189}