blob: e6941c36d3069018dee4ee3b39e28e6e5c99775a [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");
Jonathan Harta9e29552016-09-09 07:17:41 -070098 components.add("org.onosproject.routing.impl.BgpSpeakerNeighbourHandler");
Jonathan Hartc22e8472015-11-17 18:25:45 -080099 components.add("org.onosproject.routing.impl.SingleSwitchFibInstaller");
100 }
101
Jonathan Hartf5829202015-02-12 09:37:02 -0800102 @Activate
103 protected void activate() {
Jonathan Hartf5829202015-02-12 09:37:02 -0800104 appId = coreService.registerApplication(BGP_ROUTER_APP);
Jonathan Hart4cb39882015-08-12 23:50:55 -0400105
Jonathan Hartc22e8472015-11-17 18:25:45 -0800106 components.forEach(name -> componentService.activate(appId, name));
107
Jonathan Hart4cb39882015-08-12 23:50:55 -0400108 ApplicationId routerAppId = coreService.getAppId(RoutingService.ROUTER_APP_ID);
109 BgpConfig bgpConfig =
110 networkConfigService.getConfig(routerAppId, RoutingService.CONFIG_CLASS);
111
112 if (bgpConfig == null) {
113 log.error("No BgpConfig found");
114 return;
115 }
116
117 getDeviceConfiguration(bgpConfig);
Jonathan Hartf5829202015-02-12 09:37:02 -0800118
119 connectivityManager = new TunnellingConnectivityManager(appId,
Jonathan Hart4cb39882015-08-12 23:50:55 -0400120 bgpConfig,
121 interfaceService,
Jonathan Hart936a7292015-03-06 18:02:57 -0800122 packetService,
Saurav Das3d038262015-04-23 12:36:58 -0700123 flowObjectiveService);
Jonathan Hartf5829202015-02-12 09:37:02 -0800124
Jonathan Hart4cb39882015-08-12 23:50:55 -0400125 icmpHandler = new IcmpHandler(interfaceService, packetService);
Jonathan Hartdf207092015-12-10 11:19:25 -0800126
Saurav Dasbd7f7422015-04-23 16:31:47 -0700127 deviceListener = new InnerDeviceListener();
Saurav Dasbd7f7422015-04-23 16:31:47 -0700128 deviceService.addListener(deviceListener);
Jonathan Hartdf207092015-12-10 11:19:25 -0800129
Jonathan Hartf5829202015-02-12 09:37:02 -0800130 connectivityManager.start();
sangho5eaf0332015-03-09 15:08:12 -0700131 icmpHandler.start();
132
Jonathan Hart49bcae92015-06-04 15:33:15 -0700133 if (deviceService.isAvailable(ctrlDeviceId)) {
134 connectivityManager.notifySwitchAvailable();
135 }
136
Jonathan Hartf5829202015-02-12 09:37:02 -0800137 log.info("BgpRouter started");
138 }
139
140 @Deactivate
141 protected void deactivate() {
Jonathan Hartc22e8472015-11-17 18:25:45 -0800142 components.forEach(name -> componentService.deactivate(appId, name));
143
Jonathan Hartf5829202015-02-12 09:37:02 -0800144 connectivityManager.stop();
sangho5eaf0332015-03-09 15:08:12 -0700145 icmpHandler.stop();
Saurav Dasbd7f7422015-04-23 16:31:47 -0700146 deviceService.removeListener(deviceListener);
Jonathan Hartdf207092015-12-10 11:19:25 -0800147
Jonathan Hartf5829202015-02-12 09:37:02 -0800148 log.info("BgpRouter stopped");
149 }
150
Jonathan Hart4cb39882015-08-12 23:50:55 -0400151 private void getDeviceConfiguration(BgpConfig bgpConfig) {
152 Optional<BgpConfig.BgpSpeakerConfig> bgpSpeaker =
153 bgpConfig.bgpSpeakers().stream().findAny();
154
155 if (!bgpSpeaker.isPresent()) {
156 log.error("BGP speaker configuration not found");
Saurav Dasfbe25c52015-03-04 11:12:00 -0800157 return;
158 }
Jonathan Hart4cb39882015-08-12 23:50:55 -0400159
160 ctrlDeviceId = bgpSpeaker.get().connectPoint().deviceId();
161
Saurav Dasfbe25c52015-03-04 11:12:00 -0800162 log.info("Control Plane OVS dpid: {}", ctrlDeviceId);
163 }
164
Saurav Dasbd7f7422015-04-23 16:31:47 -0700165 // Triggers driver setup when a device is (re)detected.
166 private class InnerDeviceListener implements DeviceListener {
Jonathan Hart7baba072015-02-23 14:27:59 -0800167 @Override
Saurav Dasbd7f7422015-04-23 16:31:47 -0700168 public void event(DeviceEvent event) {
169 switch (event.type()) {
Jonathan Hartdf207092015-12-10 11:19:25 -0800170 case DEVICE_ADDED:
171 case DEVICE_AVAILABILITY_CHANGED:
172 if (deviceService.isAvailable(event.subject().id())) {
173 log.info("Device connected {}", event.subject().id());
Jonathan Hart7baba072015-02-23 14:27:59 -0800174
Jonathan Hartdf207092015-12-10 11:19:25 -0800175 if (event.subject().id().equals(ctrlDeviceId)) {
176 connectivityManager.notifySwitchAvailable();
Saurav Dasbd7f7422015-04-23 16:31:47 -0700177 }
Jonathan Hartdf207092015-12-10 11:19:25 -0800178 }
179 break;
180 // TODO other cases
181 case DEVICE_UPDATED:
182 case DEVICE_REMOVED:
183 case DEVICE_SUSPENDED:
184 case PORT_ADDED:
185 case PORT_UPDATED:
186 case PORT_REMOVED:
187 default:
188 break;
Jonathan Hart7baba072015-02-23 14:27:59 -0800189 }
190 }
Saurav Dasbd7f7422015-04-23 16:31:47 -0700191 }
Jonathan Hartdf207092015-12-10 11:19:25 -0800192
Jonathan Hartf5829202015-02-12 09:37:02 -0800193}