blob: ce43a6a14c3a34fa4c26c40972db6b8426be4878 [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");
Jonathan Harta9e29552016-09-09 07:17:41 -070097 components.add("org.onosproject.routing.impl.BgpSpeakerNeighbourHandler");
Jonathan Hartc22e8472015-11-17 18:25:45 -080098 }
99
Jonathan Hartf5829202015-02-12 09:37:02 -0800100 @Activate
101 protected void activate() {
Jonathan Hartf5829202015-02-12 09:37:02 -0800102 appId = coreService.registerApplication(BGP_ROUTER_APP);
Jonathan Hart4cb39882015-08-12 23:50:55 -0400103
Jonathan Hartc22e8472015-11-17 18:25:45 -0800104 components.forEach(name -> componentService.activate(appId, name));
105
Jonathan Hart4cb39882015-08-12 23:50:55 -0400106 ApplicationId routerAppId = coreService.getAppId(RoutingService.ROUTER_APP_ID);
107 BgpConfig bgpConfig =
108 networkConfigService.getConfig(routerAppId, RoutingService.CONFIG_CLASS);
109
110 if (bgpConfig == null) {
111 log.error("No BgpConfig found");
112 return;
113 }
114
115 getDeviceConfiguration(bgpConfig);
Jonathan Hartf5829202015-02-12 09:37:02 -0800116
117 connectivityManager = new TunnellingConnectivityManager(appId,
Jonathan Hart4cb39882015-08-12 23:50:55 -0400118 bgpConfig,
119 interfaceService,
Jonathan Hart936a7292015-03-06 18:02:57 -0800120 packetService,
Saurav Das3d038262015-04-23 12:36:58 -0700121 flowObjectiveService);
Jonathan Hartf5829202015-02-12 09:37:02 -0800122
Jonathan Hart4cb39882015-08-12 23:50:55 -0400123 icmpHandler = new IcmpHandler(interfaceService, packetService);
Jonathan Hartdf207092015-12-10 11:19:25 -0800124
Saurav Dasbd7f7422015-04-23 16:31:47 -0700125 deviceListener = new InnerDeviceListener();
Saurav Dasbd7f7422015-04-23 16:31:47 -0700126 deviceService.addListener(deviceListener);
Jonathan Hartdf207092015-12-10 11:19:25 -0800127
Jonathan Hartf5829202015-02-12 09:37:02 -0800128 connectivityManager.start();
sangho5eaf0332015-03-09 15:08:12 -0700129 icmpHandler.start();
130
Jonathan Hart49bcae92015-06-04 15:33:15 -0700131 if (deviceService.isAvailable(ctrlDeviceId)) {
132 connectivityManager.notifySwitchAvailable();
133 }
134
Jonathan Hartf5829202015-02-12 09:37:02 -0800135 log.info("BgpRouter started");
136 }
137
138 @Deactivate
139 protected void deactivate() {
Jonathan Hartc22e8472015-11-17 18:25:45 -0800140 components.forEach(name -> componentService.deactivate(appId, name));
141
Jonathan Hartf5829202015-02-12 09:37:02 -0800142 connectivityManager.stop();
sangho5eaf0332015-03-09 15:08:12 -0700143 icmpHandler.stop();
Saurav Dasbd7f7422015-04-23 16:31:47 -0700144 deviceService.removeListener(deviceListener);
Jonathan Hartdf207092015-12-10 11:19:25 -0800145
Jonathan Hartf5829202015-02-12 09:37:02 -0800146 log.info("BgpRouter stopped");
147 }
148
Jonathan Hart4cb39882015-08-12 23:50:55 -0400149 private void getDeviceConfiguration(BgpConfig bgpConfig) {
150 Optional<BgpConfig.BgpSpeakerConfig> bgpSpeaker =
151 bgpConfig.bgpSpeakers().stream().findAny();
152
153 if (!bgpSpeaker.isPresent()) {
154 log.error("BGP speaker configuration not found");
Saurav Dasfbe25c52015-03-04 11:12:00 -0800155 return;
156 }
Jonathan Hart4cb39882015-08-12 23:50:55 -0400157
158 ctrlDeviceId = bgpSpeaker.get().connectPoint().deviceId();
159
Saurav Dasfbe25c52015-03-04 11:12:00 -0800160 log.info("Control Plane OVS dpid: {}", ctrlDeviceId);
161 }
162
Saurav Dasbd7f7422015-04-23 16:31:47 -0700163 // Triggers driver setup when a device is (re)detected.
164 private class InnerDeviceListener implements DeviceListener {
Jonathan Hart7baba072015-02-23 14:27:59 -0800165 @Override
Saurav Dasbd7f7422015-04-23 16:31:47 -0700166 public void event(DeviceEvent event) {
167 switch (event.type()) {
Jonathan Hartdf207092015-12-10 11:19:25 -0800168 case DEVICE_ADDED:
169 case DEVICE_AVAILABILITY_CHANGED:
170 if (deviceService.isAvailable(event.subject().id())) {
171 log.info("Device connected {}", event.subject().id());
Jonathan Hart7baba072015-02-23 14:27:59 -0800172
Jonathan Hartdf207092015-12-10 11:19:25 -0800173 if (event.subject().id().equals(ctrlDeviceId)) {
174 connectivityManager.notifySwitchAvailable();
Saurav Dasbd7f7422015-04-23 16:31:47 -0700175 }
Jonathan Hartdf207092015-12-10 11:19:25 -0800176 }
177 break;
178 // TODO other cases
179 case DEVICE_UPDATED:
180 case DEVICE_REMOVED:
181 case DEVICE_SUSPENDED:
182 case PORT_ADDED:
183 case PORT_UPDATED:
184 case PORT_REMOVED:
185 default:
186 break;
Jonathan Hart7baba072015-02-23 14:27:59 -0800187 }
188 }
Saurav Dasbd7f7422015-04-23 16:31:47 -0700189 }
Jonathan Hartdf207092015-12-10 11:19:25 -0800190
Jonathan Hartf5829202015-02-12 09:37:02 -0800191}